SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
Reading CPython
Akira Nonaka
May 28, 2021
1
Why?
2
Tools
1. CLion
3
Getting source code
1. https://github.com/python/cpython
2. tag v3.10.0b1
4
Build
1. ./configure
2. make (took 3 min on my 2012 Mac)
3. ./python.exe
4. Python 3.10.0b1 (tags/v3.10.0b1:ba4217537c, May 25
2021, 10:09:16) [Clang 12.0.0 (clang-1200.0.32.29)] on
darwin
Type "help", "copyright", "credits" or "license" for more
information.
>>>
5
Directories
•Python/
•Python interpreter code
•Objects/
•Built in objects
•Modules/
•Standard libraries, built in modules
•Programs/
• Main entry point.
6
Directories
•Grammar/
•Python syntax definition in PEG
•PEP 617 -- New PEG parser for CPython
•Parser/
•Language parser (generated from PEG grammar)
•Tools/
•Supporting tools to build python including peg_generator
7
Main Loop
8
Read–eval–print loop
• We call it REPL!
• Read
• Eval
• Print
9
Demo: Finding REPL
10
Read-eval-print loop
•PyRun_InteractiveOneObjectEx() in pythonrun.c
•Run parser.
• _PyParser_ASTFromFile()
• Parser read tokens from input.
• Convert to AST
•Run the AST
• run_mod(mod, filename, d, d, flags, arena)
• Compile AST to byte code
• Run byte code
• https://github.com/python/cpython/blob/5d569ef9dd57cf03473ef0c04f0e58b6c5cb5d04/
Python/pythonrun.c#L195
11
run_mod(mod, filename, d, d, flags,
arena);
•Execute the module (AST)
•PyCodeObject *co = _PyAST_Compile(mod, filename, flags,
-1, arena);
• Compile AST into code object
•PyObject *v = run_eval_code_obj(tstate, co, globals, locals);
•Execute code object
•Using thread state, globals and locals
12
Parsing
13
Parser
A parser is a software component that takes input data
(frequently text) and builds a data structure – often some
kind of parse tree, abstract syntax tree or other
hierarchical structure, giving a structural representation
of the input while checking for correct syntax.
14
Parser for CPython
1. Parser/parser.c
2. Do not try to read it!
3. $ wc -l parser.c
32831 parser.c
4. It is NOT hand written!
5. $ head -1 parser.c
// @generated by pegen.py from ./Grammar/
python.gram
15
Parser Generator
•Input: Grammar/python.gram
•Output: Parser/Parser/parser.c
•Parser Generator
• Tools/peg_generator/pegen/
• This is python module.
• So, in order to build python, python is required.
• PEP 617 -- New PEG parser for CPython
• https://www.python.org/dev/peps/pep-0617/
16
PEG parser
•Introduced in 3.9
•Both classic parser and new PEG parser co-exists in the
source tree.
•You can switch back to the classic parser using a
command line switch (-X oldparser)
•In 3.10, all the classic parser code is removed. Thus,
code is much cleaner and easy to read.
17
Tokenizer
•tok_get() in tokenizer.c
•Read text from input and return token.
•Tokens (sometimes called terminal symbol) are
•Keywords
•Variable names
•Numbers
•Etc.
18
Demo: Changing Grammar
1. vi Grammar/python.gram
2. make regen-pegen
3. make
4. ./python.exe
5. demo
19
Python code block
20
Python Indentation
1. Most of the programming languages like C, C++, and Java
use braces { } to define a block of code. Python, however,
uses indentation.
2. A code block (body of a function, loop, etc.) starts with
indentation and ends with the first unindented line.
3. The amount of indentation is up to you, but it must be
consistent throughout that block.
4. https://www.programiz.com/python-programming/
statement-indentation-comments
21
Tokenizer
•Stateful, line oriented.
•struct tok_state in tokenizer.h
•Remembers all indentation column position on the indent-stack.
•When indent gets deeper, it pushes the column position on the stack.
Returns virtual token INDENT
•When indent gets shallower, it pops the stack. Returns virtual token
DEDENT.
•https://github.com/python/cpython/blob/
28be3191a9db2769ed05e55c6bcbccdd029656dd/Parser/
tokenizer.c#L1205
22
struct tok_state
•Holds the state of the tokenizer
•Line buffer
•Indent stack
•etc.
•https://github.com/python/cpython/blob/
28be3191a9db2769ed05e55c6bcbccdd029656dd/
Parser/tokenizer.h#L31
23
Block definition in PEG
block[asdl_stmt_seq*] (memo):
| NEWLINE INDENT a=statements DEDENT { a }
| simple_stmts
| invalid_block
24

Weitere ähnliche Inhalte

Mehr von Xoxzo Inc.

Linear algebra power of abstraction - LearnDay@Xoxzo #5
Linear algebra power of abstraction - LearnDay@Xoxzo #5Linear algebra power of abstraction - LearnDay@Xoxzo #5
Linear algebra power of abstraction - LearnDay@Xoxzo #5Xoxzo Inc.
 
