Benjamin Peterson | 1bf494b | 2016-09-07 11:28:35 -0700 | [diff] [blame] | 1 | #include <stdbool.h> |
| 2 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3 | #include "Python.h" |
| 4 | #include "code.h" |
| 5 | #include "structmember.h" |
| 6 | |
| 7 | #define NAME_CHARS \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 8 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 9 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 10 | /* Holder for co_extra information */ |
| 11 | typedef struct { |
| 12 | Py_ssize_t ce_size; |
| 13 | void **ce_extras; |
| 14 | } _PyCodeObjectExtra; |
| 15 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 16 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 17 | |
| 18 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 19 | all_name_chars(PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 20 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 21 | static char ok_name_char[256]; |
| 22 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 23 | PyUnicodeObject *u = (PyUnicodeObject *)o; |
| 24 | const unsigned char *s; |
| 25 | |
| 26 | if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 || |
| 27 | PyUnicode_MAX_CHAR_VALUE(u) >= 128) |
| 28 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 29 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | if (ok_name_char[*name_chars] == 0) { |
| 31 | unsigned char *p; |
| 32 | for (p = name_chars; *p; p++) |
| 33 | ok_name_char[*p] = 1; |
| 34 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 35 | s = PyUnicode_1BYTE_DATA(u); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | while (*s) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | if (ok_name_char[*s++] == 0) |
| 38 | return 0; |
| 39 | } |
| 40 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void |
| 44 | intern_strings(PyObject *tuple) |
| 45 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 47 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 49 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 50 | if (v == NULL || !PyUnicode_CheckExact(v)) { |
| 51 | Py_FatalError("non-string found in code slot"); |
| 52 | } |
| 53 | PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 54 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| 58 | PyCodeObject * |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 59 | PyCode_New(int argcount, int kwonlyargcount, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | int nlocals, int stacksize, int flags, |
| 61 | PyObject *code, PyObject *consts, PyObject *names, |
| 62 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 63 | PyObject *filename, PyObject *name, int firstlineno, |
| 64 | PyObject *lnotab) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 65 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | PyCodeObject *co; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 67 | unsigned char *cell2arg = NULL; |
| 68 | Py_ssize_t i, n_cellvars; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | /* Check argument types */ |
| 71 | if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 || |
| 72 | code == NULL || |
| 73 | consts == NULL || !PyTuple_Check(consts) || |
| 74 | names == NULL || !PyTuple_Check(names) || |
| 75 | varnames == NULL || !PyTuple_Check(varnames) || |
| 76 | freevars == NULL || !PyTuple_Check(freevars) || |
| 77 | cellvars == NULL || !PyTuple_Check(cellvars) || |
| 78 | name == NULL || !PyUnicode_Check(name) || |
| 79 | filename == NULL || !PyUnicode_Check(filename) || |
| 80 | lnotab == NULL || !PyBytes_Check(lnotab) || |
| 81 | !PyObject_CheckReadBuffer(code)) { |
| 82 | PyErr_BadInternalCall(); |
| 83 | return NULL; |
| 84 | } |
Victor Stinner | 7c74de4 | 2013-10-10 15:55:14 +0200 | [diff] [blame] | 85 | |
| 86 | /* Ensure that the filename is a ready Unicode string */ |
| 87 | if (PyUnicode_READY(filename) < 0) |
| 88 | return NULL; |
| 89 | |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 90 | n_cellvars = PyTuple_GET_SIZE(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | intern_strings(names); |
| 92 | intern_strings(varnames); |
| 93 | intern_strings(freevars); |
| 94 | intern_strings(cellvars); |
| 95 | /* Intern selected string constants */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 96 | for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | PyObject *v = PyTuple_GetItem(consts, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 98 | if (!all_name_chars(v)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | continue; |
| 100 | PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
| 101 | } |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 102 | /* Create mapping between cells and arguments if needed. */ |
| 103 | if (n_cellvars) { |
| 104 | Py_ssize_t total_args = argcount + kwonlyargcount + |
| 105 | ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); |
| 106 | Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars; |
Benjamin Peterson | 1bf494b | 2016-09-07 11:28:35 -0700 | [diff] [blame] | 107 | bool used_cell2arg = false; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 108 | cell2arg = PyMem_MALLOC(alloc_size); |
| 109 | if (cell2arg == NULL) |
| 110 | return NULL; |
| 111 | memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size); |
| 112 | /* Find cells which are also arguments. */ |
| 113 | for (i = 0; i < n_cellvars; i++) { |
| 114 | Py_ssize_t j; |
| 115 | PyObject *cell = PyTuple_GET_ITEM(cellvars, i); |
| 116 | for (j = 0; j < total_args; j++) { |
| 117 | PyObject *arg = PyTuple_GET_ITEM(varnames, j); |
| 118 | if (!PyUnicode_Compare(cell, arg)) { |
| 119 | cell2arg[i] = j; |
Benjamin Peterson | 1bf494b | 2016-09-07 11:28:35 -0700 | [diff] [blame] | 120 | used_cell2arg = true; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | if (!used_cell2arg) { |
| 126 | PyMem_FREE(cell2arg); |
| 127 | cell2arg = NULL; |
| 128 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | } |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 130 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
| 131 | if (co == NULL) { |
| 132 | if (cell2arg) |
| 133 | PyMem_FREE(cell2arg); |
| 134 | return NULL; |
| 135 | } |
| 136 | co->co_argcount = argcount; |
| 137 | co->co_kwonlyargcount = kwonlyargcount; |
| 138 | co->co_nlocals = nlocals; |
| 139 | co->co_stacksize = stacksize; |
| 140 | co->co_flags = flags; |
| 141 | Py_INCREF(code); |
| 142 | co->co_code = code; |
| 143 | Py_INCREF(consts); |
| 144 | co->co_consts = consts; |
| 145 | Py_INCREF(names); |
| 146 | co->co_names = names; |
| 147 | Py_INCREF(varnames); |
| 148 | co->co_varnames = varnames; |
| 149 | Py_INCREF(freevars); |
| 150 | co->co_freevars = freevars; |
| 151 | Py_INCREF(cellvars); |
| 152 | co->co_cellvars = cellvars; |
| 153 | co->co_cell2arg = cell2arg; |
| 154 | Py_INCREF(filename); |
| 155 | co->co_filename = filename; |
| 156 | Py_INCREF(name); |
| 157 | co->co_name = name; |
| 158 | co->co_firstlineno = firstlineno; |
| 159 | Py_INCREF(lnotab); |
| 160 | co->co_lnotab = lnotab; |
| 161 | co->co_zombieframe = NULL; |
| 162 | co->co_weakreflist = NULL; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 163 | co->co_extra = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 167 | PyCodeObject * |
| 168 | PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) |
| 169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | static PyObject *emptystring = NULL; |
| 171 | static PyObject *nulltuple = NULL; |
| 172 | PyObject *filename_ob = NULL; |
| 173 | PyObject *funcname_ob = NULL; |
| 174 | PyCodeObject *result = NULL; |
| 175 | if (emptystring == NULL) { |
| 176 | emptystring = PyBytes_FromString(""); |
| 177 | if (emptystring == NULL) |
| 178 | goto failed; |
| 179 | } |
| 180 | if (nulltuple == NULL) { |
| 181 | nulltuple = PyTuple_New(0); |
| 182 | if (nulltuple == NULL) |
| 183 | goto failed; |
| 184 | } |
| 185 | funcname_ob = PyUnicode_FromString(funcname); |
| 186 | if (funcname_ob == NULL) |
| 187 | goto failed; |
| 188 | filename_ob = PyUnicode_DecodeFSDefault(filename); |
| 189 | if (filename_ob == NULL) |
| 190 | goto failed; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | result = PyCode_New(0, /* argcount */ |
| 193 | 0, /* kwonlyargcount */ |
| 194 | 0, /* nlocals */ |
| 195 | 0, /* stacksize */ |
| 196 | 0, /* flags */ |
| 197 | emptystring, /* code */ |
| 198 | nulltuple, /* consts */ |
| 199 | nulltuple, /* names */ |
| 200 | nulltuple, /* varnames */ |
| 201 | nulltuple, /* freevars */ |
| 202 | nulltuple, /* cellvars */ |
| 203 | filename_ob, /* filename */ |
| 204 | funcname_ob, /* name */ |
| 205 | firstlineno, /* firstlineno */ |
| 206 | emptystring /* lnotab */ |
| 207 | ); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 208 | |
| 209 | failed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | Py_XDECREF(funcname_ob); |
| 211 | Py_XDECREF(filename_ob); |
| 212 | return result; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 213 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 214 | |
| 215 | #define OFF(x) offsetof(PyCodeObject, x) |
| 216 | |
| 217 | static PyMemberDef code_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 219 | {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY}, |
| 220 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 221 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
| 222 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
| 223 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 224 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 225 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
| 226 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
| 227 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 228 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
| 229 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
| 230 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
| 231 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 232 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
| 233 | {NULL} /* Sentinel */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | /* Helper for code_new: return a shallow copy of a tuple that is |
| 237 | guaranteed to contain exact strings, by converting string subclasses |
| 238 | to exact strings and complaining if a non-string is found. */ |
| 239 | static PyObject* |
| 240 | validate_and_copy_tuple(PyObject *tup) |
| 241 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | PyObject *newtuple; |
| 243 | PyObject *item; |
| 244 | Py_ssize_t i, len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | len = PyTuple_GET_SIZE(tup); |
| 247 | newtuple = PyTuple_New(len); |
| 248 | if (newtuple == NULL) |
| 249 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | for (i = 0; i < len; i++) { |
| 252 | item = PyTuple_GET_ITEM(tup, i); |
| 253 | if (PyUnicode_CheckExact(item)) { |
| 254 | Py_INCREF(item); |
| 255 | } |
| 256 | else if (!PyUnicode_Check(item)) { |
| 257 | PyErr_Format( |
| 258 | PyExc_TypeError, |
| 259 | "name tuples must contain only " |
| 260 | "strings, not '%.500s'", |
| 261 | item->ob_type->tp_name); |
| 262 | Py_DECREF(newtuple); |
| 263 | return NULL; |
| 264 | } |
| 265 | else { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 266 | item = _PyUnicode_Copy(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | if (item == NULL) { |
| 268 | Py_DECREF(newtuple); |
| 269 | return NULL; |
| 270 | } |
| 271 | } |
| 272 | PyTuple_SET_ITEM(newtuple, i, item); |
| 273 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | return newtuple; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | PyDoc_STRVAR(code_doc, |
Georg Brandl | 8c1a50a | 2009-07-18 09:07:48 +0000 | [diff] [blame] | 279 | "code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\ |
Georg Brandl | a1e7e13 | 2008-01-26 09:39:23 +0000 | [diff] [blame] | 280 | constants, names, varnames, filename, name, firstlineno,\n\ |
| 281 | lnotab[, freevars[, cellvars]])\n\ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 282 | \n\ |
| 283 | Create a code object. Not for the faint of heart."); |
| 284 | |
| 285 | static PyObject * |
| 286 | code_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | int argcount; |
| 289 | int kwonlyargcount; |
| 290 | int nlocals; |
| 291 | int stacksize; |
| 292 | int flags; |
| 293 | PyObject *co = NULL; |
| 294 | PyObject *code; |
| 295 | PyObject *consts; |
| 296 | PyObject *names, *ournames = NULL; |
| 297 | PyObject *varnames, *ourvarnames = NULL; |
| 298 | PyObject *freevars = NULL, *ourfreevars = NULL; |
| 299 | PyObject *cellvars = NULL, *ourcellvars = NULL; |
| 300 | PyObject *filename; |
| 301 | PyObject *name; |
| 302 | int firstlineno; |
| 303 | PyObject *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code", |
| 306 | &argcount, &kwonlyargcount, |
| 307 | &nlocals, &stacksize, &flags, |
| 308 | &code, |
| 309 | &PyTuple_Type, &consts, |
| 310 | &PyTuple_Type, &names, |
| 311 | &PyTuple_Type, &varnames, |
| 312 | &filename, &name, |
| 313 | &firstlineno, &lnotab, |
| 314 | &PyTuple_Type, &freevars, |
| 315 | &PyTuple_Type, &cellvars)) |
| 316 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | if (argcount < 0) { |
| 319 | PyErr_SetString( |
| 320 | PyExc_ValueError, |
| 321 | "code: argcount must not be negative"); |
| 322 | goto cleanup; |
| 323 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | if (kwonlyargcount < 0) { |
| 326 | PyErr_SetString( |
| 327 | PyExc_ValueError, |
| 328 | "code: kwonlyargcount must not be negative"); |
| 329 | goto cleanup; |
| 330 | } |
| 331 | if (nlocals < 0) { |
| 332 | PyErr_SetString( |
| 333 | PyExc_ValueError, |
| 334 | "code: nlocals must not be negative"); |
| 335 | goto cleanup; |
| 336 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | ournames = validate_and_copy_tuple(names); |
| 339 | if (ournames == NULL) |
| 340 | goto cleanup; |
| 341 | ourvarnames = validate_and_copy_tuple(varnames); |
| 342 | if (ourvarnames == NULL) |
| 343 | goto cleanup; |
| 344 | if (freevars) |
| 345 | ourfreevars = validate_and_copy_tuple(freevars); |
| 346 | else |
| 347 | ourfreevars = PyTuple_New(0); |
| 348 | if (ourfreevars == NULL) |
| 349 | goto cleanup; |
| 350 | if (cellvars) |
| 351 | ourcellvars = validate_and_copy_tuple(cellvars); |
| 352 | else |
| 353 | ourcellvars = PyTuple_New(0); |
| 354 | if (ourcellvars == NULL) |
| 355 | goto cleanup; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | co = (PyObject *)PyCode_New(argcount, kwonlyargcount, |
| 358 | nlocals, stacksize, flags, |
| 359 | code, consts, ournames, ourvarnames, |
| 360 | ourfreevars, ourcellvars, filename, |
| 361 | name, firstlineno, lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 362 | cleanup: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | Py_XDECREF(ournames); |
| 364 | Py_XDECREF(ourvarnames); |
| 365 | Py_XDECREF(ourfreevars); |
| 366 | Py_XDECREF(ourcellvars); |
| 367 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void |
| 371 | code_dealloc(PyCodeObject *co) |
| 372 | { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 373 | if (co->co_extra != NULL) { |
| 374 | PyThreadState *tstate = PyThreadState_Get(); |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 375 | _PyCodeObjectExtra *co_extra = co->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 376 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 377 | for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 378 | freefunc free_extra = tstate->co_extra_freefuncs[i]; |
| 379 | |
| 380 | if (free_extra != NULL) { |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 381 | free_extra(co_extra->ce_extras[i]); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | PyMem_FREE(co->co_extra); |
| 386 | } |
| 387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | Py_XDECREF(co->co_code); |
| 389 | Py_XDECREF(co->co_consts); |
| 390 | Py_XDECREF(co->co_names); |
| 391 | Py_XDECREF(co->co_varnames); |
| 392 | Py_XDECREF(co->co_freevars); |
| 393 | Py_XDECREF(co->co_cellvars); |
| 394 | Py_XDECREF(co->co_filename); |
| 395 | Py_XDECREF(co->co_name); |
| 396 | Py_XDECREF(co->co_lnotab); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 397 | if (co->co_cell2arg != NULL) |
| 398 | PyMem_FREE(co->co_cell2arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | if (co->co_zombieframe != NULL) |
| 400 | PyObject_GC_Del(co->co_zombieframe); |
| 401 | if (co->co_weakreflist != NULL) |
| 402 | PyObject_ClearWeakRefs((PyObject*)co); |
| 403 | PyObject_DEL(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static PyObject * |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 407 | code_sizeof(PyCodeObject *co, void *unused) |
| 408 | { |
| 409 | Py_ssize_t res; |
| 410 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 411 | res = _PyObject_SIZE(Py_TYPE(co)); |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 412 | if (co->co_cell2arg != NULL && co->co_cellvars != NULL) |
| 413 | res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char); |
| 414 | return PyLong_FromSsize_t(res); |
| 415 | } |
| 416 | |
| 417 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 418 | code_repr(PyCodeObject *co) |
| 419 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | int lineno; |
| 421 | if (co->co_firstlineno != 0) |
| 422 | lineno = co->co_firstlineno; |
| 423 | else |
| 424 | lineno = -1; |
| 425 | if (co->co_filename && PyUnicode_Check(co->co_filename)) { |
| 426 | return PyUnicode_FromFormat( |
Victor Stinner | aaa4e9a | 2011-01-05 03:33:26 +0000 | [diff] [blame] | 427 | "<code object %U at %p, file \"%U\", line %d>", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | co->co_name, co, co->co_filename, lineno); |
| 429 | } else { |
| 430 | return PyUnicode_FromFormat( |
Victor Stinner | aaa4e9a | 2011-01-05 03:33:26 +0000 | [diff] [blame] | 431 | "<code object %U at %p, file ???, line %d>", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | co->co_name, co, lineno); |
| 433 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 436 | PyObject* |
| 437 | _PyCode_ConstantKey(PyObject *op) |
| 438 | { |
| 439 | PyObject *key; |
| 440 | |
| 441 | /* Py_None and Py_Ellipsis are singleton */ |
| 442 | if (op == Py_None || op == Py_Ellipsis |
| 443 | || PyLong_CheckExact(op) |
| 444 | || PyBool_Check(op) |
| 445 | || PyBytes_CheckExact(op) |
| 446 | || PyUnicode_CheckExact(op) |
| 447 | /* code_richcompare() uses _PyCode_ConstantKey() internally */ |
| 448 | || PyCode_Check(op)) { |
| 449 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 450 | } |
| 451 | else if (PyFloat_CheckExact(op)) { |
| 452 | double d = PyFloat_AS_DOUBLE(op); |
| 453 | /* all we need is to make the tuple different in either the 0.0 |
| 454 | * or -0.0 case from all others, just to avoid the "coercion". |
| 455 | */ |
| 456 | if (d == 0.0 && copysign(1.0, d) < 0.0) |
| 457 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); |
| 458 | else |
| 459 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 460 | } |
| 461 | else if (PyComplex_CheckExact(op)) { |
| 462 | Py_complex z; |
| 463 | int real_negzero, imag_negzero; |
| 464 | /* For the complex case we must make complex(x, 0.) |
| 465 | different from complex(x, -0.) and complex(0., y) |
| 466 | different from complex(-0., y), for any x and y. |
| 467 | All four complex zeros must be distinguished.*/ |
| 468 | z = PyComplex_AsCComplex(op); |
| 469 | real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0; |
| 470 | imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0; |
| 471 | /* use True, False and None singleton as tags for the real and imag |
| 472 | * sign, to make tuples different */ |
| 473 | if (real_negzero && imag_negzero) { |
| 474 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True); |
| 475 | } |
| 476 | else if (imag_negzero) { |
| 477 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False); |
| 478 | } |
| 479 | else if (real_negzero) { |
| 480 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); |
| 481 | } |
| 482 | else { |
| 483 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 484 | } |
| 485 | } |
| 486 | else if (PyTuple_CheckExact(op)) { |
| 487 | Py_ssize_t i, len; |
| 488 | PyObject *tuple; |
| 489 | |
| 490 | len = PyTuple_GET_SIZE(op); |
| 491 | tuple = PyTuple_New(len); |
| 492 | if (tuple == NULL) |
| 493 | return NULL; |
| 494 | |
| 495 | for (i=0; i < len; i++) { |
| 496 | PyObject *item, *item_key; |
| 497 | |
| 498 | item = PyTuple_GET_ITEM(op, i); |
| 499 | item_key = _PyCode_ConstantKey(item); |
| 500 | if (item_key == NULL) { |
| 501 | Py_DECREF(tuple); |
| 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | PyTuple_SET_ITEM(tuple, i, item_key); |
| 506 | } |
| 507 | |
| 508 | key = PyTuple_Pack(3, Py_TYPE(op), op, tuple); |
| 509 | Py_DECREF(tuple); |
| 510 | } |
| 511 | else if (PyFrozenSet_CheckExact(op)) { |
| 512 | Py_ssize_t pos = 0; |
| 513 | PyObject *item; |
| 514 | Py_hash_t hash; |
| 515 | Py_ssize_t i, len; |
| 516 | PyObject *tuple, *set; |
| 517 | |
| 518 | len = PySet_GET_SIZE(op); |
| 519 | tuple = PyTuple_New(len); |
| 520 | if (tuple == NULL) |
| 521 | return NULL; |
| 522 | |
| 523 | i = 0; |
| 524 | while (_PySet_NextEntry(op, &pos, &item, &hash)) { |
| 525 | PyObject *item_key; |
| 526 | |
| 527 | item_key = _PyCode_ConstantKey(item); |
| 528 | if (item_key == NULL) { |
| 529 | Py_DECREF(tuple); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | assert(i < len); |
| 534 | PyTuple_SET_ITEM(tuple, i, item_key); |
| 535 | i++; |
| 536 | } |
| 537 | set = PyFrozenSet_New(tuple); |
| 538 | Py_DECREF(tuple); |
| 539 | if (set == NULL) |
| 540 | return NULL; |
| 541 | |
| 542 | key = PyTuple_Pack(3, Py_TYPE(op), op, set); |
| 543 | Py_DECREF(set); |
| 544 | return key; |
| 545 | } |
| 546 | else { |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 547 | /* for other types, use the object identifier as a unique identifier |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 548 | * to ensure that they are seen as unequal. */ |
| 549 | PyObject *obj_id = PyLong_FromVoidPtr(op); |
| 550 | if (obj_id == NULL) |
| 551 | return NULL; |
| 552 | |
| 553 | key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id); |
| 554 | Py_DECREF(obj_id); |
| 555 | } |
| 556 | return key; |
| 557 | } |
| 558 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 559 | static PyObject * |
| 560 | code_richcompare(PyObject *self, PyObject *other, int op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | PyCodeObject *co, *cp; |
| 563 | int eq; |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 564 | PyObject *consts1, *consts2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | PyObject *res; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | if ((op != Py_EQ && op != Py_NE) || |
| 568 | !PyCode_Check(self) || |
| 569 | !PyCode_Check(other)) { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 570 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | co = (PyCodeObject *)self; |
| 574 | cp = (PyCodeObject *)other; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ); |
| 577 | if (eq <= 0) goto unequal; |
| 578 | eq = co->co_argcount == cp->co_argcount; |
| 579 | if (!eq) goto unequal; |
| 580 | eq = co->co_kwonlyargcount == cp->co_kwonlyargcount; |
| 581 | if (!eq) goto unequal; |
| 582 | eq = co->co_nlocals == cp->co_nlocals; |
| 583 | if (!eq) goto unequal; |
| 584 | eq = co->co_flags == cp->co_flags; |
| 585 | if (!eq) goto unequal; |
| 586 | eq = co->co_firstlineno == cp->co_firstlineno; |
| 587 | if (!eq) goto unequal; |
| 588 | eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ); |
| 589 | if (eq <= 0) goto unequal; |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 590 | |
| 591 | /* compare constants */ |
| 592 | consts1 = _PyCode_ConstantKey(co->co_consts); |
| 593 | if (!consts1) |
| 594 | return NULL; |
| 595 | consts2 = _PyCode_ConstantKey(cp->co_consts); |
| 596 | if (!consts2) { |
| 597 | Py_DECREF(consts1); |
| 598 | return NULL; |
| 599 | } |
| 600 | eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ); |
| 601 | Py_DECREF(consts1); |
| 602 | Py_DECREF(consts2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | if (eq <= 0) goto unequal; |
Victor Stinner | efb2413 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ); |
| 606 | if (eq <= 0) goto unequal; |
| 607 | eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ); |
| 608 | if (eq <= 0) goto unequal; |
| 609 | eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ); |
| 610 | if (eq <= 0) goto unequal; |
| 611 | eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ); |
| 612 | if (eq <= 0) goto unequal; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | if (op == Py_EQ) |
| 615 | res = Py_True; |
| 616 | else |
| 617 | res = Py_False; |
| 618 | goto done; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 619 | |
| 620 | unequal: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | if (eq < 0) |
| 622 | return NULL; |
| 623 | if (op == Py_NE) |
| 624 | res = Py_True; |
| 625 | else |
| 626 | res = Py_False; |
Guido van Rossum | b6bb0c7 | 2006-08-24 04:12:18 +0000 | [diff] [blame] | 627 | |
| 628 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | Py_INCREF(res); |
| 630 | return res; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 633 | static Py_hash_t |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 634 | code_hash(PyCodeObject *co) |
| 635 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 636 | Py_hash_t h, h0, h1, h2, h3, h4, h5, h6; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | h0 = PyObject_Hash(co->co_name); |
| 638 | if (h0 == -1) return -1; |
| 639 | h1 = PyObject_Hash(co->co_code); |
| 640 | if (h1 == -1) return -1; |
| 641 | h2 = PyObject_Hash(co->co_consts); |
| 642 | if (h2 == -1) return -1; |
| 643 | h3 = PyObject_Hash(co->co_names); |
| 644 | if (h3 == -1) return -1; |
| 645 | h4 = PyObject_Hash(co->co_varnames); |
| 646 | if (h4 == -1) return -1; |
| 647 | h5 = PyObject_Hash(co->co_freevars); |
| 648 | if (h5 == -1) return -1; |
| 649 | h6 = PyObject_Hash(co->co_cellvars); |
| 650 | if (h6 == -1) return -1; |
| 651 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
| 652 | co->co_argcount ^ co->co_kwonlyargcount ^ |
| 653 | co->co_nlocals ^ co->co_flags; |
| 654 | if (h == -1) h = -2; |
| 655 | return h; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* XXX code objects need to participate in GC? */ |
| 659 | |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 660 | static struct PyMethodDef code_methods[] = { |
| 661 | {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS}, |
| 662 | {NULL, NULL} /* sentinel */ |
| 663 | }; |
| 664 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 665 | PyTypeObject PyCode_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 667 | "code", |
| 668 | sizeof(PyCodeObject), |
| 669 | 0, |
| 670 | (destructor)code_dealloc, /* tp_dealloc */ |
| 671 | 0, /* tp_print */ |
| 672 | 0, /* tp_getattr */ |
| 673 | 0, /* tp_setattr */ |
| 674 | 0, /* tp_reserved */ |
| 675 | (reprfunc)code_repr, /* tp_repr */ |
| 676 | 0, /* tp_as_number */ |
| 677 | 0, /* tp_as_sequence */ |
| 678 | 0, /* tp_as_mapping */ |
| 679 | (hashfunc)code_hash, /* tp_hash */ |
| 680 | 0, /* tp_call */ |
| 681 | 0, /* tp_str */ |
| 682 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 683 | 0, /* tp_setattro */ |
| 684 | 0, /* tp_as_buffer */ |
| 685 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 686 | code_doc, /* tp_doc */ |
| 687 | 0, /* tp_traverse */ |
| 688 | 0, /* tp_clear */ |
| 689 | code_richcompare, /* tp_richcompare */ |
| 690 | offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */ |
| 691 | 0, /* tp_iter */ |
| 692 | 0, /* tp_iternext */ |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 693 | code_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | code_memberlist, /* tp_members */ |
| 695 | 0, /* tp_getset */ |
| 696 | 0, /* tp_base */ |
| 697 | 0, /* tp_dict */ |
| 698 | 0, /* tp_descr_get */ |
| 699 | 0, /* tp_descr_set */ |
| 700 | 0, /* tp_dictoffset */ |
| 701 | 0, /* tp_init */ |
| 702 | 0, /* tp_alloc */ |
| 703 | code_new, /* tp_new */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 704 | }; |
| 705 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 706 | /* Use co_lnotab to compute the line number from a bytecode index, addrq. See |
| 707 | lnotab_notes.txt for the details of the lnotab representation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 708 | */ |
| 709 | |
| 710 | int |
| 711 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
| 712 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 713 | Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab); |
| 715 | int line = co->co_firstlineno; |
| 716 | int addr = 0; |
| 717 | while (--size >= 0) { |
| 718 | addr += *p++; |
| 719 | if (addr > addrq) |
| 720 | break; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 721 | line += (signed char)*p; |
| 722 | p++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | } |
| 724 | return line; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 725 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 726 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 727 | /* Update *bounds to describe the first and one-past-the-last instructions in |
| 728 | the same line as lasti. Return the number of that line. */ |
| 729 | int |
| 730 | _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 731 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 732 | Py_ssize_t size; |
| 733 | int addr, line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | unsigned char* p; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab); |
| 737 | size = PyBytes_GET_SIZE(co->co_lnotab) / 2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | addr = 0; |
| 740 | line = co->co_firstlineno; |
| 741 | assert(line > 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | /* possible optimization: if f->f_lasti == instr_ub |
| 744 | (likely to be a common case) then we already know |
| 745 | instr_lb -- if we stored the matching value of p |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 746 | somewhere we could skip the first while loop. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | /* See lnotab_notes.txt for the description of |
| 749 | co_lnotab. A point to remember: increments to p |
| 750 | come in (addr, line) pairs. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | bounds->ap_lower = 0; |
| 753 | while (size > 0) { |
| 754 | if (addr + *p > lasti) |
| 755 | break; |
| 756 | addr += *p++; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 757 | if ((signed char)*p) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | bounds->ap_lower = addr; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 759 | line += (signed char)*p; |
| 760 | p++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | --size; |
| 762 | } |
| 763 | |
| 764 | if (size > 0) { |
| 765 | while (--size >= 0) { |
| 766 | addr += *p++; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 767 | if ((signed char)*p) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | break; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 769 | p++; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 770 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | bounds->ap_upper = addr; |
| 772 | } |
| 773 | else { |
| 774 | bounds->ap_upper = INT_MAX; |
| 775 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | return line; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 778 | } |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 779 | |
| 780 | |
| 781 | int |
| 782 | _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra) |
| 783 | { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 784 | assert(*extra == NULL); |
| 785 | |
| 786 | if (!PyCode_Check(code)) { |
| 787 | PyErr_BadInternalCall(); |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 788 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 791 | PyCodeObject *o = (PyCodeObject*) code; |
| 792 | _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 793 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 794 | |
| 795 | if (co_extra == NULL || co_extra->ce_size <= index) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 799 | *extra = co_extra->ce_extras[index]; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | |
| 804 | int |
| 805 | _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra) |
| 806 | { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 807 | PyThreadState *tstate = PyThreadState_Get(); |
| 808 | |
| 809 | if (!PyCode_Check(code) || index < 0 || |
| 810 | index >= tstate->co_extra_user_count) { |
| 811 | PyErr_BadInternalCall(); |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 812 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 815 | PyCodeObject *o = (PyCodeObject*) code; |
| 816 | _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 817 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 818 | if (co_extra == NULL) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 819 | o->co_extra = (_PyCodeObjectExtra*) PyMem_Malloc( |
| 820 | sizeof(_PyCodeObjectExtra)); |
| 821 | if (o->co_extra == NULL) { |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 822 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 823 | } |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 824 | co_extra = (_PyCodeObjectExtra *) o->co_extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 825 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 826 | co_extra->ce_extras = PyMem_Malloc( |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 827 | tstate->co_extra_user_count * sizeof(void*)); |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 828 | if (co_extra->ce_extras == NULL) { |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 829 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 832 | co_extra->ce_size = tstate->co_extra_user_count; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 833 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 834 | for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) { |
| 835 | co_extra->ce_extras[i] = NULL; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 836 | } |
| 837 | } |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 838 | else if (co_extra->ce_size <= index) { |
| 839 | co_extra->ce_extras = PyMem_Realloc( |
| 840 | co_extra->ce_extras, tstate->co_extra_user_count * sizeof(void*)); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 841 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 842 | if (co_extra->ce_extras == NULL) { |
Brett Cannon | 3788b85 | 2016-09-07 12:51:08 -0700 | [diff] [blame] | 843 | return -1; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 846 | co_extra->ce_size = tstate->co_extra_user_count; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 847 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 848 | for (Py_ssize_t i = co_extra->ce_size; i < co_extra->ce_size; i++) { |
| 849 | co_extra->ce_extras[i] = NULL; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
Brett Cannon | d0600ed | 2016-09-07 14:30:39 -0700 | [diff] [blame] | 853 | co_extra->ce_extras[index] = extra; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 854 | return 0; |
| 855 | } |