Are you familiar with the five-step Python code execution application? Want to learn more about the actual operation steps of Python code execution in actual operation? If you want to understand it, you can click on our article to get a better understanding of it.
The following process is required for Python code execution:
1. Tokenizer performs lexical analysis and splits the source program into tokens.
2. Parser creates CST based on the Token
3. CST is converted to AST
4. AST is compiled into bytecode
5. Execute the bytecode
When Python code is executed, Python calls the PyParser_ASTFromFile function to convert the code content of the file to AST:
- mod_ty
- PyParser_ASTFromFile(FILE *fp, const char *filename,
int start, char *ps1,
- char *ps2, PyCompilerFlags *flags, int *errcode,
- PyArena *arena)
- {
- mod_ty mod;
- perrdetail err;
- node *n = PyParser_ParseFileFlags(fp, filename,
&_PyParser_Grammar,
- start, ps1, ps2, &err, PARSER_FLAGS(flags));
- if (n) {
- mod = PyAST_FromNode(n, flags, filename, arena);PyNode_Free(n);
- return mod;
- }
- else {
- err_input(&err);
- if (errcode)
- *errerrcode = err.error;
- return NULL;
- }
- }
In
- PyParser_ParseFileFlags
After the file is converted to CST in the Python code, the PyAST_FromNode function converts CST to AST. This function is defined in include \ ast. h:
- PyAPI_FUNC(mod_ty) PyAST_FromNode(const node *, PyCompilerFlags *flags,
- const char *, PyArena *);
The above is an introduction to the relevant content for Python code execution. I forget you will get something.