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