|
| 1 | +# CPython source code |
| 2 | + |
| 3 | +This section gives an overview of CPython's code structure and provides |
| 4 | +a summary of file locations for modules and built-ins. |
| 5 | + |
| 6 | + |
| 7 | +## Source code layout |
| 8 | + |
| 9 | +For a Python module, the typical layout is: |
| 10 | + |
| 11 | +* `Lib/<module>.py` |
| 12 | +* `Modules/_<module>.c` (if there's also a C accelerator module) |
| 13 | +* `Lib/test/test_<module>.py` |
| 14 | +* `Doc/library/<module>.rst` |
| 15 | + |
| 16 | +For an extension module, the typical layout is: |
| 17 | + |
| 18 | +* `Modules/<module>module.c` |
| 19 | +* `Lib/test/test_<module>.py` |
| 20 | +* `Doc/library/<module>.rst` |
| 21 | + |
| 22 | +For builtin types, the typical layout is: |
| 23 | + |
| 24 | +* `Objects/<builtin>object.c` |
| 25 | +* `Lib/test/test_<builtin>.py` |
| 26 | +* [`Doc/library/stdtypes.rst`](../Doc/library/stdtypes.rst) |
| 27 | + |
| 28 | +For builtin functions, the typical layout is: |
| 29 | + |
| 30 | +* [`Python/bltinmodule.c`](../Python/bltinmodule.c) |
| 31 | +* [`Lib/test/test_builtin.py`](../Lib/test/test_builtin.py) |
| 32 | +* [`Doc/library/functions.rst`](../Doc/library/functions.rst) |
| 33 | + |
| 34 | +Some exceptions to these layouts are: |
| 35 | + |
| 36 | +* built-in type `int` is at [`Objects/longobject.c`](../Objects/longobject.c) |
| 37 | +* built-in type `str` is at [`Objects/unicodeobject.c`](../Objects/unicodeobject.c) |
| 38 | +* built-in module `sys` is at [`Python/sysmodule.c`](../Python/sysmodule.c) |
| 39 | +* built-in module `marshal` is at [`Python/marshal.c`](../Python/marshal.c) |
| 40 | +* Windows-only module `winreg` is at [`PC/winreg.c`](../PC/winreg.c) |
| 41 | + |
| 42 | + |
| 43 | +## Additional references |
| 44 | + |
| 45 | +The CPython code base is constantly changing and evolving. |
| 46 | +Here's a sample of references about CPython's architecture aimed at |
| 47 | +building your understanding of CPython internals and its evolution: |
| 48 | + |
| 49 | + |
| 50 | +### Current references |
| 51 | + |
| 52 | +| Title | Brief | Author | Version | |
| 53 | +|------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------|------------------|---------| |
| 54 | +| [A guide from parser to objects, observed using GDB](https://hackmd.io/s/ByMHBMjFe) | Code walk from Parser, AST, Sym Table and Objects | Louie Lu | 3.7.a0 | |
| 55 | +| [Green Tree Snakes](https://greentreesnakes.readthedocs.io/en/latest/) | The missing Python AST docs | Thomas Kluyver | 3.6 | |
| 56 | +| [Yet another guided tour of CPython](https://paper.dropbox.com/doc/Yet-another-guided-tour-of-CPython-XY7KgFGn88zMNivGJ4Jzv) | A guide for how CPython REPL works | Guido van Rossum | 3.5 | |
| 57 | +| [Python Asynchronous I/O Walkthrough](https://www.youtube.com/playlist?list=PLpEcQSRWP2IjVRlTUptdD05kG-UkJynQT) | How CPython async I/O, generator and coroutine works | Philip Guo | 3.5 | |
| 58 | +| [Coding Patterns for Python Extensions](https://pythonextensionpatterns.readthedocs.io/en/latest/) | Reliable patterns of coding Python Extensions in C | Paul Ross | 3.9+ | |
| 59 | +| [Your Guide to the CPython Source Code](https://realpython.com/cpython-source-code-guide/) | Your Guide to the CPython Source Code | Anthony Shaw | 3.8 | |
| 60 | + |
| 61 | + |
| 62 | +### Historical references |
| 63 | + |
| 64 | +| Title | Brief | Author | Version | |
| 65 | +|---------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|-----------------|---------| |
| 66 | +| [Python's Innards Series](https://tech.blog.aknin.name/category/my-projects/pythons-innards/) | ceval, objects, pystate and miscellaneous topics | Yaniv Aknin | 3.1 | |
| 67 | +| [Eli Bendersky's Python Internals](https://eli.thegreenplace.net/tag/python-internals) | Objects, Symbol tables and miscellaneous topics | Eli Bendersky | 3.x | |
| 68 | +| [A guide from parser to objects, observed using Eclipse](https://docs.google.com/document/d/1nzNN1jeNCC_bg1LADCvtTuGKvcyMskV1w8Ad2iLlwoI/) | Code walk from Parser, AST, Sym Table and Objects | Prashanth Raghu | 2.7.12 | |
| 69 | +| [CPython internals: A ten-hour codewalk through the Python interpreter source code](https://www.youtube.com/playlist?list=PLzV58Zm8FuBL6OAv1Yu6AwXZrnsFbbR0S) | Code walk from source code to generators | Philip Guo | 2.7.8 | |
0 commit comments