IT業界、40年やってます (老人の独り言) 

現在68歳、IT関連で勉強したい事が盛りだくさんで、目移り状態です(^o^)

書籍: Cpython Internals を読んでます

今一番人気の Python の内部構造を解説した本です。

Python 3.9 バージョンを解説。(最新は 3.11 です)

ちなみに、 Cpython のソースコードGithub

https://github.com/python/cpython

です。名前の通り C言語で書かれてます。

cpython internals

目次
Foreword 13
Introduction 15
How to Use This Book . . . . . . . . . . . . . . . . . . . . 16
Bonus Material & Learning Resources . . . . . . . . . . . 17
Getting the CPython Source Code 21
Setting up Your Development Environment 24
IDE or Editor? . . . . . . . . . . . . . . . . . . . . . . . . 24
Setting up Visual Studio . . . . . . . . . . . . . . . . . . . 26
Setting up Visual Studio Code . . . . . . . . . . . . . . . . 28
Setting up JetBrains CLion . . . . . . . . . . . . . . . . . 33
Setting up Vim . . . . . . . . . . . . . . . . . . . . . . . . 37
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Compiling CPython 43
Compiling CPython on macOS . . . . . . . . . . . . . . . 44
Compiling CPython on Linux . . . . . . . . . . . . . . . . 46
Installing a Custom Version . . . . . . . . . . . . . . . . . 48
A Quick Primer on Make . . . . . . . . . . . . . . . . . . 48
CPython’s Make Targets . . . . . . . . . . . . . . . . . . . 50
Compiling CPython on Windows . . . . . . . . . . . . . . 52
Profile Guided Optimization . . . . . . . . . . . . . . . . 58
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 60

The Python Language and Grammar 61
Why CPython Is Written in C and Not Python . . . . . . . 62
The Python Language Specification . . . . . . . . . . . . . 64
Using the Parser Generator . . . . . . . . . . . . . . . . . 69
The Parser Generator . . . . . . . . . . . . . . . . . . . . 69
Regenerating Grammar . . . . . . . . . . . . . . . . . . . 70
A More Complex Example . . . . . . . . . . . . . . . . . . 75
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Con􀑀guration and Input 80
Configuration State . . . . . . . . . . . . . . . . . . . . . 83
Build Configuration . . . . . . . . . . . . . . . . . . . . . 86
Building a Module From Input . . . . . . . . . . . . . . . 87
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Lexing and Parsing with Syntax Trees 94
Concrete Syntax Tree Generation . . . . . . . . . . . . . . 95
The CPython Parser-Tokenizer . . . . . . . . . . . . . . . 98
Abstract Syntax Trees . . . . . . . . . . . . . . . . . . . . 103
Important Terms to Remember . . . . . . . . . . . . . . . 113
Example: Adding an Almost Equal Comparison Operator . 113
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 118
The Compiler 119
Related Source Files . . . . . . . . . . . . . . . . . . . . . 121
Important Terms . . . . . . . . . . . . . . . . . . . . . . 121
Instantiating a Compiler . . . . . . . . . . . . . . . . . . 122
Future Flags and Compiler Flags . . . . . . . . . . . . . . 123
Symbol Tables . . . . . . . . . . . . . . . . . . . . . . . . 125
Core Compilation Process . . . . . . . . . . . . . . . . . . 132
Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Creating a Code Object . . . . . . . . . . . . . . . . . . . 143
Using Instaviz to Show a Code Object . . . . . . . . . . . . 144
Example: Implementing the “Almost-Equal” Operator . . . 147
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 152

