1 /**
2   Mirror _code.h
3 
4 See_Also:
5 <a href="http://docs.python.org/c-api/code.html"> Code Objects </a>
6   */
7 
8 // TODO: does code.h really not exist in python 2.4?
9 module deimos.python.code;
10 
11 import deimos.python.pyport;
12 import deimos.python.object;
13 
14 extern(C):
15 
16 /** Bytecode object
17 
18   subclass of PyObject.
19  */
20 struct PyCodeObject {
21     mixin PyObject_HEAD;
22 
23     /** #arguments, except *args */
24     int co_argcount;
25     version(Python_3_4_Or_Later) {
26         /** #keyword only arguments */
27         int co_kwonlyargcount;	
28     }
29     /** #local variables */
30     int co_nlocals;
31     /** #entries needed for evaluation stack */
32     int co_stacksize;
33     /** CO_..., see below */
34     int co_flags;
35 
36     version(Python_3_6_Or_Later) {
37         /** first source line number */
38         int co_firstlineno;
39     }
40 
41     /** instruction opcodes */
42     PyObject* co_code;
43     /** list (constants used) */
44     PyObject* co_consts;
45     /** list of strings (names used) */
46     PyObject* co_names;
47     /** tuple of strings (local variable names) */
48     PyObject* co_varnames;
49     /** tuple of strings (free variable names) */
50     PyObject* co_freevars;
51     /** tuple of strings (cell variable names) */
52     PyObject* co_cellvars;
53 
54     version(Python_3_7_Or_Later) {
55         Py_ssize_t *co_cell2arg;
56     }else version(Python_3_3_Or_Later) {
57         ubyte *co_cell2arg;
58     }
59 
60     /** string (where it was loaded from) */
61     PyObject* co_filename;
62     /** string (name, for reference) */
63     PyObject* co_name;
64     version(Python_3_6_Or_Later) {
65     }else{
66         /** first source line number */
67         int co_firstlineno;
68     }
69     /** string (encoding addr<->lineno mapping) See
70        Objects/lnotab_notes.txt for details. */
71     PyObject* co_lnotab;
72     version(Python_2_5_Or_Later) {
73         /** for optimization only (see frameobject.c) */
74         /// Availability: >= 2.5
75         void *co_zombieframe;
76     }
77     version(Python_2_7_Or_Later) {
78         /** to support weakrefs to code objects */
79         /// Availability: >= 2.7
80         PyObject* co_weakreflist;
81     }
82     version(Python_3_6_Or_Later) {
83         /** Scratch space for extra data relating to the code object.
84           Type is a void* to keep the format private in codeobject.c to force
85           people to go through the proper APIs */
86         void* co_extra;
87     }
88 }
89 
90 /** Masks for co_flags above */
91 enum int CO_OPTIMIZED   = 0x0001;
92 /// ditto
93 enum int CO_NEWLOCALS   = 0x0002;
94 /// ditto
95 enum int CO_VARARGS     = 0x0004;
96 /// ditto
97 enum int CO_VARKEYWORDS = 0x0008;
98 /// ditto
99 enum int CO_NESTED      = 0x0010;
100 /// ditto
101 enum int CO_GENERATOR   = 0x0020;
102 /// ditto
103 enum int CO_NOFREE      = 0x0040;
104 version(Python_3_5_Or_Later) {
105     /** The CO_COROUTINE flag is set for coroutine functions (defined with
106        ``async def`` keywords) */
107     enum int CO_COROUTINE   = 0x0080;
108     /// _
109     enum int CO_ITERABLE_COROUTINE      = 0x0100;
110 }
111 
112 version(Python_2_5_Or_Later){
113     // Removed in 2.5
114 }else{
115     /// Availability: <= 2.5
116     enum int CO_GENERATOR_ALLOWED      = 0x1000;
117 }
118 /// _
119 enum int CO_FUTURE_DIVISION        = 0x2000;
120 version(Python_2_5_Or_Later){
121     /** do absolute imports by default */
122     /// Availability: >= 2.5
123     enum int CO_FUTURE_ABSOLUTE_IMPORT = 0x4000;
124     /// Availability: >= 2.5
125     enum int CO_FUTURE_WITH_STATEMENT  = 0x8000;
126     /// ditto
127     enum int CO_FUTURE_PRINT_FUNCTION  = 0x10000;
128     /// ditto
129     enum int CO_FUTURE_UNICODE_LITERALS  = 0x20000;
130 }
131 version(Python_3_2_Or_Later) {
132     /// Availability: 3.2
133     enum CO_FUTURE_BARRY_AS_BDFL =  0x40000;
134 }
135 version(Python_3_5_Or_Later) {
136     /// Availability: 3.5
137     enum CO_FUTURE_GENERATOR_STOP =  0x80000;
138 }
139 
140 version(Python_3_5_Or_Later) {
141     /// Availability: 3.7
142     enum CO_FUTURE_ANNOTATIONS =  0x100000;
143 }
144 
145 version(Python_3_7_Or_Later) {
146     enum CO_CELL_NOT_AN_ARG = -1;
147 }else version(Python_3_2_Or_Later) {
148     enum CO_CELL_NOT_AN_ARG = 255;
149 }
150 
151 /** Max static block nesting within a function */
152 enum int CO_MAXBLOCKS = 20;
153 
154 /// _
155 mixin(PyAPI_DATA!"PyTypeObject PyCode_Type");
156 
157 // D translations of C macros:
158 /// _
159 int PyCode_Check()(PyObject* op) {
160     return op.ob_type == &PyCode_Type;
161 }
162 /// _
163 size_t PyCode_GetNumFree()(PyObject* op) {
164     return PyObject_Length((cast(PyCodeObject *) op).co_freevars);
165 }
166 
167 /// _
168 PyCodeObject* PyCode_New(
169         int argcount,
170         int nlocals,
171         int stacksize,
172         int flags,
173         PyObject* code,
174         PyObject* consts,
175         PyObject* names,
176         PyObject* varnames,
177         PyObject* freevars,
178         PyObject* cellvars,
179         PyObject* filenames,
180         PyObject* name,
181         int firstlineno,
182         PyObject* lnotab);
183 
184 version(Python_2_7_Or_Later) {
185     /** Creates a new empty code object with the specified source location. */
186     /// Availability: >= 2.7
187     PyCodeObject* PyCode_NewEmpty(const(char)* filename,
188             const(char)* funcname, int firstlineno);
189 }
190 /** Return the line number associated with the specified bytecode index
191    in this code object.  If you just need the line number of a frame,
192    use PyFrame_GetLineNumber() instead. */
193 int PyCode_Addr2Line(PyCodeObject *, int);
194 
195 /// _
196 struct PyAddrPair {
197     /// _
198     int ap_lower;
199     /// _
200     int ap_upper;
201 }
202 
203 version(Python_2_7_Or_Later) {
204     /** Update *bounds to describe the first and one-past-the-last instructions in the
205       same line as lasti.  Return the number of that line.
206      */
207     /// Availability: 2.7
208     int _PyCode_CheckLineNumber(PyCodeObject* co,
209                                         int lasti, PyAddrPair *bounds);
210 }else {
211     /**Check whether lasti (an instruction offset) falls outside bounds
212        and whether it is a line number that should be traced.  Returns
213        a line number if it should be traced or -1 if the line should not.
214 
215        If lasti is not within bounds, updates bounds.
216      */
217     /// Availability: 2.5,2.6
218     int PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds);
219 }
220 version(Python_2_6_Or_Later){
221     /// Availability: >= 2.6
222     PyObject* PyCode_Optimize(PyObject* code, PyObject* consts,
223             PyObject* names, PyObject* lineno_obj);
224 }