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