The Evaluation Loop 154
Stack Frames . . . . . . . . . . . . . . . . . . . . . . . . 155
Related Source Files . . . . . . . . . . . . . . . . . . . . . 156
Important Terms . . . . . . . . . . . . . . . . . . . . . . 156
Constructing Thread State . . . . . . . . . . . . . . . . . 156
Constructing Frame Objects . . . . . . . . . . . . . . . . . 158
Frame Execution . . . . . . . . . . . . . . . . . . . . . . 166
The Value Stack . . . . . . . . . . . . . . . . . . . . . . . 169
Example: Adding an Item to a List . . . . . . . . . . . . . 175
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Memory Management 182
Memory Allocation in C . . . . . . . . . . . . . . . . . . . 182
Design of the Python Memory Management System . . . . 186
The CPython Memory Allocator . . . . . . . . . . . . . . . 188
The Object and PyMem Memory Allocation Domains . . . 198
The Raw Memory Allocation Domain . . . . . . . . . . . . 202
Custom Domain Allocators . . . . . . . . . . . . . . . . . 202
Custom Memory Allocation Sanitizers . . . . . . . . . . . 203
The PyArena Memory Arena . . . . . . . . . . . . . . . . 206
Reference Counting . . . . . . . . . . . . . . . . . . . . . 207
Garbage Collection . . . . . . . . . . . . . . . . . . . . . 214
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Parallelism and Concurrency 226
Models of Parallelism and Concurrency . . . . . . . . . . . 228
The Structure of a Process . . . . . . . . . . . . . . . . . . 228
Multi-Process Parallelism . . . . . . . . . . . . . . . . . . 231
Multithreading . . . . . . . . . . . . . . . . . . . . . . . 255
Asynchronous Programming . . . . . . . . . . . . . . . . 268
Generators . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Asynchronous Generators . . . . . . . . . . . . . . . . . . 281
Subinterpreters . . . . . . . . . . . . . . . . . . . . . . . 282
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 286

Objects and Types 288
Examples in This Chapter . . . . . . . . . . . . . . . . . . 289
Builtin Types . . . . . . . . . . . . . . . . . . . . . . . . 290
Object and Variable Object Types . . . . . . . . . . . . . . 291
The type Type . . . . . . . . . . . . . . . . . . . . . . . . 292
Bool and Long Integer Type . . . . . . . . . . . . . . . . . 296
Unicode String Type . . . . . . . . . . . . . . . . . . . . . 301
Dictionary Type . . . . . . . . . . . . . . . . . . . . . . . 311
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 316
The Standard Library 318
Python Modules . . . . . . . . . . . . . . . . . . . . . . . 318
Python and C Modules . . . . . . . . . . . . . . . . . . . 320
The Test Suite 324
Running the Test Suite on Windows . . . . . . . . . . . . 324
Running the Test Suite on Linux/macOS . . . . . . . . . . 325
Test Flags . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Running Specific Tests . . . . . . . . . . . . . . . . . . . 326
Testing Modules . . . . . . . . . . . . . . . . . . . . . . . 328
Test Utilities . . . . . . . . . . . . . . . . . . . . . . . . . 329
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Debugging 331
Using the Crash Handler . . . . . . . . . . . . . . . . . . 332
Compiling Debug Support . . . . . . . . . . . . . . . . . . 332
Using Lldb for macOS . . . . . . . . . . . . . . . . . . . . 333
Using Gdb . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Using Visual Studio Debugger . . . . . . . . . . . . . . . . 340
Using CLion Debugger . . . . . . . . . . . . . . . . . . . 342
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 352

Benchmarking, Pro􀑀ling, and Tracing 353
Using Timeit for Micro-Benchmarks . . . . . . . . . . . . 354
Using the Python Benchmark Suite for Runtime Benchmarks 356
Profiling Python Code with cProfile . . . . . . . . . . . . . 362
Profiling C Code with Dtrace . . . . . . . . . . . . . . . . 365
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 370

Writing C Extensions for CPython . . . . . . . . . . . . . 371
Using This Knowledge to Improve Your Python Applications 372
Using This Knowledge to Contribute to the CPython Project 373
Keep Learning . . . . . . . . . . . . . . . . . . . . . . . . 376

Appendix 1 : Introduction to C for Python Programmers 378
C Preprocessor . . . . . . . . . . . . . . . . . . . . . . . 378
Basic C Syntax . . . . . . . . . . . . . . . . . . . . . . . . 381
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 389

 

 

読み始めたばかりですが、 VS Code のデバッガーを使って動きを追ってみました。

 

今まで PHPPython のプログラムは VS Code のデバッガーで動きを確認してましたが、C言語のデバッガーは初めてで、Windows の WSL2 Ubuntu 18.02 上の Cpython でのデバッグ環境構築は少々手こずりました。