A code object is the compiled form of a module, a function, a class body or a lambda.
The body of a .pyc file is the module’s code object, and each function or
class defined in it is another code object inside co_consts. In the file a
code object is the type byte c, then the fields below, in order, with
nothing between them.
Fields
The numbers are 32-bit little-endian integers. The other fields are whole
marshal objects, each with its own type
byte. The strings holding names are u text.
| Field | Stored as | What it holds |
|---|---|---|
co_argcount | 32-bit number | How many positional parameters the code has |
co_kwonlyargcount | 32-bit number | How many keyword-only parameters the code has |
co_nlocals | 32-bit number | How many local variables, parameters included |
co_stacksize | 32-bit number | The most items the code needs on the stack at one time |
co_flags | 32-bit number | Facts about the code, as bits: see Flags |
co_code | s bytes | The bytes of the instructions |
co_consts | tuple | The constants the code uses, referred to by index. Nested functions and classes are here as code objects. A function’s first constant is its documentation string, or None |
co_names | tuple of strings | Names used as attributes and as global names |
co_varnames | tuple of strings | The names of the local variables, parameters first |
co_freevars | tuple of strings | Free variables: names this code uses but an enclosing function defines |
co_cellvars | tuple of strings | Local variables that nested functions refer to |
co_filename | string | The path of the source file as given to the compiler |
co_name | string | The name of the function or class, or <module> for a module |
co_firstlineno | 32-bit number | The source line the code starts at |
co_lnotab | s bytes | The line number table |
Files with a magic number below 3030, all of them early pre-releases, have no
co_kwonlyargcount.
Flags
co_flags is a set of bits, from CPython’s Include/code.h.
In 3.0 that header has the __future__ bits in a disabled block marked as
no longer used, so 3.0 does not define them; 3.1 and 3.2 do. A bit named CO_FUTURE_… says the matching
from __future__ import was in effect.
| Bit | Name | Meaning | Releases |
|---|---|---|---|
| 0x0001 | CO_OPTIMIZED | The code uses fast local variables | All |
| 0x0002 | CO_NEWLOCALS | A new dictionary is made for the locals when the code runs | All |
| 0x0004 | CO_VARARGS | The code has a *args parameter | All |
| 0x0008 | CO_VARKEYWORDS | The code has a **kwargs parameter | All |
| 0x0010 | CO_NESTED | The code is a nested function | All |
| 0x0020 | CO_GENERATOR | The code is a generator function | All |
| 0x0040 | CO_NOFREE | The code has no free or cell variables | All |
| 0x2000 | CO_FUTURE_DIVISION | division | 3.1, 3.2 |
| 0x4000 | CO_FUTURE_ABSOLUTE_IMPORT | absolute_import | 3.1, 3.2 |
| 0x8000 | CO_FUTURE_WITH_STATEMENT | with_statement | 3.1, 3.2 |
| 0x10000 | CO_FUTURE_PRINT_FUNCTION | print_function | 3.1, 3.2 |
| 0x20000 | CO_FUTURE_UNICODE_LITERALS | unicode_literals | 3.1, 3.2 |
| 0x40000 | CO_FUTURE_BARRY_AS_BDFL | barry_as_FLUFL | 3.1, 3.2 |