PERSONAL BLOG
Category: Programming

Python: interpreter, bytecode, PVM

Feb. 17, 2024

Python is interpreted programming language. He doesn't convert the code into machine code (unlike C or C++). Instead, Python interpreter transfers the programming code into bytecode, which is executed on the PVM (Python Virtual Machine). Let's see how it works using an example of the most popular interpreter implementation - CPython.

Interpreter is a program, which converts your python instructions into bytecode and executes them. In fact, interpreter is an interlayer between the source code and hardware. There are two types of interpreters:

  • Simple interpreter. It takes an instruction, translate and immediately execute it. Then takes next instruction.
  • "Compiled interpreter". The system consists of a compiler and an interpreter. The compiler translates the source programming code into bytecode, and then the interpreter (PVM) executes it.

CPython:

  • "Compiled interpreter" 
  • CPython is the default and most widely used implementation of the Python language
  • Written in C
  • The source code of CPython is opened

In addition, the CPython interpreter has a feature - it can work in dialog mode (REPL - read-eval-print loop). The interpreter reads the completed language construct, executes it, prints the results, and proceeds to wait for the user to enter the next instruction.

How CPython executes programs

The Python interpreter executes any program step by step.

Step 1. Initialization

After the program is launched, Python-interpreter reads the code, checks the formatting and syntax. If an error is found, the interpreter immediately stops and displays an error warning. Also, at this steps some preparation steps happen:

  • parsing command line arguments
  • setting of program flags
  • environment variables reading
  • etc

Step 2. Compilation

CPython translates the source code into bytecode (low-level, platform-independent representation of the source code). It necessary for the performance purposes - bytecode is executed much faster than the source instructions.

If the interpreter has the write access, it'll save the bytecode as a *.pyc module. If the source code hasn't been changed since the last compilation, next time the program is launched CPython will load the *.pyc file excluding the compilation process (which improves the performance).

Step 3. Execution

Once the bytecode is compiled it's sent to the Python Virtual Machine. This is where the bytecode is executed. If an error happens during the execution, the interpreter stops immediately and returns an error message

PVM is a part of a Python interpreter. In fact, this is a big loop, which iterates through the bytecode instructions and performs the corresponding operations.