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
120
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Lightning Talk "Opening CPython Built-in Classes" (Europython)
Jesús Espino
July 24, 2015
More Decks by Jesús Espino
See All by Jesús Espino
Brewed for Speed: How Go’s Green Tea GC Works
jespino
0
40
Deep dive into the select statment
jespino
0
40
Exploring a complex codebase with AI
jespino
0
35
Explorando el Open Source con IA
jespino
0
25
Deep dive into a go binary
jespino
1
260
Deep dive in a SQL query (PgIbz)
jespino
2
190
Understanding the go runtime
jespino
1
200
Tests end-to-end en entornos complejos con testcontainers
jespino
0
65
Dissecting Slices, Maps and Channels in Go
jespino
0
110
Other Decks in Programming
See All in Programming
『コードを書く以外の』エンジニアリング〜課金基盤移行プロジェクト推進のためのTips4選
yuriko1211
0
560
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
300
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
170
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
170
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
2
310
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
720
霧の中の代数的エフェクト
funnyycat
1
450
Google Apps Script で Ruby を動かす
kawahara
0
120
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
110
メールのエイリアス機能を履き違えない
isshinfunada
0
190
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
360
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
130
Featured
See All Featured
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
750
The World Runs on Bad Software
bkeepers
PRO
72
12k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
230
Bash Introduction
62gerente
615
220k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
430
How STYLIGHT went responsive
nonsquared
100
6.2k
Navigating Team Friction
lara
192
16k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
450
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Between Models and Reality
mayunak
4
380
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