blob: adef625b296226e6988b6826cb7699ac0fb02181 [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
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030030 if (!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)) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030066 if (PyUnicode_READY(v) == -1) {
67 PyErr_Clear();
68 continue;
69 }
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030070 if (all_name_chars(v)) {
71 PyObject *w = v;
72 PyUnicode_InternInPlace(&v);
73 if (w != v) {
74 PyTuple_SET_ITEM(tuple, i, v);
75 modified = 1;
76 }
77 }
78 }
79 else if (PyTuple_CheckExact(v)) {
80 intern_string_constants(v);
81 }
82 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050083 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030084 PyObject *tmp = PySequence_Tuple(v);
85 if (tmp == NULL) {
86 PyErr_Clear();
87 continue;
88 }
89 if (intern_string_constants(tmp)) {
90 v = PyFrozenSet_New(tmp);
91 if (v == NULL) {
92 PyErr_Clear();
93 }
94 else {
95 PyTuple_SET_ITEM(tuple, i, v);
Yury Selivanovd2fd3592016-11-09 09:42:14 -050096 Py_DECREF(w);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030097 modified = 1;
98 }
99 }
100 Py_DECREF(tmp);
101 }
102 }
103 return modified;
104}
105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106
107PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000108PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 int nlocals, int stacksize, int flags,
110 PyObject *code, PyObject *consts, PyObject *names,
111 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
112 PyObject *filename, PyObject *name, int firstlineno,
113 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200116 Py_ssize_t *cell2arg = NULL;
Benjamin Peterson90037602011-06-25 22:54:45 -0500117 Py_ssize_t i, n_cellvars;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 /* Check argument types */
120 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200121 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 consts == NULL || !PyTuple_Check(consts) ||
123 names == NULL || !PyTuple_Check(names) ||
124 varnames == NULL || !PyTuple_Check(varnames) ||
125 freevars == NULL || !PyTuple_Check(freevars) ||
126 cellvars == NULL || !PyTuple_Check(cellvars) ||
127 name == NULL || !PyUnicode_Check(name) ||
128 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200129 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 PyErr_BadInternalCall();
131 return NULL;
132 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200133
134 /* Ensure that the filename is a ready Unicode string */
135 if (PyUnicode_READY(filename) < 0)
136 return NULL;
137
Benjamin Peterson90037602011-06-25 22:54:45 -0500138 n_cellvars = PyTuple_GET_SIZE(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 intern_strings(names);
140 intern_strings(varnames);
141 intern_strings(freevars);
142 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300143 intern_string_constants(consts);
Benjamin Peterson90037602011-06-25 22:54:45 -0500144 /* Create mapping between cells and arguments if needed. */
145 if (n_cellvars) {
146 Py_ssize_t total_args = argcount + kwonlyargcount +
147 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700148 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200149 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
150 if (cell2arg == NULL) {
151 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500152 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200153 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500154 /* Find cells which are also arguments. */
155 for (i = 0; i < n_cellvars; i++) {
156 Py_ssize_t j;
157 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200158 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500159 for (j = 0; j < total_args; j++) {
160 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200161 int cmp = PyUnicode_Compare(cell, arg);
162 if (cmp == -1 && PyErr_Occurred()) {
163 PyMem_FREE(cell2arg);
164 return NULL;
165 }
166 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500167 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700168 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500169 break;
170 }
171 }
172 }
173 if (!used_cell2arg) {
174 PyMem_FREE(cell2arg);
175 cell2arg = NULL;
176 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500178 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
179 if (co == NULL) {
180 if (cell2arg)
181 PyMem_FREE(cell2arg);
182 return NULL;
183 }
184 co->co_argcount = argcount;
185 co->co_kwonlyargcount = kwonlyargcount;
186 co->co_nlocals = nlocals;
187 co->co_stacksize = stacksize;
188 co->co_flags = flags;
189 Py_INCREF(code);
190 co->co_code = code;
191 Py_INCREF(consts);
192 co->co_consts = consts;
193 Py_INCREF(names);
194 co->co_names = names;
195 Py_INCREF(varnames);
196 co->co_varnames = varnames;
197 Py_INCREF(freevars);
198 co->co_freevars = freevars;
199 Py_INCREF(cellvars);
200 co->co_cellvars = cellvars;
201 co->co_cell2arg = cell2arg;
202 Py_INCREF(filename);
203 co->co_filename = filename;
204 Py_INCREF(name);
205 co->co_name = name;
206 co->co_firstlineno = firstlineno;
207 Py_INCREF(lnotab);
208 co->co_lnotab = lnotab;
209 co->co_zombieframe = NULL;
210 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700211 co->co_extra = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213}
214
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000215PyCodeObject *
216PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 static PyObject *emptystring = NULL;
219 static PyObject *nulltuple = NULL;
220 PyObject *filename_ob = NULL;
221 PyObject *funcname_ob = NULL;
222 PyCodeObject *result = NULL;
223 if (emptystring == NULL) {
224 emptystring = PyBytes_FromString("");
225 if (emptystring == NULL)
226 goto failed;
227 }
228 if (nulltuple == NULL) {
229 nulltuple = PyTuple_New(0);
230 if (nulltuple == NULL)
231 goto failed;
232 }
233 funcname_ob = PyUnicode_FromString(funcname);
234 if (funcname_ob == NULL)
235 goto failed;
236 filename_ob = PyUnicode_DecodeFSDefault(filename);
237 if (filename_ob == NULL)
238 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 result = PyCode_New(0, /* argcount */
241 0, /* kwonlyargcount */
242 0, /* nlocals */
243 0, /* stacksize */
244 0, /* flags */
245 emptystring, /* code */
246 nulltuple, /* consts */
247 nulltuple, /* names */
248 nulltuple, /* varnames */
249 nulltuple, /* freevars */
250 nulltuple, /* cellvars */
251 filename_ob, /* filename */
252 funcname_ob, /* name */
253 firstlineno, /* firstlineno */
254 emptystring /* lnotab */
255 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000256
257failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 Py_XDECREF(funcname_ob);
259 Py_XDECREF(filename_ob);
260 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000261}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262
263#define OFF(x) offsetof(PyCodeObject, x)
264
265static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
267 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
268 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
269 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
270 {"co_flags", T_INT, OFF(co_flags), READONLY},
271 {"co_code", T_OBJECT, OFF(co_code), READONLY},
272 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
273 {"co_names", T_OBJECT, OFF(co_names), READONLY},
274 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
275 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
276 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
277 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
278 {"co_name", T_OBJECT, OFF(co_name), READONLY},
279 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
280 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
281 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282};
283
284/* Helper for code_new: return a shallow copy of a tuple that is
285 guaranteed to contain exact strings, by converting string subclasses
286 to exact strings and complaining if a non-string is found. */
287static PyObject*
288validate_and_copy_tuple(PyObject *tup)
289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyObject *newtuple;
291 PyObject *item;
292 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 len = PyTuple_GET_SIZE(tup);
295 newtuple = PyTuple_New(len);
296 if (newtuple == NULL)
297 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 for (i = 0; i < len; i++) {
300 item = PyTuple_GET_ITEM(tup, i);
301 if (PyUnicode_CheckExact(item)) {
302 Py_INCREF(item);
303 }
304 else if (!PyUnicode_Check(item)) {
305 PyErr_Format(
306 PyExc_TypeError,
307 "name tuples must contain only "
308 "strings, not '%.500s'",
309 item->ob_type->tp_name);
310 Py_DECREF(newtuple);
311 return NULL;
312 }
313 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100314 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (item == NULL) {
316 Py_DECREF(newtuple);
317 return NULL;
318 }
319 }
320 PyTuple_SET_ITEM(newtuple, i, item);
321 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324}
325
326PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000327"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000328 constants, names, varnames, filename, name, firstlineno,\n\
329 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330\n\
331Create a code object. Not for the faint of heart.");
332
333static PyObject *
334code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 int argcount;
337 int kwonlyargcount;
338 int nlocals;
339 int stacksize;
340 int flags;
341 PyObject *co = NULL;
342 PyObject *code;
343 PyObject *consts;
344 PyObject *names, *ournames = NULL;
345 PyObject *varnames, *ourvarnames = NULL;
346 PyObject *freevars = NULL, *ourfreevars = NULL;
347 PyObject *cellvars = NULL, *ourcellvars = NULL;
348 PyObject *filename;
349 PyObject *name;
350 int firstlineno;
351 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
354 &argcount, &kwonlyargcount,
355 &nlocals, &stacksize, &flags,
356 &code,
357 &PyTuple_Type, &consts,
358 &PyTuple_Type, &names,
359 &PyTuple_Type, &varnames,
360 &filename, &name,
361 &firstlineno, &lnotab,
362 &PyTuple_Type, &freevars,
363 &PyTuple_Type, &cellvars))
364 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (argcount < 0) {
367 PyErr_SetString(
368 PyExc_ValueError,
369 "code: argcount must not be negative");
370 goto cleanup;
371 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 if (kwonlyargcount < 0) {
374 PyErr_SetString(
375 PyExc_ValueError,
376 "code: kwonlyargcount must not be negative");
377 goto cleanup;
378 }
379 if (nlocals < 0) {
380 PyErr_SetString(
381 PyExc_ValueError,
382 "code: nlocals must not be negative");
383 goto cleanup;
384 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 ournames = validate_and_copy_tuple(names);
387 if (ournames == NULL)
388 goto cleanup;
389 ourvarnames = validate_and_copy_tuple(varnames);
390 if (ourvarnames == NULL)
391 goto cleanup;
392 if (freevars)
393 ourfreevars = validate_and_copy_tuple(freevars);
394 else
395 ourfreevars = PyTuple_New(0);
396 if (ourfreevars == NULL)
397 goto cleanup;
398 if (cellvars)
399 ourcellvars = validate_and_copy_tuple(cellvars);
400 else
401 ourcellvars = PyTuple_New(0);
402 if (ourcellvars == NULL)
403 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
406 nlocals, stacksize, flags,
407 code, consts, ournames, ourvarnames,
408 ourfreevars, ourcellvars, filename,
409 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 Py_XDECREF(ournames);
412 Py_XDECREF(ourvarnames);
413 Py_XDECREF(ourfreevars);
414 Py_XDECREF(ourcellvars);
415 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416}
417
418static void
419code_dealloc(PyCodeObject *co)
420{
Brett Cannon5c4de282016-09-07 11:16:41 -0700421 if (co->co_extra != NULL) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700422 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannond0600ed2016-09-07 14:30:39 -0700423 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700424
Brett Cannond0600ed2016-09-07 14:30:39 -0700425 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700426 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700427
428 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700429 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700430 }
431 }
432
Victor Stinner23e79442017-06-28 02:12:00 +0200433 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700434 }
435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 Py_XDECREF(co->co_code);
437 Py_XDECREF(co->co_consts);
438 Py_XDECREF(co->co_names);
439 Py_XDECREF(co->co_varnames);
440 Py_XDECREF(co->co_freevars);
441 Py_XDECREF(co->co_cellvars);
442 Py_XDECREF(co->co_filename);
443 Py_XDECREF(co->co_name);
444 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500445 if (co->co_cell2arg != NULL)
446 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (co->co_zombieframe != NULL)
448 PyObject_GC_Del(co->co_zombieframe);
449 if (co->co_weakreflist != NULL)
450 PyObject_ClearWeakRefs((PyObject*)co);
451 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452}
453
454static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200455code_sizeof(PyCodeObject *co, void *unused)
456{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900457 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
458 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200459
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300460 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200461 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300462 }
463 if (co_extra != NULL) {
464 res += sizeof(_PyCodeObjectExtra) +
465 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
466 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200467 return PyLong_FromSsize_t(res);
468}
469
470static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471code_repr(PyCodeObject *co)
472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 int lineno;
474 if (co->co_firstlineno != 0)
475 lineno = co->co_firstlineno;
476 else
477 lineno = -1;
478 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
479 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000480 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 co->co_name, co, co->co_filename, lineno);
482 } else {
483 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000484 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 co->co_name, co, lineno);
486 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487}
488
Victor Stinnerefb24132016-01-22 12:33:12 +0100489PyObject*
490_PyCode_ConstantKey(PyObject *op)
491{
492 PyObject *key;
493
494 /* Py_None and Py_Ellipsis are singleton */
495 if (op == Py_None || op == Py_Ellipsis
496 || PyLong_CheckExact(op)
497 || PyBool_Check(op)
498 || PyBytes_CheckExact(op)
499 || PyUnicode_CheckExact(op)
500 /* code_richcompare() uses _PyCode_ConstantKey() internally */
501 || PyCode_Check(op)) {
502 key = PyTuple_Pack(2, Py_TYPE(op), op);
503 }
504 else if (PyFloat_CheckExact(op)) {
505 double d = PyFloat_AS_DOUBLE(op);
506 /* all we need is to make the tuple different in either the 0.0
507 * or -0.0 case from all others, just to avoid the "coercion".
508 */
509 if (d == 0.0 && copysign(1.0, d) < 0.0)
510 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
511 else
512 key = PyTuple_Pack(2, Py_TYPE(op), op);
513 }
514 else if (PyComplex_CheckExact(op)) {
515 Py_complex z;
516 int real_negzero, imag_negzero;
517 /* For the complex case we must make complex(x, 0.)
518 different from complex(x, -0.) and complex(0., y)
519 different from complex(-0., y), for any x and y.
520 All four complex zeros must be distinguished.*/
521 z = PyComplex_AsCComplex(op);
522 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
523 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
524 /* use True, False and None singleton as tags for the real and imag
525 * sign, to make tuples different */
526 if (real_negzero && imag_negzero) {
527 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
528 }
529 else if (imag_negzero) {
530 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
531 }
532 else if (real_negzero) {
533 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
534 }
535 else {
536 key = PyTuple_Pack(2, Py_TYPE(op), op);
537 }
538 }
539 else if (PyTuple_CheckExact(op)) {
540 Py_ssize_t i, len;
541 PyObject *tuple;
542
543 len = PyTuple_GET_SIZE(op);
544 tuple = PyTuple_New(len);
545 if (tuple == NULL)
546 return NULL;
547
548 for (i=0; i < len; i++) {
549 PyObject *item, *item_key;
550
551 item = PyTuple_GET_ITEM(op, i);
552 item_key = _PyCode_ConstantKey(item);
553 if (item_key == NULL) {
554 Py_DECREF(tuple);
555 return NULL;
556 }
557
558 PyTuple_SET_ITEM(tuple, i, item_key);
559 }
560
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200561 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100562 Py_DECREF(tuple);
563 }
564 else if (PyFrozenSet_CheckExact(op)) {
565 Py_ssize_t pos = 0;
566 PyObject *item;
567 Py_hash_t hash;
568 Py_ssize_t i, len;
569 PyObject *tuple, *set;
570
571 len = PySet_GET_SIZE(op);
572 tuple = PyTuple_New(len);
573 if (tuple == NULL)
574 return NULL;
575
576 i = 0;
577 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
578 PyObject *item_key;
579
580 item_key = _PyCode_ConstantKey(item);
581 if (item_key == NULL) {
582 Py_DECREF(tuple);
583 return NULL;
584 }
585
586 assert(i < len);
587 PyTuple_SET_ITEM(tuple, i, item_key);
588 i++;
589 }
590 set = PyFrozenSet_New(tuple);
591 Py_DECREF(tuple);
592 if (set == NULL)
593 return NULL;
594
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200595 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100596 Py_DECREF(set);
597 return key;
598 }
599 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000600 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100601 * to ensure that they are seen as unequal. */
602 PyObject *obj_id = PyLong_FromVoidPtr(op);
603 if (obj_id == NULL)
604 return NULL;
605
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200606 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100607 Py_DECREF(obj_id);
608 }
609 return key;
610}
611
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000612static PyObject *
613code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 PyCodeObject *co, *cp;
616 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100617 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 if ((op != Py_EQ && op != Py_NE) ||
621 !PyCode_Check(self) ||
622 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500623 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 co = (PyCodeObject *)self;
627 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
630 if (eq <= 0) goto unequal;
631 eq = co->co_argcount == cp->co_argcount;
632 if (!eq) goto unequal;
633 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
634 if (!eq) goto unequal;
635 eq = co->co_nlocals == cp->co_nlocals;
636 if (!eq) goto unequal;
637 eq = co->co_flags == cp->co_flags;
638 if (!eq) goto unequal;
639 eq = co->co_firstlineno == cp->co_firstlineno;
640 if (!eq) goto unequal;
641 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
642 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100643
644 /* compare constants */
645 consts1 = _PyCode_ConstantKey(co->co_consts);
646 if (!consts1)
647 return NULL;
648 consts2 = _PyCode_ConstantKey(cp->co_consts);
649 if (!consts2) {
650 Py_DECREF(consts1);
651 return NULL;
652 }
653 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
654 Py_DECREF(consts1);
655 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
659 if (eq <= 0) goto unequal;
660 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
661 if (eq <= 0) goto unequal;
662 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
663 if (eq <= 0) goto unequal;
664 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
665 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 if (op == Py_EQ)
668 res = Py_True;
669 else
670 res = Py_False;
671 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000672
673 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 if (eq < 0)
675 return NULL;
676 if (op == Py_NE)
677 res = Py_True;
678 else
679 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000680
681 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 Py_INCREF(res);
683 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684}
685
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000686static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687code_hash(PyCodeObject *co)
688{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000689 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 h0 = PyObject_Hash(co->co_name);
691 if (h0 == -1) return -1;
692 h1 = PyObject_Hash(co->co_code);
693 if (h1 == -1) return -1;
694 h2 = PyObject_Hash(co->co_consts);
695 if (h2 == -1) return -1;
696 h3 = PyObject_Hash(co->co_names);
697 if (h3 == -1) return -1;
698 h4 = PyObject_Hash(co->co_varnames);
699 if (h4 == -1) return -1;
700 h5 = PyObject_Hash(co->co_freevars);
701 if (h5 == -1) return -1;
702 h6 = PyObject_Hash(co->co_cellvars);
703 if (h6 == -1) return -1;
704 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
705 co->co_argcount ^ co->co_kwonlyargcount ^
706 co->co_nlocals ^ co->co_flags;
707 if (h == -1) h = -2;
708 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709}
710
711/* XXX code objects need to participate in GC? */
712
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200713static struct PyMethodDef code_methods[] = {
714 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
715 {NULL, NULL} /* sentinel */
716};
717
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 PyVarObject_HEAD_INIT(&PyType_Type, 0)
720 "code",
721 sizeof(PyCodeObject),
722 0,
723 (destructor)code_dealloc, /* tp_dealloc */
724 0, /* tp_print */
725 0, /* tp_getattr */
726 0, /* tp_setattr */
727 0, /* tp_reserved */
728 (reprfunc)code_repr, /* tp_repr */
729 0, /* tp_as_number */
730 0, /* tp_as_sequence */
731 0, /* tp_as_mapping */
732 (hashfunc)code_hash, /* tp_hash */
733 0, /* tp_call */
734 0, /* tp_str */
735 PyObject_GenericGetAttr, /* tp_getattro */
736 0, /* tp_setattro */
737 0, /* tp_as_buffer */
738 Py_TPFLAGS_DEFAULT, /* tp_flags */
739 code_doc, /* tp_doc */
740 0, /* tp_traverse */
741 0, /* tp_clear */
742 code_richcompare, /* tp_richcompare */
743 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
744 0, /* tp_iter */
745 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200746 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 code_memberlist, /* tp_members */
748 0, /* tp_getset */
749 0, /* tp_base */
750 0, /* tp_dict */
751 0, /* tp_descr_get */
752 0, /* tp_descr_set */
753 0, /* tp_dictoffset */
754 0, /* tp_init */
755 0, /* tp_alloc */
756 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757};
758
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000759/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
760 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761*/
762
763int
764PyCode_Addr2Line(PyCodeObject *co, int addrq)
765{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000766 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
768 int line = co->co_firstlineno;
769 int addr = 0;
770 while (--size >= 0) {
771 addr += *p++;
772 if (addr > addrq)
773 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100774 line += (signed char)*p;
775 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 }
777 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000779
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000780/* Update *bounds to describe the first and one-past-the-last instructions in
781 the same line as lasti. Return the number of that line. */
782int
783_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000784{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000785 Py_ssize_t size;
786 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
790 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 addr = 0;
793 line = co->co_firstlineno;
794 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 /* possible optimization: if f->f_lasti == instr_ub
797 (likely to be a common case) then we already know
798 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700799 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 /* See lnotab_notes.txt for the description of
802 co_lnotab. A point to remember: increments to p
803 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 bounds->ap_lower = 0;
806 while (size > 0) {
807 if (addr + *p > lasti)
808 break;
809 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100810 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100812 line += (signed char)*p;
813 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 --size;
815 }
816
817 if (size > 0) {
818 while (--size >= 0) {
819 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100820 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100822 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000823 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 bounds->ap_upper = addr;
825 }
826 else {
827 bounds->ap_upper = INT_MAX;
828 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831}
Brett Cannon5c4de282016-09-07 11:16:41 -0700832
833
834int
835_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
836{
Brett Cannon5c4de282016-09-07 11:16:41 -0700837 if (!PyCode_Check(code)) {
838 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700839 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700840 }
841
Brett Cannond0600ed2016-09-07 14:30:39 -0700842 PyCodeObject *o = (PyCodeObject*) code;
843 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700844
Brett Cannond0600ed2016-09-07 14:30:39 -0700845 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700846 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700847 return 0;
848 }
849
Brett Cannond0600ed2016-09-07 14:30:39 -0700850 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700851 return 0;
852}
853
854
855int
856_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
857{
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700858 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -0700859
860 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700861 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700862 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700863 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700864 }
865
Brett Cannond0600ed2016-09-07 14:30:39 -0700866 PyCodeObject *o = (PyCodeObject*) code;
867 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700868
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300869 if (co_extra == NULL || co_extra->ce_size <= index) {
870 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
871 co_extra = PyMem_Realloc(
872 co_extra,
873 sizeof(_PyCodeObjectExtra) +
874 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +0000875 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700876 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700877 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300878 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700879 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700880 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700881 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300882 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700883 }
884
885 if (co_extra->ce_extras[index] != NULL) {
886 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700887 if (free != NULL) {
888 free(co_extra->ce_extras[index]);
889 }
Brett Cannon5c4de282016-09-07 11:16:41 -0700890 }
891
Brett Cannond0600ed2016-09-07 14:30:39 -0700892 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700893 return 0;
894}