Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _codeobjects: |
| 4 | |
| 5 | Code Objects |
| 6 | ------------ |
| 7 | |
| 8 | .. sectionauthor:: Jeffrey Yasskin <jyasskin@gmail.com> |
| 9 | |
| 10 | |
| 11 | .. index:: |
| 12 | object: code |
| 13 | |
| 14 | Code objects are a low-level detail of the CPython implementation. |
| 15 | Each one represents a chunk of executable code that hasn't yet been |
| 16 | bound into a function. |
| 17 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 18 | .. c:type:: PyCodeObject |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 19 | |
| 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 24 | .. c:var:: PyTypeObject PyCode_Type |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 25 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 26 | This is an instance of :c:type:`PyTypeObject` representing the Python |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 27 | :class:`code` type. |
| 28 | |
| 29 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 30 | .. c:function:: int PyCode_Check(PyObject *co) |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 31 | |
| 32 | Return true if *co* is a :class:`code` object |
| 33 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 34 | .. c:function:: int PyCode_GetNumFree(PyObject *co) |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 35 | |
| 36 | Return the number of free variables in *co*. |
| 37 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 38 | .. 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 Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 39 | |
| 40 | Return a new code object. If you need a dummy code object to |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 41 | create a frame, use :c:func:`PyCode_NewEmpty` instead. Calling |
| 42 | :c:func:`PyCode_New` directly can bind you to a precise Python |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 43 | version since the definition of the bytecode changes often. |
| 44 | |
| 45 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 46 | .. c:function:: int PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 47 | |
| 48 | Return a new empty code object with the specified filename, |
| 49 | function name, and first line number. It is illegal to |
| 50 | :keyword:`exec` or :func:`eval` the resulting code object. |