1.. highlight:: c
2
3.. index:: object; code, code object
4
5.. _codeobjects:
6
7Code Objects
8------------
9
10.. sectionauthor:: Jeffrey Yasskin <[email protected]>
11
12Code objects are a low-level detail of the CPython implementation.
13Each one represents a chunk of executable code that hasn't yet been
14bound into a function.
15
16.. c:type:: PyCodeObject
17
18   The C structure of the objects used to describe code objects.  The
19   fields of this type are subject to change at any time.
20
21
22.. c:var:: PyTypeObject PyCode_Type
23
24   This is an instance of :c:type:`PyTypeObject` representing the Python
25   :class:`code` type.
26
27
28.. c:function:: int PyCode_Check(PyObject *co)
29
30   Return true if *co* is a :class:`code` object.  This function always succeeds.
31
32.. c:function:: int PyCode_GetNumFree(PyCodeObject *co)
33
34   Return the number of free variables in *co*.
35
36.. c:function:: PyCodeObject* PyCode_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *linetable, PyObject *exceptiontable)
37
38   Return a new code object.  If you need a dummy code object to create a frame,
39   use :c:func:`PyCode_NewEmpty` instead.  Calling :c:func:`PyCode_New` directly
40   will bind you to a precise Python version since the definition of the bytecode
41   changes often. The many arguments of this function are inter-dependent in complex
42   ways, meaning that subtle changes to values are likely to result in incorrect
43   execution or VM crashes. Use this function only with extreme care.
44
45   .. versionchanged:: 3.11
46      Added ``exceptiontable`` parameter.
47
48.. c:function:: PyCodeObject* PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *linetable, PyObject *exceptiontable)
49
50   Similar to :c:func:`PyCode_New`, but with an extra "posonlyargcount" for positional-only arguments.
51   The same caveats that apply to ``PyCode_New`` also apply to this function.
52
53   .. versionadded:: 3.8
54
55   .. versionchanged:: 3.11
56      Added ``exceptiontable`` parameter.
57
58.. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
59
60   Return a new empty code object with the specified filename,
61   function name, and first line number. The resulting code
62   object will raise an ``Exception`` if executed.
63
64.. c:function:: int PyCode_Addr2Line(PyCodeObject *co, int byte_offset)
65
66    Return the line number of the instruction that occurs on or before ``byte_offset`` and ends after it.
67    If you just need the line number of a frame, use :c:func:`PyFrame_GetLineNumber` instead.
68
69    For efficiently iterating over the line numbers in a code object, use `the API described in PEP 626
70    <https://peps.python.org/pep-0626/#out-of-process-debuggers-and-profilers>`_.
71
72.. c:function:: int PyCode_Addr2Location(PyObject *co, int byte_offset, int *start_line, int *start_column, int *end_line, int *end_column)
73
74   Sets the passed ``int`` pointers to the source code line and column numbers
75   for the instruction at ``byte_offset``. Sets the value to ``0`` when
76   information is not available for any particular element.
77
78   Returns ``1`` if the function succeeds and 0 otherwise.
79
80   .. versionadded:: 3.11
81
82.. c:function:: PyObject* PyCode_GetCode(PyCodeObject *co)
83
84   Equivalent to the Python code ``getattr(co, 'co_code')``.
85   Returns a strong reference to a :c:type:`PyBytesObject` representing the
86   bytecode in a code object. On error, ``NULL`` is returned and an exception
87   is raised.
88
89   This ``PyBytesObject`` may be created on-demand by the interpreter and does
90   not necessarily represent the bytecode actually executed by CPython. The
91   primary use case for this function is debuggers and profilers.
92
93   .. versionadded:: 3.11
94
95.. c:function:: PyObject* PyCode_GetVarnames(PyCodeObject *co)
96
97   Equivalent to the Python code ``getattr(co, 'co_varnames')``.
98   Returns a new reference to a :c:type:`PyTupleObject` containing the names of
99   the local variables. On error, ``NULL`` is returned and an exception
100   is raised.
101
102   .. versionadded:: 3.11
103
104.. c:function:: PyObject* PyCode_GetCellvars(PyCodeObject *co)
105
106   Equivalent to the Python code ``getattr(co, 'co_cellvars')``.
107   Returns a new reference to a :c:type:`PyTupleObject` containing the names of
108   the local variables that are referenced by nested functions. On error, ``NULL``
109   is returned and an exception is raised.
110
111   .. versionadded:: 3.11
112
113.. c:function:: PyObject* PyCode_GetFreevars(PyCodeObject *co)
114
115   Equivalent to the Python code ``getattr(co, 'co_freevars')``.
116   Returns a new reference to a :c:type:`PyTupleObject` containing the names of
117   the free variables. On error, ``NULL`` is returned and an exception is raised.
118
119   .. versionadded:: 3.11
120