blob: 827fafaf227c7c1370a75adeb204ccfe56eba5f6 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001#include "Python.h"
2#include "code.h"
3#include "structmember.h"
4
5#define NAME_CHARS \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007
8/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
9
10static int
11all_name_chars(unsigned char *s)
12{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000013 static char ok_name_char[256];
14 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Antoine Pitrouc83ea132010-05-09 14:46:46 +000016 if (ok_name_char[*name_chars] == 0) {
17 unsigned char *p;
18 for (p = name_chars; *p; p++)
19 ok_name_char[*p] = 1;
20 }
21 while (*s) {
22 if (ok_name_char[*s++] == 0)
23 return 0;
24 }
25 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026}
27
28static void
29intern_strings(PyObject *tuple)
30{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000031 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032
Antoine Pitrouc83ea132010-05-09 14:46:46 +000033 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
34 PyObject *v = PyTuple_GET_ITEM(tuple, i);
35 if (v == NULL || !PyString_CheckExact(v)) {
36 Py_FatalError("non-string found in code slot");
37 }
38 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
39 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040}
41
Serhiy Storchaka67edf732016-09-30 10:38:08 +030042/* Intern selected string constants */
43static int
44intern_string_constants(PyObject *tuple)
45{
46 int modified = 0;
47 Py_ssize_t i;
48
49 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
50 PyObject *v = PyTuple_GET_ITEM(tuple, i);
51 if (PyString_CheckExact(v)) {
52 if (all_name_chars((unsigned char *)PyString_AS_STRING(v))) {
53 PyObject *w = v;
54 PyString_InternInPlace(&v);
55 if (w != v) {
56 PyTuple_SET_ITEM(tuple, i, v);
57 modified = 1;
58 }
59 }
60 }
61 else if (PyTuple_CheckExact(v)) {
62 intern_string_constants(v);
63 }
64 else if (PyFrozenSet_CheckExact(v)) {
65 PyObject *tmp = PySequence_Tuple(v);
66 if (tmp == NULL) {
67 PyErr_Clear();
68 continue;
69 }
70 if (intern_string_constants(tmp)) {
71 v = PyFrozenSet_New(tmp);
72 if (v == NULL) {
73 PyErr_Clear();
74 }
75 else {
76 PyTuple_SET_ITEM(tuple, i, v);
77 modified = 1;
78 }
79 }
80 Py_DECREF(tmp);
81 }
82 }
83 return modified;
84}
85
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086
87PyCodeObject *
88PyCode_New(int argcount, int nlocals, int stacksize, int flags,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089 PyObject *code, PyObject *consts, PyObject *names,
90 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
91 PyObject *filename, PyObject *name, int firstlineno,
92 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000093{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000094 PyCodeObject *co;
95 Py_ssize_t i;
96 /* Check argument types */
97 if (argcount < 0 || nlocals < 0 ||
98 code == NULL ||
99 consts == NULL || !PyTuple_Check(consts) ||
100 names == NULL || !PyTuple_Check(names) ||
101 varnames == NULL || !PyTuple_Check(varnames) ||
102 freevars == NULL || !PyTuple_Check(freevars) ||
103 cellvars == NULL || !PyTuple_Check(cellvars) ||
104 name == NULL || !PyString_Check(name) ||
105 filename == NULL || !PyString_Check(filename) ||
106 lnotab == NULL || !PyString_Check(lnotab) ||
107 !PyObject_CheckReadBuffer(code)) {
108 PyErr_BadInternalCall();
109 return NULL;
110 }
111 intern_strings(names);
112 intern_strings(varnames);
113 intern_strings(freevars);
114 intern_strings(cellvars);
Serhiy Storchaka67edf732016-09-30 10:38:08 +0300115 intern_string_constants(consts);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000116 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
117 if (co != NULL) {
118 co->co_argcount = argcount;
119 co->co_nlocals = nlocals;
120 co->co_stacksize = stacksize;
121 co->co_flags = flags;
122 Py_INCREF(code);
123 co->co_code = code;
124 Py_INCREF(consts);
125 co->co_consts = consts;
126 Py_INCREF(names);
127 co->co_names = names;
128 Py_INCREF(varnames);
129 co->co_varnames = varnames;
130 Py_INCREF(freevars);
131 co->co_freevars = freevars;
132 Py_INCREF(cellvars);
133 co->co_cellvars = cellvars;
134 Py_INCREF(filename);
135 co->co_filename = filename;
136 Py_INCREF(name);
137 co->co_name = name;
138 co->co_firstlineno = firstlineno;
139 Py_INCREF(lnotab);
140 co->co_lnotab = lnotab;
141 co->co_zombieframe = NULL;
142 co->co_weakreflist = NULL;
143 }
144 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145}
146
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000147PyCodeObject *
148PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
149{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000150 static PyObject *emptystring = NULL;
151 static PyObject *nulltuple = NULL;
152 PyObject *filename_ob = NULL;
153 PyObject *funcname_ob = NULL;
154 PyCodeObject *result = NULL;
155 if (emptystring == NULL) {
156 emptystring = PyString_FromString("");
157 if (emptystring == NULL)
158 goto failed;
159 }
160 if (nulltuple == NULL) {
161 nulltuple = PyTuple_New(0);
162 if (nulltuple == NULL)
163 goto failed;
164 }
165 funcname_ob = PyString_FromString(funcname);
166 if (funcname_ob == NULL)
167 goto failed;
168 filename_ob = PyString_FromString(filename);
169 if (filename_ob == NULL)
170 goto failed;
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000171
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 result = PyCode_New(0, /* argcount */
173 0, /* nlocals */
174 0, /* stacksize */
175 0, /* flags */
176 emptystring, /* code */
177 nulltuple, /* consts */
178 nulltuple, /* names */
179 nulltuple, /* varnames */
180 nulltuple, /* freevars */
181 nulltuple, /* cellvars */
182 filename_ob, /* filename */
183 funcname_ob, /* name */
184 firstlineno, /* firstlineno */
185 emptystring /* lnotab */
186 );
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000187
188failed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 Py_XDECREF(funcname_ob);
190 Py_XDECREF(filename_ob);
191 return result;
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000192}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194#define OFF(x) offsetof(PyCodeObject, x)
195
196static PyMemberDef code_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
198 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
199 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
200 {"co_flags", T_INT, OFF(co_flags), READONLY},
201 {"co_code", T_OBJECT, OFF(co_code), READONLY},
202 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
203 {"co_names", T_OBJECT, OFF(co_names), READONLY},
204 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
205 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
206 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
207 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
208 {"co_name", T_OBJECT, OFF(co_name), READONLY},
209 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
210 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
211 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212};
213
214/* Helper for code_new: return a shallow copy of a tuple that is
215 guaranteed to contain exact strings, by converting string subclasses
216 to exact strings and complaining if a non-string is found. */
217static PyObject*
218validate_and_copy_tuple(PyObject *tup)
219{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 PyObject *newtuple;
221 PyObject *item;
222 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 len = PyTuple_GET_SIZE(tup);
225 newtuple = PyTuple_New(len);
226 if (newtuple == NULL)
227 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000229 for (i = 0; i < len; i++) {
230 item = PyTuple_GET_ITEM(tup, i);
231 if (PyString_CheckExact(item)) {
232 Py_INCREF(item);
233 }
234 else if (!PyString_Check(item)) {
235 PyErr_Format(
236 PyExc_TypeError,
237 "name tuples must contain only "
238 "strings, not '%.500s'",
239 item->ob_type->tp_name);
240 Py_DECREF(newtuple);
241 return NULL;
242 }
243 else {
244 item = PyString_FromStringAndSize(
245 PyString_AS_STRING(item),
246 PyString_GET_SIZE(item));
247 if (item == NULL) {
248 Py_DECREF(newtuple);
249 return NULL;
250 }
251 }
252 PyTuple_SET_ITEM(newtuple, i, item);
253 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000255 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256}
257
258PyDoc_STRVAR(code_doc,
259"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
260 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
261\n\
262Create a code object. Not for the faint of heart.");
263
264static PyObject *
265code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
266{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 int argcount;
268 int nlocals;
269 int stacksize;
270 int flags;
271 PyObject *co = NULL;
272 PyObject *code;
273 PyObject *consts;
274 PyObject *names, *ournames = NULL;
275 PyObject *varnames, *ourvarnames = NULL;
276 PyObject *freevars = NULL, *ourfreevars = NULL;
277 PyObject *cellvars = NULL, *ourcellvars = NULL;
278 PyObject *filename;
279 PyObject *name;
280 int firstlineno;
281 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000283 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
284 &argcount, &nlocals, &stacksize, &flags,
285 &code,
286 &PyTuple_Type, &consts,
287 &PyTuple_Type, &names,
288 &PyTuple_Type, &varnames,
289 &filename, &name,
290 &firstlineno, &lnotab,
291 &PyTuple_Type, &freevars,
292 &PyTuple_Type, &cellvars))
293 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000295 if (argcount < 0) {
296 PyErr_SetString(
297 PyExc_ValueError,
298 "code: argcount must not be negative");
299 goto cleanup;
300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000302 if (nlocals < 0) {
303 PyErr_SetString(
304 PyExc_ValueError,
305 "code: nlocals must not be negative");
306 goto cleanup;
307 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 ournames = validate_and_copy_tuple(names);
310 if (ournames == NULL)
311 goto cleanup;
312 ourvarnames = validate_and_copy_tuple(varnames);
313 if (ourvarnames == NULL)
314 goto cleanup;
315 if (freevars)
316 ourfreevars = validate_and_copy_tuple(freevars);
317 else
318 ourfreevars = PyTuple_New(0);
319 if (ourfreevars == NULL)
320 goto cleanup;
321 if (cellvars)
322 ourcellvars = validate_and_copy_tuple(cellvars);
323 else
324 ourcellvars = PyTuple_New(0);
325 if (ourcellvars == NULL)
326 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000328 co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
329 code, consts, ournames, ourvarnames,
330 ourfreevars, ourcellvars, filename,
331 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332 cleanup:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000333 Py_XDECREF(ournames);
334 Py_XDECREF(ourvarnames);
335 Py_XDECREF(ourfreevars);
336 Py_XDECREF(ourcellvars);
337 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338}
339
340static void
341code_dealloc(PyCodeObject *co)
342{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000343 Py_XDECREF(co->co_code);
344 Py_XDECREF(co->co_consts);
345 Py_XDECREF(co->co_names);
346 Py_XDECREF(co->co_varnames);
347 Py_XDECREF(co->co_freevars);
348 Py_XDECREF(co->co_cellvars);
349 Py_XDECREF(co->co_filename);
350 Py_XDECREF(co->co_name);
351 Py_XDECREF(co->co_lnotab);
352 if (co->co_zombieframe != NULL)
353 PyObject_GC_Del(co->co_zombieframe);
354 if (co->co_weakreflist != NULL)
355 PyObject_ClearWeakRefs((PyObject*)co);
356 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357}
358
359static PyObject *
360code_repr(PyCodeObject *co)
361{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000362 char buf[500];
363 int lineno = -1;
364 char *filename = "???";
365 char *name = "???";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000367 if (co->co_firstlineno != 0)
368 lineno = co->co_firstlineno;
369 if (co->co_filename && PyString_Check(co->co_filename))
370 filename = PyString_AS_STRING(co->co_filename);
371 if (co->co_name && PyString_Check(co->co_name))
372 name = PyString_AS_STRING(co->co_name);
373 PyOS_snprintf(buf, sizeof(buf),
374 "<code object %.100s at %p, file \"%.300s\", line %d>",
375 name, co, filename, lineno);
376 return PyString_FromString(buf);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377}
378
379static int
380code_compare(PyCodeObject *co, PyCodeObject *cp)
381{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000382 int cmp;
383 cmp = PyObject_Compare(co->co_name, cp->co_name);
384 if (cmp) return cmp;
385 cmp = co->co_argcount - cp->co_argcount;
386 if (cmp) goto normalize;
387 cmp = co->co_nlocals - cp->co_nlocals;
388 if (cmp) goto normalize;
389 cmp = co->co_flags - cp->co_flags;
390 if (cmp) goto normalize;
391 cmp = co->co_firstlineno - cp->co_firstlineno;
392 if (cmp) goto normalize;
393 cmp = PyObject_Compare(co->co_code, cp->co_code);
394 if (cmp) return cmp;
395 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
396 if (cmp) return cmp;
397 cmp = PyObject_Compare(co->co_names, cp->co_names);
398 if (cmp) return cmp;
399 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
400 if (cmp) return cmp;
401 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
402 if (cmp) return cmp;
403 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
404 return cmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405
406 normalize:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000407 if (cmp > 0)
408 return 1;
409 else if (cmp < 0)
410 return -1;
411 else
412 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000413}
414
Victor Stinner77911652016-01-22 12:33:12 +0100415PyObject*
416_PyCode_ConstantKey(PyObject *op)
417{
418 PyObject *key;
419
420 /* Py_None is a singleton */
421 if (op == Py_None
422 || PyInt_CheckExact(op)
423 || PyLong_CheckExact(op)
424 || PyBool_Check(op)
425 || PyBytes_CheckExact(op)
426#ifdef Py_USING_UNICODE
427 || PyUnicode_CheckExact(op)
428#endif
429 /* code_richcompare() uses _PyCode_ConstantKey() internally */
430 || PyCode_Check(op)) {
431 key = PyTuple_Pack(2, Py_TYPE(op), op);
432 }
433 else if (PyFloat_CheckExact(op)) {
434 double d = PyFloat_AS_DOUBLE(op);
435 /* all we need is to make the tuple different in either the 0.0
436 * or -0.0 case from all others, just to avoid the "coercion".
437 */
438 if (d == 0.0 && copysign(1.0, d) < 0.0)
439 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
440 else
441 key = PyTuple_Pack(2, Py_TYPE(op), op);
442 }
443#ifndef WITHOUT_COMPLEX
444 else if (PyComplex_CheckExact(op)) {
445 Py_complex z;
446 int real_negzero, imag_negzero;
447 /* For the complex case we must make complex(x, 0.)
448 different from complex(x, -0.) and complex(0., y)
449 different from complex(-0., y), for any x and y.
450 All four complex zeros must be distinguished.*/
451 z = PyComplex_AsCComplex(op);
452 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
453 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
454 /* use True, False and None singleton as tags for the real and imag
455 * sign, to make tuples different */
456 if (real_negzero && imag_negzero) {
457 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
458 }
459 else if (imag_negzero) {
460 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
461 }
462 else if (real_negzero) {
463 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
464 }
465 else {
466 key = PyTuple_Pack(2, Py_TYPE(op), op);
467 }
468 }
469#endif
470 else if (PyTuple_CheckExact(op)) {
471 Py_ssize_t i, len;
472 PyObject *tuple;
473
474 len = PyTuple_GET_SIZE(op);
475 tuple = PyTuple_New(len);
476 if (tuple == NULL)
477 return NULL;
478
479 for (i=0; i < len; i++) {
480 PyObject *item, *item_key;
481
482 item = PyTuple_GET_ITEM(op, i);
483 item_key = _PyCode_ConstantKey(item);
484 if (item_key == NULL) {
485 Py_DECREF(tuple);
486 return NULL;
487 }
488
489 PyTuple_SET_ITEM(tuple, i, item_key);
490 }
491
492 key = PyTuple_Pack(3, Py_TYPE(op), op, tuple);
493 Py_DECREF(tuple);
494 }
495 else if (PyFrozenSet_CheckExact(op)) {
496 Py_ssize_t pos = 0;
497 PyObject *item;
498 long hash;
499 Py_ssize_t i, len;
500 PyObject *tuple, *set;
501
502 len = PySet_GET_SIZE(op);
503 tuple = PyTuple_New(len);
504 if (tuple == NULL)
505 return NULL;
506
507 i = 0;
508 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
509 PyObject *item_key;
510
511 item_key = _PyCode_ConstantKey(item);
512 if (item_key == NULL) {
513 Py_DECREF(tuple);
514 return NULL;
515 }
516
517 assert(i < len);
518 PyTuple_SET_ITEM(tuple, i, item_key);
519 i++;
520 }
521 set = PyFrozenSet_New(tuple);
522 Py_DECREF(tuple);
523 if (set == NULL)
524 return NULL;
525
526 key = PyTuple_Pack(3, Py_TYPE(op), op, set);
527 Py_DECREF(set);
528 return key;
529 }
530 else {
Martin Panter6a8163a2016-04-15 02:14:19 +0000531 /* for other types, use the object identifier as a unique identifier
Victor Stinner77911652016-01-22 12:33:12 +0100532 * to ensure that they are seen as unequal. */
533 PyObject *obj_id = PyLong_FromVoidPtr(op);
534 if (obj_id == NULL)
535 return NULL;
536
537 key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id);
538 Py_DECREF(obj_id);
539 }
540 return key;
541}
542
Steven Bethard6a644f92008-03-18 22:08:20 +0000543static PyObject *
544code_richcompare(PyObject *self, PyObject *other, int op)
545{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000546 PyCodeObject *co, *cp;
547 int eq;
Victor Stinner77911652016-01-22 12:33:12 +0100548 PyObject *consts1, *consts2;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000549 PyObject *res;
Steven Bethard6a644f92008-03-18 22:08:20 +0000550
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000551 if ((op != Py_EQ && op != Py_NE) ||
552 !PyCode_Check(self) ||
553 !PyCode_Check(other)) {
Steven Bethard6a644f92008-03-18 22:08:20 +0000554
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000555 /* Py3K warning if types are not equal and comparison
556 isn't == or != */
557 if (PyErr_WarnPy3k("code inequality comparisons not supported "
558 "in 3.x", 1) < 0) {
559 return NULL;
560 }
Steven Bethard6a644f92008-03-18 22:08:20 +0000561
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000562 Py_INCREF(Py_NotImplemented);
563 return Py_NotImplemented;
564 }
Steven Bethard6a644f92008-03-18 22:08:20 +0000565
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000566 co = (PyCodeObject *)self;
567 cp = (PyCodeObject *)other;
Steven Bethard6a644f92008-03-18 22:08:20 +0000568
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000569 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
570 if (eq <= 0) goto unequal;
571 eq = co->co_argcount == cp->co_argcount;
572 if (!eq) goto unequal;
573 eq = co->co_nlocals == cp->co_nlocals;
574 if (!eq) goto unequal;
575 eq = co->co_flags == cp->co_flags;
576 if (!eq) goto unequal;
577 eq = co->co_firstlineno == cp->co_firstlineno;
578 if (!eq) goto unequal;
579 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
580 if (eq <= 0) goto unequal;
Victor Stinner77911652016-01-22 12:33:12 +0100581
582 /* compare constants */
583 consts1 = _PyCode_ConstantKey(co->co_consts);
584 if (!consts1)
585 return NULL;
586 consts2 = _PyCode_ConstantKey(cp->co_consts);
587 if (!consts2) {
588 Py_DECREF(consts1);
589 return NULL;
590 }
591 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
592 Py_DECREF(consts1);
593 Py_DECREF(consts2);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000594 if (eq <= 0) goto unequal;
Victor Stinner77911652016-01-22 12:33:12 +0100595
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000596 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
597 if (eq <= 0) goto unequal;
598 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
599 if (eq <= 0) goto unequal;
600 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
601 if (eq <= 0) goto unequal;
602 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
603 if (eq <= 0) goto unequal;
Steven Bethard6a644f92008-03-18 22:08:20 +0000604
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000605 if (op == Py_EQ)
606 res = Py_True;
607 else
608 res = Py_False;
609 goto done;
Steven Bethard6a644f92008-03-18 22:08:20 +0000610
611 unequal:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000612 if (eq < 0)
613 return NULL;
614 if (op == Py_NE)
615 res = Py_True;
616 else
617 res = Py_False;
Steven Bethard6a644f92008-03-18 22:08:20 +0000618
619 done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000620 Py_INCREF(res);
621 return res;
Steven Bethard6a644f92008-03-18 22:08:20 +0000622}
623
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624static long
625code_hash(PyCodeObject *co)
626{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000627 long h, h0, h1, h2, h3, h4, h5, h6;
628 h0 = PyObject_Hash(co->co_name);
629 if (h0 == -1) return -1;
630 h1 = PyObject_Hash(co->co_code);
631 if (h1 == -1) return -1;
632 h2 = PyObject_Hash(co->co_consts);
633 if (h2 == -1) return -1;
634 h3 = PyObject_Hash(co->co_names);
635 if (h3 == -1) return -1;
636 h4 = PyObject_Hash(co->co_varnames);
637 if (h4 == -1) return -1;
638 h5 = PyObject_Hash(co->co_freevars);
639 if (h5 == -1) return -1;
640 h6 = PyObject_Hash(co->co_cellvars);
641 if (h6 == -1) return -1;
642 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
643 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
644 if (h == -1) h = -2;
645 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646}
647
648/* XXX code objects need to participate in GC? */
649
650PyTypeObject PyCode_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000651 PyVarObject_HEAD_INIT(&PyType_Type, 0)
652 "code",
653 sizeof(PyCodeObject),
654 0,
655 (destructor)code_dealloc, /* tp_dealloc */
656 0, /* tp_print */
657 0, /* tp_getattr */
658 0, /* tp_setattr */
659 (cmpfunc)code_compare, /* tp_compare */
660 (reprfunc)code_repr, /* tp_repr */
661 0, /* tp_as_number */
662 0, /* tp_as_sequence */
663 0, /* tp_as_mapping */
664 (hashfunc)code_hash, /* tp_hash */
665 0, /* tp_call */
666 0, /* tp_str */
667 PyObject_GenericGetAttr, /* tp_getattro */
668 0, /* tp_setattro */
669 0, /* tp_as_buffer */
670 Py_TPFLAGS_DEFAULT, /* tp_flags */
671 code_doc, /* tp_doc */
672 0, /* tp_traverse */
673 0, /* tp_clear */
674 code_richcompare, /* tp_richcompare */
675 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
676 0, /* tp_iter */
677 0, /* tp_iternext */
678 0, /* tp_methods */
679 code_memberlist, /* tp_members */
680 0, /* tp_getset */
681 0, /* tp_base */
682 0, /* tp_dict */
683 0, /* tp_descr_get */
684 0, /* tp_descr_set */
685 0, /* tp_dictoffset */
686 0, /* tp_init */
687 0, /* tp_alloc */
688 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689};
690
Jeffrey Yasskin655d8352009-05-23 23:23:01 +0000691/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
692 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693*/
694
695int
696PyCode_Addr2Line(PyCodeObject *co, int addrq)
697{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000698 int size = PyString_Size(co->co_lnotab) / 2;
699 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
700 int line = co->co_firstlineno;
701 int addr = 0;
702 while (--size >= 0) {
703 addr += *p++;
704 if (addr > addrq)
705 break;
706 line += *p++;
707 }
708 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709}
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000710
Jeffrey Yasskin655d8352009-05-23 23:23:01 +0000711/* Update *bounds to describe the first and one-past-the-last instructions in
712 the same line as lasti. Return the number of that line. */
713int
714_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000715{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000716 int size, addr, line;
717 unsigned char* p;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000718
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
720 size = PyString_GET_SIZE(co->co_lnotab) / 2;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000721
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 addr = 0;
723 line = co->co_firstlineno;
724 assert(line > 0);
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000725
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000726 /* possible optimization: if f->f_lasti == instr_ub
727 (likely to be a common case) then we already know
728 instr_lb -- if we stored the matching value of p
Martin Panter65076572016-09-07 12:03:06 +0000729 somewhere we could skip the first while loop. */
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000730
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000731 /* See lnotab_notes.txt for the description of
732 co_lnotab. A point to remember: increments to p
733 come in (addr, line) pairs. */
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000734
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000735 bounds->ap_lower = 0;
736 while (size > 0) {
737 if (addr + *p > lasti)
738 break;
739 addr += *p++;
740 if (*p)
741 bounds->ap_lower = addr;
742 line += *p++;
743 --size;
744 }
745
746 if (size > 0) {
747 while (--size >= 0) {
748 addr += *p++;
749 if (*p++)
750 break;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000751 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000752 bounds->ap_upper = addr;
753 }
754 else {
755 bounds->ap_upper = INT_MAX;
756 }
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000757
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000758 return line;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000759}