blob: 7e74607bd1127d04da89c235e49d723bf3603dd7 [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
42
43PyCodeObject *
44PyCode_New(int argcount, int nlocals, int stacksize, int flags,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000045 PyObject *code, PyObject *consts, PyObject *names,
46 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
47 PyObject *filename, PyObject *name, int firstlineno,
48 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000049{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000050 PyCodeObject *co;
51 Py_ssize_t i;
52 /* Check argument types */
53 if (argcount < 0 || nlocals < 0 ||
54 code == NULL ||
55 consts == NULL || !PyTuple_Check(consts) ||
56 names == NULL || !PyTuple_Check(names) ||
57 varnames == NULL || !PyTuple_Check(varnames) ||
58 freevars == NULL || !PyTuple_Check(freevars) ||
59 cellvars == NULL || !PyTuple_Check(cellvars) ||
60 name == NULL || !PyString_Check(name) ||
61 filename == NULL || !PyString_Check(filename) ||
62 lnotab == NULL || !PyString_Check(lnotab) ||
63 !PyObject_CheckReadBuffer(code)) {
64 PyErr_BadInternalCall();
65 return NULL;
66 }
67 intern_strings(names);
68 intern_strings(varnames);
69 intern_strings(freevars);
70 intern_strings(cellvars);
71 /* Intern selected string constants */
72 for (i = PyTuple_Size(consts); --i >= 0; ) {
73 PyObject *v = PyTuple_GetItem(consts, i);
74 if (!PyString_Check(v))
75 continue;
76 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
77 continue;
78 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
79 }
80 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
81 if (co != NULL) {
82 co->co_argcount = argcount;
83 co->co_nlocals = nlocals;
84 co->co_stacksize = stacksize;
85 co->co_flags = flags;
86 Py_INCREF(code);
87 co->co_code = code;
88 Py_INCREF(consts);
89 co->co_consts = consts;
90 Py_INCREF(names);
91 co->co_names = names;
92 Py_INCREF(varnames);
93 co->co_varnames = varnames;
94 Py_INCREF(freevars);
95 co->co_freevars = freevars;
96 Py_INCREF(cellvars);
97 co->co_cellvars = cellvars;
98 Py_INCREF(filename);
99 co->co_filename = filename;
100 Py_INCREF(name);
101 co->co_name = name;
102 co->co_firstlineno = firstlineno;
103 Py_INCREF(lnotab);
104 co->co_lnotab = lnotab;
105 co->co_zombieframe = NULL;
106 co->co_weakreflist = NULL;
107 }
108 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109}
110
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000111PyCodeObject *
112PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
113{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000114 static PyObject *emptystring = NULL;
115 static PyObject *nulltuple = NULL;
116 PyObject *filename_ob = NULL;
117 PyObject *funcname_ob = NULL;
118 PyCodeObject *result = NULL;
119 if (emptystring == NULL) {
120 emptystring = PyString_FromString("");
121 if (emptystring == NULL)
122 goto failed;
123 }
124 if (nulltuple == NULL) {
125 nulltuple = PyTuple_New(0);
126 if (nulltuple == NULL)
127 goto failed;
128 }
129 funcname_ob = PyString_FromString(funcname);
130 if (funcname_ob == NULL)
131 goto failed;
132 filename_ob = PyString_FromString(filename);
133 if (filename_ob == NULL)
134 goto failed;
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000135
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000136 result = PyCode_New(0, /* argcount */
137 0, /* nlocals */
138 0, /* stacksize */
139 0, /* flags */
140 emptystring, /* code */
141 nulltuple, /* consts */
142 nulltuple, /* names */
143 nulltuple, /* varnames */
144 nulltuple, /* freevars */
145 nulltuple, /* cellvars */
146 filename_ob, /* filename */
147 funcname_ob, /* name */
148 firstlineno, /* firstlineno */
149 emptystring /* lnotab */
150 );
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000151
152failed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000153 Py_XDECREF(funcname_ob);
154 Py_XDECREF(filename_ob);
155 return result;
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000156}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157
158#define OFF(x) offsetof(PyCodeObject, x)
159
160static PyMemberDef code_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
162 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
163 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
164 {"co_flags", T_INT, OFF(co_flags), READONLY},
165 {"co_code", T_OBJECT, OFF(co_code), READONLY},
166 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
167 {"co_names", T_OBJECT, OFF(co_names), READONLY},
168 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
169 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
170 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
171 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
172 {"co_name", T_OBJECT, OFF(co_name), READONLY},
173 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
174 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
175 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176};
177
178/* Helper for code_new: return a shallow copy of a tuple that is
179 guaranteed to contain exact strings, by converting string subclasses
180 to exact strings and complaining if a non-string is found. */
181static PyObject*
182validate_and_copy_tuple(PyObject *tup)
183{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000184 PyObject *newtuple;
185 PyObject *item;
186 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000188 len = PyTuple_GET_SIZE(tup);
189 newtuple = PyTuple_New(len);
190 if (newtuple == NULL)
191 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000193 for (i = 0; i < len; i++) {
194 item = PyTuple_GET_ITEM(tup, i);
195 if (PyString_CheckExact(item)) {
196 Py_INCREF(item);
197 }
198 else if (!PyString_Check(item)) {
199 PyErr_Format(
200 PyExc_TypeError,
201 "name tuples must contain only "
202 "strings, not '%.500s'",
203 item->ob_type->tp_name);
204 Py_DECREF(newtuple);
205 return NULL;
206 }
207 else {
208 item = PyString_FromStringAndSize(
209 PyString_AS_STRING(item),
210 PyString_GET_SIZE(item));
211 if (item == NULL) {
212 Py_DECREF(newtuple);
213 return NULL;
214 }
215 }
216 PyTuple_SET_ITEM(newtuple, i, item);
217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000219 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220}
221
222PyDoc_STRVAR(code_doc,
223"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
224 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
225\n\
226Create a code object. Not for the faint of heart.");
227
228static PyObject *
229code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
230{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000231 int argcount;
232 int nlocals;
233 int stacksize;
234 int flags;
235 PyObject *co = NULL;
236 PyObject *code;
237 PyObject *consts;
238 PyObject *names, *ournames = NULL;
239 PyObject *varnames, *ourvarnames = NULL;
240 PyObject *freevars = NULL, *ourfreevars = NULL;
241 PyObject *cellvars = NULL, *ourcellvars = NULL;
242 PyObject *filename;
243 PyObject *name;
244 int firstlineno;
245 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
248 &argcount, &nlocals, &stacksize, &flags,
249 &code,
250 &PyTuple_Type, &consts,
251 &PyTuple_Type, &names,
252 &PyTuple_Type, &varnames,
253 &filename, &name,
254 &firstlineno, &lnotab,
255 &PyTuple_Type, &freevars,
256 &PyTuple_Type, &cellvars))
257 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000259 if (argcount < 0) {
260 PyErr_SetString(
261 PyExc_ValueError,
262 "code: argcount must not be negative");
263 goto cleanup;
264 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000266 if (nlocals < 0) {
267 PyErr_SetString(
268 PyExc_ValueError,
269 "code: nlocals must not be negative");
270 goto cleanup;
271 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000273 ournames = validate_and_copy_tuple(names);
274 if (ournames == NULL)
275 goto cleanup;
276 ourvarnames = validate_and_copy_tuple(varnames);
277 if (ourvarnames == NULL)
278 goto cleanup;
279 if (freevars)
280 ourfreevars = validate_and_copy_tuple(freevars);
281 else
282 ourfreevars = PyTuple_New(0);
283 if (ourfreevars == NULL)
284 goto cleanup;
285 if (cellvars)
286 ourcellvars = validate_and_copy_tuple(cellvars);
287 else
288 ourcellvars = PyTuple_New(0);
289 if (ourcellvars == NULL)
290 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000292 co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
293 code, consts, ournames, ourvarnames,
294 ourfreevars, ourcellvars, filename,
295 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000296 cleanup:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000297 Py_XDECREF(ournames);
298 Py_XDECREF(ourvarnames);
299 Py_XDECREF(ourfreevars);
300 Py_XDECREF(ourcellvars);
301 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302}
303
304static void
305code_dealloc(PyCodeObject *co)
306{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000307 Py_XDECREF(co->co_code);
308 Py_XDECREF(co->co_consts);
309 Py_XDECREF(co->co_names);
310 Py_XDECREF(co->co_varnames);
311 Py_XDECREF(co->co_freevars);
312 Py_XDECREF(co->co_cellvars);
313 Py_XDECREF(co->co_filename);
314 Py_XDECREF(co->co_name);
315 Py_XDECREF(co->co_lnotab);
316 if (co->co_zombieframe != NULL)
317 PyObject_GC_Del(co->co_zombieframe);
318 if (co->co_weakreflist != NULL)
319 PyObject_ClearWeakRefs((PyObject*)co);
320 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321}
322
323static PyObject *
324code_repr(PyCodeObject *co)
325{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000326 char buf[500];
327 int lineno = -1;
328 char *filename = "???";
329 char *name = "???";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 if (co->co_firstlineno != 0)
332 lineno = co->co_firstlineno;
333 if (co->co_filename && PyString_Check(co->co_filename))
334 filename = PyString_AS_STRING(co->co_filename);
335 if (co->co_name && PyString_Check(co->co_name))
336 name = PyString_AS_STRING(co->co_name);
337 PyOS_snprintf(buf, sizeof(buf),
338 "<code object %.100s at %p, file \"%.300s\", line %d>",
339 name, co, filename, lineno);
340 return PyString_FromString(buf);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341}
342
343static int
344code_compare(PyCodeObject *co, PyCodeObject *cp)
345{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 int cmp;
347 cmp = PyObject_Compare(co->co_name, cp->co_name);
348 if (cmp) return cmp;
349 cmp = co->co_argcount - cp->co_argcount;
350 if (cmp) goto normalize;
351 cmp = co->co_nlocals - cp->co_nlocals;
352 if (cmp) goto normalize;
353 cmp = co->co_flags - cp->co_flags;
354 if (cmp) goto normalize;
355 cmp = co->co_firstlineno - cp->co_firstlineno;
356 if (cmp) goto normalize;
357 cmp = PyObject_Compare(co->co_code, cp->co_code);
358 if (cmp) return cmp;
359 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
360 if (cmp) return cmp;
361 cmp = PyObject_Compare(co->co_names, cp->co_names);
362 if (cmp) return cmp;
363 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
364 if (cmp) return cmp;
365 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
366 if (cmp) return cmp;
367 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
368 return cmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369
370 normalize:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000371 if (cmp > 0)
372 return 1;
373 else if (cmp < 0)
374 return -1;
375 else
376 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377}
378
Victor Stinner77911652016-01-22 12:33:12 +0100379PyObject*
380_PyCode_ConstantKey(PyObject *op)
381{
382 PyObject *key;
383
384 /* Py_None is a singleton */
385 if (op == Py_None
386 || PyInt_CheckExact(op)
387 || PyLong_CheckExact(op)
388 || PyBool_Check(op)
389 || PyBytes_CheckExact(op)
390#ifdef Py_USING_UNICODE
391 || PyUnicode_CheckExact(op)
392#endif
393 /* code_richcompare() uses _PyCode_ConstantKey() internally */
394 || PyCode_Check(op)) {
395 key = PyTuple_Pack(2, Py_TYPE(op), op);
396 }
397 else if (PyFloat_CheckExact(op)) {
398 double d = PyFloat_AS_DOUBLE(op);
399 /* all we need is to make the tuple different in either the 0.0
400 * or -0.0 case from all others, just to avoid the "coercion".
401 */
402 if (d == 0.0 && copysign(1.0, d) < 0.0)
403 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
404 else
405 key = PyTuple_Pack(2, Py_TYPE(op), op);
406 }
407#ifndef WITHOUT_COMPLEX
408 else if (PyComplex_CheckExact(op)) {
409 Py_complex z;
410 int real_negzero, imag_negzero;
411 /* For the complex case we must make complex(x, 0.)
412 different from complex(x, -0.) and complex(0., y)
413 different from complex(-0., y), for any x and y.
414 All four complex zeros must be distinguished.*/
415 z = PyComplex_AsCComplex(op);
416 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
417 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
418 /* use True, False and None singleton as tags for the real and imag
419 * sign, to make tuples different */
420 if (real_negzero && imag_negzero) {
421 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
422 }
423 else if (imag_negzero) {
424 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
425 }
426 else if (real_negzero) {
427 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
428 }
429 else {
430 key = PyTuple_Pack(2, Py_TYPE(op), op);
431 }
432 }
433#endif
434 else if (PyTuple_CheckExact(op)) {
435 Py_ssize_t i, len;
436 PyObject *tuple;
437
438 len = PyTuple_GET_SIZE(op);
439 tuple = PyTuple_New(len);
440 if (tuple == NULL)
441 return NULL;
442
443 for (i=0; i < len; i++) {
444 PyObject *item, *item_key;
445
446 item = PyTuple_GET_ITEM(op, i);
447 item_key = _PyCode_ConstantKey(item);
448 if (item_key == NULL) {
449 Py_DECREF(tuple);
450 return NULL;
451 }
452
453 PyTuple_SET_ITEM(tuple, i, item_key);
454 }
455
456 key = PyTuple_Pack(3, Py_TYPE(op), op, tuple);
457 Py_DECREF(tuple);
458 }
459 else if (PyFrozenSet_CheckExact(op)) {
460 Py_ssize_t pos = 0;
461 PyObject *item;
462 long hash;
463 Py_ssize_t i, len;
464 PyObject *tuple, *set;
465
466 len = PySet_GET_SIZE(op);
467 tuple = PyTuple_New(len);
468 if (tuple == NULL)
469 return NULL;
470
471 i = 0;
472 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
473 PyObject *item_key;
474
475 item_key = _PyCode_ConstantKey(item);
476 if (item_key == NULL) {
477 Py_DECREF(tuple);
478 return NULL;
479 }
480
481 assert(i < len);
482 PyTuple_SET_ITEM(tuple, i, item_key);
483 i++;
484 }
485 set = PyFrozenSet_New(tuple);
486 Py_DECREF(tuple);
487 if (set == NULL)
488 return NULL;
489
490 key = PyTuple_Pack(3, Py_TYPE(op), op, set);
491 Py_DECREF(set);
492 return key;
493 }
494 else {
495 /* for other types, use the object identifier as an unique identifier
496 * to ensure that they are seen as unequal. */
497 PyObject *obj_id = PyLong_FromVoidPtr(op);
498 if (obj_id == NULL)
499 return NULL;
500
501 key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id);
502 Py_DECREF(obj_id);
503 }
504 return key;
505}
506
Steven Bethard6a644f92008-03-18 22:08:20 +0000507static PyObject *
508code_richcompare(PyObject *self, PyObject *other, int op)
509{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000510 PyCodeObject *co, *cp;
511 int eq;
Victor Stinner77911652016-01-22 12:33:12 +0100512 PyObject *consts1, *consts2;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000513 PyObject *res;
Steven Bethard6a644f92008-03-18 22:08:20 +0000514
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 if ((op != Py_EQ && op != Py_NE) ||
516 !PyCode_Check(self) ||
517 !PyCode_Check(other)) {
Steven Bethard6a644f92008-03-18 22:08:20 +0000518
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000519 /* Py3K warning if types are not equal and comparison
520 isn't == or != */
521 if (PyErr_WarnPy3k("code inequality comparisons not supported "
522 "in 3.x", 1) < 0) {
523 return NULL;
524 }
Steven Bethard6a644f92008-03-18 22:08:20 +0000525
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526 Py_INCREF(Py_NotImplemented);
527 return Py_NotImplemented;
528 }
Steven Bethard6a644f92008-03-18 22:08:20 +0000529
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000530 co = (PyCodeObject *)self;
531 cp = (PyCodeObject *)other;
Steven Bethard6a644f92008-03-18 22:08:20 +0000532
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000533 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
534 if (eq <= 0) goto unequal;
535 eq = co->co_argcount == cp->co_argcount;
536 if (!eq) goto unequal;
537 eq = co->co_nlocals == cp->co_nlocals;
538 if (!eq) goto unequal;
539 eq = co->co_flags == cp->co_flags;
540 if (!eq) goto unequal;
541 eq = co->co_firstlineno == cp->co_firstlineno;
542 if (!eq) goto unequal;
543 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
544 if (eq <= 0) goto unequal;
Victor Stinner77911652016-01-22 12:33:12 +0100545
546 /* compare constants */
547 consts1 = _PyCode_ConstantKey(co->co_consts);
548 if (!consts1)
549 return NULL;
550 consts2 = _PyCode_ConstantKey(cp->co_consts);
551 if (!consts2) {
552 Py_DECREF(consts1);
553 return NULL;
554 }
555 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
556 Py_DECREF(consts1);
557 Py_DECREF(consts2);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000558 if (eq <= 0) goto unequal;
Victor Stinner77911652016-01-22 12:33:12 +0100559
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000560 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
561 if (eq <= 0) goto unequal;
562 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
563 if (eq <= 0) goto unequal;
564 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
565 if (eq <= 0) goto unequal;
566 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
567 if (eq <= 0) goto unequal;
Steven Bethard6a644f92008-03-18 22:08:20 +0000568
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000569 if (op == Py_EQ)
570 res = Py_True;
571 else
572 res = Py_False;
573 goto done;
Steven Bethard6a644f92008-03-18 22:08:20 +0000574
575 unequal:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000576 if (eq < 0)
577 return NULL;
578 if (op == Py_NE)
579 res = Py_True;
580 else
581 res = Py_False;
Steven Bethard6a644f92008-03-18 22:08:20 +0000582
583 done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000584 Py_INCREF(res);
585 return res;
Steven Bethard6a644f92008-03-18 22:08:20 +0000586}
587
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588static long
589code_hash(PyCodeObject *co)
590{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000591 long h, h0, h1, h2, h3, h4, h5, h6;
592 h0 = PyObject_Hash(co->co_name);
593 if (h0 == -1) return -1;
594 h1 = PyObject_Hash(co->co_code);
595 if (h1 == -1) return -1;
596 h2 = PyObject_Hash(co->co_consts);
597 if (h2 == -1) return -1;
598 h3 = PyObject_Hash(co->co_names);
599 if (h3 == -1) return -1;
600 h4 = PyObject_Hash(co->co_varnames);
601 if (h4 == -1) return -1;
602 h5 = PyObject_Hash(co->co_freevars);
603 if (h5 == -1) return -1;
604 h6 = PyObject_Hash(co->co_cellvars);
605 if (h6 == -1) return -1;
606 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
607 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
608 if (h == -1) h = -2;
609 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610}
611
612/* XXX code objects need to participate in GC? */
613
614PyTypeObject PyCode_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000615 PyVarObject_HEAD_INIT(&PyType_Type, 0)
616 "code",
617 sizeof(PyCodeObject),
618 0,
619 (destructor)code_dealloc, /* tp_dealloc */
620 0, /* tp_print */
621 0, /* tp_getattr */
622 0, /* tp_setattr */
623 (cmpfunc)code_compare, /* tp_compare */
624 (reprfunc)code_repr, /* tp_repr */
625 0, /* tp_as_number */
626 0, /* tp_as_sequence */
627 0, /* tp_as_mapping */
628 (hashfunc)code_hash, /* tp_hash */
629 0, /* tp_call */
630 0, /* tp_str */
631 PyObject_GenericGetAttr, /* tp_getattro */
632 0, /* tp_setattro */
633 0, /* tp_as_buffer */
634 Py_TPFLAGS_DEFAULT, /* tp_flags */
635 code_doc, /* tp_doc */
636 0, /* tp_traverse */
637 0, /* tp_clear */
638 code_richcompare, /* tp_richcompare */
639 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
640 0, /* tp_iter */
641 0, /* tp_iternext */
642 0, /* tp_methods */
643 code_memberlist, /* tp_members */
644 0, /* tp_getset */
645 0, /* tp_base */
646 0, /* tp_dict */
647 0, /* tp_descr_get */
648 0, /* tp_descr_set */
649 0, /* tp_dictoffset */
650 0, /* tp_init */
651 0, /* tp_alloc */
652 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653};
654
Jeffrey Yasskin655d8352009-05-23 23:23:01 +0000655/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
656 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657*/
658
659int
660PyCode_Addr2Line(PyCodeObject *co, int addrq)
661{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000662 int size = PyString_Size(co->co_lnotab) / 2;
663 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
664 int line = co->co_firstlineno;
665 int addr = 0;
666 while (--size >= 0) {
667 addr += *p++;
668 if (addr > addrq)
669 break;
670 line += *p++;
671 }
672 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673}
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000674
Jeffrey Yasskin655d8352009-05-23 23:23:01 +0000675/* Update *bounds to describe the first and one-past-the-last instructions in
676 the same line as lasti. Return the number of that line. */
677int
678_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000679{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000680 int size, addr, line;
681 unsigned char* p;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000682
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000683 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
684 size = PyString_GET_SIZE(co->co_lnotab) / 2;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000685
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000686 addr = 0;
687 line = co->co_firstlineno;
688 assert(line > 0);
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000689
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000690 /* possible optimization: if f->f_lasti == instr_ub
691 (likely to be a common case) then we already know
692 instr_lb -- if we stored the matching value of p
693 somwhere we could skip the first while loop. */
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000694
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000695 /* See lnotab_notes.txt for the description of
696 co_lnotab. A point to remember: increments to p
697 come in (addr, line) pairs. */
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000698
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000699 bounds->ap_lower = 0;
700 while (size > 0) {
701 if (addr + *p > lasti)
702 break;
703 addr += *p++;
704 if (*p)
705 bounds->ap_lower = addr;
706 line += *p++;
707 --size;
708 }
709
710 if (size > 0) {
711 while (--size >= 0) {
712 addr += *p++;
713 if (*p++)
714 break;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000715 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000716 bounds->ap_upper = addr;
717 }
718 else {
719 bounds->ap_upper = INT_MAX;
720 }
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000721
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 return line;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000723}