The Takumi Method - LearnDay@Xoxzo #1
The Takumi Method - LearnDay@Xoxzo #1The Takumi Method - LearnDay@Xoxzo #1
The Takumi Method - LearnDay@Xoxzo #1Xoxzo Inc.
 
Django osc2018-okinawa
Django osc2018-okinawaDjango osc2018-okinawa
Django osc2018-okinawaXoxzo Inc.
 
多要素認証やSMSマーケティングを支えるテレフォニーAPI活用入門
多要素認証やSMSマーケティングを支えるテレフォニーAPI活用入門多要素認証やSMSマーケティングを支えるテレフォニーAPI活用入門
多要素認証やSMSマーケティングを支えるテレフォニーAPI活用入門Xoxzo Inc.
 
Working in a Fully Remote Organization: Challenges and How We Made it Work - ...
Working in a Fully Remote Organization: Challenges and How We Made it Work - ...Working in a Fully Remote Organization: Challenges and How We Made it Work - ...
Working in a Fully Remote Organization: Challenges and How We Made it Work - ...Xoxzo Inc.
 
JomLaunch 5.0 Sep 2017 by Iqbal Abdullah
JomLaunch 5.0 Sep 2017 by Iqbal AbdullahJomLaunch 5.0 Sep 2017 by Iqbal Abdullah
JomLaunch 5.0 Sep 2017 by Iqbal AbdullahXoxzo Inc.
 
Introduction to Data Analysis
Introduction to Data AnalysisIntroduction to Data Analysis
Introduction to Data AnalysisXoxzo Inc.
 
XoxzoテレフォニーAPI入門2017
XoxzoテレフォニーAPI入門2017XoxzoテレフォニーAPI入門2017
XoxzoテレフォニーAPI入門2017Xoxzo Inc.
 
初心者のためのPythonによるWebAPI活用方入門
初心者のためのPythonによるWebAPI活用方入門初心者のためのPythonによるWebAPI活用方入門
初心者のためのPythonによるWebAPI活用方入門Xoxzo Inc.
 
djangoのmigrationはどう動いているか
djangoのmigrationはどう動いているかdjangoのmigrationはどう動いているか
djangoのmigrationはどう動いているかXoxzo Inc.
 
PyCon APAC 2017
PyCon APAC 2017PyCon APAC 2017
PyCon APAC 2017Xoxzo Inc.
 
Pythonの隠れた武器
Pythonの隠れた武器Pythonの隠れた武器
Pythonの隠れた武器Xoxzo Inc.
 
The Secret Weapon Of Python
The Secret Weapon Of PythonThe Secret Weapon Of Python
The Secret Weapon Of PythonXoxzo Inc.
 

Mehr von Xoxzo Inc. (13)

Linear algebra power of abstraction - LearnDay@Xoxzo #5
Linear algebra power of abstraction - LearnDay@Xoxzo #5Linear algebra power of abstraction - LearnDay@Xoxzo #5
Linear algebra power of abstraction - LearnDay@Xoxzo #5
 
The Takumi Method - LearnDay@Xoxzo #1
The Takumi Method - LearnDay@Xoxzo #1The Takumi Method - LearnDay@Xoxzo #1
The Takumi Method - LearnDay@Xoxzo #1
 
Django osc2018-okinawa
Django osc2018-okinawaDjango osc2018-okinawa
Django osc2018-okinawa
 
多要素認証やSMSマーケティングを支えるテレフォニーAPI活用入門
多要素認証やSMSマーケティングを支えるテレフォニーAPI活用入門多要素認証やSMSマーケティングを支えるテレフォニーAPI活用入門
多要素認証やSMSマーケティングを支えるテレフォニーAPI活用入門
 
Working in a Fully Remote Organization: Challenges and How We Made it Work - ...
Working in a Fully Remote Organization: Challenges and How We Made it Work - ...Working in a Fully Remote Organization: Challenges and How We Made it Work - ...
Working in a Fully Remote Organization: Challenges and How We Made it Work - ...
 
JomLaunch 5.0 Sep 2017 by Iqbal Abdullah
JomLaunch 5.0 Sep 2017 by Iqbal AbdullahJomLaunch 5.0 Sep 2017 by Iqbal Abdullah
JomLaunch 5.0 Sep 2017 by Iqbal Abdullah
 
Introduction to Data Analysis
Introduction to Data AnalysisIntroduction to Data Analysis
Introduction to Data Analysis
 
XoxzoテレフォニーAPI入門2017
XoxzoテレフォニーAPI入門2017XoxzoテレフォニーAPI入門2017
XoxzoテレフォニーAPI入門2017
 
初心者のためのPythonによるWebAPI活用方入門
初心者のためのPythonによるWebAPI活用方入門初心者のためのPythonによるWebAPI活用方入門
初心者のためのPythonによるWebAPI活用方入門
 
