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 | c83ea13 | 2010-05-09 14:46:46 +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 |
Serhiy Storchaka | ab8b75a | 2016-10-04 18:17:08 +0300 | [diff] [blame^] | 11 | all_name_chars(PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 12 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 13 | static char ok_name_char[256]; |
Serhiy Storchaka | ab8b75a | 2016-10-04 18:17:08 +0300 | [diff] [blame^] | 14 | static const unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
| 15 | const unsigned char *s, *e; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 16 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 17 | if (ok_name_char[*name_chars] == 0) { |
Serhiy Storchaka | ab8b75a | 2016-10-04 18:17:08 +0300 | [diff] [blame^] | 18 | const unsigned char *p; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 19 | for (p = name_chars; *p; p++) |
| 20 | ok_name_char[*p] = 1; |
| 21 | } |
Serhiy Storchaka | ab8b75a | 2016-10-04 18:17:08 +0300 | [diff] [blame^] | 22 | s = (unsigned char *)PyString_AS_STRING(o); |
| 23 | e = s + PyString_GET_SIZE(o); |
| 24 | while (s != e) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 25 | if (ok_name_char[*s++] == 0) |
| 26 | return 0; |
| 27 | } |
| 28 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static void |
| 32 | intern_strings(PyObject *tuple) |
| 33 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 34 | Py_ssize_t i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 35 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 36 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 37 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 38 | if (v == NULL || !PyString_CheckExact(v)) { |
| 39 | Py_FatalError("non-string found in code slot"); |
| 40 | } |
| 41 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 42 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Serhiy Storchaka | 67edf73 | 2016-09-30 10:38:08 +0300 | [diff] [blame] | 45 | /* Intern selected string constants */ |
| 46 | static int |
| 47 | intern_string_constants(PyObject *tuple) |
| 48 | { |
| 49 | int modified = 0; |
| 50 | Py_ssize_t i; |
| 51 | |
| 52 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 53 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 54 | if (PyString_CheckExact(v)) { |
Serhiy Storchaka | ab8b75a | 2016-10-04 18:17:08 +0300 | [diff] [blame^] | 55 | if (all_name_chars(v)) { |
Serhiy Storchaka | 67edf73 | 2016-09-30 10:38:08 +0300 | [diff] [blame] | 56 | PyObject *w = v; |
| 57 | PyString_InternInPlace(&v); |
| 58 | if (w != v) { |
| 59 | PyTuple_SET_ITEM(tuple, i, v); |
| 60 | modified = 1; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | else if (PyTuple_CheckExact(v)) { |
| 65 | intern_string_constants(v); |
| 66 | } |
| 67 | else if (PyFrozenSet_CheckExact(v)) { |
| 68 | PyObject *tmp = PySequence_Tuple(v); |
| 69 | if (tmp == NULL) { |
| 70 | PyErr_Clear(); |
| 71 | continue; |
| 72 | } |
| 73 | if (intern_string_constants(tmp)) { |
| 74 | v = PyFrozenSet_New(tmp); |
| 75 | if (v == NULL) { |
| 76 | PyErr_Clear(); |
| 77 | } |
| 78 | else { |
| 79 | PyTuple_SET_ITEM(tuple, i, v); |
| 80 | modified = 1; |
| 81 | } |
| 82 | } |
| 83 | Py_DECREF(tmp); |
| 84 | } |
| 85 | } |
| 86 | return modified; |
| 87 | } |
| 88 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 89 | |
| 90 | PyCodeObject * |
| 91 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 92 | PyObject *code, PyObject *consts, PyObject *names, |
| 93 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 94 | PyObject *filename, PyObject *name, int firstlineno, |
| 95 | PyObject *lnotab) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 96 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 97 | PyCodeObject *co; |
| 98 | Py_ssize_t i; |
| 99 | /* Check argument types */ |
| 100 | if (argcount < 0 || nlocals < 0 || |
| 101 | code == NULL || |
| 102 | consts == NULL || !PyTuple_Check(consts) || |
| 103 | names == NULL || !PyTuple_Check(names) || |
| 104 | varnames == NULL || !PyTuple_Check(varnames) || |
| 105 | freevars == NULL || !PyTuple_Check(freevars) || |
| 106 | cellvars == NULL || !PyTuple_Check(cellvars) || |
| 107 | name == NULL || !PyString_Check(name) || |
| 108 | filename == NULL || !PyString_Check(filename) || |
| 109 | lnotab == NULL || !PyString_Check(lnotab) || |
| 110 | !PyObject_CheckReadBuffer(code)) { |
| 111 | PyErr_BadInternalCall(); |
| 112 | return NULL; |
| 113 | } |
| 114 | intern_strings(names); |
| 115 | intern_strings(varnames); |
| 116 | intern_strings(freevars); |
| 117 | intern_strings(cellvars); |
Serhiy Storchaka | 67edf73 | 2016-09-30 10:38:08 +0300 | [diff] [blame] | 118 | intern_string_constants(consts); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 119 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
| 120 | if (co != NULL) { |
| 121 | co->co_argcount = argcount; |
| 122 | co->co_nlocals = nlocals; |
| 123 | co->co_stacksize = stacksize; |
| 124 | co->co_flags = flags; |
| 125 | Py_INCREF(code); |
| 126 | co->co_code = code; |
| 127 | Py_INCREF(consts); |
| 128 | co->co_consts = consts; |
| 129 | Py_INCREF(names); |
| 130 | co->co_names = names; |
| 131 | Py_INCREF(varnames); |
| 132 | co->co_varnames = varnames; |
| 133 | Py_INCREF(freevars); |
| 134 | co->co_freevars = freevars; |
| 135 | Py_INCREF(cellvars); |
| 136 | co->co_cellvars = cellvars; |
| 137 | Py_INCREF(filename); |
| 138 | co->co_filename = filename; |
| 139 | Py_INCREF(name); |
| 140 | co->co_name = name; |
| 141 | co->co_firstlineno = firstlineno; |
| 142 | Py_INCREF(lnotab); |
| 143 | co->co_lnotab = lnotab; |
| 144 | co->co_zombieframe = NULL; |
| 145 | co->co_weakreflist = NULL; |
| 146 | } |
| 147 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 150 | PyCodeObject * |
| 151 | PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) |
| 152 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 153 | static PyObject *emptystring = NULL; |
| 154 | static PyObject *nulltuple = NULL; |
| 155 | PyObject *filename_ob = NULL; |
| 156 | PyObject *funcname_ob = NULL; |
| 157 | PyCodeObject *result = NULL; |
| 158 | if (emptystring == NULL) { |
| 159 | emptystring = PyString_FromString(""); |
| 160 | if (emptystring == NULL) |
| 161 | goto failed; |
| 162 | } |
| 163 | if (nulltuple == NULL) { |
| 164 | nulltuple = PyTuple_New(0); |
| 165 | if (nulltuple == NULL) |
| 166 | goto failed; |
| 167 | } |
| 168 | funcname_ob = PyString_FromString(funcname); |
| 169 | if (funcname_ob == NULL) |
| 170 | goto failed; |
| 171 | filename_ob = PyString_FromString(filename); |
| 172 | if (filename_ob == NULL) |
| 173 | goto failed; |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 175 | result = PyCode_New(0, /* argcount */ |
| 176 | 0, /* nlocals */ |
| 177 | 0, /* stacksize */ |
| 178 | 0, /* flags */ |
| 179 | emptystring, /* code */ |
| 180 | nulltuple, /* consts */ |
| 181 | nulltuple, /* names */ |
| 182 | nulltuple, /* varnames */ |
| 183 | nulltuple, /* freevars */ |
| 184 | nulltuple, /* cellvars */ |
| 185 | filename_ob, /* filename */ |
| 186 | funcname_ob, /* name */ |
| 187 | firstlineno, /* firstlineno */ |
| 188 | emptystring /* lnotab */ |
| 189 | ); |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 190 | |
| 191 | failed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 192 | Py_XDECREF(funcname_ob); |
| 193 | Py_XDECREF(filename_ob); |
| 194 | return result; |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 195 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 196 | |
| 197 | #define OFF(x) offsetof(PyCodeObject, x) |
| 198 | |
| 199 | static PyMemberDef code_memberlist[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 200 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 201 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 202 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
| 203 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
| 204 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 205 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 206 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
| 207 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
| 208 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 209 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
| 210 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
| 211 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
| 212 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 213 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
| 214 | {NULL} /* Sentinel */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | /* Helper for code_new: return a shallow copy of a tuple that is |
| 218 | guaranteed to contain exact strings, by converting string subclasses |
| 219 | to exact strings and complaining if a non-string is found. */ |
| 220 | static PyObject* |
| 221 | validate_and_copy_tuple(PyObject *tup) |
| 222 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 223 | PyObject *newtuple; |
| 224 | PyObject *item; |
| 225 | Py_ssize_t i, len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 226 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 227 | len = PyTuple_GET_SIZE(tup); |
| 228 | newtuple = PyTuple_New(len); |
| 229 | if (newtuple == NULL) |
| 230 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 232 | for (i = 0; i < len; i++) { |
| 233 | item = PyTuple_GET_ITEM(tup, i); |
| 234 | if (PyString_CheckExact(item)) { |
| 235 | Py_INCREF(item); |
| 236 | } |
| 237 | else if (!PyString_Check(item)) { |
| 238 | PyErr_Format( |
| 239 | PyExc_TypeError, |
| 240 | "name tuples must contain only " |
| 241 | "strings, not '%.500s'", |
| 242 | item->ob_type->tp_name); |
| 243 | Py_DECREF(newtuple); |
| 244 | return NULL; |
| 245 | } |
| 246 | else { |
| 247 | item = PyString_FromStringAndSize( |
| 248 | PyString_AS_STRING(item), |
| 249 | PyString_GET_SIZE(item)); |
| 250 | if (item == NULL) { |
| 251 | Py_DECREF(newtuple); |
| 252 | return NULL; |
| 253 | } |
| 254 | } |
| 255 | PyTuple_SET_ITEM(newtuple, i, item); |
| 256 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 257 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 258 | return newtuple; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | PyDoc_STRVAR(code_doc, |
| 262 | "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\ |
| 263 | varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\ |
| 264 | \n\ |
| 265 | Create a code object. Not for the faint of heart."); |
| 266 | |
| 267 | static PyObject * |
| 268 | code_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 269 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 270 | int argcount; |
| 271 | int nlocals; |
| 272 | int stacksize; |
| 273 | int flags; |
| 274 | PyObject *co = NULL; |
| 275 | PyObject *code; |
| 276 | PyObject *consts; |
| 277 | PyObject *names, *ournames = NULL; |
| 278 | PyObject *varnames, *ourvarnames = NULL; |
| 279 | PyObject *freevars = NULL, *ourfreevars = NULL; |
| 280 | PyObject *cellvars = NULL, *ourcellvars = NULL; |
| 281 | PyObject *filename; |
| 282 | PyObject *name; |
| 283 | int firstlineno; |
| 284 | PyObject *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 286 | if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code", |
| 287 | &argcount, &nlocals, &stacksize, &flags, |
| 288 | &code, |
| 289 | &PyTuple_Type, &consts, |
| 290 | &PyTuple_Type, &names, |
| 291 | &PyTuple_Type, &varnames, |
| 292 | &filename, &name, |
| 293 | &firstlineno, &lnotab, |
| 294 | &PyTuple_Type, &freevars, |
| 295 | &PyTuple_Type, &cellvars)) |
| 296 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | if (argcount < 0) { |
| 299 | PyErr_SetString( |
| 300 | PyExc_ValueError, |
| 301 | "code: argcount must not be negative"); |
| 302 | goto cleanup; |
| 303 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 305 | if (nlocals < 0) { |
| 306 | PyErr_SetString( |
| 307 | PyExc_ValueError, |
| 308 | "code: nlocals must not be negative"); |
| 309 | goto cleanup; |
| 310 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 312 | ournames = validate_and_copy_tuple(names); |
| 313 | if (ournames == NULL) |
| 314 | goto cleanup; |
| 315 | ourvarnames = validate_and_copy_tuple(varnames); |
| 316 | if (ourvarnames == NULL) |
| 317 | goto cleanup; |
| 318 | if (freevars) |
| 319 | ourfreevars = validate_and_copy_tuple(freevars); |
| 320 | else |
| 321 | ourfreevars = PyTuple_New(0); |
| 322 | if (ourfreevars == NULL) |
| 323 | goto cleanup; |
| 324 | if (cellvars) |
| 325 | ourcellvars = validate_and_copy_tuple(cellvars); |
| 326 | else |
| 327 | ourcellvars = PyTuple_New(0); |
| 328 | if (ourcellvars == NULL) |
| 329 | goto cleanup; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 331 | co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
| 332 | code, consts, ournames, ourvarnames, |
| 333 | ourfreevars, ourcellvars, filename, |
| 334 | name, firstlineno, lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 335 | cleanup: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 336 | Py_XDECREF(ournames); |
| 337 | Py_XDECREF(ourvarnames); |
| 338 | Py_XDECREF(ourfreevars); |
| 339 | Py_XDECREF(ourcellvars); |
| 340 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static void |
| 344 | code_dealloc(PyCodeObject *co) |
| 345 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 346 | Py_XDECREF(co->co_code); |
| 347 | Py_XDECREF(co->co_consts); |
| 348 | Py_XDECREF(co->co_names); |
| 349 | Py_XDECREF(co->co_varnames); |
| 350 | Py_XDECREF(co->co_freevars); |
| 351 | Py_XDECREF(co->co_cellvars); |
| 352 | Py_XDECREF(co->co_filename); |
| 353 | Py_XDECREF(co->co_name); |
| 354 | Py_XDECREF(co->co_lnotab); |
| 355 | if (co->co_zombieframe != NULL) |
| 356 | PyObject_GC_Del(co->co_zombieframe); |
| 357 | if (co->co_weakreflist != NULL) |
| 358 | PyObject_ClearWeakRefs((PyObject*)co); |
| 359 | PyObject_DEL(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static PyObject * |
| 363 | code_repr(PyCodeObject *co) |
| 364 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 365 | char buf[500]; |
| 366 | int lineno = -1; |
| 367 | char *filename = "???"; |
| 368 | char *name = "???"; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 370 | if (co->co_firstlineno != 0) |
| 371 | lineno = co->co_firstlineno; |
| 372 | if (co->co_filename && PyString_Check(co->co_filename)) |
| 373 | filename = PyString_AS_STRING(co->co_filename); |
| 374 | if (co->co_name && PyString_Check(co->co_name)) |
| 375 | name = PyString_AS_STRING(co->co_name); |
| 376 | PyOS_snprintf(buf, sizeof(buf), |
| 377 | "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 378 | name, co, filename, lineno); |
| 379 | return PyString_FromString(buf); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int |
| 383 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
| 384 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 385 | int cmp; |
| 386 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 387 | if (cmp) return cmp; |
| 388 | cmp = co->co_argcount - cp->co_argcount; |
| 389 | if (cmp) goto normalize; |
| 390 | cmp = co->co_nlocals - cp->co_nlocals; |
| 391 | if (cmp) goto normalize; |
| 392 | cmp = co->co_flags - cp->co_flags; |
| 393 | if (cmp) goto normalize; |
| 394 | cmp = co->co_firstlineno - cp->co_firstlineno; |
| 395 | if (cmp) goto normalize; |
| 396 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
| 397 | if (cmp) return cmp; |
| 398 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
| 399 | if (cmp) return cmp; |
| 400 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
| 401 | if (cmp) return cmp; |
| 402 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
| 403 | if (cmp) return cmp; |
| 404 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 405 | if (cmp) return cmp; |
| 406 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
| 407 | return cmp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 408 | |
| 409 | normalize: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 410 | if (cmp > 0) |
| 411 | return 1; |
| 412 | else if (cmp < 0) |
| 413 | return -1; |
| 414 | else |
| 415 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Victor Stinner | 7791165 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 418 | PyObject* |
| 419 | _PyCode_ConstantKey(PyObject *op) |
| 420 | { |
| 421 | PyObject *key; |
| 422 | |
| 423 | /* Py_None is a singleton */ |
| 424 | if (op == Py_None |
| 425 | || PyInt_CheckExact(op) |
| 426 | || PyLong_CheckExact(op) |
| 427 | || PyBool_Check(op) |
| 428 | || PyBytes_CheckExact(op) |
| 429 | #ifdef Py_USING_UNICODE |
| 430 | || PyUnicode_CheckExact(op) |
| 431 | #endif |
| 432 | /* code_richcompare() uses _PyCode_ConstantKey() internally */ |
| 433 | || PyCode_Check(op)) { |
| 434 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 435 | } |
| 436 | else if (PyFloat_CheckExact(op)) { |
| 437 | double d = PyFloat_AS_DOUBLE(op); |
| 438 | /* all we need is to make the tuple different in either the 0.0 |
| 439 | * or -0.0 case from all others, just to avoid the "coercion". |
| 440 | */ |
| 441 | if (d == 0.0 && copysign(1.0, d) < 0.0) |
| 442 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); |
| 443 | else |
| 444 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 445 | } |
| 446 | #ifndef WITHOUT_COMPLEX |
| 447 | else if (PyComplex_CheckExact(op)) { |
| 448 | Py_complex z; |
| 449 | int real_negzero, imag_negzero; |
| 450 | /* For the complex case we must make complex(x, 0.) |
| 451 | different from complex(x, -0.) and complex(0., y) |
| 452 | different from complex(-0., y), for any x and y. |
| 453 | All four complex zeros must be distinguished.*/ |
| 454 | z = PyComplex_AsCComplex(op); |
| 455 | real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0; |
| 456 | imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0; |
| 457 | /* use True, False and None singleton as tags for the real and imag |
| 458 | * sign, to make tuples different */ |
| 459 | if (real_negzero && imag_negzero) { |
| 460 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True); |
| 461 | } |
| 462 | else if (imag_negzero) { |
| 463 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False); |
| 464 | } |
| 465 | else if (real_negzero) { |
| 466 | key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); |
| 467 | } |
| 468 | else { |
| 469 | key = PyTuple_Pack(2, Py_TYPE(op), op); |
| 470 | } |
| 471 | } |
| 472 | #endif |
| 473 | else if (PyTuple_CheckExact(op)) { |
| 474 | Py_ssize_t i, len; |
| 475 | PyObject *tuple; |
| 476 | |
| 477 | len = PyTuple_GET_SIZE(op); |
| 478 | tuple = PyTuple_New(len); |
| 479 | if (tuple == NULL) |
| 480 | return NULL; |
| 481 | |
| 482 | for (i=0; i < len; i++) { |
| 483 | PyObject *item, *item_key; |
| 484 | |
| 485 | item = PyTuple_GET_ITEM(op, i); |
| 486 | item_key = _PyCode_ConstantKey(item); |
| 487 | if (item_key == NULL) { |
| 488 | Py_DECREF(tuple); |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | PyTuple_SET_ITEM(tuple, i, item_key); |
| 493 | } |
| 494 | |
| 495 | key = PyTuple_Pack(3, Py_TYPE(op), op, tuple); |
| 496 | Py_DECREF(tuple); |
| 497 | } |
| 498 | else if (PyFrozenSet_CheckExact(op)) { |
| 499 | Py_ssize_t pos = 0; |
| 500 | PyObject *item; |
| 501 | long hash; |
| 502 | Py_ssize_t i, len; |
| 503 | PyObject *tuple, *set; |
| 504 | |
| 505 | len = PySet_GET_SIZE(op); |
| 506 | tuple = PyTuple_New(len); |
| 507 | if (tuple == NULL) |
| 508 | return NULL; |
| 509 | |
| 510 | i = 0; |
| 511 | while (_PySet_NextEntry(op, &pos, &item, &hash)) { |
| 512 | PyObject *item_key; |
| 513 | |
| 514 | item_key = _PyCode_ConstantKey(item); |
| 515 | if (item_key == NULL) { |
| 516 | Py_DECREF(tuple); |
| 517 | return NULL; |
| 518 | } |
| 519 | |
| 520 | assert(i < len); |
| 521 | PyTuple_SET_ITEM(tuple, i, item_key); |
| 522 | i++; |
| 523 | } |
| 524 | set = PyFrozenSet_New(tuple); |
| 525 | Py_DECREF(tuple); |
| 526 | if (set == NULL) |
| 527 | return NULL; |
| 528 | |
| 529 | key = PyTuple_Pack(3, Py_TYPE(op), op, set); |
| 530 | Py_DECREF(set); |
| 531 | return key; |
| 532 | } |
| 533 | else { |
Martin Panter | 6a8163a | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 534 | /* for other types, use the object identifier as a unique identifier |
Victor Stinner | 7791165 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 535 | * to ensure that they are seen as unequal. */ |
| 536 | PyObject *obj_id = PyLong_FromVoidPtr(op); |
| 537 | if (obj_id == NULL) |
| 538 | return NULL; |
| 539 | |
| 540 | key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id); |
| 541 | Py_DECREF(obj_id); |
| 542 | } |
| 543 | return key; |
| 544 | } |
| 545 | |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 546 | static PyObject * |
| 547 | code_richcompare(PyObject *self, PyObject *other, int op) |
| 548 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 549 | PyCodeObject *co, *cp; |
| 550 | int eq; |
Victor Stinner | 7791165 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 551 | PyObject *consts1, *consts2; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 552 | PyObject *res; |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 553 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 554 | if ((op != Py_EQ && op != Py_NE) || |
| 555 | !PyCode_Check(self) || |
| 556 | !PyCode_Check(other)) { |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 558 | /* Py3K warning if types are not equal and comparison |
| 559 | isn't == or != */ |
| 560 | if (PyErr_WarnPy3k("code inequality comparisons not supported " |
| 561 | "in 3.x", 1) < 0) { |
| 562 | return NULL; |
| 563 | } |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 565 | Py_INCREF(Py_NotImplemented); |
| 566 | return Py_NotImplemented; |
| 567 | } |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 568 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 569 | co = (PyCodeObject *)self; |
| 570 | cp = (PyCodeObject *)other; |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 571 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 572 | eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ); |
| 573 | if (eq <= 0) goto unequal; |
| 574 | eq = co->co_argcount == cp->co_argcount; |
| 575 | if (!eq) goto unequal; |
| 576 | eq = co->co_nlocals == cp->co_nlocals; |
| 577 | if (!eq) goto unequal; |
| 578 | eq = co->co_flags == cp->co_flags; |
| 579 | if (!eq) goto unequal; |
| 580 | eq = co->co_firstlineno == cp->co_firstlineno; |
| 581 | if (!eq) goto unequal; |
| 582 | eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ); |
| 583 | if (eq <= 0) goto unequal; |
Victor Stinner | 7791165 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 584 | |
| 585 | /* compare constants */ |
| 586 | consts1 = _PyCode_ConstantKey(co->co_consts); |
| 587 | if (!consts1) |
| 588 | return NULL; |
| 589 | consts2 = _PyCode_ConstantKey(cp->co_consts); |
| 590 | if (!consts2) { |
| 591 | Py_DECREF(consts1); |
| 592 | return NULL; |
| 593 | } |
| 594 | eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ); |
| 595 | Py_DECREF(consts1); |
| 596 | Py_DECREF(consts2); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 597 | if (eq <= 0) goto unequal; |
Victor Stinner | 7791165 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 598 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 599 | eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ); |
| 600 | if (eq <= 0) goto unequal; |
| 601 | eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ); |
| 602 | if (eq <= 0) goto unequal; |
| 603 | eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ); |
| 604 | if (eq <= 0) goto unequal; |
| 605 | eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ); |
| 606 | if (eq <= 0) goto unequal; |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 608 | if (op == Py_EQ) |
| 609 | res = Py_True; |
| 610 | else |
| 611 | res = Py_False; |
| 612 | goto done; |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 613 | |
| 614 | unequal: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 615 | if (eq < 0) |
| 616 | return NULL; |
| 617 | if (op == Py_NE) |
| 618 | res = Py_True; |
| 619 | else |
| 620 | res = Py_False; |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 621 | |
| 622 | done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 623 | Py_INCREF(res); |
| 624 | return res; |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 627 | static long |
| 628 | code_hash(PyCodeObject *co) |
| 629 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 630 | long h, h0, h1, h2, h3, h4, h5, h6; |
| 631 | h0 = PyObject_Hash(co->co_name); |
| 632 | if (h0 == -1) return -1; |
| 633 | h1 = PyObject_Hash(co->co_code); |
| 634 | if (h1 == -1) return -1; |
| 635 | h2 = PyObject_Hash(co->co_consts); |
| 636 | if (h2 == -1) return -1; |
| 637 | h3 = PyObject_Hash(co->co_names); |
| 638 | if (h3 == -1) return -1; |
| 639 | h4 = PyObject_Hash(co->co_varnames); |
| 640 | if (h4 == -1) return -1; |
| 641 | h5 = PyObject_Hash(co->co_freevars); |
| 642 | if (h5 == -1) return -1; |
| 643 | h6 = PyObject_Hash(co->co_cellvars); |
| 644 | if (h6 == -1) return -1; |
| 645 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
| 646 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
| 647 | if (h == -1) h = -2; |
| 648 | return h; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* XXX code objects need to participate in GC? */ |
| 652 | |
| 653 | PyTypeObject PyCode_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 654 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 655 | "code", |
| 656 | sizeof(PyCodeObject), |
| 657 | 0, |
| 658 | (destructor)code_dealloc, /* tp_dealloc */ |
| 659 | 0, /* tp_print */ |
| 660 | 0, /* tp_getattr */ |
| 661 | 0, /* tp_setattr */ |
| 662 | (cmpfunc)code_compare, /* tp_compare */ |
| 663 | (reprfunc)code_repr, /* tp_repr */ |
| 664 | 0, /* tp_as_number */ |
| 665 | 0, /* tp_as_sequence */ |
| 666 | 0, /* tp_as_mapping */ |
| 667 | (hashfunc)code_hash, /* tp_hash */ |
| 668 | 0, /* tp_call */ |
| 669 | 0, /* tp_str */ |
| 670 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 671 | 0, /* tp_setattro */ |
| 672 | 0, /* tp_as_buffer */ |
| 673 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 674 | code_doc, /* tp_doc */ |
| 675 | 0, /* tp_traverse */ |
| 676 | 0, /* tp_clear */ |
| 677 | code_richcompare, /* tp_richcompare */ |
| 678 | offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */ |
| 679 | 0, /* tp_iter */ |
| 680 | 0, /* tp_iternext */ |
| 681 | 0, /* tp_methods */ |
| 682 | code_memberlist, /* tp_members */ |
| 683 | 0, /* tp_getset */ |
| 684 | 0, /* tp_base */ |
| 685 | 0, /* tp_dict */ |
| 686 | 0, /* tp_descr_get */ |
| 687 | 0, /* tp_descr_set */ |
| 688 | 0, /* tp_dictoffset */ |
| 689 | 0, /* tp_init */ |
| 690 | 0, /* tp_alloc */ |
| 691 | code_new, /* tp_new */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 692 | }; |
| 693 | |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 694 | /* Use co_lnotab to compute the line number from a bytecode index, addrq. See |
| 695 | lnotab_notes.txt for the details of the lnotab representation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 696 | */ |
| 697 | |
| 698 | int |
| 699 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
| 700 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 701 | int size = PyString_Size(co->co_lnotab) / 2; |
| 702 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
| 703 | int line = co->co_firstlineno; |
| 704 | int addr = 0; |
| 705 | while (--size >= 0) { |
| 706 | addr += *p++; |
| 707 | if (addr > addrq) |
| 708 | break; |
| 709 | line += *p++; |
| 710 | } |
| 711 | return line; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 712 | } |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 713 | |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 714 | /* Update *bounds to describe the first and one-past-the-last instructions in |
| 715 | the same line as lasti. Return the number of that line. */ |
| 716 | int |
| 717 | _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds) |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 718 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 719 | int size, addr, line; |
| 720 | unsigned char* p; |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 722 | p = (unsigned char*)PyString_AS_STRING(co->co_lnotab); |
| 723 | size = PyString_GET_SIZE(co->co_lnotab) / 2; |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 725 | addr = 0; |
| 726 | line = co->co_firstlineno; |
| 727 | assert(line > 0); |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 728 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 729 | /* possible optimization: if f->f_lasti == instr_ub |
| 730 | (likely to be a common case) then we already know |
| 731 | instr_lb -- if we stored the matching value of p |
Martin Panter | 6507657 | 2016-09-07 12:03:06 +0000 | [diff] [blame] | 732 | somewhere we could skip the first while loop. */ |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 733 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 734 | /* See lnotab_notes.txt for the description of |
| 735 | co_lnotab. A point to remember: increments to p |
| 736 | come in (addr, line) pairs. */ |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 737 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 738 | bounds->ap_lower = 0; |
| 739 | while (size > 0) { |
| 740 | if (addr + *p > lasti) |
| 741 | break; |
| 742 | addr += *p++; |
| 743 | if (*p) |
| 744 | bounds->ap_lower = addr; |
| 745 | line += *p++; |
| 746 | --size; |
| 747 | } |
| 748 | |
| 749 | if (size > 0) { |
| 750 | while (--size >= 0) { |
| 751 | addr += *p++; |
| 752 | if (*p++) |
| 753 | break; |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 754 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 755 | bounds->ap_upper = addr; |
| 756 | } |
| 757 | else { |
| 758 | bounds->ap_upper = INT_MAX; |
| 759 | } |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 761 | return line; |
Jeremy Hylton | a4ebc13 | 2006-04-18 14:47:00 +0000 | [diff] [blame] | 762 | } |