Code Objects

Code objects are a low-level detail of the CPython implementation.Each one represents a chunk of executable code that hasn’t yet beenbound into a function.

typePyCodeObject

The C structure of the objects used to describe code objects. Thefields of this type are subject to change at any time.

PyTypeObjectPyCode_Type

This is an instance ofPyTypeObject representing the Pythoncode object.

intPyCode_Check(PyObject*co)

Return true ifco is acode object.This function always succeeds.

intPyCode_GetNumFree(PyCodeObject*co)

Return the number of free variables inco.

PyCodeObject*PyCode_New(intargcount,intkwonlyargcount,intnlocals,intstacksize,intflags,PyObject*code,PyObject*consts,PyObject*names,PyObject*varnames,PyObject*freevars,PyObject*cellvars,PyObject*filename,PyObject*name,PyObject*qualname,intfirstlineno,PyObject*linetable,PyObject*exceptiontable)
Return value: New reference.

Return a new code object. If you need a dummy code object to create a frame,usePyCode_NewEmpty() instead. CallingPyCode_New() directlywill bind you to a precise Python version since the definition of the bytecodechanges often. The many arguments of this function are inter-dependent in complexways, meaning that subtle changes to values are likely to result in incorrectexecution or VM crashes. Use this function only with extreme care.

Changed in version 3.11:Addedqualname andexceptiontable parameters.

PyCodeObject*PyCode_NewWithPosOnlyArgs(intargcount,intposonlyargcount,intkwonlyargcount,intnlocals,intstacksize,intflags,PyObject*code,PyObject*consts,PyObject*names,PyObject*varnames,PyObject*freevars,PyObject*cellvars,PyObject*filename,PyObject*name,PyObject*qualname,intfirstlineno,PyObject*linetable,PyObject*exceptiontable)
Return value: New reference.

Similar toPyCode_New(), but with an extra “posonlyargcount” for positional-only arguments.The same caveats that apply toPyCode_New also apply to this function.

New in version 3.8.

Changed in version 3.11:Addedqualname andexceptiontable parameters.

PyCodeObject*PyCode_NewEmpty(constchar*filename,constchar*funcname,intfirstlineno)
Return value: New reference.

Return a new empty code object with the specified filename,function name, and first line number. The resulting codeobject will raise anException if executed.

intPyCode_Addr2Line(PyCodeObject*co,intbyte_offset)

Return the line number of the instruction that occurs on or beforebyte_offset and ends after it.If you just need the line number of a frame, usePyFrame_GetLineNumber() instead.

For efficiently iterating over the line numbers in a code object, usethe API described in PEP 626.

intPyCode_Addr2Location(PyObject*co,intbyte_offset,int*start_line,int*start_column,int*end_line,int*end_column)

Sets the passedint pointers to the source code line and column numbersfor the instruction atbyte_offset. Sets the value to0 wheninformation is not available for any particular element.

Returns1 if the function succeeds and 0 otherwise.

New in version 3.11.

PyObject*PyCode_GetCode(PyCodeObject*co)

Equivalent to the Python codegetattr(co,'co_code').Returns a strong reference to aPyBytesObject representing thebytecode in a code object. On error,NULL is returned and an exceptionis raised.

ThisPyBytesObject may be created on-demand by the interpreter and doesnot necessarily represent the bytecode actually executed by CPython. Theprimary use case for this function is debuggers and profilers.

New in version 3.11.

PyObject*PyCode_GetVarnames(PyCodeObject*co)

Equivalent to the Python codegetattr(co,'co_varnames').Returns a new reference to aPyTupleObject containing the names ofthe local variables. On error,NULL is returned and an exceptionis raised.

New in version 3.11.

PyObject*PyCode_GetCellvars(PyCodeObject*co)

Equivalent to the Python codegetattr(co,'co_cellvars').Returns a new reference to aPyTupleObject containing the names ofthe local variables that are referenced by nested functions. On error,NULLis returned and an exception is raised.

New in version 3.11.

PyObject*PyCode_GetFreevars(PyCodeObject*co)

Equivalent to the Python codegetattr(co,'co_freevars').Returns a new reference to aPyTupleObject containing the names ofthe free variables. On error,NULL is returned and an exception is raised.

New in version 3.11.