blob: b7c4059976e62290af3efaf6d5a345caff422261 [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
Serhiy Storchakaab8b75a2016-10-04 18:17:08 +030011all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000013 static char ok_name_char[256];
Serhiy Storchakaab8b75a2016-10-04 18:17:08 +030014 static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
15 const unsigned char *s, *e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000016
Antoine Pitrouc83ea132010-05-09 14:46:46 +000017 if (ok_name_char[*name_chars] == 0) {
Serhiy Storchakaab8b75a2016-10-04 18:17:08 +030018 const unsigned char *p;
Antoine Pitrouc83ea132010-05-09 14:46:46 +000019 for (p = name_chars; *p; p++)
20 ok_name_char[*p] = 1;
21 }
Serhiy Storchakaab8b75a2016-10-04 18:17:08 +030022 s = (unsigned char *)PyString_AS_STRING(o);
23 e = s + PyString_GET_SIZE(o);
24 while (s != e) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000025 if (ok_name_char[*s++] == 0)
26 return 0;
27 }
28 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029}
30
31static void
32intern_strings(PyObject *tuple)
33{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000034 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035
Antoine Pitrouc83ea132010-05-09 14:46:46 +000036 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 Hylton3e0055f2005-10-20 19:59:25 +000043}
44
Serhiy Storchaka67edf732016-09-30 10:38:08 +030045/* Intern selected string constants */
46static int
47intern_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 Storchakaab8b75a2016-10-04 18:17:08 +030055 if (all_name_chars(v)) {
Serhiy Storchaka67edf732016-09-30 10:38:08 +030056 PyObject *w = v;
57 PyString_InternInPlace(&v);
58 if (w != v) {
59 PyTuple_SET_ITEM(tuple, i, v);
60 modified = 1;
61 }
62 }
63 }
64 else if (PyTuple_CheckExact(v)) {
65 intern_string_constants(v);
66 }
67 else if (PyFrozenSet_CheckExact(v)) {
68 PyObject *tmp = PySequence_Tuple(v);
69 if (tmp == NULL) {
70 PyErr_Clear();
71 continue;
72 }
73 if (intern_string_constants(tmp)) {
74 v = PyFrozenSet_New(tmp);
75 if (v == NULL) {
76 PyErr_Clear();
77 }
78 else {
79 PyTuple_SET_ITEM(tuple, i, v);
80 modified = 1;
81 }
82 }
83 Py_DECREF(tmp);
84 }
85 }
86 return modified;
87}
88
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089
90PyCodeObject *
91PyCode_New(int argcount, int nlocals, int stacksize, int flags,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000092 PyObject *code, PyObject *consts, PyObject *names,
93 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
94 PyObject *filename, PyObject *name, int firstlineno,
95 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000097 PyCodeObject *co;
98 Py_ssize_t i;
99 /* Check argument types */
100 if (argcount < 0 || nlocals < 0 ||
101 code == NULL ||
102 consts == NULL || !PyTuple_Check(consts) ||
103 names == NULL || !PyTuple_Check(names) ||
104 varnames == NULL || !PyTuple_Check(varnames) ||
105 freevars == NULL || !PyTuple_Check(freevars) ||
106 cellvars == NULL || !PyTuple_Check(cellvars) ||
107 name == NULL || !PyString_Check(name) ||
108 filename == NULL || !PyString_Check(filename) ||
109 lnotab == NULL || !PyString_Check(lnotab) ||
110 !PyObject_CheckReadBuffer(code)) {
111 PyErr_BadInternalCall();
112 return NULL;
113 }
114 intern_strings(names);
115 intern_strings(varnames);
116 intern_strings(freevars);
117 intern_strings(cellvars);
Serhiy Storchaka67edf732016-09-30 10:38:08 +0300118 intern_string_constants(consts);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
120 if (co != NULL) {
121 co->co_argcount = argcount;
122 co->co_nlocals = nlocals;
123 co->co_stacksize = stacksize;
124 co->co_flags = flags;
125 Py_INCREF(code);
126 co->co_code = code;
127 Py_INCREF(consts);
128 co->co_consts = consts;
129 Py_INCREF(names);
130 co->co_names = names;
131 Py_INCREF(varnames);
132 co->co_varnames = varnames;
133 Py_INCREF(freevars);
134 co->co_freevars = freevars;
135 Py_INCREF(cellvars);
136 co->co_cellvars = cellvars;
137 Py_INCREF(filename);
138 co->co_filename = filename;
139 Py_INCREF(name);
140 co->co_name = name;
141 co->co_firstlineno = firstlineno;
142 Py_INCREF(lnotab);
143 co->co_lnotab = lnotab;
144 co->co_zombieframe = NULL;
145 co->co_weakreflist = NULL;
146 }
147 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148}
149
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000150PyCodeObject *
151PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
152{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000153 static PyObject *emptystring = NULL;
154 static PyObject *nulltuple = NULL;
155 PyObject *filename_ob = NULL;
156 PyObject *funcname_ob = NULL;
157 PyCodeObject *result = NULL;
158 if (emptystring == NULL) {
159 emptystring = PyString_FromString("");
160 if (emptystring == NULL)
161 goto failed;
162 }
163 if (nulltuple == NULL) {
164 nulltuple = PyTuple_New(0);
165 if (nulltuple == NULL)
166 goto failed;
167 }
168 funcname_ob = PyString_FromString(funcname);
169 if (funcname_ob == NULL)
170 goto failed;
171 filename_ob = PyString_FromString(filename);
172 if (filename_ob == NULL)
173 goto failed;
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000174
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000175 result = PyCode_New(0, /* argcount */
176 0, /* nlocals */
177 0, /* stacksize */
178 0, /* flags */
179 emptystring, /* code */
180 nulltuple, /* consts */
181 nulltuple, /* names */
182 nulltuple, /* varnames */
183 nulltuple, /* freevars */
184 nulltuple, /* cellvars */
185 filename_ob, /* filename */
186 funcname_ob, /* name */
187 firstlineno, /* firstlineno */
188 emptystring /* lnotab */
189 );
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000190
191failed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000192 Py_XDECREF(funcname_ob);
193 Py_XDECREF(filename_ob);
194 return result;
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000195}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
197#define OFF(x) offsetof(PyCodeObject, x)
198
199static PyMemberDef code_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000200 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
201 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
202 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
203 {"co_flags", T_INT, OFF(co_flags), READONLY},
204 {"co_code", T_OBJECT, OFF(co_code), READONLY},
205 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
206 {"co_names", T_OBJECT, OFF(co_names), READONLY},
207 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
208 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
209 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
210 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
211 {"co_name", T_OBJECT, OFF(co_name), READONLY},
212 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
213 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
214 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215};
216
217/* Helper for code_new: return a shallow copy of a tuple that is
218 guaranteed to contain exact strings, by converting string subclasses
219 to exact strings and complaining if a non-string is found. */
220static PyObject*
221validate_and_copy_tuple(PyObject *tup)
222{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 PyObject *newtuple;
224 PyObject *item;
225 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000227 len = PyTuple_GET_SIZE(tup);
228 newtuple = PyTuple_New(len);
229 if (newtuple == NULL)
230 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000232 for (i = 0; i < len; i++) {
233 item = PyTuple_GET_ITEM(tup, i);
234 if (PyString_CheckExact(item)) {
235 Py_INCREF(item);
236 }
237 else if (!PyString_Check(item)) {
238 PyErr_Format(
239 PyExc_TypeError,
240 "name tuples must contain only "
241 "strings, not '%.500s'",
242 item->ob_type->tp_name);
243 Py_DECREF(newtuple);
244 return NULL;
245 }
246 else {
247 item = PyString_FromStringAndSize(
248 PyString_AS_STRING(item),
249 PyString_GET_SIZE(item));
250 if (item == NULL) {
251 Py_DECREF(newtuple);
252 return NULL;
253 }
254 }
255 PyTuple_SET_ITEM(newtuple, i, item);
256 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000258 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259}
260
261PyDoc_STRVAR(code_doc,
262"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
263 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
264\n\
265Create a code object. Not for the faint of heart.");
266
267static PyObject *
268code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
269{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000270 int argcount;
271 int nlocals;
272 int stacksize;
273 int flags;
274 PyObject *co = NULL;
275 PyObject *code;
276 PyObject *consts;
277 PyObject *names, *ournames = NULL;
278 PyObject *varnames, *ourvarnames = NULL;
279 PyObject *freevars = NULL, *ourfreevars = NULL;
280 PyObject *cellvars = NULL, *ourcellvars = NULL;
281 PyObject *filename;
282 PyObject *name;
283 int firstlineno;
284 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000286 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
287 &argcount, &nlocals, &stacksize, &flags,
288 &code,
289 &PyTuple_Type, &consts,
290 &PyTuple_Type, &names,
291 &PyTuple_Type, &varnames,
292 &filename, &name,
293 &firstlineno, &lnotab,
294 &PyTuple_Type, &freevars,
295 &PyTuple_Type, &cellvars))
296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 if (argcount < 0) {
299 PyErr_SetString(
300 PyExc_ValueError,
301 "code: argcount must not be negative");
302 goto cleanup;
303 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000305 if (nlocals < 0) {
306 PyErr_SetString(
307 PyExc_ValueError,
308 "code: nlocals must not be negative");
309 goto cleanup;
310 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000312 ournames = validate_and_copy_tuple(names);
313 if (ournames == NULL)
314 goto cleanup;
315 ourvarnames = validate_and_copy_tuple(varnames);
316 if (ourvarnames == NULL)
317 goto cleanup;
318 if (freevars)
319 ourfreevars = validate_and_copy_tuple(freevars);
320 else
321 ourfreevars = PyTuple_New(0);
322 if (ourfreevars == NULL)
323 goto cleanup;
324 if (cellvars)
325 ourcellvars = validate_and_copy_tuple(cellvars);
326 else
327 ourcellvars = PyTuple_New(0);
328 if (ourcellvars == NULL)
329 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
332 code, consts, ournames, ourvarnames,
333 ourfreevars, ourcellvars, filename,
334 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000335 cleanup:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000336 Py_XDECREF(ournames);
337 Py_XDECREF(ourvarnames);
338 Py_XDECREF(ourfreevars);
339 Py_XDECREF(ourcellvars);
340 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341}
342
343static void
344code_dealloc(PyCodeObject *co)
345{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 Py_XDECREF(co->co_code);
347 Py_XDECREF(co->co_consts);
348 Py_XDECREF(co->co_names);
349 Py_XDECREF(co->co_varnames);
350 Py_XDECREF(co->co_freevars);
351 Py_XDECREF(co->co_cellvars);
352 Py_XDECREF(co->co_filename);
353 Py_XDECREF(co->co_name);
354 Py_XDECREF(co->co_lnotab);
355 if (co->co_zombieframe != NULL)
356 PyObject_GC_Del(co->co_zombieframe);
357 if (co->co_weakreflist != NULL)
358 PyObject_ClearWeakRefs((PyObject*)co);
359 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360}
361
362static PyObject *
363code_repr(PyCodeObject *co)
364{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 char buf[500];
366 int lineno = -1;
367 char *filename = "???";
368 char *name = "???";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 if (co->co_firstlineno != 0)
371 lineno = co->co_firstlineno;
372 if (co->co_filename && PyString_Check(co->co_filename))
373 filename = PyString_AS_STRING(co->co_filename);
374 if (co->co_name && PyString_Check(co->co_name))
375 name = PyString_AS_STRING(co->co_name);
376 PyOS_snprintf(buf, sizeof(buf),
377 "<code object %.100s at %p, file \"%.300s\", line %d>",
378 name, co, filename, lineno);
379 return PyString_FromString(buf);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380}
381
382static int
383code_compare(PyCodeObject *co, PyCodeObject *cp)
384{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000385 int cmp;
386 cmp = PyObject_Compare(co->co_name, cp->co_name);
387 if (cmp) return cmp;
388 cmp = co->co_argcount - cp->co_argcount;
389 if (cmp) goto normalize;
390 cmp = co->co_nlocals - cp->co_nlocals;
391 if (cmp) goto normalize;
392 cmp = co->co_flags - cp->co_flags;
393 if (cmp) goto normalize;
394 cmp = co->co_firstlineno - cp->co_firstlineno;
395 if (cmp) goto normalize;
396 cmp = PyObject_Compare(co->co_code, cp->co_code);
397 if (cmp) return cmp;
398 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
399 if (cmp) return cmp;
400 cmp = PyObject_Compare(co->co_names, cp->co_names);
401 if (cmp) return cmp;
402 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
403 if (cmp) return cmp;
404 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
405 if (cmp) return cmp;
406 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
407 return cmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408
409 normalize:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000410 if (cmp > 0)
411 return 1;
412 else if (cmp < 0)
413 return -1;
414 else
415 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416}
417
Victor Stinner77911652016-01-22 12:33:12 +0100418PyObject*
419_PyCode_ConstantKey(PyObject *op)
420{
421 PyObject *key;
422
423 /* Py_None is a singleton */
424 if (op == Py_None
425 || PyInt_CheckExact(op)
426 || PyLong_CheckExact(op)
427 || PyBool_Check(op)
428 || PyBytes_CheckExact(op)
429#ifdef Py_USING_UNICODE
430 || PyUnicode_CheckExact(op)
431#endif
432 /* code_richcompare() uses _PyCode_ConstantKey() internally */
433 || PyCode_Check(op)) {
434 key = PyTuple_Pack(2, Py_TYPE(op), op);
435 }
436 else if (PyFloat_CheckExact(op)) {
437 double d = PyFloat_AS_DOUBLE(op);
438 /* all we need is to make the tuple different in either the 0.0
439 * or -0.0 case from all others, just to avoid the "coercion".
440 */
441 if (d == 0.0 && copysign(1.0, d) < 0.0)
442 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
443 else
444 key = PyTuple_Pack(2, Py_TYPE(op), op);
445 }
446#ifndef WITHOUT_COMPLEX
447 else if (PyComplex_CheckExact(op)) {
448 Py_complex z;
449 int real_negzero, imag_negzero;
450 /* For the complex case we must make complex(x, 0.)
451 different from complex(x, -0.) and complex(0., y)
452 different from complex(-0., y), for any x and y.
453 All four complex zeros must be distinguished.*/
454 z = PyComplex_AsCComplex(op);
455 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
456 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
457 /* use True, False and None singleton as tags for the real and imag
458 * sign, to make tuples different */
459 if (real_negzero && imag_negzero) {
460 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
461 }
462 else if (imag_negzero) {
463 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
464 }
465 else if (real_negzero) {
466 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
467 }
468 else {
469 key = PyTuple_Pack(2, Py_TYPE(op), op);
470 }
471 }
472#endif
473 else if (PyTuple_CheckExact(op)) {
474 Py_ssize_t i, len;
475 PyObject *tuple;
476
477 len = PyTuple_GET_SIZE(op);
478 tuple = PyTuple_New(len);
479 if (tuple == NULL)
480 return NULL;
481
482 for (i=0; i < len; i++) {
483 PyObject *item, *item_key;
484
485 item = PyTuple_GET_ITEM(op, i);
486 item_key = _PyCode_ConstantKey(item);
487 if (item_key == NULL) {
488 Py_DECREF(tuple);
489 return NULL;
490 }
491
492 PyTuple_SET_ITEM(tuple, i, item_key);
493 }
494
495 key = PyTuple_Pack(3, Py_TYPE(op), op, tuple);
496 Py_DECREF(tuple);
497 }
498 else if (PyFrozenSet_CheckExact(op)) {
499 Py_ssize_t pos = 0;
500 PyObject *item;
501 long hash;
502 Py_ssize_t i, len;
503 PyObject *tuple, *set;
504
505 len = PySet_GET_SIZE(op);
506 tuple = PyTuple_New(len);
507 if (tuple == NULL)
508 return NULL;
509
510 i = 0;
511 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
512 PyObject *item_key;
513
514 item_key = _PyCode_ConstantKey(item);
515 if (item_key == NULL) {
516 Py_DECREF(tuple);
517 return NULL;
518 }
519
520 assert(i < len);
521 PyTuple_SET_ITEM(tuple, i, item_key);
522 i++;
523 }
524 set = PyFrozenSet_New(tuple);
525 Py_DECREF(tuple);
526 if (set == NULL)
527 return NULL;
528
529 key = PyTuple_Pack(3, Py_TYPE(op), op, set);
530 Py_DECREF(set);
531 return key;
532 }
533 else {
Martin Panter6a8163a2016-04-15 02:14:19 +0000534 /* for other types, use the object identifier as a unique identifier
Victor Stinner77911652016-01-22 12:33:12 +0100535 * 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 Bethard6a644f92008-03-18 22:08:20 +0000546static PyObject *
547code_richcompare(PyObject *self, PyObject *other, int op)
548{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000549 PyCodeObject *co, *cp;
550 int eq;
Victor Stinner77911652016-01-22 12:33:12 +0100551 PyObject *consts1, *consts2;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000552 PyObject *res;
Steven Bethard6a644f92008-03-18 22:08:20 +0000553
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000554 if ((op != Py_EQ && op != Py_NE) ||
555 !PyCode_Check(self) ||
556 !PyCode_Check(other)) {
Steven Bethard6a644f92008-03-18 22:08:20 +0000557
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000558 /* 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 Bethard6a644f92008-03-18 22:08:20 +0000564
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000565 Py_INCREF(Py_NotImplemented);
566 return Py_NotImplemented;
567 }
Steven Bethard6a644f92008-03-18 22:08:20 +0000568
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000569 co = (PyCodeObject *)self;
570 cp = (PyCodeObject *)other;
Steven Bethard6a644f92008-03-18 22:08:20 +0000571
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000572 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 Stinner77911652016-01-22 12:33:12 +0100584
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 Pitrouc83ea132010-05-09 14:46:46 +0000597 if (eq <= 0) goto unequal;
Victor Stinner77911652016-01-22 12:33:12 +0100598
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000599 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 Bethard6a644f92008-03-18 22:08:20 +0000607
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000608 if (op == Py_EQ)
609 res = Py_True;
610 else
611 res = Py_False;
612 goto done;
Steven Bethard6a644f92008-03-18 22:08:20 +0000613
614 unequal:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000615 if (eq < 0)
616 return NULL;
617 if (op == Py_NE)
618 res = Py_True;
619 else
620 res = Py_False;
Steven Bethard6a644f92008-03-18 22:08:20 +0000621
622 done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000623 Py_INCREF(res);
624 return res;
Steven Bethard6a644f92008-03-18 22:08:20 +0000625}
626
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627static long
628code_hash(PyCodeObject *co)
629{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000630 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 Hylton3e0055f2005-10-20 19:59:25 +0000649}
650
651/* XXX code objects need to participate in GC? */
652
653PyTypeObject PyCode_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000654 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 Hylton3e0055f2005-10-20 19:59:25 +0000692};
693
Jeffrey Yasskin655d8352009-05-23 23:23:01 +0000694/* 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 Hylton3e0055f2005-10-20 19:59:25 +0000696*/
697
698int
699PyCode_Addr2Line(PyCodeObject *co, int addrq)
700{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000701 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 Hylton3e0055f2005-10-20 19:59:25 +0000712}
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000713
Jeffrey Yasskin655d8352009-05-23 23:23:01 +0000714/* 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. */
716int
717_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000718{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 int size, addr, line;
720 unsigned char* p;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000721
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
723 size = PyString_GET_SIZE(co->co_lnotab) / 2;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000724
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000725 addr = 0;
726 line = co->co_firstlineno;
727 assert(line > 0);
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000728
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000729 /* 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 Panter65076572016-09-07 12:03:06 +0000732 somewhere we could skip the first while loop. */
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000733
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000734 /* 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 Hyltona4ebc132006-04-18 14:47:00 +0000737
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000738 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 Hyltona4ebc132006-04-18 14:47:00 +0000754 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000755 bounds->ap_upper = addr;
756 }
757 else {
758 bounds->ap_upper = INT_MAX;
759 }
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000760
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000761 return line;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000762}