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