Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda