Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Lightning Talk "Opening CPython Built-in Classe...
Search
Jesús Espino
July 24, 2015
Programming
0
97
Lightning Talk "Opening CPython Built-in Classes" (Europython)
Jesús Espino
July 24, 2015
Tweet
Share
More Decks by Jesús Espino
See All by Jesús Espino
Exploring a complex codebase with AI
jespino
0
7
Explorando el Open Source con IA
jespino
0
6
Deep dive into a go binary
jespino
1
160
Deep dive in a SQL query (PgIbz)
jespino
2
100
Understanding the go runtime
jespino
1
140
Tests end-to-end en entornos complejos con testcontainers
jespino
0
33
Dissecting Slices, Maps and Channels in Go
jespino
0
59
Understanding the memory model of 5 languages
jespino
0
150
The Javascript Toolset
jespino
1
91
Other Decks in Programming
See All in Programming
Ruby Parser progress report 2025
yui_knk
1
240
Claude Codeで実装以外の開発フロー、どこまで自動化できるか?失敗と成功
ndadayo
3
1.8k
Updates on MLS on Ruby (and maybe more)
sylph01
1
160
令和最新版手のひらコンピュータ
koba789
14
8.2k
【第4回】関東Kaggler会「Kaggleは執筆に役立つ」
mipypf
0
950
TanStack DB ~状態管理の新しい考え方~
bmthd
2
370
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
340
AI時代のドメイン駆動設計-DDD実践におけるAI活用のあり方 / ddd-in-ai-era
minodriven
25
9.4k
ECS初心者の仲間 – TUIツール「e1s」の紹介
keidarcy
0
130
FindyにおけるTakumi活用と脆弱性管理のこれから
rvirus0817
0
240
[FEConf 2025] 모노레포 절망편, 14개 레포로 부활하기까지 걸린 1년
mmmaxkim
0
1.4k
AHC051解法紹介
eijirou
0
640
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.5k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
We Have a Design System, Now What?
morganepeng
53
7.8k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
830
Designing for humans not robots
tammielis
253
25k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
How STYLIGHT went responsive
nonsquared
100
5.8k
How GitHub (no longer) Works
holman
315
140k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
21k
Transcript
Opening CPython Built-in Classes JESUS ESPINO GARCIA, DEVELOPER #007000
What’s the problem?
Closed built-in classes >>> int.x = 3 Traceback (most recent
call last): File "<stdin>", line 1, in <module> TypeError: can't set attributes of built-in/extension type 'int'
The solution
Master Key >>> import masterkey >>> masterkey.open_class(int) >>> int.x =
3 >>> (10).x 3
Application
Fix broken float comparisons >>> assert 1.3332 == 1.3333 Traceback
(most recent call last): File "<stdin>", line 1, in <module> AssertionError >>> float.__eq__ = lambda self, other: abs(abs(self) - abs(other)) < 0.1 >>> 1.3332 == 1.3333 True >>> 1.7 == 1.1 False
Add sense to the << operator >>> 5 << 1
10 >>> 5 << 2 20 >>> int.__lshift__ = lambda self, other: self * (10 ** other) >>> 5 << 1 50 >>> 5 << 2 500
More semantic bool __repr__ >>> True == True True >>>
True == False False >>> bool.__repr__ = lambda x: ‘Yes’ if True else ‘No’ >>> True == True Yes >>> True == False No
Be more rubyist >>> from masterkey import rubint >>> (3).times(print)
0 1 2 >>> (3).upto(5, print) 4 5 >>> (3).downto(1, print) 2 1
Close your own classes >>> class MyClass: ... pass >>>
masterkey.close_class(MyClass) >>> MyClass.x = 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't set attributes of built-in/extension type 'MyClass'
Of course, this is a joke
The black magic import ctypes from operator import xor longsize
= ctypes.sizeof(ctypes.c_long) def open_class(klass): int_flags = ctypes.c_long.from_address(id(klass) + longsize * 21) int_flags.value |= 1 << 9 def close_class(klass): int_flags = ctypes.c_long.from_address(id(klass) + longsize * 21) int_flags.value = xor(int_flags.value, 1 << 9)
The code http://www.github.com/jespino/python-master-key