Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
How is CPython implemented?

So I lately came across an explanation for Python's interpreter and compiler (CPython specifically).

Please correct me if I'm wrong. I just want to be sure I understand these specific concepts.

So CPython gets both compiled (to bytecode) and then interpreted (in the PVM)? And what does the PVM do exactly? Does it read the bytecode line by line, and translate each one to binary instructions that can be executed on a specific computer? Does this mean that a computer based on an Intel processor needs a different PVM from an AMD-based computer?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  1. Yes, CPython is compiled to bytecode which is then executed by the virtual machine.
  2. The virtual machine executes instructions one-by-one. It's written in C (but you can write it in another language) and looks like a huge if/else statement like "if the current instruction is this, do this; if the instruction is this, do another thing", and so on. Instructions aren't translated to binary - that's why it's called an interpreter.
    1. You can find the list of instructions here: https://docs.python.org/3.10/library/dis.html#python-bytecode-instructions
    2. The implementation of the VM is available here: https://github.com/python/cpython/blob/f71a69aa9209cf67cc1060051b147d6afa379bba/Python/ceval.c#L1718
  3. Bytecode doesn't have a concept of "line": it's just a stream of bytes. The interpreter can read one byte at a time and use another if/else statement to decide what instruction it's looking at. For example:
    curr_byte = read_byte()
    if curr_byte == 0x00:
       # Parse instruction with no arguments
       curr_instruction = DO_THING_A;
       args = NULL;
    elif curr_byte == 0x01:
       another_byte = read_byte()
       if another_byte == 0x00:
          # Parse a two-byte instruction
          curr_instruction = DO_THING_B;
          args = NULL;
       else:
          # Parse a one-byte instruction
          # with one argument
          curr_instruction = DO_THING_C;
          args = another_byte >> 1; # or whatever
    elif curr_byte == ...:
       ... # go on and on and on
    
  4. The entire point of bytecode is that it can be executed by another program (the interpreter, or virtual machine) on almost any hardware. For example, in order to get CPython running on new hardware, you'll need a C toolchain (compiler, linker, assembler etc) for this hardware and a bunch of functions that Python can call to do low-level stuff (allocate memory, output text, do networking etc). Once you have that, write C code that can execute the bytecode - and that's it.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!