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
90
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
0
69
Deep dive in a SQL query (PgIbz)
jespino
2
73
Understanding the go runtime
jespino
0
80
Tests end-to-end en entornos complejos con testcontainers
jespino
0
20
Dissecting Slices, Maps and Channels in Go
jespino
0
44
Understanding the memory model of 5 languages
jespino
0
100
The Javascript Toolset
jespino
1
76
Deep dive in a SQL query
jespino
0
80
10 Aha! Moments reading the go source code
jespino
0
96
Other Decks in Programming
See All in Programming
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
310
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
160
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
9
2.1k
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
250
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
190
Jakarta EE meets AI
ivargrimstad
0
340
Jaspr Dart Web Framework 박제창 @Devfest 2024
itsmedreamwalker
0
120
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.1k
ドメインイベント増えすぎ問題
h0r15h0
2
480
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
270
103 Early Hints
sugi_0000
1
280
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
1
300
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.6k
Designing Experiences People Love
moore
139
23k
Visualization
eitanlees
146
15k
Mobile First: as difficult as doing things right
swwweet
222
9k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Docker and Python
trallard
43
3.2k
The Cost Of JavaScript in 2023
addyosmani
46
7k
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