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
Deep dive into the select statment
jespino
0
13
Exploring a complex codebase with AI
jespino
0
8
Explorando el Open Source con IA
jespino
0
7
Deep dive into a go binary
jespino
1
170
Deep dive in a SQL query (PgIbz)
jespino
2
120
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
66
Understanding the memory model of 5 languages
jespino
0
160
Other Decks in Programming
See All in Programming
Google Opalで使える37のライブラリ
mickey_kubo
2
120
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
500
エンジニアインターン「Treasure」とHonoの2年、そして未来へ / Our Journey with Hono Two Years at Treasure and Beyond
carta_engineering
0
370
What's new in Spring Modulith?
olivergierke
1
160
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
340
All About Angular's New Signal Forms
manfredsteyer
PRO
0
200
Developer Joy - The New Paradigm
hollycummins
1
280
Building, Deploying, and Monitoring Ruby Web Applications with Falcon (Kaigi on Rails 2025)
ioquatix
4
2.4k
Domain-centric? Why Hexagonal, Onion, and Clean Architecture Are Answers to the Wrong Question
olivergierke
3
930
Goで実践するドメイン駆動開発 AIと歩み始めた新規プロダクト開発の現在地
imkaoru
4
870
理論と実務のギャップを超える
eycjur
0
170
CSC305 Lecture 08
javiergs
PRO
0
250
Featured
See All Featured
Making Projects Easy
brettharned
120
6.4k
Scaling GitHub
holman
463
140k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
What's in a price? How to price your products and services
michaelherold
246
12k
4 Signs Your Business is Dying
shpigford
185
22k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
23
1.5k
Context Engineering - Making Every Token Count
addyosmani
7
270
Agile that works and the tools we love
rasmusluckow
331
21k
The Cult of Friendly URLs
andyhume
79
6.6k
The Cost Of JavaScript in 2023
addyosmani
55
9k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
990
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
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