blob: 9c9356338243649c9ef5850222696aa5403195cc [file] [log] [blame]
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00001.. highlightlang:: c
2
3.. _codeobjects:
4
5Code Objects
6------------
7
8.. sectionauthor:: Jeffrey Yasskin <jyasskin@gmail.com>
9
10
11.. index::
12 object: code
13
14Code objects are a low-level detail of the CPython implementation.
15Each one represents a chunk of executable code that hasn't yet been
16bound into a function.
17
Georg Brandl60203b42010-10-06 10:11:56 +000018.. c:type:: PyCodeObject
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000019
20 The C structure of the objects used to describe code objects. The
21 fields of this type are subject to change at any time.
22
23
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:var:: PyTypeObject PyCode_Type
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000025
Georg Brandl60203b42010-10-06 10:11:56 +000026 This is an instance of :c:type:`PyTypeObject` representing the Python
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000027 :class:`code` type.
28
29
Georg Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: int PyCode_Check(PyObject *co)
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000031
Martin Panterd21e0b52015-10-10 10:36:22 +000032 Return true if *co* is a :class:`code` object.
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000033
Eli Bendersky08131682012-06-03 08:07:47 +030034.. c:function:: int PyCode_GetNumFree(PyCodeObject *co)
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000035
36 Return the number of free variables in *co*.
37
Eli Bendersky08131682012-06-03 08:07:47 +030038.. 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)
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000039
40 Return a new code object. If you need a dummy code object to
Georg Brandl60203b42010-10-06 10:11:56 +000041 create a frame, use :c:func:`PyCode_NewEmpty` instead. Calling
42 :c:func:`PyCode_New` directly can bind you to a precise Python
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000043 version since the definition of the bytecode changes often.
44
45
Eli Bendersky08131682012-06-03 08:07:47 +030046.. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000047
48 Return a new empty code object with the specified filename,
49 function name, and first line number. It is illegal to
Georg Brandl375aec22011-01-15 17:03:02 +000050 :func:`exec` or :func:`eval` the resulting code object.