blob: eee9bfeaf7cf827f37afe276dc3340ccebfedf18 [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
Brett Cannond0600ed2016-09-07 14:30:39 -07007/* Holder for co_extra information */
8typedef struct {
9 Py_ssize_t ce_size;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +030010 void *ce_extras[1];
Brett Cannond0600ed2016-09-07 14:30:39 -070011} _PyCodeObjectExtra;
12
Benjamin Peterson8e0ad462017-09-07 23:35:53 -070013/* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020015all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000016{
Benjamin Peterson9020ac72017-09-07 18:06:23 -070017 /* [a-zA-Z0-9_] */
18 static const bool ok_name_char[128] = {
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
23 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
24 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
25 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
26 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
27 };
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030028 const unsigned char *s, *e;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020029
Benjamin Peterson9020ac72017-09-07 18:06:23 -070030 if (PyUnicode_READY(o) == -1 || !PyUnicode_IS_ASCII(o))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020031 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030033 s = PyUnicode_1BYTE_DATA(o);
34 e = s + PyUnicode_GET_LENGTH(o);
Benjamin Peterson9020ac72017-09-07 18:06:23 -070035 for (; s != e; s++) {
36 if (!ok_name_char[*s])
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 return 0;
38 }
39 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040}
41
42static void
43intern_strings(PyObject *tuple)
44{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
48 PyObject *v = PyTuple_GET_ITEM(tuple, i);
49 if (v == NULL || !PyUnicode_CheckExact(v)) {
50 Py_FatalError("non-string found in code slot");
51 }
52 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
53 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054}
55
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030056/* Intern selected string constants */
57static int
58intern_string_constants(PyObject *tuple)
59{
60 int modified = 0;
61 Py_ssize_t i;
62
63 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
64 PyObject *v = PyTuple_GET_ITEM(tuple, i);
65 if (PyUnicode_CheckExact(v)) {
66 if (all_name_chars(v)) {
67 PyObject *w = v;
68 PyUnicode_InternInPlace(&v);
69 if (w != v) {
70 PyTuple_SET_ITEM(tuple, i, v);
71 modified = 1;
72 }
73 }
74 }
75 else if (PyTuple_CheckExact(v)) {
76 intern_string_constants(v);
77 }
78 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050079 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030080 PyObject *tmp = PySequence_Tuple(v);
81 if (tmp == NULL) {
82 PyErr_Clear();
83 continue;
84 }
85 if (intern_string_constants(tmp)) {
86 v = PyFrozenSet_New(tmp);
87 if (v == NULL) {
88 PyErr_Clear();
89 }
90 else {
91 PyTuple_SET_ITEM(tuple, i, v);
Yury Selivanovd2fd3592016-11-09 09:42:14 -050092 Py_DECREF(w);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030093 modified = 1;
94 }
95 }
96 Py_DECREF(tmp);
97 }
98 }
99 return modified;
100}
101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102
103PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000104PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 int nlocals, int stacksize, int flags,
106 PyObject *code, PyObject *consts, PyObject *names,
107 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
108 PyObject *filename, PyObject *name, int firstlineno,
109 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200112 Py_ssize_t *cell2arg = NULL;
Benjamin Peterson90037602011-06-25 22:54:45 -0500113 Py_ssize_t i, n_cellvars;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 /* Check argument types */
116 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200117 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 consts == NULL || !PyTuple_Check(consts) ||
119 names == NULL || !PyTuple_Check(names) ||
120 varnames == NULL || !PyTuple_Check(varnames) ||
121 freevars == NULL || !PyTuple_Check(freevars) ||
122 cellvars == NULL || !PyTuple_Check(cellvars) ||
123 name == NULL || !PyUnicode_Check(name) ||
124 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200125 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 PyErr_BadInternalCall();
127 return NULL;
128 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200129
130 /* Ensure that the filename is a ready Unicode string */
131 if (PyUnicode_READY(filename) < 0)
132 return NULL;
133
Benjamin Peterson90037602011-06-25 22:54:45 -0500134 n_cellvars = PyTuple_GET_SIZE(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 intern_strings(names);
136 intern_strings(varnames);
137 intern_strings(freevars);
138 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300139 intern_string_constants(consts);
Benjamin Peterson90037602011-06-25 22:54:45 -0500140 /* Create mapping between cells and arguments if needed. */
141 if (n_cellvars) {
142 Py_ssize_t total_args = argcount + kwonlyargcount +
143 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700144 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200145 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
146 if (cell2arg == NULL) {
147 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500148 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200149 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500150 /* Find cells which are also arguments. */
151 for (i = 0; i < n_cellvars; i++) {
152 Py_ssize_t j;
153 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200154 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500155 for (j = 0; j < total_args; j++) {
156 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200157 int cmp = PyUnicode_Compare(cell, arg);
158 if (cmp == -1 && PyErr_Occurred()) {
159 PyMem_FREE(cell2arg);
160 return NULL;
161 }
162 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500163 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700164 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500165 break;
166 }
167 }
168 }
169 if (!used_cell2arg) {
170 PyMem_FREE(cell2arg);
171 cell2arg = NULL;
172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500174 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
175 if (co == NULL) {
176 if (cell2arg)
177 PyMem_FREE(cell2arg);
178 return NULL;
179 }
180 co->co_argcount = argcount;
181 co->co_kwonlyargcount = kwonlyargcount;
182 co->co_nlocals = nlocals;
183 co->co_stacksize = stacksize;
184 co->co_flags = flags;
185 Py_INCREF(code);
186 co->co_code = code;
187 Py_INCREF(consts);
188 co->co_consts = consts;
189 Py_INCREF(names);
190 co->co_names = names;
191 Py_INCREF(varnames);
192 co->co_varnames = varnames;
193 Py_INCREF(freevars);
194 co->co_freevars = freevars;
195 Py_INCREF(cellvars);
196 co->co_cellvars = cellvars;
197 co->co_cell2arg = cell2arg;
198 Py_INCREF(filename);
199 co->co_filename = filename;
200 Py_INCREF(name);
201 co->co_name = name;
202 co->co_firstlineno = firstlineno;
203 Py_INCREF(lnotab);
204 co->co_lnotab = lnotab;
205 co->co_zombieframe = NULL;
206 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700207 co->co_extra = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209}
210
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000211PyCodeObject *
212PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 static PyObject *emptystring = NULL;
215 static PyObject *nulltuple = NULL;
216 PyObject *filename_ob = NULL;
217 PyObject *funcname_ob = NULL;
218 PyCodeObject *result = NULL;
219 if (emptystring == NULL) {
220 emptystring = PyBytes_FromString("");
221 if (emptystring == NULL)
222 goto failed;
223 }
224 if (nulltuple == NULL) {
225 nulltuple = PyTuple_New(0);
226 if (nulltuple == NULL)
227 goto failed;
228 }
229 funcname_ob = PyUnicode_FromString(funcname);
230 if (funcname_ob == NULL)
231 goto failed;
232 filename_ob = PyUnicode_DecodeFSDefault(filename);
233 if (filename_ob == NULL)
234 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 result = PyCode_New(0, /* argcount */
237 0, /* kwonlyargcount */
238 0, /* nlocals */
239 0, /* stacksize */
240 0, /* flags */
241 emptystring, /* code */
242 nulltuple, /* consts */
243 nulltuple, /* names */
244 nulltuple, /* varnames */
245 nulltuple, /* freevars */
246 nulltuple, /* cellvars */
247 filename_ob, /* filename */
248 funcname_ob, /* name */
249 firstlineno, /* firstlineno */
250 emptystring /* lnotab */
251 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000252
253failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Py_XDECREF(funcname_ob);
255 Py_XDECREF(filename_ob);
256 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000257}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258
259#define OFF(x) offsetof(PyCodeObject, x)
260
261static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
263 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
264 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
265 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
266 {"co_flags", T_INT, OFF(co_flags), READONLY},
267 {"co_code", T_OBJECT, OFF(co_code), READONLY},
268 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
269 {"co_names", T_OBJECT, OFF(co_names), READONLY},
270 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
271 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
272 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
273 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
274 {"co_name", T_OBJECT, OFF(co_name), READONLY},
275 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
276 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
277 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278};
279
280/* Helper for code_new: return a shallow copy of a tuple that is
281 guaranteed to contain exact strings, by converting string subclasses
282 to exact strings and complaining if a non-string is found. */
283static PyObject*
284validate_and_copy_tuple(PyObject *tup)
285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 PyObject *newtuple;
287 PyObject *item;
288 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 len = PyTuple_GET_SIZE(tup);
291 newtuple = PyTuple_New(len);
292 if (newtuple == NULL)
293 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 for (i = 0; i < len; i++) {
296 item = PyTuple_GET_ITEM(tup, i);
297 if (PyUnicode_CheckExact(item)) {
298 Py_INCREF(item);
299 }
300 else if (!PyUnicode_Check(item)) {
301 PyErr_Format(
302 PyExc_TypeError,
303 "name tuples must contain only "
304 "strings, not '%.500s'",
305 item->ob_type->tp_name);
306 Py_DECREF(newtuple);
307 return NULL;
308 }
309 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100310 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (item == NULL) {
312 Py_DECREF(newtuple);
313 return NULL;
314 }
315 }
316 PyTuple_SET_ITEM(newtuple, i, item);
317 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320}
321
322PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000323"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000324 constants, names, varnames, filename, name, firstlineno,\n\
325 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000326\n\
327Create a code object. Not for the faint of heart.");
328
329static PyObject *
330code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 int argcount;
333 int kwonlyargcount;
334 int nlocals;
335 int stacksize;
336 int flags;
337 PyObject *co = NULL;
338 PyObject *code;
339 PyObject *consts;
340 PyObject *names, *ournames = NULL;
341 PyObject *varnames, *ourvarnames = NULL;
342 PyObject *freevars = NULL, *ourfreevars = NULL;
343 PyObject *cellvars = NULL, *ourcellvars = NULL;
344 PyObject *filename;
345 PyObject *name;
346 int firstlineno;
347 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
350 &argcount, &kwonlyargcount,
351 &nlocals, &stacksize, &flags,
352 &code,
353 &PyTuple_Type, &consts,
354 &PyTuple_Type, &names,
355 &PyTuple_Type, &varnames,
356 &filename, &name,
357 &firstlineno, &lnotab,
358 &PyTuple_Type, &freevars,
359 &PyTuple_Type, &cellvars))
360 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (argcount < 0) {
363 PyErr_SetString(
364 PyExc_ValueError,
365 "code: argcount must not be negative");
366 goto cleanup;
367 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (kwonlyargcount < 0) {
370 PyErr_SetString(
371 PyExc_ValueError,
372 "code: kwonlyargcount must not be negative");
373 goto cleanup;
374 }
375 if (nlocals < 0) {
376 PyErr_SetString(
377 PyExc_ValueError,
378 "code: nlocals must not be negative");
379 goto cleanup;
380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 ournames = validate_and_copy_tuple(names);
383 if (ournames == NULL)
384 goto cleanup;
385 ourvarnames = validate_and_copy_tuple(varnames);
386 if (ourvarnames == NULL)
387 goto cleanup;
388 if (freevars)
389 ourfreevars = validate_and_copy_tuple(freevars);
390 else
391 ourfreevars = PyTuple_New(0);
392 if (ourfreevars == NULL)
393 goto cleanup;
394 if (cellvars)
395 ourcellvars = validate_and_copy_tuple(cellvars);
396 else
397 ourcellvars = PyTuple_New(0);
398 if (ourcellvars == NULL)
399 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
402 nlocals, stacksize, flags,
403 code, consts, ournames, ourvarnames,
404 ourfreevars, ourcellvars, filename,
405 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000406 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 Py_XDECREF(ournames);
408 Py_XDECREF(ourvarnames);
409 Py_XDECREF(ourfreevars);
410 Py_XDECREF(ourcellvars);
411 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412}
413
414static void
415code_dealloc(PyCodeObject *co)
416{
Brett Cannon5c4de282016-09-07 11:16:41 -0700417 if (co->co_extra != NULL) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700418 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannond0600ed2016-09-07 14:30:39 -0700419 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700420
Brett Cannond0600ed2016-09-07 14:30:39 -0700421 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700422 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700423
424 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700425 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700426 }
427 }
428
Victor Stinner23e79442017-06-28 02:12:00 +0200429 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700430 }
431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 Py_XDECREF(co->co_code);
433 Py_XDECREF(co->co_consts);
434 Py_XDECREF(co->co_names);
435 Py_XDECREF(co->co_varnames);
436 Py_XDECREF(co->co_freevars);
437 Py_XDECREF(co->co_cellvars);
438 Py_XDECREF(co->co_filename);
439 Py_XDECREF(co->co_name);
440 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500441 if (co->co_cell2arg != NULL)
442 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (co->co_zombieframe != NULL)
444 PyObject_GC_Del(co->co_zombieframe);
445 if (co->co_weakreflist != NULL)
446 PyObject_ClearWeakRefs((PyObject*)co);
447 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000448}
449
450static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200451code_sizeof(PyCodeObject *co, void *unused)
452{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900453 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
454 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200455
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300456 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200457 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300458 }
459 if (co_extra != NULL) {
460 res += sizeof(_PyCodeObjectExtra) +
461 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
462 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200463 return PyLong_FromSsize_t(res);
464}
465
466static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467code_repr(PyCodeObject *co)
468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 int lineno;
470 if (co->co_firstlineno != 0)
471 lineno = co->co_firstlineno;
472 else
473 lineno = -1;
474 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
475 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000476 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 co->co_name, co, co->co_filename, lineno);
478 } else {
479 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000480 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 co->co_name, co, lineno);
482 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483}
484
Victor Stinnerefb24132016-01-22 12:33:12 +0100485PyObject*
486_PyCode_ConstantKey(PyObject *op)
487{
488 PyObject *key;
489
490 /* Py_None and Py_Ellipsis are singleton */
491 if (op == Py_None || op == Py_Ellipsis
492 || PyLong_CheckExact(op)
493 || PyBool_Check(op)
494 || PyBytes_CheckExact(op)
495 || PyUnicode_CheckExact(op)
496 /* code_richcompare() uses _PyCode_ConstantKey() internally */
497 || PyCode_Check(op)) {
498 key = PyTuple_Pack(2, Py_TYPE(op), op);
499 }
500 else if (PyFloat_CheckExact(op)) {
501 double d = PyFloat_AS_DOUBLE(op);
502 /* all we need is to make the tuple different in either the 0.0
503 * or -0.0 case from all others, just to avoid the "coercion".
504 */
505 if (d == 0.0 && copysign(1.0, d) < 0.0)
506 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
507 else
508 key = PyTuple_Pack(2, Py_TYPE(op), op);
509 }
510 else if (PyComplex_CheckExact(op)) {
511 Py_complex z;
512 int real_negzero, imag_negzero;
513 /* For the complex case we must make complex(x, 0.)
514 different from complex(x, -0.) and complex(0., y)
515 different from complex(-0., y), for any x and y.
516 All four complex zeros must be distinguished.*/
517 z = PyComplex_AsCComplex(op);
518 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
519 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
520 /* use True, False and None singleton as tags for the real and imag
521 * sign, to make tuples different */
522 if (real_negzero && imag_negzero) {
523 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
524 }
525 else if (imag_negzero) {
526 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
527 }
528 else if (real_negzero) {
529 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
530 }
531 else {
532 key = PyTuple_Pack(2, Py_TYPE(op), op);
533 }
534 }
535 else if (PyTuple_CheckExact(op)) {
536 Py_ssize_t i, len;
537 PyObject *tuple;
538
539 len = PyTuple_GET_SIZE(op);
540 tuple = PyTuple_New(len);
541 if (tuple == NULL)
542 return NULL;
543
544 for (i=0; i < len; i++) {
545 PyObject *item, *item_key;
546
547 item = PyTuple_GET_ITEM(op, i);
548 item_key = _PyCode_ConstantKey(item);
549 if (item_key == NULL) {
550 Py_DECREF(tuple);
551 return NULL;
552 }
553
554 PyTuple_SET_ITEM(tuple, i, item_key);
555 }
556
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200557 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100558 Py_DECREF(tuple);
559 }
560 else if (PyFrozenSet_CheckExact(op)) {
561 Py_ssize_t pos = 0;
562 PyObject *item;
563 Py_hash_t hash;
564 Py_ssize_t i, len;
565 PyObject *tuple, *set;
566
567 len = PySet_GET_SIZE(op);
568 tuple = PyTuple_New(len);
569 if (tuple == NULL)
570 return NULL;
571
572 i = 0;
573 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
574 PyObject *item_key;
575
576 item_key = _PyCode_ConstantKey(item);
577 if (item_key == NULL) {
578 Py_DECREF(tuple);
579 return NULL;
580 }
581
582 assert(i < len);
583 PyTuple_SET_ITEM(tuple, i, item_key);
584 i++;
585 }
586 set = PyFrozenSet_New(tuple);
587 Py_DECREF(tuple);
588 if (set == NULL)
589 return NULL;
590
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200591 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100592 Py_DECREF(set);
593 return key;
594 }
595 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000596 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100597 * to ensure that they are seen as unequal. */
598 PyObject *obj_id = PyLong_FromVoidPtr(op);
599 if (obj_id == NULL)
600 return NULL;
601
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200602 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100603 Py_DECREF(obj_id);
604 }
605 return key;
606}
607
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000608static PyObject *
609code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 PyCodeObject *co, *cp;
612 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100613 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 if ((op != Py_EQ && op != Py_NE) ||
617 !PyCode_Check(self) ||
618 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500619 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 co = (PyCodeObject *)self;
623 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
626 if (eq <= 0) goto unequal;
627 eq = co->co_argcount == cp->co_argcount;
628 if (!eq) goto unequal;
629 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
630 if (!eq) goto unequal;
631 eq = co->co_nlocals == cp->co_nlocals;
632 if (!eq) goto unequal;
633 eq = co->co_flags == cp->co_flags;
634 if (!eq) goto unequal;
635 eq = co->co_firstlineno == cp->co_firstlineno;
636 if (!eq) goto unequal;
637 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
638 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100639
640 /* compare constants */
641 consts1 = _PyCode_ConstantKey(co->co_consts);
642 if (!consts1)
643 return NULL;
644 consts2 = _PyCode_ConstantKey(cp->co_consts);
645 if (!consts2) {
646 Py_DECREF(consts1);
647 return NULL;
648 }
649 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
650 Py_DECREF(consts1);
651 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
655 if (eq <= 0) goto unequal;
656 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
657 if (eq <= 0) goto unequal;
658 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
659 if (eq <= 0) goto unequal;
660 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
661 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 if (op == Py_EQ)
664 res = Py_True;
665 else
666 res = Py_False;
667 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000668
669 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 if (eq < 0)
671 return NULL;
672 if (op == Py_NE)
673 res = Py_True;
674 else
675 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000676
677 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 Py_INCREF(res);
679 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680}
681
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000682static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683code_hash(PyCodeObject *co)
684{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000685 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 h0 = PyObject_Hash(co->co_name);
687 if (h0 == -1) return -1;
688 h1 = PyObject_Hash(co->co_code);
689 if (h1 == -1) return -1;
690 h2 = PyObject_Hash(co->co_consts);
691 if (h2 == -1) return -1;
692 h3 = PyObject_Hash(co->co_names);
693 if (h3 == -1) return -1;
694 h4 = PyObject_Hash(co->co_varnames);
695 if (h4 == -1) return -1;
696 h5 = PyObject_Hash(co->co_freevars);
697 if (h5 == -1) return -1;
698 h6 = PyObject_Hash(co->co_cellvars);
699 if (h6 == -1) return -1;
700 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
701 co->co_argcount ^ co->co_kwonlyargcount ^
702 co->co_nlocals ^ co->co_flags;
703 if (h == -1) h = -2;
704 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705}
706
707/* XXX code objects need to participate in GC? */
708
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200709static struct PyMethodDef code_methods[] = {
710 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
711 {NULL, NULL} /* sentinel */
712};
713
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyVarObject_HEAD_INIT(&PyType_Type, 0)
716 "code",
717 sizeof(PyCodeObject),
718 0,
719 (destructor)code_dealloc, /* tp_dealloc */
720 0, /* tp_print */
721 0, /* tp_getattr */
722 0, /* tp_setattr */
723 0, /* tp_reserved */
724 (reprfunc)code_repr, /* tp_repr */
725 0, /* tp_as_number */
726 0, /* tp_as_sequence */
727 0, /* tp_as_mapping */
728 (hashfunc)code_hash, /* tp_hash */
729 0, /* tp_call */
730 0, /* tp_str */
731 PyObject_GenericGetAttr, /* tp_getattro */
732 0, /* tp_setattro */
733 0, /* tp_as_buffer */
734 Py_TPFLAGS_DEFAULT, /* tp_flags */
735 code_doc, /* tp_doc */
736 0, /* tp_traverse */
737 0, /* tp_clear */
738 code_richcompare, /* tp_richcompare */
739 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
740 0, /* tp_iter */
741 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200742 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 code_memberlist, /* tp_members */
744 0, /* tp_getset */
745 0, /* tp_base */
746 0, /* tp_dict */
747 0, /* tp_descr_get */
748 0, /* tp_descr_set */
749 0, /* tp_dictoffset */
750 0, /* tp_init */
751 0, /* tp_alloc */
752 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753};
754
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000755/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
756 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757*/
758
759int
760PyCode_Addr2Line(PyCodeObject *co, int addrq)
761{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000762 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
764 int line = co->co_firstlineno;
765 int addr = 0;
766 while (--size >= 0) {
767 addr += *p++;
768 if (addr > addrq)
769 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100770 line += (signed char)*p;
771 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 }
773 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000775
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000776/* Update *bounds to describe the first and one-past-the-last instructions in
777 the same line as lasti. Return the number of that line. */
778int
779_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000781 Py_ssize_t size;
782 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
786 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 addr = 0;
789 line = co->co_firstlineno;
790 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 /* possible optimization: if f->f_lasti == instr_ub
793 (likely to be a common case) then we already know
794 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700795 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 /* See lnotab_notes.txt for the description of
798 co_lnotab. A point to remember: increments to p
799 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 bounds->ap_lower = 0;
802 while (size > 0) {
803 if (addr + *p > lasti)
804 break;
805 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100806 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100808 line += (signed char)*p;
809 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 --size;
811 }
812
813 if (size > 0) {
814 while (--size >= 0) {
815 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100816 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100818 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 bounds->ap_upper = addr;
821 }
822 else {
823 bounds->ap_upper = INT_MAX;
824 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827}
Brett Cannon5c4de282016-09-07 11:16:41 -0700828
829
830int
831_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
832{
Brett Cannon5c4de282016-09-07 11:16:41 -0700833 if (!PyCode_Check(code)) {
834 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700835 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700836 }
837
Brett Cannond0600ed2016-09-07 14:30:39 -0700838 PyCodeObject *o = (PyCodeObject*) code;
839 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700840
Brett Cannond0600ed2016-09-07 14:30:39 -0700841 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700842 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700843 return 0;
844 }
845
Brett Cannond0600ed2016-09-07 14:30:39 -0700846 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700847 return 0;
848}
849
850
851int
852_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
853{
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700854 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -0700855
856 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700857 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700858 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700859 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700860 }
861
Brett Cannond0600ed2016-09-07 14:30:39 -0700862 PyCodeObject *o = (PyCodeObject*) code;
863 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700864
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300865 if (co_extra == NULL || co_extra->ce_size <= index) {
866 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
867 co_extra = PyMem_Realloc(
868 co_extra,
869 sizeof(_PyCodeObjectExtra) +
870 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +0000871 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700872 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700873 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300874 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700875 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700876 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700877 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300878 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700879 }
880
881 if (co_extra->ce_extras[index] != NULL) {
882 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700883 if (free != NULL) {
884 free(co_extra->ce_extras[index]);
885 }
Brett Cannon5c4de282016-09-07 11:16:41 -0700886 }
887
Brett Cannond0600ed2016-09-07 14:30:39 -0700888 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700889 return 0;
890}