• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. highlight:: c
2
3.. _codeobjects:
4
5.. index:: object; code, code object
6
7Code Objects
8------------
9
10.. sectionauthor:: Jeffrey Yasskin <jyasskin@gmail.com>
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 *lnotab)
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   can bind you to a precise Python version since the definition of the bytecode
41   changes often.
42
43.. 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 *lnotab)
44
45   Similar to :c:func:`PyCode_New`, but with an extra "posonlyargcount" for positional-only arguments.
46
47   .. versionadded:: 3.8
48
49.. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
50
51   Return a new empty code object with the specified filename,
52   function name, and first line number.  It is illegal to
53   :func:`exec` or :func:`eval` the resulting code object.
54
55.. c:function:: int PyCode_Addr2Line(PyCodeObject *co, int byte_offset)
56
57    Return the line number of the instruction that occurs on or before ``byte_offset`` and ends after it.
58    If you just need the line number of a frame, use :c:func:`PyFrame_GetLineNumber` instead.
59
60    For efficiently iterating over the line numbers in a code object, use `the API described in PEP 626
61    <https://www.python.org/dev/peps/pep-0626/#out-of-process-debuggers-and-profilers>`_.
62