blob: 946ad6e2929d1f007e4032a1bda2b8610043722e [file] [log] [blame]
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +00001.. highlightlang:: c
2
3.. _codeobjects:
4
Tommy Beadle24b9e1a2016-06-02 19:26:51 -04005.. index:: object; code, code object
6
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +00007Code Objects
8------------
9
10.. sectionauthor:: Jeffrey Yasskin <jyasskin@gmail.com>
11
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000012Code 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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010016.. c:type:: PyCodeObject
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000017
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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010022.. c:var:: PyTypeObject PyCode_Type
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000023
Sandro Tosi98ed08f2012-01-14 16:42:02 +010024 This is an instance of :c:type:`PyTypeObject` representing the Python
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000025 :class:`code` type.
26
27
Sandro Tosi98ed08f2012-01-14 16:42:02 +010028.. c:function:: int PyCode_Check(PyObject *co)
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000029
Martin Panter4ed35fc2015-10-10 10:52:35 +000030 Return true if *co* is a :class:`code` object.
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000031
Sandro Tosi98ed08f2012-01-14 16:42:02 +010032.. c:function:: int PyCode_GetNumFree(PyObject *co)
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000033
34 Return the number of free variables in *co*.
35
Sandro Tosi98ed08f2012-01-14 16:42:02 +010036.. c:function:: PyCodeObject *PyCode_New(int argcount, 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)
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000037
38 Return a new code object. If you need a dummy code object to
Sandro Tosi98ed08f2012-01-14 16:42:02 +010039 create a frame, use :c:func:`PyCode_NewEmpty` instead. Calling
40 :c:func:`PyCode_New` directly can bind you to a precise Python
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000041 version since the definition of the bytecode changes often.
42
43
Sandro Tosi98ed08f2012-01-14 16:42:02 +010044.. c:function:: int PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +000045
46 Return a new empty code object with the specified filename,
47 function name, and first line number. It is illegal to
48 :keyword:`exec` or :func:`eval` the resulting code object.