blob: 79ac1b5340fbf338039fda7b16b43ad50bded205 [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;
Antoine Pitrouc83ea132010-05-09 14:46:46 +000098 /* Check argument types */
99 if (argcount < 0 || nlocals < 0 ||
100 code == NULL ||
101 consts == NULL || !PyTuple_Check(consts) ||
102 names == NULL || !PyTuple_Check(names) ||
103 varnames == NULL || !PyTuple_Check(varnames) ||
104 freevars == NULL || !PyTuple_Check(freevars) ||
105 cellvars == NULL || !PyTuple_Check(cellvars) ||
106 name == NULL || !PyString_Check(name) ||
107 filename == NULL || !PyString_Check(filename) ||
108 lnotab == NULL || !PyString_Check(lnotab) ||
109 !PyObject_CheckReadBuffer(code)) {
110 PyErr_BadInternalCall();
111 return NULL;
112 }
113 intern_strings(names);
114 intern_strings(varnames);
115 intern_strings(freevars);
116 intern_strings(cellvars);
Serhiy Storchaka67edf732016-09-30 10:38:08 +0300117 intern_string_constants(consts);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000118 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
119 if (co != NULL) {
120 co->co_argcount = argcount;
121 co->co_nlocals = nlocals;
122 co->co_stacksize = stacksize;
123 co->co_flags = flags;
124 Py_INCREF(code);
125 co->co_code = code;
126 Py_INCREF(consts);
127 co->co_consts = consts;
128 Py_INCREF(names);
129 co->co_names = names;
130 Py_INCREF(varnames);
131 co->co_varnames = varnames;
132 Py_INCREF(freevars);
133 co->co_freevars = freevars;
134 Py_INCREF(cellvars);
135 co->co_cellvars = cellvars;
136 Py_INCREF(filename);
137 co->co_filename = filename;
138 Py_INCREF(name);
139 co->co_name = name;
140 co->co_firstlineno = firstlineno;
141 Py_INCREF(lnotab);
142 co->co_lnotab = lnotab;
143 co->co_zombieframe = NULL;
144 co->co_weakreflist = NULL;
145 }
146 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000147}
148
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000149PyCodeObject *
150PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
151{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000152 static PyObject *emptystring = NULL;
153 static PyObject *nulltuple = NULL;
154 PyObject *filename_ob = NULL;
155 PyObject *funcname_ob = NULL;
156 PyCodeObject *result = NULL;
157 if (emptystring == NULL) {
158 emptystring = PyString_FromString("");
159 if (emptystring == NULL)
160 goto failed;
161 }
162 if (nulltuple == NULL) {
163 nulltuple = PyTuple_New(0);
164 if (nulltuple == NULL)
165 goto failed;
166 }
167 funcname_ob = PyString_FromString(funcname);
168 if (funcname_ob == NULL)
169 goto failed;
170 filename_ob = PyString_FromString(filename);
171 if (filename_ob == NULL)
172 goto failed;
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000173
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 result = PyCode_New(0, /* argcount */
175 0, /* nlocals */
176 0, /* stacksize */
177 0, /* flags */
178 emptystring, /* code */
179 nulltuple, /* consts */
180 nulltuple, /* names */
181 nulltuple, /* varnames */
182 nulltuple, /* freevars */
183 nulltuple, /* cellvars */
184 filename_ob, /* filename */
185 funcname_ob, /* name */
186 firstlineno, /* firstlineno */
187 emptystring /* lnotab */
188 );
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000189
190failed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000191 Py_XDECREF(funcname_ob);
192 Py_XDECREF(filename_ob);
193 return result;
Jeffrey Yasskin1aa47002009-05-08 21:51:06 +0000194}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196#define OFF(x) offsetof(PyCodeObject, x)
197
198static PyMemberDef code_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
200 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
201 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
202 {"co_flags", T_INT, OFF(co_flags), READONLY},
203 {"co_code", T_OBJECT, OFF(co_code), READONLY},
204 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
205 {"co_names", T_OBJECT, OFF(co_names), READONLY},
206 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
207 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
208 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
209 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
210 {"co_name", T_OBJECT, OFF(co_name), READONLY},
211 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
212 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
213 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214};
215
216/* Helper for code_new: return a shallow copy of a tuple that is
217 guaranteed to contain exact strings, by converting string subclasses
218 to exact strings and complaining if a non-string is found. */
219static PyObject*
220validate_and_copy_tuple(PyObject *tup)
221{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000222 PyObject *newtuple;
223 PyObject *item;
224 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000226 len = PyTuple_GET_SIZE(tup);
227 newtuple = PyTuple_New(len);
228 if (newtuple == NULL)
229 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000231 for (i = 0; i < len; i++) {
232 item = PyTuple_GET_ITEM(tup, i);
233 if (PyString_CheckExact(item)) {
234 Py_INCREF(item);
235 }
236 else if (!PyString_Check(item)) {
237 PyErr_Format(
238 PyExc_TypeError,
239 "name tuples must contain only "
240 "strings, not '%.500s'",
241 item->ob_type->tp_name);
242 Py_DECREF(newtuple);
243 return NULL;
244 }
245 else {
246 item = PyString_FromStringAndSize(
247 PyString_AS_STRING(item),
248 PyString_GET_SIZE(item));
249 if (item == NULL) {
250 Py_DECREF(newtuple);
251 return NULL;
252 }
253 }
254 PyTuple_SET_ITEM(newtuple, i, item);
255 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000257 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258}
259
260PyDoc_STRVAR(code_doc,
261"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
262 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
263\n\
264Create a code object. Not for the faint of heart.");
265
266static PyObject *
267code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
268{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000269 int argcount;
270 int nlocals;
271 int stacksize;
272 int flags;
273 PyObject *co = NULL;
274 PyObject *code;
275 PyObject *consts;
276 PyObject *names, *ournames = NULL;
277 PyObject *varnames, *ourvarnames = NULL;
278 PyObject *freevars = NULL, *ourfreevars = NULL;
279 PyObject *cellvars = NULL, *ourcellvars = NULL;
280 PyObject *filename;
281 PyObject *name;
282 int firstlineno;
283 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000285 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
286 &argcount, &nlocals, &stacksize, &flags,
287 &code,
288 &PyTuple_Type, &consts,
289 &PyTuple_Type, &names,
290 &PyTuple_Type, &varnames,
291 &filename, &name,
292 &firstlineno, &lnotab,
293 &PyTuple_Type, &freevars,
294 &PyTuple_Type, &cellvars))
295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000296
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000297 if (argcount < 0) {
298 PyErr_SetString(
299 PyExc_ValueError,
300 "code: argcount must not be negative");
301 goto cleanup;
302 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 if (nlocals < 0) {
305 PyErr_SetString(
306 PyExc_ValueError,
307 "code: nlocals must not be negative");
308 goto cleanup;
309 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000311 ournames = validate_and_copy_tuple(names);
312 if (ournames == NULL)
313 goto cleanup;
314 ourvarnames = validate_and_copy_tuple(varnames);
315 if (ourvarnames == NULL)
316 goto cleanup;
317 if (freevars)
318 ourfreevars = validate_and_copy_tuple(freevars);
319 else
320 ourfreevars = PyTuple_New(0);
321 if (ourfreevars == NULL)
322 goto cleanup;
323 if (cellvars)
324 ourcellvars = validate_and_copy_tuple(cellvars);
325 else
326 ourcellvars = PyTuple_New(0);
327 if (ourcellvars == NULL)
328 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000330 co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
331 code, consts, ournames, ourvarnames,
332 ourfreevars, ourcellvars, filename,
333 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334 cleanup:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 Py_XDECREF(ournames);
336 Py_XDECREF(ourvarnames);
337 Py_XDECREF(ourfreevars);
338 Py_XDECREF(ourcellvars);
339 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340}
341
342static void
343code_dealloc(PyCodeObject *co)
344{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000345 Py_XDECREF(co->co_code);
346 Py_XDECREF(co->co_consts);
347 Py_XDECREF(co->co_names);
348 Py_XDECREF(co->co_varnames);
349 Py_XDECREF(co->co_freevars);
350 Py_XDECREF(co->co_cellvars);
351 Py_XDECREF(co->co_filename);
352 Py_XDECREF(co->co_name);
353 Py_XDECREF(co->co_lnotab);
354 if (co->co_zombieframe != NULL)
355 PyObject_GC_Del(co->co_zombieframe);
356 if (co->co_weakreflist != NULL)
357 PyObject_ClearWeakRefs((PyObject*)co);
358 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359}
360
361static PyObject *
362code_repr(PyCodeObject *co)
363{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000364 char buf[500];
365 int lineno = -1;
366 char *filename = "???";
367 char *name = "???";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000369 if (co->co_firstlineno != 0)
370 lineno = co->co_firstlineno;
371 if (co->co_filename && PyString_Check(co->co_filename))
372 filename = PyString_AS_STRING(co->co_filename);
373 if (co->co_name && PyString_Check(co->co_name))
374 name = PyString_AS_STRING(co->co_name);
375 PyOS_snprintf(buf, sizeof(buf),
376 "<code object %.100s at %p, file \"%.300s\", line %d>",
377 name, co, filename, lineno);
378 return PyString_FromString(buf);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379}
380
381static int
382code_compare(PyCodeObject *co, PyCodeObject *cp)
383{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000384 int cmp;
385 cmp = PyObject_Compare(co->co_name, cp->co_name);
386 if (cmp) return cmp;
387 cmp = co->co_argcount - cp->co_argcount;
388 if (cmp) goto normalize;
389 cmp = co->co_nlocals - cp->co_nlocals;
390 if (cmp) goto normalize;
391 cmp = co->co_flags - cp->co_flags;
392 if (cmp) goto normalize;
393 cmp = co->co_firstlineno - cp->co_firstlineno;
394 if (cmp) goto normalize;
395 cmp = PyObject_Compare(co->co_code, cp->co_code);
396 if (cmp) return cmp;
397 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
398 if (cmp) return cmp;
399 cmp = PyObject_Compare(co->co_names, cp->co_names);
400 if (cmp) return cmp;
401 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
402 if (cmp) return cmp;
403 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
404 if (cmp) return cmp;
405 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
406 return cmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407
408 normalize:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000409 if (cmp > 0)
410 return 1;
411 else if (cmp < 0)
412 return -1;
413 else
414 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415}
416
Victor Stinner77911652016-01-22 12:33:12 +0100417PyObject*
418_PyCode_ConstantKey(PyObject *op)
419{
420 PyObject *key;
421
422 /* Py_None is a singleton */
423 if (op == Py_None
424 || PyInt_CheckExact(op)
425 || PyLong_CheckExact(op)
426 || PyBool_Check(op)
427 || PyBytes_CheckExact(op)
428#ifdef Py_USING_UNICODE
429 || PyUnicode_CheckExact(op)
430#endif
431 /* code_richcompare() uses _PyCode_ConstantKey() internally */
432 || PyCode_Check(op)) {
433 key = PyTuple_Pack(2, Py_TYPE(op), op);
434 }
435 else if (PyFloat_CheckExact(op)) {
436 double d = PyFloat_AS_DOUBLE(op);
437 /* all we need is to make the tuple different in either the 0.0
438 * or -0.0 case from all others, just to avoid the "coercion".
439 */
440 if (d == 0.0 && copysign(1.0, d) < 0.0)
441 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
442 else
443 key = PyTuple_Pack(2, Py_TYPE(op), op);
444 }
445#ifndef WITHOUT_COMPLEX
446 else if (PyComplex_CheckExact(op)) {
447 Py_complex z;
448 int real_negzero, imag_negzero;
449 /* For the complex case we must make complex(x, 0.)
450 different from complex(x, -0.) and complex(0., y)
451 different from complex(-0., y), for any x and y.
452 All four complex zeros must be distinguished.*/
453 z = PyComplex_AsCComplex(op);
454 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
455 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
456 /* use True, False and None singleton as tags for the real and imag
457 * sign, to make tuples different */
458 if (real_negzero && imag_negzero) {
459 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
460 }
461 else if (imag_negzero) {
462 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
463 }
464 else if (real_negzero) {
465 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
466 }
467 else {
468 key = PyTuple_Pack(2, Py_TYPE(op), op);
469 }
470 }
471#endif
472 else if (PyTuple_CheckExact(op)) {
473 Py_ssize_t i, len;
474 PyObject *tuple;
475
476 len = PyTuple_GET_SIZE(op);
477 tuple = PyTuple_New(len);
478 if (tuple == NULL)
479 return NULL;
480
481 for (i=0; i < len; i++) {
482 PyObject *item, *item_key;
483
484 item = PyTuple_GET_ITEM(op, i);
485 item_key = _PyCode_ConstantKey(item);
486 if (item_key == NULL) {
487 Py_DECREF(tuple);
488 return NULL;
489 }
490
491 PyTuple_SET_ITEM(tuple, i, item_key);
492 }
493
494 key = PyTuple_Pack(3, Py_TYPE(op), op, tuple);
495 Py_DECREF(tuple);
496 }
497 else if (PyFrozenSet_CheckExact(op)) {
498 Py_ssize_t pos = 0;
499 PyObject *item;
500 long hash;
501 Py_ssize_t i, len;
502 PyObject *tuple, *set;
503
504 len = PySet_GET_SIZE(op);
505 tuple = PyTuple_New(len);
506 if (tuple == NULL)
507 return NULL;
508
509 i = 0;
510 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
511 PyObject *item_key;
512
513 item_key = _PyCode_ConstantKey(item);
514 if (item_key == NULL) {
515 Py_DECREF(tuple);
516 return NULL;
517 }
518
519 assert(i < len);
520 PyTuple_SET_ITEM(tuple, i, item_key);
521 i++;
522 }
523 set = PyFrozenSet_New(tuple);
524 Py_DECREF(tuple);
525 if (set == NULL)
526 return NULL;
527
528 key = PyTuple_Pack(3, Py_TYPE(op), op, set);
529 Py_DECREF(set);
530 return key;
531 }
532 else {
Martin Panter6a8163a2016-04-15 02:14:19 +0000533 /* for other types, use the object identifier as a unique identifier
Victor Stinner77911652016-01-22 12:33:12 +0100534 * to ensure that they are seen as unequal. */
535 PyObject *obj_id = PyLong_FromVoidPtr(op);
536 if (obj_id == NULL)
537 return NULL;
538
539 key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id);
540 Py_DECREF(obj_id);
541 }
542 return key;
543}
544
Steven Bethard6a644f92008-03-18 22:08:20 +0000545static PyObject *
546code_richcompare(PyObject *self, PyObject *other, int op)
547{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000548 PyCodeObject *co, *cp;
549 int eq;
Victor Stinner77911652016-01-22 12:33:12 +0100550 PyObject *consts1, *consts2;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000551 PyObject *res;
Steven Bethard6a644f92008-03-18 22:08:20 +0000552
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000553 if ((op != Py_EQ && op != Py_NE) ||
554 !PyCode_Check(self) ||
555 !PyCode_Check(other)) {
Steven Bethard6a644f92008-03-18 22:08:20 +0000556
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000557 /* Py3K warning if types are not equal and comparison
558 isn't == or != */
559 if (PyErr_WarnPy3k("code inequality comparisons not supported "
560 "in 3.x", 1) < 0) {
561 return NULL;
562 }
Steven Bethard6a644f92008-03-18 22:08:20 +0000563
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000564 Py_INCREF(Py_NotImplemented);
565 return Py_NotImplemented;
566 }
Steven Bethard6a644f92008-03-18 22:08:20 +0000567
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000568 co = (PyCodeObject *)self;
569 cp = (PyCodeObject *)other;
Steven Bethard6a644f92008-03-18 22:08:20 +0000570
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000571 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
572 if (eq <= 0) goto unequal;
573 eq = co->co_argcount == cp->co_argcount;
574 if (!eq) goto unequal;
575 eq = co->co_nlocals == cp->co_nlocals;
576 if (!eq) goto unequal;
577 eq = co->co_flags == cp->co_flags;
578 if (!eq) goto unequal;
579 eq = co->co_firstlineno == cp->co_firstlineno;
580 if (!eq) goto unequal;
581 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
582 if (eq <= 0) goto unequal;
Victor Stinner77911652016-01-22 12:33:12 +0100583
584 /* compare constants */
585 consts1 = _PyCode_ConstantKey(co->co_consts);
586 if (!consts1)
587 return NULL;
588 consts2 = _PyCode_ConstantKey(cp->co_consts);
589 if (!consts2) {
590 Py_DECREF(consts1);
591 return NULL;
592 }
593 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
594 Py_DECREF(consts1);
595 Py_DECREF(consts2);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000596 if (eq <= 0) goto unequal;
Victor Stinner77911652016-01-22 12:33:12 +0100597
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000598 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
599 if (eq <= 0) goto unequal;
600 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
601 if (eq <= 0) goto unequal;
602 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
603 if (eq <= 0) goto unequal;
604 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
605 if (eq <= 0) goto unequal;
Steven Bethard6a644f92008-03-18 22:08:20 +0000606
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000607 if (op == Py_EQ)
608 res = Py_True;
609 else
610 res = Py_False;
611 goto done;
Steven Bethard6a644f92008-03-18 22:08:20 +0000612
613 unequal:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000614 if (eq < 0)
615 return NULL;
616 if (op == Py_NE)
617 res = Py_True;
618 else
619 res = Py_False;
Steven Bethard6a644f92008-03-18 22:08:20 +0000620
621 done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 Py_INCREF(res);
623 return res;
Steven Bethard6a644f92008-03-18 22:08:20 +0000624}
625
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626static long
627code_hash(PyCodeObject *co)
628{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000629 long h, h0, h1, h2, h3, h4, h5, h6;
630 h0 = PyObject_Hash(co->co_name);
631 if (h0 == -1) return -1;
632 h1 = PyObject_Hash(co->co_code);
633 if (h1 == -1) return -1;
634 h2 = PyObject_Hash(co->co_consts);
635 if (h2 == -1) return -1;
636 h3 = PyObject_Hash(co->co_names);
637 if (h3 == -1) return -1;
638 h4 = PyObject_Hash(co->co_varnames);
639 if (h4 == -1) return -1;
640 h5 = PyObject_Hash(co->co_freevars);
641 if (h5 == -1) return -1;
642 h6 = PyObject_Hash(co->co_cellvars);
643 if (h6 == -1) return -1;
644 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
645 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
646 if (h == -1) h = -2;
647 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648}
649
650/* XXX code objects need to participate in GC? */
651
652PyTypeObject PyCode_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000653 PyVarObject_HEAD_INIT(&PyType_Type, 0)
654 "code",
655 sizeof(PyCodeObject),
656 0,
657 (destructor)code_dealloc, /* tp_dealloc */
658 0, /* tp_print */
659 0, /* tp_getattr */
660 0, /* tp_setattr */
661 (cmpfunc)code_compare, /* tp_compare */
662 (reprfunc)code_repr, /* tp_repr */
663 0, /* tp_as_number */
664 0, /* tp_as_sequence */
665 0, /* tp_as_mapping */
666 (hashfunc)code_hash, /* tp_hash */
667 0, /* tp_call */
668 0, /* tp_str */
669 PyObject_GenericGetAttr, /* tp_getattro */
670 0, /* tp_setattro */
671 0, /* tp_as_buffer */
672 Py_TPFLAGS_DEFAULT, /* tp_flags */
673 code_doc, /* tp_doc */
674 0, /* tp_traverse */
675 0, /* tp_clear */
676 code_richcompare, /* tp_richcompare */
677 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
678 0, /* tp_iter */
679 0, /* tp_iternext */
680 0, /* tp_methods */
681 code_memberlist, /* tp_members */
682 0, /* tp_getset */
683 0, /* tp_base */
684 0, /* tp_dict */
685 0, /* tp_descr_get */
686 0, /* tp_descr_set */
687 0, /* tp_dictoffset */
688 0, /* tp_init */
689 0, /* tp_alloc */
690 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691};
692
Jeffrey Yasskin655d8352009-05-23 23:23:01 +0000693/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
694 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695*/
696
697int
698PyCode_Addr2Line(PyCodeObject *co, int addrq)
699{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000700 int size = PyString_Size(co->co_lnotab) / 2;
701 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
702 int line = co->co_firstlineno;
703 int addr = 0;
704 while (--size >= 0) {
705 addr += *p++;
706 if (addr > addrq)
707 break;
708 line += *p++;
709 }
710 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711}
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000712
Jeffrey Yasskin655d8352009-05-23 23:23:01 +0000713/* Update *bounds to describe the first and one-past-the-last instructions in
714 the same line as lasti. Return the number of that line. */
715int
716_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000717{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000718 int size, addr, line;
719 unsigned char* p;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000720
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000721 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
722 size = PyString_GET_SIZE(co->co_lnotab) / 2;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000723
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000724 addr = 0;
725 line = co->co_firstlineno;
726 assert(line > 0);
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000727
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000728 /* possible optimization: if f->f_lasti == instr_ub
729 (likely to be a common case) then we already know
730 instr_lb -- if we stored the matching value of p
Martin Panter65076572016-09-07 12:03:06 +0000731 somewhere we could skip the first while loop. */
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000732
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000733 /* See lnotab_notes.txt for the description of
734 co_lnotab. A point to remember: increments to p
735 come in (addr, line) pairs. */
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000736
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000737 bounds->ap_lower = 0;
738 while (size > 0) {
739 if (addr + *p > lasti)
740 break;
741 addr += *p++;
742 if (*p)
743 bounds->ap_lower = addr;
744 line += *p++;
745 --size;
746 }
747
748 if (size > 0) {
749 while (--size >= 0) {
750 addr += *p++;
751 if (*p++)
752 break;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000753 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000754 bounds->ap_upper = addr;
755 }
756 else {
757 bounds->ap_upper = INT_MAX;
758 }
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000759
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000760 return line;
Jeremy Hyltona4ebc132006-04-18 14:47:00 +0000761}