blob: 10d89f297c843fbd7d7c3a60b620723cf4215292 [file] [log] [blame]
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00001.. highlightlang:: c
2
3.. _codeobjects:
4
Tommy Beadlee9b84032016-06-02 19:26:51 -04005.. index:: object; code, code object
6
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00007Code Objects
8------------
9
10.. sectionauthor:: Jeffrey Yasskin <jyasskin@gmail.com>
11
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +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
Georg Brandl60203b42010-10-06 10:11:56 +000016.. c:type:: PyCodeObject
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +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
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:var:: PyTypeObject PyCode_Type
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000023
Georg Brandl60203b42010-10-06 10:11:56 +000024 This is an instance of :c:type:`PyTypeObject` representing the Python
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000025 :class:`code` type.
26
27
Georg Brandl60203b42010-10-06 10:11:56 +000028.. c:function:: int PyCode_Check(PyObject *co)
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000029
Martin Panterd21e0b52015-10-10 10:36:22 +000030 Return true if *co* is a :class:`code` object.
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000031
Eli Bendersky08131682012-06-03 08:07:47 +030032.. c:function:: int PyCode_GetNumFree(PyCodeObject *co)
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000033
34 Return the number of free variables in *co*.
35
Eli Bendersky08131682012-06-03 08:07:47 +030036.. 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 +000037
38 Return a new code object. If you need a dummy code object to
Georg Brandl60203b42010-10-06 10:11:56 +000039 create a frame, use :c:func:`PyCode_NewEmpty` instead. Calling
40 :c:func:`PyCode_New` directly can bind you to a precise Python
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000041 version since the definition of the bytecode changes often.
42
43
Eli Bendersky08131682012-06-03 08:07:47 +030044.. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000045
46 Return a new empty code object with the specified filename,
47 function name, and first line number. It is illegal to
Georg Brandl375aec22011-01-15 17:03:02 +000048 :func:`exec` or :func:`eval` the resulting code object.