Terminology

Python is the programming language, while CPython is the default Python interpreter (that is written in C). Python is defined as both a compiled & interpreted language.


Marshalling refers to the serialization of Python objects so that they can be stored (as bytes) Freezing is to package Python scripts into a single executable


.pyc files contains the compiled Python byte code .pyd files are frozen Python modules (i.e. DLL files made using Python)

How CPython Works

https://tenthousandmeters.com/

Untitled

  1. Python source code .py gets compiled to Python byte code .pyc
  2. Byte code & library modules gets passed to the Python virtual machine
  3. Virtual machine interprets the byte code & executes it

Python Byte Code

File Format .pyc

https://nowave.it/python-bytecode-analysis-1.html

https://nowave.it/python-bytecode-analysis-1.html

Code Objects

https://late.am/post/2012/03/26/exploring-python-code-objects.html https://blog.svenskithesource.be/posts/code-objects

When we compile source code in Python, the result is a code object. These objects contain lots of metadata, and are especially useful in cases where it is difficult to recover the source code. Eventually, these marshalled objects will get passed to CPython as a PyCodeObject struct to be interpreted.

// <https://github.com/python/cpython/blob/3.10/Include/cpython/code.h>
struct PyCodeObject {
    PyObject_HEAD
    int co_argcount;            /* #arguments, except *args */
    int co_posonlyargcount;     /* #positional only arguments */
    int co_kwonlyargcount;      /* #keyword only arguments */
    int co_nlocals;             /* #local variables */
    int co_stacksize;           /* #entries needed for evaluation stack */
    int co_flags;               /* CO_..., see below */
    int co_firstlineno;         /* first source line number */
    PyObject *co_code;          /* instruction opcodes */
    PyObject *co_consts;        /* list (constants used) */
    PyObject *co_names;         /* list of strings (names used) */
    PyObject *co_varnames;      /* tuple of strings (local variable names) */
    PyObject *co_freevars;      /* tuple of strings (free variable names) */
    PyObject *co_cellvars;      /* tuple of strings (cell variable names) */
    Py_ssize_t *co_cell2arg;    /* Maps cell vars which are arguments. */
    PyObject *co_filename;      /* unicode (where it was loaded from) */
    PyObject *co_name;          /* unicode (name, for reference) */
    PyObject *co_linetable;     /* string (encoding addr<->lineno mapping) */
    // truncated ...
};