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)) { |
Yury Selivanov | 48aa605 | 2016-11-09 09:41:15 -0500 | [diff] [blame] | 68 | PyObject *w = v; |
Serhiy Storchaka | 67edf73 | 2016-09-30 10:38:08 +0300 | [diff] [blame] | 69 | PyObject *tmp = PySequence_Tuple(v); |
| 70 | if (tmp == NULL) { |
| 71 | PyErr_Clear(); |
| 72 | continue; |
| 73 | } |
| 74 | if (intern_string_constants(tmp)) { |
| 75 | v = PyFrozenSet_New(tmp); |
| 76 | if (v == NULL) { |
| 77 | PyErr_Clear(); |
| 78 | } |
| 79 | else { |
| 80 | PyTuple_SET_ITEM(tuple, i, v); |
Yury Selivanov | 48aa605 | 2016-11-09 09:41:15 -0500 | [diff] [blame] | 81 | Py_DECREF(w); |
Serhiy Storchaka | 67edf73 | 2016-09-30 10:38:08 +0300 | [diff] [blame] | 82 | modified = 1; |
| 83 | } |
| 84 | } |
| 85 | Py_DECREF(tmp); |
| 86 | } |
| 87 | } |
| 88 | return modified; |
| 89 | } |
| 90 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 91 | |
| 92 | PyCodeObject * |
| 93 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 94 | PyObject *code, PyObject *consts, PyObject *names, |
| 95 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 96 | PyObject *filename, PyObject *name, int firstlineno, |
| 97 | PyObject *lnotab) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 98 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 99 | PyCodeObject *co; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 100 | /* Check argument types */ |
| 101 | if (argcount < 0 || nlocals < 0 || |
| 102 | code == NULL || |
| 103 | consts == NULL || !PyTuple_Check(consts) || |
| 104 | names == NULL || !PyTuple_Check(names) || |
| 105 | varnames == NULL || !PyTuple_Check(varnames) || |
| 106 | freevars == NULL || !PyTuple_Check(freevars) || |
| 107 | cellvars == NULL || !PyTuple_Check(cellvars) || |
| 108 | name == NULL || !PyString_Check(name) || |
| 109 | filename == NULL || !PyString_Check(filename) || |
| 110 | lnotab == NULL || !PyString_Check(lnotab) || |
| 111 | !PyObject_CheckReadBuffer(code)) { |
| 112 | PyErr_BadInternalCall(); |
| 113 | return NULL; |
| 114 | } |
| 115 | intern_strings(names); |
| 116 | intern_strings(varnames); |
| 117 | intern_strings(freevars); |
| 118 | intern_strings(cellvars); |
Serhiy Storchaka | 67edf73 | 2016-09-30 10:38:08 +0300 | [diff] [blame] | 119 | intern_string_constants(consts); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 120 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
| 121 | if (co != NULL) { |
| 122 | co->co_argcount = argcount; |
| 123 | co->co_nlocals = nlocals; |
| 124 | co->co_stacksize = stacksize; |
| 125 | co->co_flags = flags; |
| 126 | Py_INCREF(code); |
| 127 | co->co_code = code; |
| 128 | Py_INCREF(consts); |
| 129 | co->co_consts = consts; |
| 130 | Py_INCREF(names); |
| 131 | co->co_names = names; |
| 132 | Py_INCREF(varnames); |
| 133 | co->co_varnames = varnames; |
| 134 | Py_INCREF(freevars); |
| 135 | co->co_freevars = freevars; |
| 136 | Py_INCREF(cellvars); |
| 137 | co->co_cellvars = cellvars; |
| 138 | Py_INCREF(filename); |
| 139 | co->co_filename = filename; |
| 140 | Py_INCREF(name); |
| 141 | co->co_name = name; |
| 142 | co->co_firstlineno = firstlineno; |
| 143 | Py_INCREF(lnotab); |
| 144 | co->co_lnotab = lnotab; |
| 145 | co->co_zombieframe = NULL; |
| 146 | co->co_weakreflist = NULL; |
| 147 | } |
| 148 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 151 | PyCodeObject * |
| 152 | PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) |
| 153 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 154 | static PyObject *emptystring = NULL; |
| 155 | static PyObject *nulltuple = NULL; |
| 156 | PyObject *filename_ob = NULL; |
| 157 | PyObject *funcname_ob = NULL; |
| 158 | PyCodeObject *result = NULL; |
| 159 | if (emptystring == NULL) { |
| 160 | emptystring = PyString_FromString(""); |
| 161 | if (emptystring == NULL) |
| 162 | goto failed; |
| 163 | } |
| 164 | if (nulltuple == NULL) { |
| 165 | nulltuple = PyTuple_New(0); |
| 166 | if (nulltuple == NULL) |
| 167 | goto failed; |
| 168 | } |
| 169 | funcname_ob = PyString_FromString(funcname); |
| 170 | if (funcname_ob == NULL) |
| 171 | goto failed; |
| 172 | filename_ob = PyString_FromString(filename); |
| 173 | if (filename_ob == NULL) |
| 174 | goto failed; |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 176 | result = PyCode_New(0, /* argcount */ |
| 177 | 0, /* nlocals */ |
| 178 | 0, /* stacksize */ |
| 179 | 0, /* flags */ |
| 180 | emptystring, /* code */ |
| 181 | nulltuple, /* consts */ |
| 182 | nulltuple, /* names */ |
| 183 | nulltuple, /* varnames */ |
| 184 | nulltuple, /* freevars */ |
| 185 | nulltuple, /* cellvars */ |
| 186 | filename_ob, /* filename */ |
| 187 | funcname_ob, /* name */ |
| 188 | firstlineno, /* firstlineno */ |
| 189 | emptystring /* lnotab */ |
| 190 | ); |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 191 | |
| 192 | failed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 193 | Py_XDECREF(funcname_ob); |
| 194 | Py_XDECREF(filename_ob); |
| 195 | return result; |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 196 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 197 | |
| 198 | #define OFF(x) offsetof(PyCodeObject, x) |
| 199 | |
| 200 | static PyMemberDef code_memberlist[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 201 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 202 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 203 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
| 204 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
| 205 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 206 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 207 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
| 208 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
| 209 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 210 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
| 211 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
| 212 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
| 213 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 214 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
| 215 | {NULL} /* Sentinel */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | /* Helper for code_new: return a shallow copy of a tuple that is |
| 219 | guaranteed to contain exact strings, by converting string subclasses |
| 220 | to exact strings and complaining if a non-string is found. */ |
| 221 | static PyObject* |
| 222 | validate_and_copy_tuple(PyObject *tup) |
| 223 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 224 | PyObject *newtuple; |
| 225 | PyObject *item; |
| 226 | Py_ssize_t i, len; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 228 | len = PyTuple_GET_SIZE(tup); |
| 229 | newtuple = PyTuple_New(len); |
| 230 | if (newtuple == NULL) |
| 231 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 233 | for (i = 0; i < len; i++) { |
| 234 | item = PyTuple_GET_ITEM(tup, i); |
| 235 | if (PyString_CheckExact(item)) { |
| 236 | Py_INCREF(item); |
| 237 | } |
| 238 | else if (!PyString_Check(item)) { |
| 239 | PyErr_Format( |
| 240 | PyExc_TypeError, |
| 241 | "name tuples must contain only " |
| 242 | "strings, not '%.500s'", |
| 243 | item->ob_type->tp_name); |
| 244 | Py_DECREF(newtuple); |
| 245 | return NULL; |
| 246 | } |
| 247 | else { |
| 248 | item = PyString_FromStringAndSize( |
| 249 | PyString_AS_STRING(item), |
| 250 | PyString_GET_SIZE(item)); |
| 251 | if (item == NULL) { |
| 252 | Py_DECREF(newtuple); |
| 253 | return NULL; |
| 254 | } |
| 255 | } |
| 256 | PyTuple_SET_ITEM(newtuple, i, item); |
| 257 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 259 | return newtuple; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | PyDoc_STRVAR(code_doc, |
| 263 | "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\ |
| 264 | varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\ |
| 265 | \n\ |
| 266 | Create a code object. Not for the faint of heart."); |
| 267 | |
| 268 | static PyObject * |
| 269 | code_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 270 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 271 | int argcount; |
| 272 | int nlocals; |
| 273 | int stacksize; |
| 274 | int flags; |
| 275 | PyObject *co = NULL; |
| 276 | PyObject *code; |
| 277 | PyObject *consts; |
| 278 | PyObject *names, *ournames = NULL; |
| 279 | PyObject *varnames, *ourvarnames = NULL; |
| 280 | PyObject *freevars = NULL, *ourfreevars = NULL; |
| 281 | PyObject *cellvars = NULL, *ourcellvars = NULL; |
| 282 | PyObject *filename; |
| 283 | PyObject *name; |
| 284 | int firstlineno; |
| 285 | PyObject *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 287 | if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code", |
| 288 | &argcount, &nlocals, &stacksize, &flags, |
| 289 | &code, |
| 290 | &PyTuple_Type, &consts, |
| 291 | &PyTuple_Type, &names, |
| 292 | &PyTuple_Type, &varnames, |
| 293 | &filename, &name, |
| 294 | &firstlineno, &lnotab, |
| 295 | &PyTuple_Type, &freevars, |
| 296 | &PyTuple_Type, &cellvars)) |
| 297 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 298 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 299 | if (argcount < 0) { |
| 300 | PyErr_SetString( |
| 301 | PyExc_ValueError, |
| 302 | "code: argcount must not be negative"); |
| 303 | goto cleanup; |
| 304 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 306 | if (nlocals < 0) { |
| 307 | PyErr_SetString( |
| 308 | PyExc_ValueError, |
| 309 | "code: nlocals must not be negative"); |
| 310 | goto cleanup; |
| 311 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 313 | ournames = validate_and_copy_tuple(names); |
| 314 | if (ournames == NULL) |
| 315 | goto cleanup; |
| 316 | ourvarnames = validate_and_copy_tuple(varnames); |
| 317 | if (ourvarnames == NULL) |
| 318 | goto cleanup; |
| 319 | if (freevars) |
| 320 | ourfreevars = validate_and_copy_tuple(freevars); |
| 321 | else |
| 322 | ourfreevars = PyTuple_New(0); |
| 323 | if (ourfreevars == NULL) |
| 324 | goto cleanup; |
| 325 | if (cellvars) |
| 326 | ourcellvars = validate_and_copy_tuple(cellvars); |
| 327 | else |
| 328 | ourcellvars = PyTuple_New(0); |
| 329 | if (ourcellvars == NULL) |
| 330 | goto cleanup; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 332 | co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
| 333 | code, consts, ournames, ourvarnames, |
| 334 | ourfreevars, ourcellvars, filename, |
| 335 | name, firstlineno, lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 336 | cleanup: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 337 | Py_XDECREF(ournames); |
| 338 | Py_XDECREF(ourvarnames); |
| 339 | Py_XDECREF(ourfreevars); |
| 340 | Py_XDECREF(ourcellvars); |
| 341 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static void |
| 345 | code_dealloc(PyCodeObject *co) |
| 346 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 347 | Py_XDECREF(co->co_code); |
| 348 | Py_XDECREF(co->co_consts); |
| 349 | Py_XDECREF(co->co_names); |
| 350 | Py_XDECREF(co->co_varnames); |
| 351 | Py_XDECREF(co->co_freevars); |
| 352 | Py_XDECREF(co->co_cellvars); |
| 353 | Py_XDECREF(co->co_filename); |
| 354 | Py_XDECREF(co->co_name); |
| 355 | Py_XDECREF(co->co_lnotab); |
| 356 | if (co->co_zombieframe != NULL) |
| 357 | PyObject_GC_Del(co->co_zombieframe); |
| 358 | if (co->co_weakreflist != NULL) |
| 359 | PyObject_ClearWeakRefs((PyObject*)co); |
| 360 | PyObject_DEL(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static PyObject * |
| 364 | code_repr(PyCodeObject *co) |
| 365 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 366 | char buf[500]; |
| 367 | int lineno = -1; |
| 368 | char *filename = "???"; |
| 369 | char *name = "???"; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 370 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 371 | if (co->co_firstlineno != 0) |
| 372 | lineno = co->co_firstlineno; |
| 373 | if (co->co_filename && PyString_Check(co->co_filename)) |
| 374 | filename = PyString_AS_STRING(co->co_filename); |
| 375 | if (co->co_name && PyString_Check(co->co_name)) |
| 376 | name = PyString_AS_STRING(co->co_name); |
| 377 | PyOS_snprintf(buf, sizeof(buf), |
| 378 | "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 379 | name, co, filename, lineno); |
| 380 | return PyString_FromString(buf); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static int |
| 384 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
| 385 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 386 | int cmp; |
| 387 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 388 | if (cmp) return cmp; |
| 389 | cmp = co->co_argcount - cp->co_argcount; |
| 390 | if (cmp) goto normalize; |
| 391 | cmp = co->co_nlocals - cp->co_nlocals; |
| 392 | if (cmp) goto normalize; |
| 393 | cmp = co->co_flags - cp->co_flags; |
| 394 | if (cmp) goto normalize; |
| 395 | cmp = co->co_firstlineno - cp->co_firstlineno; |
| 396 | if (cmp) goto normalize; |
| 397 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
| 398 | if (cmp) return cmp; |
| 399 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
| 400 | if (cmp) return cmp; |
| 401 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
| 402 | if (cmp) return cmp; |
| 403 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
| 404 | if (cmp) return cmp; |
| 405 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 406 | if (cmp) return cmp; |
| 407 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
| 408 | return cmp; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 409 | |
| 410 | normalize: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 411 | if (cmp > 0) |
| 412 | return 1; |
| 413 | else if (cmp < 0) |
| 414 | return -1; |
| 415 | else |
| 416 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Victor Stinner | 7791165 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 419 | PyObject* |
| 420 | _PyCode_ConstantKey(PyObject *op) |
| 421 | { |
| 422 | PyObject *key; |
| 423 | |
| 424 | /* Py_None is a singleton */ |
| 425 | if (op == Py_None |
Serhiy Storchaka | 48c8bf2 | 2018-07-31 09:09:36 +0300 | [diff] [blame^] | 426 | || _PyAnyInt_CheckExact(op) |
| 427 | || PyBool_Check(op) |
| 428 | || PyBytes_CheckExact(op) |
Victor Stinner | 7791165 | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 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 | } |