blob: 964ae62146ba2b7ea3f563271509547fa38578d0 [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 Pitrouf95a1b32010-05-09 15:52:27 +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
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013 static char ok_name_char[256];
14 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020015 PyUnicodeObject *u = (PyUnicodeObject *)o;
16 const unsigned char *s;
17
18 if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 ||
19 PyUnicode_MAX_CHAR_VALUE(u) >= 128)
20 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 if (ok_name_char[*name_chars] == 0) {
23 unsigned char *p;
24 for (p = name_chars; *p; p++)
25 ok_name_char[*p] = 1;
26 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020027 s = PyUnicode_1BYTE_DATA(u);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 while (*s) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 if (ok_name_char[*s++] == 0)
30 return 0;
31 }
32 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033}
34
35static void
36intern_strings(PyObject *tuple)
37{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
41 PyObject *v = PyTuple_GET_ITEM(tuple, i);
42 if (v == NULL || !PyUnicode_CheckExact(v)) {
43 Py_FatalError("non-string found in code slot");
44 }
45 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
46 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047}
48
49
50PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +000051PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 int nlocals, int stacksize, int flags,
53 PyObject *code, PyObject *consts, PyObject *names,
54 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
55 PyObject *filename, PyObject *name, int firstlineno,
56 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 PyCodeObject *co;
Benjamin Peterson90037602011-06-25 22:54:45 -050059 unsigned char *cell2arg = NULL;
60 Py_ssize_t i, n_cellvars;
Guido van Rossum00bc0e02007-10-15 02:52:41 +000061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 /* Check argument types */
63 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
64 code == NULL ||
65 consts == NULL || !PyTuple_Check(consts) ||
66 names == NULL || !PyTuple_Check(names) ||
67 varnames == NULL || !PyTuple_Check(varnames) ||
68 freevars == NULL || !PyTuple_Check(freevars) ||
69 cellvars == NULL || !PyTuple_Check(cellvars) ||
70 name == NULL || !PyUnicode_Check(name) ||
71 filename == NULL || !PyUnicode_Check(filename) ||
72 lnotab == NULL || !PyBytes_Check(lnotab) ||
73 !PyObject_CheckReadBuffer(code)) {
74 PyErr_BadInternalCall();
75 return NULL;
76 }
Victor Stinner7c74de42013-10-10 15:55:14 +020077
78 /* Ensure that the filename is a ready Unicode string */
79 if (PyUnicode_READY(filename) < 0)
80 return NULL;
81
Benjamin Peterson90037602011-06-25 22:54:45 -050082 n_cellvars = PyTuple_GET_SIZE(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 intern_strings(names);
84 intern_strings(varnames);
85 intern_strings(freevars);
86 intern_strings(cellvars);
87 /* Intern selected string constants */
Benjamin Peterson90037602011-06-25 22:54:45 -050088 for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 PyObject *v = PyTuple_GetItem(consts, i);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020090 if (!all_name_chars(v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 continue;
92 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
93 }
Benjamin Peterson90037602011-06-25 22:54:45 -050094 /* Create mapping between cells and arguments if needed. */
95 if (n_cellvars) {
96 Py_ssize_t total_args = argcount + kwonlyargcount +
97 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
98 Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
99 int used_cell2arg = 0;
100 cell2arg = PyMem_MALLOC(alloc_size);
101 if (cell2arg == NULL)
102 return NULL;
103 memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
104 /* Find cells which are also arguments. */
105 for (i = 0; i < n_cellvars; i++) {
106 Py_ssize_t j;
107 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
108 for (j = 0; j < total_args; j++) {
109 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
110 if (!PyUnicode_Compare(cell, arg)) {
111 cell2arg[i] = j;
112 used_cell2arg = 1;
113 break;
114 }
115 }
116 }
117 if (!used_cell2arg) {
118 PyMem_FREE(cell2arg);
119 cell2arg = NULL;
120 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500122 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
123 if (co == NULL) {
124 if (cell2arg)
125 PyMem_FREE(cell2arg);
126 return NULL;
127 }
128 co->co_argcount = argcount;
129 co->co_kwonlyargcount = kwonlyargcount;
130 co->co_nlocals = nlocals;
131 co->co_stacksize = stacksize;
132 co->co_flags = flags;
133 Py_INCREF(code);
134 co->co_code = code;
135 Py_INCREF(consts);
136 co->co_consts = consts;
137 Py_INCREF(names);
138 co->co_names = names;
139 Py_INCREF(varnames);
140 co->co_varnames = varnames;
141 Py_INCREF(freevars);
142 co->co_freevars = freevars;
143 Py_INCREF(cellvars);
144 co->co_cellvars = cellvars;
145 co->co_cell2arg = cell2arg;
146 Py_INCREF(filename);
147 co->co_filename = filename;
148 Py_INCREF(name);
149 co->co_name = name;
150 co->co_firstlineno = firstlineno;
151 Py_INCREF(lnotab);
152 co->co_lnotab = lnotab;
153 co->co_zombieframe = NULL;
154 co->co_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000156}
157
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000158PyCodeObject *
159PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 static PyObject *emptystring = NULL;
162 static PyObject *nulltuple = NULL;
163 PyObject *filename_ob = NULL;
164 PyObject *funcname_ob = NULL;
165 PyCodeObject *result = NULL;
166 if (emptystring == NULL) {
167 emptystring = PyBytes_FromString("");
168 if (emptystring == NULL)
169 goto failed;
170 }
171 if (nulltuple == NULL) {
172 nulltuple = PyTuple_New(0);
173 if (nulltuple == NULL)
174 goto failed;
175 }
176 funcname_ob = PyUnicode_FromString(funcname);
177 if (funcname_ob == NULL)
178 goto failed;
179 filename_ob = PyUnicode_DecodeFSDefault(filename);
180 if (filename_ob == NULL)
181 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 result = PyCode_New(0, /* argcount */
184 0, /* kwonlyargcount */
185 0, /* nlocals */
186 0, /* stacksize */
187 0, /* flags */
188 emptystring, /* code */
189 nulltuple, /* consts */
190 nulltuple, /* names */
191 nulltuple, /* varnames */
192 nulltuple, /* freevars */
193 nulltuple, /* cellvars */
194 filename_ob, /* filename */
195 funcname_ob, /* name */
196 firstlineno, /* firstlineno */
197 emptystring /* lnotab */
198 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000199
200failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 Py_XDECREF(funcname_ob);
202 Py_XDECREF(filename_ob);
203 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000204}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
206#define OFF(x) offsetof(PyCodeObject, x)
207
208static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
210 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
211 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
212 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
213 {"co_flags", T_INT, OFF(co_flags), READONLY},
214 {"co_code", T_OBJECT, OFF(co_code), READONLY},
215 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
216 {"co_names", T_OBJECT, OFF(co_names), READONLY},
217 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
218 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
219 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
220 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
221 {"co_name", T_OBJECT, OFF(co_name), READONLY},
222 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
223 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
224 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225};
226
227/* Helper for code_new: return a shallow copy of a tuple that is
228 guaranteed to contain exact strings, by converting string subclasses
229 to exact strings and complaining if a non-string is found. */
230static PyObject*
231validate_and_copy_tuple(PyObject *tup)
232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 PyObject *newtuple;
234 PyObject *item;
235 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 len = PyTuple_GET_SIZE(tup);
238 newtuple = PyTuple_New(len);
239 if (newtuple == NULL)
240 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 for (i = 0; i < len; i++) {
243 item = PyTuple_GET_ITEM(tup, i);
244 if (PyUnicode_CheckExact(item)) {
245 Py_INCREF(item);
246 }
247 else if (!PyUnicode_Check(item)) {
248 PyErr_Format(
249 PyExc_TypeError,
250 "name tuples must contain only "
251 "strings, not '%.500s'",
252 item->ob_type->tp_name);
253 Py_DECREF(newtuple);
254 return NULL;
255 }
256 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100257 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 if (item == NULL) {
259 Py_DECREF(newtuple);
260 return NULL;
261 }
262 }
263 PyTuple_SET_ITEM(newtuple, i, item);
264 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267}
268
269PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000270"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000271 constants, names, varnames, filename, name, firstlineno,\n\
272 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273\n\
274Create a code object. Not for the faint of heart.");
275
276static PyObject *
277code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 int argcount;
280 int kwonlyargcount;
281 int nlocals;
282 int stacksize;
283 int flags;
284 PyObject *co = NULL;
285 PyObject *code;
286 PyObject *consts;
287 PyObject *names, *ournames = NULL;
288 PyObject *varnames, *ourvarnames = NULL;
289 PyObject *freevars = NULL, *ourfreevars = NULL;
290 PyObject *cellvars = NULL, *ourcellvars = NULL;
291 PyObject *filename;
292 PyObject *name;
293 int firstlineno;
294 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
297 &argcount, &kwonlyargcount,
298 &nlocals, &stacksize, &flags,
299 &code,
300 &PyTuple_Type, &consts,
301 &PyTuple_Type, &names,
302 &PyTuple_Type, &varnames,
303 &filename, &name,
304 &firstlineno, &lnotab,
305 &PyTuple_Type, &freevars,
306 &PyTuple_Type, &cellvars))
307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (argcount < 0) {
310 PyErr_SetString(
311 PyExc_ValueError,
312 "code: argcount must not be negative");
313 goto cleanup;
314 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 if (kwonlyargcount < 0) {
317 PyErr_SetString(
318 PyExc_ValueError,
319 "code: kwonlyargcount must not be negative");
320 goto cleanup;
321 }
322 if (nlocals < 0) {
323 PyErr_SetString(
324 PyExc_ValueError,
325 "code: nlocals must not be negative");
326 goto cleanup;
327 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 ournames = validate_and_copy_tuple(names);
330 if (ournames == NULL)
331 goto cleanup;
332 ourvarnames = validate_and_copy_tuple(varnames);
333 if (ourvarnames == NULL)
334 goto cleanup;
335 if (freevars)
336 ourfreevars = validate_and_copy_tuple(freevars);
337 else
338 ourfreevars = PyTuple_New(0);
339 if (ourfreevars == NULL)
340 goto cleanup;
341 if (cellvars)
342 ourcellvars = validate_and_copy_tuple(cellvars);
343 else
344 ourcellvars = PyTuple_New(0);
345 if (ourcellvars == NULL)
346 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
349 nlocals, stacksize, flags,
350 code, consts, ournames, ourvarnames,
351 ourfreevars, ourcellvars, filename,
352 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 Py_XDECREF(ournames);
355 Py_XDECREF(ourvarnames);
356 Py_XDECREF(ourfreevars);
357 Py_XDECREF(ourcellvars);
358 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359}
360
361static void
362code_dealloc(PyCodeObject *co)
363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 Py_XDECREF(co->co_code);
365 Py_XDECREF(co->co_consts);
366 Py_XDECREF(co->co_names);
367 Py_XDECREF(co->co_varnames);
368 Py_XDECREF(co->co_freevars);
369 Py_XDECREF(co->co_cellvars);
370 Py_XDECREF(co->co_filename);
371 Py_XDECREF(co->co_name);
372 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500373 if (co->co_cell2arg != NULL)
374 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 if (co->co_zombieframe != NULL)
376 PyObject_GC_Del(co->co_zombieframe);
377 if (co->co_weakreflist != NULL)
378 PyObject_ClearWeakRefs((PyObject*)co);
379 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380}
381
382static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200383code_sizeof(PyCodeObject *co, void *unused)
384{
385 Py_ssize_t res;
386
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200387 res = _PyObject_SIZE(Py_TYPE(co));
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200388 if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
389 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char);
390 return PyLong_FromSsize_t(res);
391}
392
393static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394code_repr(PyCodeObject *co)
395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 int lineno;
397 if (co->co_firstlineno != 0)
398 lineno = co->co_firstlineno;
399 else
400 lineno = -1;
401 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
402 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000403 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 co->co_name, co, co->co_filename, lineno);
405 } else {
406 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000407 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 co->co_name, co, lineno);
409 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410}
411
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100412PyObject*
413_PyCode_ConstantKey(PyObject *op)
414{
415 PyObject *key;
416
417 /* Py_None and Py_Ellipsis are singleton */
418 if (op == Py_None || op == Py_Ellipsis
419 || PyLong_CheckExact(op)
420 || PyBool_Check(op)
421 || PyBytes_CheckExact(op)
422 || PyUnicode_CheckExact(op)
423 /* code_richcompare() uses _PyCode_ConstantKey() internally */
424 || PyCode_Check(op)) {
425 key = PyTuple_Pack(2, Py_TYPE(op), op);
426 }
427 else if (PyFloat_CheckExact(op)) {
428 double d = PyFloat_AS_DOUBLE(op);
429 /* all we need is to make the tuple different in either the 0.0
430 * or -0.0 case from all others, just to avoid the "coercion".
431 */
432 if (d == 0.0 && copysign(1.0, d) < 0.0)
433 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
434 else
435 key = PyTuple_Pack(2, Py_TYPE(op), op);
436 }
437 else if (PyComplex_CheckExact(op)) {
438 Py_complex z;
439 int real_negzero, imag_negzero;
440 /* For the complex case we must make complex(x, 0.)
441 different from complex(x, -0.) and complex(0., y)
442 different from complex(-0., y), for any x and y.
443 All four complex zeros must be distinguished.*/
444 z = PyComplex_AsCComplex(op);
445 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
446 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
447 /* use True, False and None singleton as tags for the real and imag
448 * sign, to make tuples different */
449 if (real_negzero && imag_negzero) {
450 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
451 }
452 else if (imag_negzero) {
453 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
454 }
455 else if (real_negzero) {
456 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
457 }
458 else {
459 key = PyTuple_Pack(2, Py_TYPE(op), op);
460 }
461 }
462 else if (PyTuple_CheckExact(op)) {
463 Py_ssize_t i, len;
464 PyObject *tuple;
465
466 len = PyTuple_GET_SIZE(op);
467 tuple = PyTuple_New(len);
468 if (tuple == NULL)
469 return NULL;
470
471 for (i=0; i < len; i++) {
472 PyObject *item, *item_key;
473
474 item = PyTuple_GET_ITEM(op, i);
475 item_key = _PyCode_ConstantKey(item);
476 if (item_key == NULL) {
477 Py_DECREF(tuple);
478 return NULL;
479 }
480
481 PyTuple_SET_ITEM(tuple, i, item_key);
482 }
483
484 key = PyTuple_Pack(3, Py_TYPE(op), op, tuple);
485 Py_DECREF(tuple);
486 }
487 else if (PyFrozenSet_CheckExact(op)) {
488 Py_ssize_t pos = 0;
489 PyObject *item;
490 Py_hash_t hash;
491 Py_ssize_t i, len;
492 PyObject *tuple, *set;
493
494 len = PySet_GET_SIZE(op);
495 tuple = PyTuple_New(len);
496 if (tuple == NULL)
497 return NULL;
498
499 i = 0;
500 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
501 PyObject *item_key;
502
503 item_key = _PyCode_ConstantKey(item);
504 if (item_key == NULL) {
505 Py_DECREF(tuple);
506 return NULL;
507 }
508
509 assert(i < len);
510 PyTuple_SET_ITEM(tuple, i, item_key);
511 i++;
512 }
513 set = PyFrozenSet_New(tuple);
514 Py_DECREF(tuple);
515 if (set == NULL)
516 return NULL;
517
518 key = PyTuple_Pack(3, Py_TYPE(op), op, set);
519 Py_DECREF(set);
520 return key;
521 }
522 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000523 /* for other types, use the object identifier as a unique identifier
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100524 * to ensure that they are seen as unequal. */
525 PyObject *obj_id = PyLong_FromVoidPtr(op);
526 if (obj_id == NULL)
527 return NULL;
528
529 key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id);
530 Py_DECREF(obj_id);
531 }
532 return key;
533}
534
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000535static PyObject *
536code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 PyCodeObject *co, *cp;
539 int eq;
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100540 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 if ((op != Py_EQ && op != Py_NE) ||
544 !PyCode_Check(self) ||
545 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500546 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 co = (PyCodeObject *)self;
550 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
553 if (eq <= 0) goto unequal;
554 eq = co->co_argcount == cp->co_argcount;
555 if (!eq) goto unequal;
556 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
557 if (!eq) goto unequal;
558 eq = co->co_nlocals == cp->co_nlocals;
559 if (!eq) goto unequal;
560 eq = co->co_flags == cp->co_flags;
561 if (!eq) goto unequal;
562 eq = co->co_firstlineno == cp->co_firstlineno;
563 if (!eq) goto unequal;
564 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
565 if (eq <= 0) goto unequal;
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100566
567 /* compare constants */
568 consts1 = _PyCode_ConstantKey(co->co_consts);
569 if (!consts1)
570 return NULL;
571 consts2 = _PyCode_ConstantKey(cp->co_consts);
572 if (!consts2) {
573 Py_DECREF(consts1);
574 return NULL;
575 }
576 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
577 Py_DECREF(consts1);
578 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (eq <= 0) goto unequal;
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
582 if (eq <= 0) goto unequal;
583 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
584 if (eq <= 0) goto unequal;
585 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
586 if (eq <= 0) goto unequal;
587 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
588 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 if (op == Py_EQ)
591 res = Py_True;
592 else
593 res = Py_False;
594 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000595
596 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 if (eq < 0)
598 return NULL;
599 if (op == Py_NE)
600 res = Py_True;
601 else
602 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000603
604 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 Py_INCREF(res);
606 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607}
608
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000609static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610code_hash(PyCodeObject *co)
611{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000612 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 h0 = PyObject_Hash(co->co_name);
614 if (h0 == -1) return -1;
615 h1 = PyObject_Hash(co->co_code);
616 if (h1 == -1) return -1;
617 h2 = PyObject_Hash(co->co_consts);
618 if (h2 == -1) return -1;
619 h3 = PyObject_Hash(co->co_names);
620 if (h3 == -1) return -1;
621 h4 = PyObject_Hash(co->co_varnames);
622 if (h4 == -1) return -1;
623 h5 = PyObject_Hash(co->co_freevars);
624 if (h5 == -1) return -1;
625 h6 = PyObject_Hash(co->co_cellvars);
626 if (h6 == -1) return -1;
627 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
628 co->co_argcount ^ co->co_kwonlyargcount ^
629 co->co_nlocals ^ co->co_flags;
630 if (h == -1) h = -2;
631 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632}
633
634/* XXX code objects need to participate in GC? */
635
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200636static struct PyMethodDef code_methods[] = {
637 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
638 {NULL, NULL} /* sentinel */
639};
640
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 PyVarObject_HEAD_INIT(&PyType_Type, 0)
643 "code",
644 sizeof(PyCodeObject),
645 0,
646 (destructor)code_dealloc, /* tp_dealloc */
647 0, /* tp_print */
648 0, /* tp_getattr */
649 0, /* tp_setattr */
650 0, /* tp_reserved */
651 (reprfunc)code_repr, /* tp_repr */
652 0, /* tp_as_number */
653 0, /* tp_as_sequence */
654 0, /* tp_as_mapping */
655 (hashfunc)code_hash, /* tp_hash */
656 0, /* tp_call */
657 0, /* tp_str */
658 PyObject_GenericGetAttr, /* tp_getattro */
659 0, /* tp_setattro */
660 0, /* tp_as_buffer */
661 Py_TPFLAGS_DEFAULT, /* tp_flags */
662 code_doc, /* tp_doc */
663 0, /* tp_traverse */
664 0, /* tp_clear */
665 code_richcompare, /* tp_richcompare */
666 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
667 0, /* tp_iter */
668 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200669 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 code_memberlist, /* tp_members */
671 0, /* tp_getset */
672 0, /* tp_base */
673 0, /* tp_dict */
674 0, /* tp_descr_get */
675 0, /* tp_descr_set */
676 0, /* tp_dictoffset */
677 0, /* tp_init */
678 0, /* tp_alloc */
679 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680};
681
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000682/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
683 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684*/
685
686int
687PyCode_Addr2Line(PyCodeObject *co, int addrq)
688{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000689 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
691 int line = co->co_firstlineno;
692 int addr = 0;
693 while (--size >= 0) {
694 addr += *p++;
695 if (addr > addrq)
696 break;
697 line += *p++;
698 }
699 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000701
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000702/* Update *bounds to describe the first and one-past-the-last instructions in
703 the same line as lasti. Return the number of that line. */
704int
705_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000706{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000707 Py_ssize_t size;
708 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
712 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 addr = 0;
715 line = co->co_firstlineno;
716 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 /* possible optimization: if f->f_lasti == instr_ub
719 (likely to be a common case) then we already know
720 instr_lb -- if we stored the matching value of p
721 somwhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 /* See lnotab_notes.txt for the description of
724 co_lnotab. A point to remember: increments to p
725 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 bounds->ap_lower = 0;
728 while (size > 0) {
729 if (addr + *p > lasti)
730 break;
731 addr += *p++;
732 if (*p)
733 bounds->ap_lower = addr;
734 line += *p++;
735 --size;
736 }
737
738 if (size > 0) {
739 while (--size >= 0) {
740 addr += *p++;
741 if (*p++)
742 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000743 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 bounds->ap_upper = addr;
745 }
746 else {
747 bounds->ap_upper = INT_MAX;
748 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751}