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
Pythonを読む
Search
tera
August 27, 2017
Technology
0
210
Pythonを読む
第9回 HojiroLTで話したスライドです。
tera
August 27, 2017
Tweet
Share
More Decks by tera
See All by tera
ピッてやるとシュッてなるやつを作った話
tera
0
190
importを理解したかった
tera
0
550
RustでShell作ろう
tera
0
880
HTTPをしゃべろう~Firefoxになるために~
tera
0
170
Other Decks in Technology
See All in Technology
KubeCon EU: Unlocking new Platform Experiences with Open Interfaces
salaboy
1
370
OCI Data Integration技術情報 / ocidi_technical_jp
oracle4engineer
PRO
1
1.5k
技術イベントはなんとかひねり出す 日経の技術広報の取り組み/techpr3
nishiuma
0
220
Kubeflow Pipelines v2 で変わる機械学習パイプライン開発
asei
4
340
オブジェクト指向CSSが叶えたかったことと、CSSのいま / The aims of Object-oriented CSS and the current state of CSS usage
shinkufencer
11
3.6k
統計的学習理論読み Chapter 2
kmatsui
1
200
チーム単位で保守性を高める:独自指標と向上にむけた実践
tarappo
0
300
ハイパフォーマンスな組織をつくるための開発生産性の考え方 / developer-productivity-high-performer-link-and-motivation
lmi
3
250
TypeScript Quiz (Encraft #12 Frontend Quiz Night)
uhyo
2
350
今さら聞けない!? AWSの生成AIサービス Amazon Bedrock入門!
minorun365
PRO
11
2.3k
ビジネスとコード品質の接合点 そしてコード品質がそこに及ぼす影響 / The Intersections of Business and Engineering, and The Impact of Code Quality There
mtx2s
10
1k
どう買う?Azure
kuniteru
1
190
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
How GitHub Uses GitHub to Build GitHub
holman
467
290k
Statistics for Hackers
jakevdp
789
220k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
225
51k
Robots, Beer and Maslow
schacon
PRO
154
7.9k
Side Projects
sachag
451
41k
Infographics Made Easy
chrislema
237
18k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
67
38k
Stop Working from a Prison Cell
hatefulcrawdad
265
19k
WebSockets: Embracing the real-time Web
robhawkes
59
6.9k
A Tale of Four Properties
chriscoyier
150
22k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
39
4.3k
Transcript
Pythonを読む
自己紹介 https://tera3939.github.io/
お話の流れ • CPythonのソースに挑む • 一旦C拡張について眺めてみる • もう一回挑んでみる
CPythonのソースに挑む
CPythonとは • Pythonの処理系には様々な種類がある ◦ pypy ◦ IronPython ◦ etc... •
その中でも最も有名なのがCPython • C言語で実装されている • ”Python”とだけかかれている場合は概ねCPythonのことを指す
ソースコードを手に入れる • GitHubからcloneしてくる ◦ git clone https://github.com/python/cpython.git • ディレクトリ内はこんな感じ
方針を立てる • 今回はビルトインのintについて見にいきたい • でも、どこを読めばいい?
intの場所を探す • Python Developer’s Guide 24.1 ◦ https://docs.python.org/devguide/exploring.html • ここにCPythonのソースのレイアウトが書いてある
ソースを眺める • Object/intobject.cとかある? → 無い…… • とりあえずPython/bltinmodule.cを眺めてみる
None
それっぽいのはあるけど…… • よくわかんない
C拡張について眺めてみる
CによるPythonの拡張 • Pythonインタプリタの拡張と埋め込み ◦ https://docs.python.jp/3/extending/index.html • Python/C API リファレンスマニュアル ◦
https://docs.python.jp/3/c-api/index.html
モジュール作成に必要な関数・構造体 • PyMethodDefの配列 ◦ メソッドテーブル ◦ メソッド名とメソッドへの関数ポインタ • PyModuleDef ◦
モジュール名、ドキュメント、メソッドテーブルの参照を持つ • PyMODINIT_FUNC PyInit_<modulename> ◦ <modulename> == PyModuleDefで定義したメソッド名 ◦ 事実上の__init__.py
型作成に必要な構造体 • 型自体の構造体 ◦ フィールドを持つ • PyTypeObject ◦ 型名、__hoge__メソッドとなる関数のポインタを持つ •
PyModuleDef • PyInit_<modulename> ◦ このInitがいつ呼ばれるかは謎 (たすけて) ◦ たぶんインポートされるとき ……
これさえ分かればどうにかなる!(???)
もう一度CPythonのソースに挑む
読んだ!
結果 • intはObjects/longobject.cに書いてある • -5~256の範囲の整数はsmall_intsという配列にキャッシュされている • その範囲内の整数は対応するsmall_intsのポインタを返す • そのためis演算子でその範囲の整数を比較するとTrueを返す ◦
a = 1; b = 1; a is b # -> True ◦ a = 500; b = 500; a is b # False
おわり