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
92
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
Deep dive into a go binary
jespino
1
130
Deep dive in a SQL query (PgIbz)
jespino
2
83
Understanding the go runtime
jespino
1
100
Tests end-to-end en entornos complejos con testcontainers
jespino
0
26
Dissecting Slices, Maps and Channels in Go
jespino
0
51
Understanding the memory model of 5 languages
jespino
0
130
The Javascript Toolset
jespino
1
84
Deep dive in a SQL query
jespino
0
93
10 Aha! Moments reading the go source code
jespino
0
110
Other Decks in Programming
See All in Programming
The Nature of Complexity in John Ousterhout’s Philosophy of Software Design
philipschwarz
PRO
0
150
「影響が少ない」を自分の目でみてみる
o0h
PRO
2
1.2k
ASP.NETアプリケーションのモダナイゼーションについて
tomokusaba
0
200
Thank you <💅>, What's the Next?
ahoxa
1
570
fieldalignmentから見るGoの構造体
kuro_kurorrr
0
120
PHP で学ぶ OAuth 入門
azuki
1
220
By the way Google Cloud Next 2025に行ってみてどうだった
ymd65536
0
110
大LLM時代にこの先生きのこるには-ITエンジニア編
fumiyakume
7
3.2k
UMAPをざっくりと理解 / Overview of UMAP
kaityo256
PRO
2
1.2k
Optimizing JRuby 10
headius
0
520
Dissecting and Reconstructing Ruby Syntactic Structures
ydah
2
1.4k
Being an ethical software engineer
xgouchet
PRO
0
230
Featured
See All Featured
How to Ace a Technical Interview
jacobian
276
23k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.4k
Fontdeck: Realign not Redesign
paulrobertlloyd
84
5.5k
BBQ
matthewcrist
88
9.6k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
23
2.7k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
680
VelocityConf: Rendering Performance Case Studies
addyosmani
329
24k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Unsuck your backbone
ammeep
670
57k
The World Runs on Bad Software
bkeepers
PRO
68
11k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
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