blob: 46bc45d5ac186d10d65a08a01ff395625399c7da [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];
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030022 static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
23 const unsigned char *s, *e;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020024
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030025 if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
26 !PyUnicode_IS_ASCII(o))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020027 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 if (ok_name_char[*name_chars] == 0) {
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030030 const unsigned char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 for (p = name_chars; *p; p++)
32 ok_name_char[*p] = 1;
33 }
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030034 s = PyUnicode_1BYTE_DATA(o);
35 e = s + PyUnicode_GET_LENGTH(o);
36 while (s != e) {
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
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030057/* Intern selected string constants */
58static int
59intern_string_constants(PyObject *tuple)
60{
61 int modified = 0;
62 Py_ssize_t i;
63
64 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
65 PyObject *v = PyTuple_GET_ITEM(tuple, i);
66 if (PyUnicode_CheckExact(v)) {
67 if (all_name_chars(v)) {
68 PyObject *w = v;
69 PyUnicode_InternInPlace(&v);
70 if (w != v) {
71 PyTuple_SET_ITEM(tuple, i, v);
72 modified = 1;
73 }
74 }
75 }
76 else if (PyTuple_CheckExact(v)) {
77 intern_string_constants(v);
78 }
79 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050080 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030081 PyObject *tmp = PySequence_Tuple(v);
82 if (tmp == NULL) {
83 PyErr_Clear();
84 continue;
85 }
86 if (intern_string_constants(tmp)) {
87 v = PyFrozenSet_New(tmp);
88 if (v == NULL) {
89 PyErr_Clear();
90 }
91 else {
92 PyTuple_SET_ITEM(tuple, i, v);
Yury Selivanovd2fd3592016-11-09 09:42:14 -050093 Py_DECREF(w);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030094 modified = 1;
95 }
96 }
97 Py_DECREF(tmp);
98 }
99 }
100 return modified;
101}
102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103
104PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000105PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 int nlocals, int stacksize, int flags,
107 PyObject *code, PyObject *consts, PyObject *names,
108 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
109 PyObject *filename, PyObject *name, int firstlineno,
110 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200113 Py_ssize_t *cell2arg = NULL;
Benjamin Peterson90037602011-06-25 22:54:45 -0500114 Py_ssize_t i, n_cellvars;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 /* Check argument types */
117 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200118 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 consts == NULL || !PyTuple_Check(consts) ||
120 names == NULL || !PyTuple_Check(names) ||
121 varnames == NULL || !PyTuple_Check(varnames) ||
122 freevars == NULL || !PyTuple_Check(freevars) ||
123 cellvars == NULL || !PyTuple_Check(cellvars) ||
124 name == NULL || !PyUnicode_Check(name) ||
125 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200126 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyErr_BadInternalCall();
128 return NULL;
129 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200130
131 /* Ensure that the filename is a ready Unicode string */
132 if (PyUnicode_READY(filename) < 0)
133 return NULL;
134
Benjamin Peterson90037602011-06-25 22:54:45 -0500135 n_cellvars = PyTuple_GET_SIZE(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 intern_strings(names);
137 intern_strings(varnames);
138 intern_strings(freevars);
139 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300140 intern_string_constants(consts);
Benjamin Peterson90037602011-06-25 22:54:45 -0500141 /* Create mapping between cells and arguments if needed. */
142 if (n_cellvars) {
143 Py_ssize_t total_args = argcount + kwonlyargcount +
144 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700145 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200146 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
147 if (cell2arg == NULL) {
148 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500149 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200150 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500151 /* Find cells which are also arguments. */
152 for (i = 0; i < n_cellvars; i++) {
153 Py_ssize_t j;
154 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200155 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500156 for (j = 0; j < total_args; j++) {
157 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200158 int cmp = PyUnicode_Compare(cell, arg);
159 if (cmp == -1 && PyErr_Occurred()) {
160 PyMem_FREE(cell2arg);
161 return NULL;
162 }
163 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500164 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700165 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500166 break;
167 }
168 }
169 }
170 if (!used_cell2arg) {
171 PyMem_FREE(cell2arg);
172 cell2arg = NULL;
173 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500175 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
176 if (co == NULL) {
177 if (cell2arg)
178 PyMem_FREE(cell2arg);
179 return NULL;
180 }
181 co->co_argcount = argcount;
182 co->co_kwonlyargcount = kwonlyargcount;
183 co->co_nlocals = nlocals;
184 co->co_stacksize = stacksize;
185 co->co_flags = flags;
186 Py_INCREF(code);
187 co->co_code = code;
188 Py_INCREF(consts);
189 co->co_consts = consts;
190 Py_INCREF(names);
191 co->co_names = names;
192 Py_INCREF(varnames);
193 co->co_varnames = varnames;
194 Py_INCREF(freevars);
195 co->co_freevars = freevars;
196 Py_INCREF(cellvars);
197 co->co_cellvars = cellvars;
198 co->co_cell2arg = cell2arg;
199 Py_INCREF(filename);
200 co->co_filename = filename;
201 Py_INCREF(name);
202 co->co_name = name;
203 co->co_firstlineno = firstlineno;
204 Py_INCREF(lnotab);
205 co->co_lnotab = lnotab;
206 co->co_zombieframe = NULL;
207 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700208 co->co_extra = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210}
211
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000212PyCodeObject *
213PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 static PyObject *emptystring = NULL;
216 static PyObject *nulltuple = NULL;
217 PyObject *filename_ob = NULL;
218 PyObject *funcname_ob = NULL;
219 PyCodeObject *result = NULL;
220 if (emptystring == NULL) {
221 emptystring = PyBytes_FromString("");
222 if (emptystring == NULL)
223 goto failed;
224 }
225 if (nulltuple == NULL) {
226 nulltuple = PyTuple_New(0);
227 if (nulltuple == NULL)
228 goto failed;
229 }
230 funcname_ob = PyUnicode_FromString(funcname);
231 if (funcname_ob == NULL)
232 goto failed;
233 filename_ob = PyUnicode_DecodeFSDefault(filename);
234 if (filename_ob == NULL)
235 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 result = PyCode_New(0, /* argcount */
238 0, /* kwonlyargcount */
239 0, /* nlocals */
240 0, /* stacksize */
241 0, /* flags */
242 emptystring, /* code */
243 nulltuple, /* consts */
244 nulltuple, /* names */
245 nulltuple, /* varnames */
246 nulltuple, /* freevars */
247 nulltuple, /* cellvars */
248 filename_ob, /* filename */
249 funcname_ob, /* name */
250 firstlineno, /* firstlineno */
251 emptystring /* lnotab */
252 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000253
254failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 Py_XDECREF(funcname_ob);
256 Py_XDECREF(filename_ob);
257 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000258}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259
260#define OFF(x) offsetof(PyCodeObject, x)
261
262static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
264 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
265 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
266 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
267 {"co_flags", T_INT, OFF(co_flags), READONLY},
268 {"co_code", T_OBJECT, OFF(co_code), READONLY},
269 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
270 {"co_names", T_OBJECT, OFF(co_names), READONLY},
271 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
272 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
273 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
274 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
275 {"co_name", T_OBJECT, OFF(co_name), READONLY},
276 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
277 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
278 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279};
280
281/* Helper for code_new: return a shallow copy of a tuple that is
282 guaranteed to contain exact strings, by converting string subclasses
283 to exact strings and complaining if a non-string is found. */
284static PyObject*
285validate_and_copy_tuple(PyObject *tup)
286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 PyObject *newtuple;
288 PyObject *item;
289 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 len = PyTuple_GET_SIZE(tup);
292 newtuple = PyTuple_New(len);
293 if (newtuple == NULL)
294 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 for (i = 0; i < len; i++) {
297 item = PyTuple_GET_ITEM(tup, i);
298 if (PyUnicode_CheckExact(item)) {
299 Py_INCREF(item);
300 }
301 else if (!PyUnicode_Check(item)) {
302 PyErr_Format(
303 PyExc_TypeError,
304 "name tuples must contain only "
305 "strings, not '%.500s'",
306 item->ob_type->tp_name);
307 Py_DECREF(newtuple);
308 return NULL;
309 }
310 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100311 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (item == NULL) {
313 Py_DECREF(newtuple);
314 return NULL;
315 }
316 }
317 PyTuple_SET_ITEM(newtuple, i, item);
318 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321}
322
323PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000324"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000325 constants, names, varnames, filename, name, firstlineno,\n\
326 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327\n\
328Create a code object. Not for the faint of heart.");
329
330static PyObject *
331code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 int argcount;
334 int kwonlyargcount;
335 int nlocals;
336 int stacksize;
337 int flags;
338 PyObject *co = NULL;
339 PyObject *code;
340 PyObject *consts;
341 PyObject *names, *ournames = NULL;
342 PyObject *varnames, *ourvarnames = NULL;
343 PyObject *freevars = NULL, *ourfreevars = NULL;
344 PyObject *cellvars = NULL, *ourcellvars = NULL;
345 PyObject *filename;
346 PyObject *name;
347 int firstlineno;
348 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
351 &argcount, &kwonlyargcount,
352 &nlocals, &stacksize, &flags,
353 &code,
354 &PyTuple_Type, &consts,
355 &PyTuple_Type, &names,
356 &PyTuple_Type, &varnames,
357 &filename, &name,
358 &firstlineno, &lnotab,
359 &PyTuple_Type, &freevars,
360 &PyTuple_Type, &cellvars))
361 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (argcount < 0) {
364 PyErr_SetString(
365 PyExc_ValueError,
366 "code: argcount must not be negative");
367 goto cleanup;
368 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 if (kwonlyargcount < 0) {
371 PyErr_SetString(
372 PyExc_ValueError,
373 "code: kwonlyargcount must not be negative");
374 goto cleanup;
375 }
376 if (nlocals < 0) {
377 PyErr_SetString(
378 PyExc_ValueError,
379 "code: nlocals must not be negative");
380 goto cleanup;
381 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 ournames = validate_and_copy_tuple(names);
384 if (ournames == NULL)
385 goto cleanup;
386 ourvarnames = validate_and_copy_tuple(varnames);
387 if (ourvarnames == NULL)
388 goto cleanup;
389 if (freevars)
390 ourfreevars = validate_and_copy_tuple(freevars);
391 else
392 ourfreevars = PyTuple_New(0);
393 if (ourfreevars == NULL)
394 goto cleanup;
395 if (cellvars)
396 ourcellvars = validate_and_copy_tuple(cellvars);
397 else
398 ourcellvars = PyTuple_New(0);
399 if (ourcellvars == NULL)
400 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
403 nlocals, stacksize, flags,
404 code, consts, ournames, ourvarnames,
405 ourfreevars, ourcellvars, filename,
406 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 Py_XDECREF(ournames);
409 Py_XDECREF(ourvarnames);
410 Py_XDECREF(ourfreevars);
411 Py_XDECREF(ourcellvars);
412 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000413}
414
415static void
416code_dealloc(PyCodeObject *co)
417{
Brett Cannon5c4de282016-09-07 11:16:41 -0700418 if (co->co_extra != NULL) {
419 PyThreadState *tstate = PyThreadState_Get();
Brett Cannond0600ed2016-09-07 14:30:39 -0700420 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700421
Brett Cannond0600ed2016-09-07 14:30:39 -0700422 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700423 freefunc free_extra = tstate->co_extra_freefuncs[i];
424
425 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700426 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700427 }
428 }
429
430 PyMem_FREE(co->co_extra);
431 }
432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 Py_XDECREF(co->co_code);
434 Py_XDECREF(co->co_consts);
435 Py_XDECREF(co->co_names);
436 Py_XDECREF(co->co_varnames);
437 Py_XDECREF(co->co_freevars);
438 Py_XDECREF(co->co_cellvars);
439 Py_XDECREF(co->co_filename);
440 Py_XDECREF(co->co_name);
441 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500442 if (co->co_cell2arg != NULL)
443 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 if (co->co_zombieframe != NULL)
445 PyObject_GC_Del(co->co_zombieframe);
446 if (co->co_weakreflist != NULL)
447 PyObject_ClearWeakRefs((PyObject*)co);
448 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449}
450
451static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200452code_sizeof(PyCodeObject *co, void *unused)
453{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900454 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
455 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200456
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200457 if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200458 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900459
460 if (co_extra != NULL)
461 res += co_extra->ce_size * 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 assert(*extra == NULL);
834
835 if (!PyCode_Check(code)) {
836 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700837 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700838 }
839
Brett Cannond0600ed2016-09-07 14:30:39 -0700840 PyCodeObject *o = (PyCodeObject*) code;
841 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700842
Brett Cannond0600ed2016-09-07 14:30:39 -0700843
844 if (co_extra == NULL || co_extra->ce_size <= index) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700845 return 0;
846 }
847
Brett Cannond0600ed2016-09-07 14:30:39 -0700848 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700849 return 0;
850}
851
852
853int
854_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
855{
Brett Cannon5c4de282016-09-07 11:16:41 -0700856 PyThreadState *tstate = PyThreadState_Get();
857
858 if (!PyCode_Check(code) || index < 0 ||
859 index >= tstate->co_extra_user_count) {
860 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700861 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700862 }
863
Brett Cannond0600ed2016-09-07 14:30:39 -0700864 PyCodeObject *o = (PyCodeObject*) code;
865 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700866
Brett Cannond0600ed2016-09-07 14:30:39 -0700867 if (co_extra == NULL) {
Brian Coleman6a9122c2017-03-02 10:32:18 +0000868 co_extra = PyMem_Malloc(sizeof(_PyCodeObjectExtra));
869 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700870 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700871 }
872
Brett Cannond0600ed2016-09-07 14:30:39 -0700873 co_extra->ce_extras = PyMem_Malloc(
Brett Cannon5c4de282016-09-07 11:16:41 -0700874 tstate->co_extra_user_count * sizeof(void*));
Brett Cannond0600ed2016-09-07 14:30:39 -0700875 if (co_extra->ce_extras == NULL) {
Brian Coleman6a9122c2017-03-02 10:32:18 +0000876 PyMem_Free(co_extra);
Brett Cannon3788b852016-09-07 12:51:08 -0700877 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700878 }
879
Brett Cannond0600ed2016-09-07 14:30:39 -0700880 co_extra->ce_size = tstate->co_extra_user_count;
Brett Cannon5c4de282016-09-07 11:16:41 -0700881
Brett Cannond0600ed2016-09-07 14:30:39 -0700882 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
883 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700884 }
Brian Coleman6a9122c2017-03-02 10:32:18 +0000885
886 o->co_extra = co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700887 }
Brett Cannond0600ed2016-09-07 14:30:39 -0700888 else if (co_extra->ce_size <= index) {
Brian Coleman6a9122c2017-03-02 10:32:18 +0000889 void** ce_extras = PyMem_Realloc(
Brett Cannond0600ed2016-09-07 14:30:39 -0700890 co_extra->ce_extras, tstate->co_extra_user_count * sizeof(void*));
Brett Cannon5c4de282016-09-07 11:16:41 -0700891
Brian Coleman6a9122c2017-03-02 10:32:18 +0000892 if (ce_extras == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700893 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700894 }
895
Brian Coleman6a9122c2017-03-02 10:32:18 +0000896 for (Py_ssize_t i = co_extra->ce_size;
897 i < tstate->co_extra_user_count;
898 i++) {
899 ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700900 }
Brian Coleman6a9122c2017-03-02 10:32:18 +0000901
902 co_extra->ce_extras = ce_extras;
903 co_extra->ce_size = tstate->co_extra_user_count;
Brett Cannon5c4de282016-09-07 11:16:41 -0700904 }
905
Brett Cannond0600ed2016-09-07 14:30:39 -0700906 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700907 return 0;
908}