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