Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "code.h" |
| 3 | #include "structmember.h" |
| 4 | |
| 5 | #define NAME_CHARS \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 7 | |
| 8 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 9 | |
| 10 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11 | all_name_chars(PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 12 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 13 | static char ok_name_char[256]; |
| 14 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15 | PyUnicodeObject *u = (PyUnicodeObject *)o; |
| 16 | const unsigned char *s; |
| 17 | |
| 18 | if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 || |
| 19 | PyUnicode_MAX_CHAR_VALUE(u) >= 128) |
| 20 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 21 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | if (ok_name_char[*name_chars] == 0) { |
| 23 | unsigned char *p; |
| 24 | for (p = name_chars; *p; p++) |
| 25 | ok_name_char[*p] = 1; |
| 26 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 27 | s = PyUnicode_1BYTE_DATA(u); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | while (*s) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | if (ok_name_char[*s++] == 0) |
| 30 | return 0; |
| 31 | } |
| 32 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static void |
| 36 | intern_strings(PyObject *tuple) |
| 37 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 39 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 41 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 42 | if (v == NULL || !PyUnicode_CheckExact(v)) { |
| 43 | Py_FatalError("non-string found in code slot"); |
| 44 | } |
| 45 | PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 46 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
| 50 | PyCodeObject * |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 51 | PyCode_New(int argcount, int kwonlyargcount, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | int nlocals, int stacksize, int flags, |
| 53 | PyObject *code, PyObject *consts, PyObject *names, |
| 54 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 55 | PyObject *filename, PyObject *name, int firstlineno, |
| 56 | PyObject *lnotab) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 57 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | PyCodeObject *co; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 59 | unsigned char *cell2arg = NULL; |
| 60 | Py_ssize_t i, n_cellvars; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 61 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | /* Check argument types */ |
| 63 | if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 || |
| 64 | code == NULL || |
| 65 | consts == NULL || !PyTuple_Check(consts) || |
| 66 | names == NULL || !PyTuple_Check(names) || |
| 67 | varnames == NULL || !PyTuple_Check(varnames) || |
| 68 | freevars == NULL || !PyTuple_Check(freevars) || |
| 69 | cellvars == NULL || !PyTuple_Check(cellvars) || |
| 70 | name == NULL || !PyUnicode_Check(name) || |
| 71 | filename == NULL || !PyUnicode_Check(filename) || |
| 72 | lnotab == NULL || !PyBytes_Check(lnotab) || |
| 73 | !PyObject_CheckReadBuffer(code)) { |
| 74 | PyErr_BadInternalCall(); |
| 75 | return NULL; |
| 76 | } |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 77 | n_cellvars = PyTuple_GET_SIZE(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | intern_strings(names); |
| 79 | intern_strings(varnames); |
| 80 | intern_strings(freevars); |
| 81 | intern_strings(cellvars); |
| 82 | /* Intern selected string constants */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 83 | for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | PyObject *v = PyTuple_GetItem(consts, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 85 | if (!all_name_chars(v)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | continue; |
| 87 | PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
| 88 | } |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 89 | /* Create mapping between cells and arguments if needed. */ |
| 90 | if (n_cellvars) { |
| 91 | Py_ssize_t total_args = argcount + kwonlyargcount + |
| 92 | ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); |
| 93 | Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars; |
| 94 | int used_cell2arg = 0; |
| 95 | cell2arg = PyMem_MALLOC(alloc_size); |
| 96 | if (cell2arg == NULL) |
| 97 | return NULL; |
| 98 | memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size); |
| 99 | /* Find cells which are also arguments. */ |
| 100 | for (i = 0; i < n_cellvars; i++) { |
| 101 | Py_ssize_t j; |
| 102 | PyObject *cell = PyTuple_GET_ITEM(cellvars, i); |
| 103 | for (j = 0; j < total_args; j++) { |
| 104 | PyObject *arg = PyTuple_GET_ITEM(varnames, j); |
| 105 | if (!PyUnicode_Compare(cell, arg)) { |
| 106 | cell2arg[i] = j; |
| 107 | used_cell2arg = 1; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | if (!used_cell2arg) { |
| 113 | PyMem_FREE(cell2arg); |
| 114 | cell2arg = NULL; |
| 115 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | } |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 117 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
| 118 | if (co == NULL) { |
| 119 | if (cell2arg) |
| 120 | PyMem_FREE(cell2arg); |
| 121 | return NULL; |
| 122 | } |
| 123 | co->co_argcount = argcount; |
| 124 | co->co_kwonlyargcount = kwonlyargcount; |
| 125 | co->co_nlocals = nlocals; |
| 126 | co->co_stacksize = stacksize; |
| 127 | co->co_flags = flags; |
| 128 | Py_INCREF(code); |
| 129 | co->co_code = code; |
| 130 | Py_INCREF(consts); |
| 131 | co->co_consts = consts; |
| 132 | Py_INCREF(names); |
| 133 | co->co_names = names; |
| 134 | Py_INCREF(varnames); |
| 135 | co->co_varnames = varnames; |
| 136 | Py_INCREF(freevars); |
| 137 | co->co_freevars = freevars; |
| 138 | Py_INCREF(cellvars); |
| 139 | co->co_cellvars = cellvars; |
| 140 | co->co_cell2arg = cell2arg; |
| 141 | Py_INCREF(filename); |
| 142 | co->co_filename = filename; |
| 143 | Py_INCREF(name); |
| 144 | co->co_name = name; |
| 145 | co->co_firstlineno = firstlineno; |
| 146 | Py_INCREF(lnotab); |
| 147 | co->co_lnotab = lnotab; |
| 148 | co->co_zombieframe = NULL; |
| 149 | co->co_weakreflist = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 153 | PyCodeObject * |
| 154 | PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) |
| 155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | static PyObject *emptystring = NULL; |
| 157 | static PyObject *nulltuple = NULL; |
| 158 | PyObject *filename_ob = NULL; |
| 159 | PyObject *funcname_ob = NULL; |
| 160 | PyCodeObject *result = NULL; |
| 161 | if (emptystring == NULL) { |
| 162 | emptystring = PyBytes_FromString(""); |
| 163 | if (emptystring == NULL) |
| 164 | goto failed; |
| 165 | } |
| 166 | if (nulltuple == NULL) { |
| 167 | nulltuple = PyTuple_New(0); |
| 168 | if (nulltuple == NULL) |
| 169 | goto failed; |
| 170 | } |
| 171 | funcname_ob = PyUnicode_FromString(funcname); |
| 172 | if (funcname_ob == NULL) |
| 173 | goto failed; |
| 174 | filename_ob = PyUnicode_DecodeFSDefault(filename); |
| 175 | if (filename_ob == NULL) |
| 176 | goto failed; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | result = PyCode_New(0, /* argcount */ |
| 179 | 0, /* kwonlyargcount */ |
| 180 | 0, /* nlocals */ |
| 181 | 0, /* stacksize */ |
| 182 | 0, /* flags */ |
| 183 | emptystring, /* code */ |
| 184 | nulltuple, /* consts */ |
| 185 | nulltuple, /* names */ |
| 186 | nulltuple, /* varnames */ |
| 187 | nulltuple, /* freevars */ |
| 188 | nulltuple, /* cellvars */ |
| 189 | filename_ob, /* filename */ |
| 190 | funcname_ob, /* name */ |
| 191 | firstlineno, /* firstlineno */ |
| 192 | emptystring /* lnotab */ |
| 193 | ); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 194 | |
| 195 | failed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | Py_XDECREF(funcname_ob); |
| 197 | Py_XDECREF(filename_ob); |
| 198 | return result; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 199 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 200 | |
| 201 | #define OFF(x) offsetof(PyCodeObject, x) |
| 202 | |
| 203 | static PyMemberDef code_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 205 | {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY}, |
| 206 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 207 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
| 208 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
| 209 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 210 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 211 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
| 212 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
| 213 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 214 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
| 215 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
| 216 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
| 217 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 218 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
| 219 | {NULL} /* Sentinel */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | /* Helper for code_new: return a shallow copy of a tuple that is |
| 223 | guaranteed to contain exact strings, by converting string subclasses |
| 224 | to exact strings and complaining if a non-string is found. */ |
| 225 | static PyObject* |
| 226 | validate_and_copy_tuple(PyObject *tup) |
| 227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | PyObject *newtuple; |
| 229 | PyObject *item; |
| 230 | Py_ssize_t i, len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | len = PyTuple_GET_SIZE(tup); |
| 233 | newtuple = PyTuple_New(len); |
| 234 | if (newtuple == NULL) |
| 235 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | for (i = 0; i < len; i++) { |
| 238 | item = PyTuple_GET_ITEM(tup, i); |
| 239 | if (PyUnicode_CheckExact(item)) { |
| 240 | Py_INCREF(item); |
| 241 | } |
| 242 | else if (!PyUnicode_Check(item)) { |
| 243 | PyErr_Format( |
| 244 | PyExc_TypeError, |
| 245 | "name tuples must contain only " |
| 246 | "strings, not '%.500s'", |
| 247 | item->ob_type->tp_name); |
| 248 | Py_DECREF(newtuple); |
| 249 | return NULL; |
| 250 | } |
| 251 | else { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 252 | item = _PyUnicode_Copy(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | if (item == NULL) { |
| 254 | Py_DECREF(newtuple); |
| 255 | return NULL; |
| 256 | } |
| 257 | } |
| 258 | PyTuple_SET_ITEM(newtuple, i, item); |
| 259 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | return newtuple; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | PyDoc_STRVAR(code_doc, |
Georg Brandl | 8c1a50a | 2009-07-18 09:07:48 +0000 | [diff] [blame] | 265 | "code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\ |
Georg Brandl | a1e7e13 | 2008-01-26 09:39:23 +0000 | [diff] [blame] | 266 | constants, names, varnames, filename, name, firstlineno,\n\ |
| 267 | lnotab[, freevars[, cellvars]])\n\ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 268 | \n\ |
| 269 | Create a code object. Not for the faint of heart."); |
| 270 | |
| 271 | static PyObject * |
| 272 | code_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | int argcount; |
| 275 | int kwonlyargcount; |
| 276 | int nlocals; |
| 277 | int stacksize; |
| 278 | int flags; |
| 279 | PyObject *co = NULL; |
| 280 | PyObject *code; |
| 281 | PyObject *consts; |
| 282 | PyObject *names, *ournames = NULL; |
| 283 | PyObject *varnames, *ourvarnames = NULL; |
| 284 | PyObject *freevars = NULL, *ourfreevars = NULL; |
| 285 | PyObject *cellvars = NULL, *ourcellvars = NULL; |
| 286 | PyObject *filename; |
| 287 | PyObject *name; |
| 288 | int firstlineno; |
| 289 | PyObject *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code", |
| 292 | &argcount, &kwonlyargcount, |
| 293 | &nlocals, &stacksize, &flags, |
| 294 | &code, |
| 295 | &PyTuple_Type, &consts, |
| 296 | &PyTuple_Type, &names, |
| 297 | &PyTuple_Type, &varnames, |
| 298 | &filename, &name, |
| 299 | &firstlineno, &lnotab, |
| 300 | &PyTuple_Type, &freevars, |
| 301 | &PyTuple_Type, &cellvars)) |
| 302 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | if (argcount < 0) { |
| 305 | PyErr_SetString( |
| 306 | PyExc_ValueError, |
| 307 | "code: argcount must not be negative"); |
| 308 | goto cleanup; |
| 309 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | if (kwonlyargcount < 0) { |
| 312 | PyErr_SetString( |
| 313 | PyExc_ValueError, |
| 314 | "code: kwonlyargcount must not be negative"); |
| 315 | goto cleanup; |
| 316 | } |
| 317 | if (nlocals < 0) { |
| 318 | PyErr_SetString( |
| 319 | PyExc_ValueError, |
| 320 | "code: nlocals must not be negative"); |
| 321 | goto cleanup; |
| 322 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | ournames = validate_and_copy_tuple(names); |
| 325 | if (ournames == NULL) |
| 326 | goto cleanup; |
| 327 | ourvarnames = validate_and_copy_tuple(varnames); |
| 328 | if (ourvarnames == NULL) |
| 329 | goto cleanup; |
| 330 | if (freevars) |
| 331 | ourfreevars = validate_and_copy_tuple(freevars); |
| 332 | else |
| 333 | ourfreevars = PyTuple_New(0); |
| 334 | if (ourfreevars == NULL) |
| 335 | goto cleanup; |
| 336 | if (cellvars) |
| 337 | ourcellvars = validate_and_copy_tuple(cellvars); |
| 338 | else |
| 339 | ourcellvars = PyTuple_New(0); |
| 340 | if (ourcellvars == NULL) |
| 341 | goto cleanup; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | co = (PyObject *)PyCode_New(argcount, kwonlyargcount, |
| 344 | nlocals, stacksize, flags, |
| 345 | code, consts, ournames, ourvarnames, |
| 346 | ourfreevars, ourcellvars, filename, |
| 347 | name, firstlineno, lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 348 | cleanup: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | Py_XDECREF(ournames); |
| 350 | Py_XDECREF(ourvarnames); |
| 351 | Py_XDECREF(ourfreevars); |
| 352 | Py_XDECREF(ourcellvars); |
| 353 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static void |
| 357 | code_dealloc(PyCodeObject *co) |
| 358 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | Py_XDECREF(co->co_code); |
| 360 | Py_XDECREF(co->co_consts); |
| 361 | Py_XDECREF(co->co_names); |
| 362 | Py_XDECREF(co->co_varnames); |
| 363 | Py_XDECREF(co->co_freevars); |
| 364 | Py_XDECREF(co->co_cellvars); |
| 365 | Py_XDECREF(co->co_filename); |
| 366 | Py_XDECREF(co->co_name); |
| 367 | Py_XDECREF(co->co_lnotab); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 368 | if (co->co_cell2arg != NULL) |
| 369 | PyMem_FREE(co->co_cell2arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | if (co->co_zombieframe != NULL) |
| 371 | PyObject_GC_Del(co->co_zombieframe); |
| 372 | if (co->co_weakreflist != NULL) |
| 373 | PyObject_ClearWeakRefs((PyObject*)co); |
| 374 | PyObject_DEL(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static PyObject * |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 378 | code_sizeof(PyCodeObject *co, void *unused) |
| 379 | { |
| 380 | Py_ssize_t res; |
| 381 | |
| 382 | res = sizeof(PyCodeObject); |
| 383 | if (co->co_cell2arg != NULL && co->co_cellvars != NULL) |
| 384 | res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char); |
| 385 | return PyLong_FromSsize_t(res); |
| 386 | } |
| 387 | |
| 388 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 389 | code_repr(PyCodeObject *co) |
| 390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | int lineno; |
| 392 | if (co->co_firstlineno != 0) |
| 393 | lineno = co->co_firstlineno; |
| 394 | else |
| 395 | lineno = -1; |
| 396 | if (co->co_filename && PyUnicode_Check(co->co_filename)) { |
| 397 | return PyUnicode_FromFormat( |
Victor Stinner | aaa4e9a | 2011-01-05 03:33:26 +0000 | [diff] [blame] | 398 | "<code object %U at %p, file \"%U\", line %d>", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | co->co_name, co, co->co_filename, lineno); |
| 400 | } else { |
| 401 | return PyUnicode_FromFormat( |
Victor Stinner | aaa4e9a | 2011-01-05 03:33:26 +0000 | [diff] [blame] | 402 | "<code object %U at %p, file ???, line %d>", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | co->co_name, co, lineno); |
| 404 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 407 | static PyObject * |
| 408 | code_richcompare(PyObject *self, PyObject *other, int op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | PyCodeObject *co, *cp; |
| 411 | int eq; |
| 412 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | if ((op != Py_EQ && op != Py_NE) || |
| 415 | !PyCode_Check(self) || |
| 416 | !PyCode_Check(other)) { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 417 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | co = (PyCodeObject *)self; |
| 421 | cp = (PyCodeObject *)other; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ); |
| 424 | if (eq <= 0) goto unequal; |
| 425 | eq = co->co_argcount == cp->co_argcount; |
| 426 | if (!eq) goto unequal; |
| 427 | eq = co->co_kwonlyargcount == cp->co_kwonlyargcount; |
| 428 | if (!eq) goto unequal; |
| 429 | eq = co->co_nlocals == cp->co_nlocals; |
| 430 | if (!eq) goto unequal; |
| 431 | eq = co->co_flags == cp->co_flags; |
| 432 | if (!eq) goto unequal; |
| 433 | eq = co->co_firstlineno == cp->co_firstlineno; |
| 434 | if (!eq) goto unequal; |
| 435 | eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ); |
| 436 | if (eq <= 0) goto unequal; |
| 437 | eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ); |
| 438 | if (eq <= 0) goto unequal; |
| 439 | eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ); |
| 440 | if (eq <= 0) goto unequal; |
| 441 | eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ); |
| 442 | if (eq <= 0) goto unequal; |
| 443 | eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ); |
| 444 | if (eq <= 0) goto unequal; |
| 445 | eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ); |
| 446 | if (eq <= 0) goto unequal; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | if (op == Py_EQ) |
| 449 | res = Py_True; |
| 450 | else |
| 451 | res = Py_False; |
| 452 | goto done; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 453 | |
| 454 | unequal: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | if (eq < 0) |
| 456 | return NULL; |
| 457 | if (op == Py_NE) |
| 458 | res = Py_True; |
| 459 | else |
| 460 | res = Py_False; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 461 | |
| 462 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | Py_INCREF(res); |
| 464 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 467 | static Py_hash_t |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 468 | code_hash(PyCodeObject *co) |
| 469 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 470 | Py_hash_t h, h0, h1, h2, h3, h4, h5, h6; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | h0 = PyObject_Hash(co->co_name); |
| 472 | if (h0 == -1) return -1; |
| 473 | h1 = PyObject_Hash(co->co_code); |
| 474 | if (h1 == -1) return -1; |
| 475 | h2 = PyObject_Hash(co->co_consts); |
| 476 | if (h2 == -1) return -1; |
| 477 | h3 = PyObject_Hash(co->co_names); |
| 478 | if (h3 == -1) return -1; |
| 479 | h4 = PyObject_Hash(co->co_varnames); |
| 480 | if (h4 == -1) return -1; |
| 481 | h5 = PyObject_Hash(co->co_freevars); |
| 482 | if (h5 == -1) return -1; |
| 483 | h6 = PyObject_Hash(co->co_cellvars); |
| 484 | if (h6 == -1) return -1; |
| 485 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
| 486 | co->co_argcount ^ co->co_kwonlyargcount ^ |
| 487 | co->co_nlocals ^ co->co_flags; |
| 488 | if (h == -1) h = -2; |
| 489 | return h; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* XXX code objects need to participate in GC? */ |
| 493 | |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 494 | static struct PyMethodDef code_methods[] = { |
| 495 | {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS}, |
| 496 | {NULL, NULL} /* sentinel */ |
| 497 | }; |
| 498 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 499 | PyTypeObject PyCode_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 501 | "code", |
| 502 | sizeof(PyCodeObject), |
| 503 | 0, |
| 504 | (destructor)code_dealloc, /* tp_dealloc */ |
| 505 | 0, /* tp_print */ |
| 506 | 0, /* tp_getattr */ |
| 507 | 0, /* tp_setattr */ |
| 508 | 0, /* tp_reserved */ |
| 509 | (reprfunc)code_repr, /* tp_repr */ |
| 510 | 0, /* tp_as_number */ |
| 511 | 0, /* tp_as_sequence */ |
| 512 | 0, /* tp_as_mapping */ |
| 513 | (hashfunc)code_hash, /* tp_hash */ |
| 514 | 0, /* tp_call */ |
| 515 | 0, /* tp_str */ |
| 516 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 517 | 0, /* tp_setattro */ |
| 518 | 0, /* tp_as_buffer */ |
| 519 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 520 | code_doc, /* tp_doc */ |
| 521 | 0, /* tp_traverse */ |
| 522 | 0, /* tp_clear */ |
| 523 | code_richcompare, /* tp_richcompare */ |
| 524 | offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */ |
| 525 | 0, /* tp_iter */ |
| 526 | 0, /* tp_iternext */ |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 527 | code_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | code_memberlist, /* tp_members */ |
| 529 | 0, /* tp_getset */ |
| 530 | 0, /* tp_base */ |
| 531 | 0, /* tp_dict */ |
| 532 | 0, /* tp_descr_get */ |
| 533 | 0, /* tp_descr_set */ |
| 534 | 0, /* tp_dictoffset */ |
| 535 | 0, /* tp_init */ |
| 536 | 0, /* tp_alloc */ |
| 537 | code_new, /* tp_new */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 540 | /* Use co_lnotab to compute the line number from a bytecode index, addrq. See |
| 541 | lnotab_notes.txt for the details of the lnotab representation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 542 | */ |
| 543 | |
| 544 | int |
| 545 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
| 546 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 547 | Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab); |
| 549 | int line = co->co_firstlineno; |
| 550 | int addr = 0; |
| 551 | while (--size >= 0) { |
| 552 | addr += *p++; |
| 553 | if (addr > addrq) |
| 554 | break; |
| 555 | line += *p++; |
| 556 | } |
| 557 | return line; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 558 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 559 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 560 | /* Update *bounds to describe the first and one-past-the-last instructions in |
| 561 | the same line as lasti. Return the number of that line. */ |
| 562 | int |
| 563 | _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 564 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 565 | Py_ssize_t size; |
| 566 | int addr, line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | unsigned char* p; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab); |
| 570 | size = PyBytes_GET_SIZE(co->co_lnotab) / 2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | addr = 0; |
| 573 | line = co->co_firstlineno; |
| 574 | assert(line > 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | /* possible optimization: if f->f_lasti == instr_ub |
| 577 | (likely to be a common case) then we already know |
| 578 | instr_lb -- if we stored the matching value of p |
| 579 | somwhere we could skip the first while loop. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | /* See lnotab_notes.txt for the description of |
| 582 | co_lnotab. A point to remember: increments to p |
| 583 | come in (addr, line) pairs. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | bounds->ap_lower = 0; |
| 586 | while (size > 0) { |
| 587 | if (addr + *p > lasti) |
| 588 | break; |
| 589 | addr += *p++; |
| 590 | if (*p) |
| 591 | bounds->ap_lower = addr; |
| 592 | line += *p++; |
| 593 | --size; |
| 594 | } |
| 595 | |
| 596 | if (size > 0) { |
| 597 | while (--size >= 0) { |
| 598 | addr += *p++; |
| 599 | if (*p++) |
| 600 | break; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 601 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | bounds->ap_upper = addr; |
| 603 | } |
| 604 | else { |
| 605 | bounds->ap_upper = INT_MAX; |
| 606 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | return line; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 609 | } |