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 of
PyTypeObjectrepresenting 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,use
PyCode_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:Added
qualnameandexceptiontableparameters.
- 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 to
PyCode_New(), but with an extra “posonlyargcount” for positional-only arguments.The same caveats that apply toPyCode_Newalso apply to this function.New in version 3.8.
Changed in version 3.11:Added
qualnameandexceptiontableparameters.
- 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 an
Exceptionif executed.
- intPyCode_Addr2Line(PyCodeObject*co,intbyte_offset)¶
Return the line number of the instruction that occurs on or before
byte_offsetand 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 passed
intpointers to the source code line and column numbersfor the instruction atbyte_offset. Sets the value to0wheninformation is not available for any particular element.Returns
1if the function succeeds and 0 otherwise.New in version 3.11.
- PyObject*PyCode_GetCode(PyCodeObject*co)¶
Equivalent to the Python code
getattr(co,'co_code').Returns a strong reference to aPyBytesObjectrepresenting thebytecode in a code object. On error,NULLis returned and an exceptionis raised.This
PyBytesObjectmay 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 code
getattr(co,'co_varnames').Returns a new reference to aPyTupleObjectcontaining the names ofthe local variables. On error,NULLis returned and an exceptionis raised.New in version 3.11.
- PyObject*PyCode_GetCellvars(PyCodeObject*co)¶
Equivalent to the Python code
getattr(co,'co_cellvars').Returns a new reference to aPyTupleObjectcontaining 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 code
getattr(co,'co_freevars').Returns a new reference to aPyTupleObjectcontaining the names ofthe free variables. On error,NULLis returned and an exception is raised.New in version 3.11.