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