A quick overview of the Python Virtual Machine — Pt. 1

  1. The Python Virtual Machine loads all the necessary dependencies and sets all the necessary contents, this is made by the Py_Initialize function.
  2. After this initialization, a function called run_file is executed in order to load the Python scripts.
  3. Py_Main executes PyRun_SimpleFileExFlags and generates the __main__ namespace of the Python program.
  4. PyParser_ASTFromFileObject is called, and calls the PyParser_ParseFileObjects. They create a parse object tree, which is something that will be translated into instructions later, the name tree suggests that these objects hold pointers to other objects.
  5. In the end it calls PyParser_ASTFromNodeObject that will be final step of the parsing, the AST is the Abstract Syntax Tree, you can also have access to this part from the python code, this is an amazing meta-programming technique for Python programs, it’s also similar to other implementations, such as Julia and LUA. Also as the Python documentation describes, the abstract syntax tree changes in all python releases.
  1. run_mod calls PyAST_CompileObject and the bytecode is generated.
  2. run_mod calls PyEval_EvalCode and the final code objects or bytecodes are created. These code objects represent your Python code, but now in a way that the virtual machine can understand and execute.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store