blob: 2f1bef2cd1c18614c877bef8204c52acc64bf587 [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];
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030014 static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
15 const unsigned char *s, *e;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020016
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030017 if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
18 !PyUnicode_IS_ASCII(o))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020019 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 if (ok_name_char[*name_chars] == 0) {
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030022 const unsigned char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 for (p = name_chars; *p; p++)
24 ok_name_char[*p] = 1;
25 }
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030026 s = PyUnicode_1BYTE_DATA(o);
27 e = s + PyUnicode_GET_LENGTH(o);
28 while (s != e) {
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
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030049/* Intern selected string constants */
50static int
51intern_string_constants(PyObject *tuple)
52{
53 int modified = 0;
54 Py_ssize_t i;
55
56 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
57 PyObject *v = PyTuple_GET_ITEM(tuple, i);
58 if (PyUnicode_CheckExact(v)) {
59 if (all_name_chars(v)) {
60 PyObject *w = v;
61 PyUnicode_InternInPlace(&v);
62 if (w != v) {
63 PyTuple_SET_ITEM(tuple, i, v);
64 modified = 1;
65 }
66 }
67 }
68 else if (PyTuple_CheckExact(v)) {
69 intern_string_constants(v);
70 }
71 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050072 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030073 PyObject *tmp = PySequence_Tuple(v);
74 if (tmp == NULL) {
75 PyErr_Clear();
76 continue;
77 }
78 if (intern_string_constants(tmp)) {
79 v = PyFrozenSet_New(tmp);
80 if (v == NULL) {
81 PyErr_Clear();
82 }
83 else {
84 PyTuple_SET_ITEM(tuple, i, v);
Yury Selivanovd2fd3592016-11-09 09:42:14 -050085 Py_DECREF(w);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030086 modified = 1;
87 }
88 }
89 Py_DECREF(tmp);
90 }
91 }
92 return modified;
93}
94
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095
96PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +000097PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 int nlocals, int stacksize, int flags,
99 PyObject *code, PyObject *consts, PyObject *names,
100 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
101 PyObject *filename, PyObject *name, int firstlineno,
102 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PyCodeObject *co;
Benjamin Peterson90037602011-06-25 22:54:45 -0500105 unsigned char *cell2arg = NULL;
106 Py_ssize_t i, n_cellvars;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 /* Check argument types */
109 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
110 code == NULL ||
111 consts == NULL || !PyTuple_Check(consts) ||
112 names == NULL || !PyTuple_Check(names) ||
113 varnames == NULL || !PyTuple_Check(varnames) ||
114 freevars == NULL || !PyTuple_Check(freevars) ||
115 cellvars == NULL || !PyTuple_Check(cellvars) ||
116 name == NULL || !PyUnicode_Check(name) ||
117 filename == NULL || !PyUnicode_Check(filename) ||
118 lnotab == NULL || !PyBytes_Check(lnotab) ||
119 !PyObject_CheckReadBuffer(code)) {
120 PyErr_BadInternalCall();
121 return NULL;
122 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200123
124 /* Ensure that the filename is a ready Unicode string */
125 if (PyUnicode_READY(filename) < 0)
126 return NULL;
127
Benjamin Peterson90037602011-06-25 22:54:45 -0500128 n_cellvars = PyTuple_GET_SIZE(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 intern_strings(names);
130 intern_strings(varnames);
131 intern_strings(freevars);
132 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300133 intern_string_constants(consts);
Benjamin Peterson90037602011-06-25 22:54:45 -0500134 /* Create mapping between cells and arguments if needed. */
135 if (n_cellvars) {
136 Py_ssize_t total_args = argcount + kwonlyargcount +
137 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
138 Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
139 int used_cell2arg = 0;
140 cell2arg = PyMem_MALLOC(alloc_size);
141 if (cell2arg == NULL)
142 return NULL;
143 memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
144 /* Find cells which are also arguments. */
145 for (i = 0; i < n_cellvars; i++) {
146 Py_ssize_t j;
147 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
148 for (j = 0; j < total_args; j++) {
149 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
150 if (!PyUnicode_Compare(cell, arg)) {
151 cell2arg[i] = j;
152 used_cell2arg = 1;
153 break;
154 }
155 }
156 }
157 if (!used_cell2arg) {
158 PyMem_FREE(cell2arg);
159 cell2arg = NULL;
160 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500162 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
163 if (co == NULL) {
164 if (cell2arg)
165 PyMem_FREE(cell2arg);
166 return NULL;
167 }
168 co->co_argcount = argcount;
169 co->co_kwonlyargcount = kwonlyargcount;
170 co->co_nlocals = nlocals;
171 co->co_stacksize = stacksize;
172 co->co_flags = flags;
173 Py_INCREF(code);
174 co->co_code = code;
175 Py_INCREF(consts);
176 co->co_consts = consts;
177 Py_INCREF(names);
178 co->co_names = names;
179 Py_INCREF(varnames);
180 co->co_varnames = varnames;
181 Py_INCREF(freevars);
182 co->co_freevars = freevars;
183 Py_INCREF(cellvars);
184 co->co_cellvars = cellvars;
185 co->co_cell2arg = cell2arg;
186 Py_INCREF(filename);
187 co->co_filename = filename;
188 Py_INCREF(name);
189 co->co_name = name;
190 co->co_firstlineno = firstlineno;
191 Py_INCREF(lnotab);
192 co->co_lnotab = lnotab;
193 co->co_zombieframe = NULL;
194 co->co_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196}
197
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000198PyCodeObject *
199PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 static PyObject *emptystring = NULL;
202 static PyObject *nulltuple = NULL;
203 PyObject *filename_ob = NULL;
204 PyObject *funcname_ob = NULL;
205 PyCodeObject *result = NULL;
206 if (emptystring == NULL) {
207 emptystring = PyBytes_FromString("");
208 if (emptystring == NULL)
209 goto failed;
210 }
211 if (nulltuple == NULL) {
212 nulltuple = PyTuple_New(0);
213 if (nulltuple == NULL)
214 goto failed;
215 }
216 funcname_ob = PyUnicode_FromString(funcname);
217 if (funcname_ob == NULL)
218 goto failed;
219 filename_ob = PyUnicode_DecodeFSDefault(filename);
220 if (filename_ob == NULL)
221 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 result = PyCode_New(0, /* argcount */
224 0, /* kwonlyargcount */
225 0, /* nlocals */
226 0, /* stacksize */
227 0, /* flags */
228 emptystring, /* code */
229 nulltuple, /* consts */
230 nulltuple, /* names */
231 nulltuple, /* varnames */
232 nulltuple, /* freevars */
233 nulltuple, /* cellvars */
234 filename_ob, /* filename */
235 funcname_ob, /* name */
236 firstlineno, /* firstlineno */
237 emptystring /* lnotab */
238 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000239
240failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 Py_XDECREF(funcname_ob);
242 Py_XDECREF(filename_ob);
243 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000244}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245
246#define OFF(x) offsetof(PyCodeObject, x)
247
248static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
250 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
251 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
252 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
253 {"co_flags", T_INT, OFF(co_flags), READONLY},
254 {"co_code", T_OBJECT, OFF(co_code), READONLY},
255 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
256 {"co_names", T_OBJECT, OFF(co_names), READONLY},
257 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
258 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
259 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
260 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
261 {"co_name", T_OBJECT, OFF(co_name), READONLY},
262 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
263 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
264 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265};
266
267/* Helper for code_new: return a shallow copy of a tuple that is
268 guaranteed to contain exact strings, by converting string subclasses
269 to exact strings and complaining if a non-string is found. */
270static PyObject*
271validate_and_copy_tuple(PyObject *tup)
272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 PyObject *newtuple;
274 PyObject *item;
275 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 len = PyTuple_GET_SIZE(tup);
278 newtuple = PyTuple_New(len);
279 if (newtuple == NULL)
280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 for (i = 0; i < len; i++) {
283 item = PyTuple_GET_ITEM(tup, i);
284 if (PyUnicode_CheckExact(item)) {
285 Py_INCREF(item);
286 }
287 else if (!PyUnicode_Check(item)) {
288 PyErr_Format(
289 PyExc_TypeError,
290 "name tuples must contain only "
291 "strings, not '%.500s'",
292 item->ob_type->tp_name);
293 Py_DECREF(newtuple);
294 return NULL;
295 }
296 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100297 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 if (item == NULL) {
299 Py_DECREF(newtuple);
300 return NULL;
301 }
302 }
303 PyTuple_SET_ITEM(newtuple, i, item);
304 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307}
308
309PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000310"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000311 constants, names, varnames, filename, name, firstlineno,\n\
312 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313\n\
314Create a code object. Not for the faint of heart.");
315
316static PyObject *
317code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 int argcount;
320 int kwonlyargcount;
321 int nlocals;
322 int stacksize;
323 int flags;
324 PyObject *co = NULL;
325 PyObject *code;
326 PyObject *consts;
327 PyObject *names, *ournames = NULL;
328 PyObject *varnames, *ourvarnames = NULL;
329 PyObject *freevars = NULL, *ourfreevars = NULL;
330 PyObject *cellvars = NULL, *ourcellvars = NULL;
331 PyObject *filename;
332 PyObject *name;
333 int firstlineno;
334 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
337 &argcount, &kwonlyargcount,
338 &nlocals, &stacksize, &flags,
339 &code,
340 &PyTuple_Type, &consts,
341 &PyTuple_Type, &names,
342 &PyTuple_Type, &varnames,
343 &filename, &name,
344 &firstlineno, &lnotab,
345 &PyTuple_Type, &freevars,
346 &PyTuple_Type, &cellvars))
347 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 if (argcount < 0) {
350 PyErr_SetString(
351 PyExc_ValueError,
352 "code: argcount must not be negative");
353 goto cleanup;
354 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (kwonlyargcount < 0) {
357 PyErr_SetString(
358 PyExc_ValueError,
359 "code: kwonlyargcount must not be negative");
360 goto cleanup;
361 }
362 if (nlocals < 0) {
363 PyErr_SetString(
364 PyExc_ValueError,
365 "code: nlocals must not be negative");
366 goto cleanup;
367 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 ournames = validate_and_copy_tuple(names);
370 if (ournames == NULL)
371 goto cleanup;
372 ourvarnames = validate_and_copy_tuple(varnames);
373 if (ourvarnames == NULL)
374 goto cleanup;
375 if (freevars)
376 ourfreevars = validate_and_copy_tuple(freevars);
377 else
378 ourfreevars = PyTuple_New(0);
379 if (ourfreevars == NULL)
380 goto cleanup;
381 if (cellvars)
382 ourcellvars = validate_and_copy_tuple(cellvars);
383 else
384 ourcellvars = PyTuple_New(0);
385 if (ourcellvars == NULL)
386 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
389 nlocals, stacksize, flags,
390 code, consts, ournames, ourvarnames,
391 ourfreevars, ourcellvars, filename,
392 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 Py_XDECREF(ournames);
395 Py_XDECREF(ourvarnames);
396 Py_XDECREF(ourfreevars);
397 Py_XDECREF(ourcellvars);
398 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399}
400
401static void
402code_dealloc(PyCodeObject *co)
403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 Py_XDECREF(co->co_code);
405 Py_XDECREF(co->co_consts);
406 Py_XDECREF(co->co_names);
407 Py_XDECREF(co->co_varnames);
408 Py_XDECREF(co->co_freevars);
409 Py_XDECREF(co->co_cellvars);
410 Py_XDECREF(co->co_filename);
411 Py_XDECREF(co->co_name);
412 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500413 if (co->co_cell2arg != NULL)
414 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (co->co_zombieframe != NULL)
416 PyObject_GC_Del(co->co_zombieframe);
417 if (co->co_weakreflist != NULL)
418 PyObject_ClearWeakRefs((PyObject*)co);
419 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420}
421
422static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200423code_sizeof(PyCodeObject *co, void *unused)
424{
425 Py_ssize_t res;
426
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200427 res = _PyObject_SIZE(Py_TYPE(co));
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200428 if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
429 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char);
430 return PyLong_FromSsize_t(res);
431}
432
433static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434code_repr(PyCodeObject *co)
435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 int lineno;
437 if (co->co_firstlineno != 0)
438 lineno = co->co_firstlineno;
439 else
440 lineno = -1;
441 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
442 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000443 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 co->co_name, co, co->co_filename, lineno);
445 } else {
446 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000447 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 co->co_name, co, lineno);
449 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450}
451
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100452PyObject*
453_PyCode_ConstantKey(PyObject *op)
454{
455 PyObject *key;
456
457 /* Py_None and Py_Ellipsis are singleton */
458 if (op == Py_None || op == Py_Ellipsis
459 || PyLong_CheckExact(op)
460 || PyBool_Check(op)
461 || PyBytes_CheckExact(op)
462 || PyUnicode_CheckExact(op)
463 /* code_richcompare() uses _PyCode_ConstantKey() internally */
464 || PyCode_Check(op)) {
465 key = PyTuple_Pack(2, Py_TYPE(op), op);
466 }
467 else if (PyFloat_CheckExact(op)) {
468 double d = PyFloat_AS_DOUBLE(op);
469 /* all we need is to make the tuple different in either the 0.0
470 * or -0.0 case from all others, just to avoid the "coercion".
471 */
472 if (d == 0.0 && copysign(1.0, d) < 0.0)
473 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
474 else
475 key = PyTuple_Pack(2, Py_TYPE(op), op);
476 }
477 else if (PyComplex_CheckExact(op)) {
478 Py_complex z;
479 int real_negzero, imag_negzero;
480 /* For the complex case we must make complex(x, 0.)
481 different from complex(x, -0.) and complex(0., y)
482 different from complex(-0., y), for any x and y.
483 All four complex zeros must be distinguished.*/
484 z = PyComplex_AsCComplex(op);
485 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
486 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
487 /* use True, False and None singleton as tags for the real and imag
488 * sign, to make tuples different */
489 if (real_negzero && imag_negzero) {
490 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
491 }
492 else if (imag_negzero) {
493 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
494 }
495 else if (real_negzero) {
496 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
497 }
498 else {
499 key = PyTuple_Pack(2, Py_TYPE(op), op);
500 }
501 }
502 else if (PyTuple_CheckExact(op)) {
503 Py_ssize_t i, len;
504 PyObject *tuple;
505
506 len = PyTuple_GET_SIZE(op);
507 tuple = PyTuple_New(len);
508 if (tuple == NULL)
509 return NULL;
510
511 for (i=0; i < len; i++) {
512 PyObject *item, *item_key;
513
514 item = PyTuple_GET_ITEM(op, i);
515 item_key = _PyCode_ConstantKey(item);
516 if (item_key == NULL) {
517 Py_DECREF(tuple);
518 return NULL;
519 }
520
521 PyTuple_SET_ITEM(tuple, i, item_key);
522 }
523
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200524 key = PyTuple_Pack(2, tuple, op);
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100525 Py_DECREF(tuple);
526 }
527 else if (PyFrozenSet_CheckExact(op)) {
528 Py_ssize_t pos = 0;
529 PyObject *item;
530 Py_hash_t hash;
531 Py_ssize_t i, len;
532 PyObject *tuple, *set;
533
534 len = PySet_GET_SIZE(op);
535 tuple = PyTuple_New(len);
536 if (tuple == NULL)
537 return NULL;
538
539 i = 0;
540 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
541 PyObject *item_key;
542
543 item_key = _PyCode_ConstantKey(item);
544 if (item_key == NULL) {
545 Py_DECREF(tuple);
546 return NULL;
547 }
548
549 assert(i < len);
550 PyTuple_SET_ITEM(tuple, i, item_key);
551 i++;
552 }
553 set = PyFrozenSet_New(tuple);
554 Py_DECREF(tuple);
555 if (set == NULL)
556 return NULL;
557
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200558 key = PyTuple_Pack(2, set, op);
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100559 Py_DECREF(set);
560 return key;
561 }
562 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000563 /* for other types, use the object identifier as a unique identifier
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100564 * to ensure that they are seen as unequal. */
565 PyObject *obj_id = PyLong_FromVoidPtr(op);
566 if (obj_id == NULL)
567 return NULL;
568
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200569 key = PyTuple_Pack(2, obj_id, op);
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100570 Py_DECREF(obj_id);
571 }
572 return key;
573}
574
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000575static PyObject *
576code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyCodeObject *co, *cp;
579 int eq;
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100580 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if ((op != Py_EQ && op != Py_NE) ||
584 !PyCode_Check(self) ||
585 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500586 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 co = (PyCodeObject *)self;
590 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
593 if (eq <= 0) goto unequal;
594 eq = co->co_argcount == cp->co_argcount;
595 if (!eq) goto unequal;
596 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
597 if (!eq) goto unequal;
598 eq = co->co_nlocals == cp->co_nlocals;
599 if (!eq) goto unequal;
600 eq = co->co_flags == cp->co_flags;
601 if (!eq) goto unequal;
602 eq = co->co_firstlineno == cp->co_firstlineno;
603 if (!eq) goto unequal;
604 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
605 if (eq <= 0) goto unequal;
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100606
607 /* compare constants */
608 consts1 = _PyCode_ConstantKey(co->co_consts);
609 if (!consts1)
610 return NULL;
611 consts2 = _PyCode_ConstantKey(cp->co_consts);
612 if (!consts2) {
613 Py_DECREF(consts1);
614 return NULL;
615 }
616 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
617 Py_DECREF(consts1);
618 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (eq <= 0) goto unequal;
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
622 if (eq <= 0) goto unequal;
623 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
624 if (eq <= 0) goto unequal;
625 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
626 if (eq <= 0) goto unequal;
627 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
628 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 if (op == Py_EQ)
631 res = Py_True;
632 else
633 res = Py_False;
634 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000635
636 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (eq < 0)
638 return NULL;
639 if (op == Py_NE)
640 res = Py_True;
641 else
642 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000643
644 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 Py_INCREF(res);
646 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647}
648
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000649static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650code_hash(PyCodeObject *co)
651{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000652 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 h0 = PyObject_Hash(co->co_name);
654 if (h0 == -1) return -1;
655 h1 = PyObject_Hash(co->co_code);
656 if (h1 == -1) return -1;
657 h2 = PyObject_Hash(co->co_consts);
658 if (h2 == -1) return -1;
659 h3 = PyObject_Hash(co->co_names);
660 if (h3 == -1) return -1;
661 h4 = PyObject_Hash(co->co_varnames);
662 if (h4 == -1) return -1;
663 h5 = PyObject_Hash(co->co_freevars);
664 if (h5 == -1) return -1;
665 h6 = PyObject_Hash(co->co_cellvars);
666 if (h6 == -1) return -1;
667 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
668 co->co_argcount ^ co->co_kwonlyargcount ^
669 co->co_nlocals ^ co->co_flags;
670 if (h == -1) h = -2;
671 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672}
673
674/* XXX code objects need to participate in GC? */
675
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200676static struct PyMethodDef code_methods[] = {
677 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
678 {NULL, NULL} /* sentinel */
679};
680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 PyVarObject_HEAD_INIT(&PyType_Type, 0)
683 "code",
684 sizeof(PyCodeObject),
685 0,
686 (destructor)code_dealloc, /* tp_dealloc */
687 0, /* tp_print */
688 0, /* tp_getattr */
689 0, /* tp_setattr */
690 0, /* tp_reserved */
691 (reprfunc)code_repr, /* tp_repr */
692 0, /* tp_as_number */
693 0, /* tp_as_sequence */
694 0, /* tp_as_mapping */
695 (hashfunc)code_hash, /* tp_hash */
696 0, /* tp_call */
697 0, /* tp_str */
698 PyObject_GenericGetAttr, /* tp_getattro */
699 0, /* tp_setattro */
700 0, /* tp_as_buffer */
701 Py_TPFLAGS_DEFAULT, /* tp_flags */
702 code_doc, /* tp_doc */
703 0, /* tp_traverse */
704 0, /* tp_clear */
705 code_richcompare, /* tp_richcompare */
706 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
707 0, /* tp_iter */
708 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200709 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 code_memberlist, /* tp_members */
711 0, /* tp_getset */
712 0, /* tp_base */
713 0, /* tp_dict */
714 0, /* tp_descr_get */
715 0, /* tp_descr_set */
716 0, /* tp_dictoffset */
717 0, /* tp_init */
718 0, /* tp_alloc */
719 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720};
721
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000722/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
723 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724*/
725
726int
727PyCode_Addr2Line(PyCodeObject *co, int addrq)
728{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000729 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
731 int line = co->co_firstlineno;
732 int addr = 0;
733 while (--size >= 0) {
734 addr += *p++;
735 if (addr > addrq)
736 break;
737 line += *p++;
738 }
739 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000742/* Update *bounds to describe the first and one-past-the-last instructions in
743 the same line as lasti. Return the number of that line. */
744int
745_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000746{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000747 Py_ssize_t size;
748 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
752 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 addr = 0;
755 line = co->co_firstlineno;
756 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 /* possible optimization: if f->f_lasti == instr_ub
759 (likely to be a common case) then we already know
760 instr_lb -- if we stored the matching value of p
Martin Panter0be894b2016-09-07 12:03:06 +0000761 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 /* See lnotab_notes.txt for the description of
764 co_lnotab. A point to remember: increments to p
765 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 bounds->ap_lower = 0;
768 while (size > 0) {
769 if (addr + *p > lasti)
770 break;
771 addr += *p++;
772 if (*p)
773 bounds->ap_lower = addr;
774 line += *p++;
775 --size;
776 }
777
778 if (size > 0) {
779 while (--size >= 0) {
780 addr += *p++;
781 if (*p++)
782 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000783 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 bounds->ap_upper = addr;
785 }
786 else {
787 bounds->ap_upper = INT_MAX;
788 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791}