djangoのmigrationはどう動いているか
djangoのmigrationはどう動いているかdjangoのmigrationはどう動いているか
djangoのmigrationはどう動いているか
 
PyCon APAC 2017
PyCon APAC 2017PyCon APAC 2017
PyCon APAC 2017
 
Pythonの隠れた武器
Pythonの隠れた武器Pythonの隠れた武器
Pythonの隠れた武器
 
The Secret Weapon Of Python
The Secret Weapon Of PythonThe Secret Weapon Of Python
The Secret Weapon Of Python
 

Kürzlich hochgeladen

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 

Kürzlich hochgeladen (20)

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 

Reading c python by Akira Learnday at Xoxzo

  • 4. Getting source code 1. https://github.com/python/cpython 2. tag v3.10.0b1 4
  • 5. Build 1. ./configure 2. make (took 3 min on my 2012 Mac) 3. ./python.exe 4. Python 3.10.0b1 (tags/v3.10.0b1:ba4217537c, May 25 2021, 10:09:16) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 5
  • 6. Directories •Python/ •Python interpreter code •Objects/ •Built in objects •Modules/ •Standard libraries, built in modules •Programs/ • Main entry point. 6
  • 7. Directories •Grammar/ •Python syntax definition in PEG •PEP 617 -- New PEG parser for CPython •Parser/ •Language parser (generated from PEG grammar) •Tools/ •Supporting tools to build python including peg_generator 7
  • 9. Read–eval–print loop • We call it REPL! • Read • Eval • Print 9
  • 11. Read-eval-print loop •PyRun_InteractiveOneObjectEx() in pythonrun.c •Run parser. • _PyParser_ASTFromFile() • Parser read tokens from input. • Convert to AST •Run the AST • run_mod(mod, filename, d, d, flags, arena) • Compile AST to byte code • Run byte code • https://github.com/python/cpython/blob/5d569ef9dd57cf03473ef0c04f0e58b6c5cb5d04/ Python/pythonrun.c#L195 11
  • 12. run_mod(mod, filename, d, d, flags, arena); •Execute the module (AST) •PyCodeObject *co = _PyAST_Compile(mod, filename, flags, -1, arena); • Compile AST into code object •PyObject *v = run_eval_code_obj(tstate, co, globals, locals); •Execute code object •Using thread state, globals and locals 12
  • 14. Parser A parser is a software component that takes input data (frequently text) and builds a data structure – often some kind of parse tree, abstract syntax tree or other hierarchical structure, giving a structural representation of the input while checking for correct syntax. 14
  • 15. Parser for CPython 1. Parser/parser.c 2. Do not try to read it! 3. $ wc -l parser.c 32831 parser.c 4. It is NOT hand written! 5. $ head -1 parser.c // @generated by pegen.py from ./Grammar/ python.gram 15
  • 16. Parser Generator •Input: Grammar/python.gram •Output: Parser/Parser/parser.c •Parser Generator • Tools/peg_generator/pegen/ • This is python module. • So, in order to build python, python is required. • PEP 617 -- New PEG parser for CPython • https://www.python.org/dev/peps/pep-0617/ 16
  • 17. PEG parser •Introduced in 3.9 •Both classic parser and new PEG parser co-exists in the source tree. •You can switch back to the classic parser using a command line switch (-X oldparser) •In 3.10, all the classic parser code is removed. Thus, code is much cleaner and easy to read. 17
  • 18. Tokenizer •tok_get() in tokenizer.c •Read text from input and return token. •Tokens (sometimes called terminal symbol) are •Keywords •Variable names •Numbers •Etc. 18
  • 19. Demo: Changing Grammar 1. vi Grammar/python.gram 2. make regen-pegen 3. make 4. ./python.exe 5. demo 19
  • 21. Python Indentation 1. Most of the programming languages like C, C++, and Java use braces { } to define a block of code. Python, however, uses indentation. 2. A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line. 3. The amount of indentation is up to you, but it must be consistent throughout that block. 4. https://www.programiz.com/python-programming/ statement-indentation-comments 21
  • 22. Tokenizer •Stateful, line oriented. •struct tok_state in tokenizer.h •Remembers all indentation column position on the indent-stack. •When indent gets deeper, it pushes the column position on the stack. Returns virtual token INDENT •When indent gets shallower, it pops the stack. Returns virtual token DEDENT. •https://github.com/python/cpython/blob/ 28be3191a9db2769ed05e55c6bcbccdd029656dd/Parser/ tokenizer.c#L1205 22
  • 23. struct tok_state •Holds the state of the tokenizer •Line buffer •Indent stack •etc. •https://github.com/python/cpython/blob/ 28be3191a9db2769ed05e55c6bcbccdd029656dd/ Parser/tokenizer.h#L31 23
  • 24. Block definition in PEG block[asdl_stmt_seq*] (memo): | NEWLINE INDENT a=statements DEDENT { a } | simple_stmts | invalid_block 24