blob: d514ec1590485f9ee3369fc68b9ee50bb8f7e622 [file] [log] [blame]
Benjamin Peterson1bf494b2016-09-07 11:28:35 -07001#include <stdbool.h>
2
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003#include "Python.h"
4#include "code.h"
5#include "structmember.h"
6
7#define NAME_CHARS \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009
Brett Cannond0600ed2016-09-07 14:30:39 -070010/* Holder for co_extra information */
11typedef struct {
12 Py_ssize_t ce_size;
13 void **ce_extras;
14} _PyCodeObjectExtra;
15
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000016/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
17
18static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020019all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 static char ok_name_char[256];
22 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020023 PyUnicodeObject *u = (PyUnicodeObject *)o;
24 const unsigned char *s;
25
26 if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 ||
27 PyUnicode_MAX_CHAR_VALUE(u) >= 128)
28 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 if (ok_name_char[*name_chars] == 0) {
31 unsigned char *p;
32 for (p = name_chars; *p; p++)
33 ok_name_char[*p] = 1;
34 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020035 s = PyUnicode_1BYTE_DATA(u);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 while (*s) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 if (ok_name_char[*s++] == 0)
38 return 0;
39 }
40 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041}
42
43static void
44intern_strings(PyObject *tuple)
45{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
49 PyObject *v = PyTuple_GET_ITEM(tuple, i);
50 if (v == NULL || !PyUnicode_CheckExact(v)) {
51 Py_FatalError("non-string found in code slot");
52 }
53 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
54 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000055}
56
57
58PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +000059PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 int nlocals, int stacksize, int flags,
61 PyObject *code, PyObject *consts, PyObject *names,
62 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
63 PyObject *filename, PyObject *name, int firstlineno,
64 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 PyCodeObject *co;
Benjamin Peterson90037602011-06-25 22:54:45 -050067 unsigned char *cell2arg = NULL;
68 Py_ssize_t i, n_cellvars;
Guido van Rossum00bc0e02007-10-15 02:52:41 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 /* Check argument types */
71 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
72 code == NULL ||
73 consts == NULL || !PyTuple_Check(consts) ||
74 names == NULL || !PyTuple_Check(names) ||
75 varnames == NULL || !PyTuple_Check(varnames) ||
76 freevars == NULL || !PyTuple_Check(freevars) ||
77 cellvars == NULL || !PyTuple_Check(cellvars) ||
78 name == NULL || !PyUnicode_Check(name) ||
79 filename == NULL || !PyUnicode_Check(filename) ||
80 lnotab == NULL || !PyBytes_Check(lnotab) ||
81 !PyObject_CheckReadBuffer(code)) {
82 PyErr_BadInternalCall();
83 return NULL;
84 }
Victor Stinner7c74de42013-10-10 15:55:14 +020085
86 /* Ensure that the filename is a ready Unicode string */
87 if (PyUnicode_READY(filename) < 0)
88 return NULL;
89
Benjamin Peterson90037602011-06-25 22:54:45 -050090 n_cellvars = PyTuple_GET_SIZE(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 intern_strings(names);
92 intern_strings(varnames);
93 intern_strings(freevars);
94 intern_strings(cellvars);
95 /* Intern selected string constants */
Benjamin Peterson90037602011-06-25 22:54:45 -050096 for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 PyObject *v = PyTuple_GetItem(consts, i);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020098 if (!all_name_chars(v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 continue;
100 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
101 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500102 /* Create mapping between cells and arguments if needed. */
103 if (n_cellvars) {
104 Py_ssize_t total_args = argcount + kwonlyargcount +
105 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
106 Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700107 bool used_cell2arg = false;
Benjamin Peterson90037602011-06-25 22:54:45 -0500108 cell2arg = PyMem_MALLOC(alloc_size);
109 if (cell2arg == NULL)
110 return NULL;
111 memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
112 /* Find cells which are also arguments. */
113 for (i = 0; i < n_cellvars; i++) {
114 Py_ssize_t j;
115 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
116 for (j = 0; j < total_args; j++) {
117 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
118 if (!PyUnicode_Compare(cell, arg)) {
119 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700120 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500121 break;
122 }
123 }
124 }
125 if (!used_cell2arg) {
126 PyMem_FREE(cell2arg);
127 cell2arg = NULL;
128 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500130 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
131 if (co == NULL) {
132 if (cell2arg)
133 PyMem_FREE(cell2arg);
134 return NULL;
135 }
136 co->co_argcount = argcount;
137 co->co_kwonlyargcount = kwonlyargcount;
138 co->co_nlocals = nlocals;
139 co->co_stacksize = stacksize;
140 co->co_flags = flags;
141 Py_INCREF(code);
142 co->co_code = code;
143 Py_INCREF(consts);
144 co->co_consts = consts;
145 Py_INCREF(names);
146 co->co_names = names;
147 Py_INCREF(varnames);
148 co->co_varnames = varnames;
149 Py_INCREF(freevars);
150 co->co_freevars = freevars;
151 Py_INCREF(cellvars);
152 co->co_cellvars = cellvars;
153 co->co_cell2arg = cell2arg;
154 Py_INCREF(filename);
155 co->co_filename = filename;
156 Py_INCREF(name);
157 co->co_name = name;
158 co->co_firstlineno = firstlineno;
159 Py_INCREF(lnotab);
160 co->co_lnotab = lnotab;
161 co->co_zombieframe = NULL;
162 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700163 co->co_extra = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165}
166
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000167PyCodeObject *
168PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 static PyObject *emptystring = NULL;
171 static PyObject *nulltuple = NULL;
172 PyObject *filename_ob = NULL;
173 PyObject *funcname_ob = NULL;
174 PyCodeObject *result = NULL;
175 if (emptystring == NULL) {
176 emptystring = PyBytes_FromString("");
177 if (emptystring == NULL)
178 goto failed;
179 }
180 if (nulltuple == NULL) {
181 nulltuple = PyTuple_New(0);
182 if (nulltuple == NULL)
183 goto failed;
184 }
185 funcname_ob = PyUnicode_FromString(funcname);
186 if (funcname_ob == NULL)
187 goto failed;
188 filename_ob = PyUnicode_DecodeFSDefault(filename);
189 if (filename_ob == NULL)
190 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 result = PyCode_New(0, /* argcount */
193 0, /* kwonlyargcount */
194 0, /* nlocals */
195 0, /* stacksize */
196 0, /* flags */
197 emptystring, /* code */
198 nulltuple, /* consts */
199 nulltuple, /* names */
200 nulltuple, /* varnames */
201 nulltuple, /* freevars */
202 nulltuple, /* cellvars */
203 filename_ob, /* filename */
204 funcname_ob, /* name */
205 firstlineno, /* firstlineno */
206 emptystring /* lnotab */
207 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000208
209failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 Py_XDECREF(funcname_ob);
211 Py_XDECREF(filename_ob);
212 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000213}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214
215#define OFF(x) offsetof(PyCodeObject, x)
216
217static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
219 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
220 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
221 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
222 {"co_flags", T_INT, OFF(co_flags), READONLY},
223 {"co_code", T_OBJECT, OFF(co_code), READONLY},
224 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
225 {"co_names", T_OBJECT, OFF(co_names), READONLY},
226 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
227 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
228 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
229 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
230 {"co_name", T_OBJECT, OFF(co_name), READONLY},
231 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
232 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
233 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234};
235
236/* Helper for code_new: return a shallow copy of a tuple that is
237 guaranteed to contain exact strings, by converting string subclasses
238 to exact strings and complaining if a non-string is found. */
239static PyObject*
240validate_and_copy_tuple(PyObject *tup)
241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 PyObject *newtuple;
243 PyObject *item;
244 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 len = PyTuple_GET_SIZE(tup);
247 newtuple = PyTuple_New(len);
248 if (newtuple == NULL)
249 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 for (i = 0; i < len; i++) {
252 item = PyTuple_GET_ITEM(tup, i);
253 if (PyUnicode_CheckExact(item)) {
254 Py_INCREF(item);
255 }
256 else if (!PyUnicode_Check(item)) {
257 PyErr_Format(
258 PyExc_TypeError,
259 "name tuples must contain only "
260 "strings, not '%.500s'",
261 item->ob_type->tp_name);
262 Py_DECREF(newtuple);
263 return NULL;
264 }
265 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100266 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (item == NULL) {
268 Py_DECREF(newtuple);
269 return NULL;
270 }
271 }
272 PyTuple_SET_ITEM(newtuple, i, item);
273 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276}
277
278PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000279"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000280 constants, names, varnames, filename, name, firstlineno,\n\
281 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282\n\
283Create a code object. Not for the faint of heart.");
284
285static PyObject *
286code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 int argcount;
289 int kwonlyargcount;
290 int nlocals;
291 int stacksize;
292 int flags;
293 PyObject *co = NULL;
294 PyObject *code;
295 PyObject *consts;
296 PyObject *names, *ournames = NULL;
297 PyObject *varnames, *ourvarnames = NULL;
298 PyObject *freevars = NULL, *ourfreevars = NULL;
299 PyObject *cellvars = NULL, *ourcellvars = NULL;
300 PyObject *filename;
301 PyObject *name;
302 int firstlineno;
303 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
306 &argcount, &kwonlyargcount,
307 &nlocals, &stacksize, &flags,
308 &code,
309 &PyTuple_Type, &consts,
310 &PyTuple_Type, &names,
311 &PyTuple_Type, &varnames,
312 &filename, &name,
313 &firstlineno, &lnotab,
314 &PyTuple_Type, &freevars,
315 &PyTuple_Type, &cellvars))
316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 if (argcount < 0) {
319 PyErr_SetString(
320 PyExc_ValueError,
321 "code: argcount must not be negative");
322 goto cleanup;
323 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (kwonlyargcount < 0) {
326 PyErr_SetString(
327 PyExc_ValueError,
328 "code: kwonlyargcount must not be negative");
329 goto cleanup;
330 }
331 if (nlocals < 0) {
332 PyErr_SetString(
333 PyExc_ValueError,
334 "code: nlocals must not be negative");
335 goto cleanup;
336 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 ournames = validate_and_copy_tuple(names);
339 if (ournames == NULL)
340 goto cleanup;
341 ourvarnames = validate_and_copy_tuple(varnames);
342 if (ourvarnames == NULL)
343 goto cleanup;
344 if (freevars)
345 ourfreevars = validate_and_copy_tuple(freevars);
346 else
347 ourfreevars = PyTuple_New(0);
348 if (ourfreevars == NULL)
349 goto cleanup;
350 if (cellvars)
351 ourcellvars = validate_and_copy_tuple(cellvars);
352 else
353 ourcellvars = PyTuple_New(0);
354 if (ourcellvars == NULL)
355 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
358 nlocals, stacksize, flags,
359 code, consts, ournames, ourvarnames,
360 ourfreevars, ourcellvars, filename,
361 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 Py_XDECREF(ournames);
364 Py_XDECREF(ourvarnames);
365 Py_XDECREF(ourfreevars);
366 Py_XDECREF(ourcellvars);
367 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368}
369
370static void
371code_dealloc(PyCodeObject *co)
372{
Brett Cannon5c4de282016-09-07 11:16:41 -0700373 if (co->co_extra != NULL) {
374 PyThreadState *tstate = PyThreadState_Get();
Brett Cannond0600ed2016-09-07 14:30:39 -0700375 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700376
Brett Cannond0600ed2016-09-07 14:30:39 -0700377 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700378 freefunc free_extra = tstate->co_extra_freefuncs[i];
379
380 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700381 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700382 }
383 }
384
385 PyMem_FREE(co->co_extra);
386 }
387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 Py_XDECREF(co->co_code);
389 Py_XDECREF(co->co_consts);
390 Py_XDECREF(co->co_names);
391 Py_XDECREF(co->co_varnames);
392 Py_XDECREF(co->co_freevars);
393 Py_XDECREF(co->co_cellvars);
394 Py_XDECREF(co->co_filename);
395 Py_XDECREF(co->co_name);
396 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500397 if (co->co_cell2arg != NULL)
398 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (co->co_zombieframe != NULL)
400 PyObject_GC_Del(co->co_zombieframe);
401 if (co->co_weakreflist != NULL)
402 PyObject_ClearWeakRefs((PyObject*)co);
403 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404}
405
406static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200407code_sizeof(PyCodeObject *co, void *unused)
408{
409 Py_ssize_t res;
410
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200411 res = _PyObject_SIZE(Py_TYPE(co));
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200412 if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
413 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char);
414 return PyLong_FromSsize_t(res);
415}
416
417static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418code_repr(PyCodeObject *co)
419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 int lineno;
421 if (co->co_firstlineno != 0)
422 lineno = co->co_firstlineno;
423 else
424 lineno = -1;
425 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
426 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000427 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 co->co_name, co, co->co_filename, lineno);
429 } else {
430 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000431 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 co->co_name, co, lineno);
433 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434}
435
Victor Stinnerefb24132016-01-22 12:33:12 +0100436PyObject*
437_PyCode_ConstantKey(PyObject *op)
438{
439 PyObject *key;
440
441 /* Py_None and Py_Ellipsis are singleton */
442 if (op == Py_None || op == Py_Ellipsis
443 || PyLong_CheckExact(op)
444 || PyBool_Check(op)
445 || PyBytes_CheckExact(op)
446 || PyUnicode_CheckExact(op)
447 /* code_richcompare() uses _PyCode_ConstantKey() internally */
448 || PyCode_Check(op)) {
449 key = PyTuple_Pack(2, Py_TYPE(op), op);
450 }
451 else if (PyFloat_CheckExact(op)) {
452 double d = PyFloat_AS_DOUBLE(op);
453 /* all we need is to make the tuple different in either the 0.0
454 * or -0.0 case from all others, just to avoid the "coercion".
455 */
456 if (d == 0.0 && copysign(1.0, d) < 0.0)
457 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
458 else
459 key = PyTuple_Pack(2, Py_TYPE(op), op);
460 }
461 else if (PyComplex_CheckExact(op)) {
462 Py_complex z;
463 int real_negzero, imag_negzero;
464 /* For the complex case we must make complex(x, 0.)
465 different from complex(x, -0.) and complex(0., y)
466 different from complex(-0., y), for any x and y.
467 All four complex zeros must be distinguished.*/
468 z = PyComplex_AsCComplex(op);
469 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
470 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
471 /* use True, False and None singleton as tags for the real and imag
472 * sign, to make tuples different */
473 if (real_negzero && imag_negzero) {
474 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
475 }
476 else if (imag_negzero) {
477 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
478 }
479 else if (real_negzero) {
480 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
481 }
482 else {
483 key = PyTuple_Pack(2, Py_TYPE(op), op);
484 }
485 }
486 else if (PyTuple_CheckExact(op)) {
487 Py_ssize_t i, len;
488 PyObject *tuple;
489
490 len = PyTuple_GET_SIZE(op);
491 tuple = PyTuple_New(len);
492 if (tuple == NULL)
493 return NULL;
494
495 for (i=0; i < len; i++) {
496 PyObject *item, *item_key;
497
498 item = PyTuple_GET_ITEM(op, i);
499 item_key = _PyCode_ConstantKey(item);
500 if (item_key == NULL) {
501 Py_DECREF(tuple);
502 return NULL;
503 }
504
505 PyTuple_SET_ITEM(tuple, i, item_key);
506 }
507
508 key = PyTuple_Pack(3, Py_TYPE(op), op, tuple);
509 Py_DECREF(tuple);
510 }
511 else if (PyFrozenSet_CheckExact(op)) {
512 Py_ssize_t pos = 0;
513 PyObject *item;
514 Py_hash_t hash;
515 Py_ssize_t i, len;
516 PyObject *tuple, *set;
517
518 len = PySet_GET_SIZE(op);
519 tuple = PyTuple_New(len);
520 if (tuple == NULL)
521 return NULL;
522
523 i = 0;
524 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
525 PyObject *item_key;
526
527 item_key = _PyCode_ConstantKey(item);
528 if (item_key == NULL) {
529 Py_DECREF(tuple);
530 return NULL;
531 }
532
533 assert(i < len);
534 PyTuple_SET_ITEM(tuple, i, item_key);
535 i++;
536 }
537 set = PyFrozenSet_New(tuple);
538 Py_DECREF(tuple);
539 if (set == NULL)
540 return NULL;
541
542 key = PyTuple_Pack(3, Py_TYPE(op), op, set);
543 Py_DECREF(set);
544 return key;
545 }
546 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000547 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100548 * to ensure that they are seen as unequal. */
549 PyObject *obj_id = PyLong_FromVoidPtr(op);
550 if (obj_id == NULL)
551 return NULL;
552
553 key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id);
554 Py_DECREF(obj_id);
555 }
556 return key;
557}
558
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000559static PyObject *
560code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 PyCodeObject *co, *cp;
563 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100564 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if ((op != Py_EQ && op != Py_NE) ||
568 !PyCode_Check(self) ||
569 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500570 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 co = (PyCodeObject *)self;
574 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
577 if (eq <= 0) goto unequal;
578 eq = co->co_argcount == cp->co_argcount;
579 if (!eq) goto unequal;
580 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
581 if (!eq) goto unequal;
582 eq = co->co_nlocals == cp->co_nlocals;
583 if (!eq) goto unequal;
584 eq = co->co_flags == cp->co_flags;
585 if (!eq) goto unequal;
586 eq = co->co_firstlineno == cp->co_firstlineno;
587 if (!eq) goto unequal;
588 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
589 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100590
591 /* compare constants */
592 consts1 = _PyCode_ConstantKey(co->co_consts);
593 if (!consts1)
594 return NULL;
595 consts2 = _PyCode_ConstantKey(cp->co_consts);
596 if (!consts2) {
597 Py_DECREF(consts1);
598 return NULL;
599 }
600 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
601 Py_DECREF(consts1);
602 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
606 if (eq <= 0) goto unequal;
607 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
608 if (eq <= 0) goto unequal;
609 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
610 if (eq <= 0) goto unequal;
611 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
612 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 if (op == Py_EQ)
615 res = Py_True;
616 else
617 res = Py_False;
618 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000619
620 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (eq < 0)
622 return NULL;
623 if (op == Py_NE)
624 res = Py_True;
625 else
626 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000627
628 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 Py_INCREF(res);
630 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631}
632
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000633static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634code_hash(PyCodeObject *co)
635{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000636 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 h0 = PyObject_Hash(co->co_name);
638 if (h0 == -1) return -1;
639 h1 = PyObject_Hash(co->co_code);
640 if (h1 == -1) return -1;
641 h2 = PyObject_Hash(co->co_consts);
642 if (h2 == -1) return -1;
643 h3 = PyObject_Hash(co->co_names);
644 if (h3 == -1) return -1;
645 h4 = PyObject_Hash(co->co_varnames);
646 if (h4 == -1) return -1;
647 h5 = PyObject_Hash(co->co_freevars);
648 if (h5 == -1) return -1;
649 h6 = PyObject_Hash(co->co_cellvars);
650 if (h6 == -1) return -1;
651 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
652 co->co_argcount ^ co->co_kwonlyargcount ^
653 co->co_nlocals ^ co->co_flags;
654 if (h == -1) h = -2;
655 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656}
657
658/* XXX code objects need to participate in GC? */
659
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200660static struct PyMethodDef code_methods[] = {
661 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
662 {NULL, NULL} /* sentinel */
663};
664
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 PyVarObject_HEAD_INIT(&PyType_Type, 0)
667 "code",
668 sizeof(PyCodeObject),
669 0,
670 (destructor)code_dealloc, /* tp_dealloc */
671 0, /* tp_print */
672 0, /* tp_getattr */
673 0, /* tp_setattr */
674 0, /* tp_reserved */
675 (reprfunc)code_repr, /* tp_repr */
676 0, /* tp_as_number */
677 0, /* tp_as_sequence */
678 0, /* tp_as_mapping */
679 (hashfunc)code_hash, /* tp_hash */
680 0, /* tp_call */
681 0, /* tp_str */
682 PyObject_GenericGetAttr, /* tp_getattro */
683 0, /* tp_setattro */
684 0, /* tp_as_buffer */
685 Py_TPFLAGS_DEFAULT, /* tp_flags */
686 code_doc, /* tp_doc */
687 0, /* tp_traverse */
688 0, /* tp_clear */
689 code_richcompare, /* tp_richcompare */
690 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
691 0, /* tp_iter */
692 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200693 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 code_memberlist, /* tp_members */
695 0, /* tp_getset */
696 0, /* tp_base */
697 0, /* tp_dict */
698 0, /* tp_descr_get */
699 0, /* tp_descr_set */
700 0, /* tp_dictoffset */
701 0, /* tp_init */
702 0, /* tp_alloc */
703 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704};
705
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000706/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
707 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708*/
709
710int
711PyCode_Addr2Line(PyCodeObject *co, int addrq)
712{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000713 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
715 int line = co->co_firstlineno;
716 int addr = 0;
717 while (--size >= 0) {
718 addr += *p++;
719 if (addr > addrq)
720 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100721 line += (signed char)*p;
722 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 }
724 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000726
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000727/* Update *bounds to describe the first and one-past-the-last instructions in
728 the same line as lasti. Return the number of that line. */
729int
730_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000731{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000732 Py_ssize_t size;
733 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
737 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 addr = 0;
740 line = co->co_firstlineno;
741 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 /* possible optimization: if f->f_lasti == instr_ub
744 (likely to be a common case) then we already know
745 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700746 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 /* See lnotab_notes.txt for the description of
749 co_lnotab. A point to remember: increments to p
750 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 bounds->ap_lower = 0;
753 while (size > 0) {
754 if (addr + *p > lasti)
755 break;
756 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100757 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100759 line += (signed char)*p;
760 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 --size;
762 }
763
764 if (size > 0) {
765 while (--size >= 0) {
766 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100767 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100769 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000770 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 bounds->ap_upper = addr;
772 }
773 else {
774 bounds->ap_upper = INT_MAX;
775 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000778}
Brett Cannon5c4de282016-09-07 11:16:41 -0700779
780
781int
782_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
783{
Brett Cannon5c4de282016-09-07 11:16:41 -0700784 assert(*extra == NULL);
785
786 if (!PyCode_Check(code)) {
787 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700788 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700789 }
790
Brett Cannond0600ed2016-09-07 14:30:39 -0700791 PyCodeObject *o = (PyCodeObject*) code;
792 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700793
Brett Cannond0600ed2016-09-07 14:30:39 -0700794
795 if (co_extra == NULL || co_extra->ce_size <= index) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700796 return 0;
797 }
798
Brett Cannond0600ed2016-09-07 14:30:39 -0700799 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700800 return 0;
801}
802
803
804int
805_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
806{
Brett Cannon5c4de282016-09-07 11:16:41 -0700807 PyThreadState *tstate = PyThreadState_Get();
808
809 if (!PyCode_Check(code) || index < 0 ||
810 index >= tstate->co_extra_user_count) {
811 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700812 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700813 }
814
Brett Cannond0600ed2016-09-07 14:30:39 -0700815 PyCodeObject *o = (PyCodeObject*) code;
816 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700817
Brett Cannond0600ed2016-09-07 14:30:39 -0700818 if (co_extra == NULL) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700819 o->co_extra = (_PyCodeObjectExtra*) PyMem_Malloc(
820 sizeof(_PyCodeObjectExtra));
821 if (o->co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700822 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700823 }
Brett Cannond0600ed2016-09-07 14:30:39 -0700824 co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700825
Brett Cannond0600ed2016-09-07 14:30:39 -0700826 co_extra->ce_extras = PyMem_Malloc(
Brett Cannon5c4de282016-09-07 11:16:41 -0700827 tstate->co_extra_user_count * sizeof(void*));
Brett Cannond0600ed2016-09-07 14:30:39 -0700828 if (co_extra->ce_extras == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700829 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700830 }
831
Brett Cannond0600ed2016-09-07 14:30:39 -0700832 co_extra->ce_size = tstate->co_extra_user_count;
Brett Cannon5c4de282016-09-07 11:16:41 -0700833
Brett Cannond0600ed2016-09-07 14:30:39 -0700834 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
835 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700836 }
837 }
Brett Cannond0600ed2016-09-07 14:30:39 -0700838 else if (co_extra->ce_size <= index) {
839 co_extra->ce_extras = PyMem_Realloc(
840 co_extra->ce_extras, tstate->co_extra_user_count * sizeof(void*));
Brett Cannon5c4de282016-09-07 11:16:41 -0700841
Brett Cannond0600ed2016-09-07 14:30:39 -0700842 if (co_extra->ce_extras == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700843 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700844 }
845
Brett Cannond0600ed2016-09-07 14:30:39 -0700846 co_extra->ce_size = tstate->co_extra_user_count;
Brett Cannon5c4de282016-09-07 11:16:41 -0700847
Brett Cannond0600ed2016-09-07 14:30:39 -0700848 for (Py_ssize_t i = co_extra->ce_size; i < co_extra->ce_size; i++) {
849 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700850 }
851 }
852
Brett Cannond0600ed2016-09-07 14:30:39 -0700853 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700854 return 0;
855}