blob: 62d7c5d329f07b1a17e6818e6a57c98ccd2cebfc [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"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +01007#include "pycore_tupleobject.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008
Brett Cannond0600ed2016-09-07 14:30:39 -07009/* Holder for co_extra information */
10typedef struct {
11 Py_ssize_t ce_size;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +030012 void *ce_extras[1];
Brett Cannond0600ed2016-09-07 14:30:39 -070013} _PyCodeObjectExtra;
14
Benjamin Peterson8e0ad462017-09-07 23:35:53 -070015/* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000016static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020017all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018{
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030019 const unsigned char *s, *e;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020020
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030021 if (!PyUnicode_IS_ASCII(o))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020022 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000023
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030024 s = PyUnicode_1BYTE_DATA(o);
25 e = s + PyUnicode_GET_LENGTH(o);
Benjamin Peterson9020ac72017-09-07 18:06:23 -070026 for (; s != e; s++) {
Benjamin Peterson2b7953d2017-09-08 10:35:49 -070027 if (!Py_ISALNUM(*s) && *s != '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 return 0;
29 }
30 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000031}
32
33static void
34intern_strings(PyObject *tuple)
35{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
39 PyObject *v = PyTuple_GET_ITEM(tuple, i);
40 if (v == NULL || !PyUnicode_CheckExact(v)) {
41 Py_FatalError("non-string found in code slot");
42 }
Victor Stinnerd17a6932018-11-09 16:56:48 +010043 PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045}
46
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030047/* Intern selected string constants */
48static int
49intern_string_constants(PyObject *tuple)
50{
51 int modified = 0;
52 Py_ssize_t i;
53
54 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
55 PyObject *v = PyTuple_GET_ITEM(tuple, i);
56 if (PyUnicode_CheckExact(v)) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030057 if (PyUnicode_READY(v) == -1) {
58 PyErr_Clear();
59 continue;
60 }
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030061 if (all_name_chars(v)) {
62 PyObject *w = v;
63 PyUnicode_InternInPlace(&v);
64 if (w != v) {
65 PyTuple_SET_ITEM(tuple, i, v);
66 modified = 1;
67 }
68 }
69 }
70 else if (PyTuple_CheckExact(v)) {
71 intern_string_constants(v);
72 }
73 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050074 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030075 PyObject *tmp = PySequence_Tuple(v);
76 if (tmp == NULL) {
77 PyErr_Clear();
78 continue;
79 }
80 if (intern_string_constants(tmp)) {
81 v = PyFrozenSet_New(tmp);
82 if (v == NULL) {
83 PyErr_Clear();
84 }
85 else {
86 PyTuple_SET_ITEM(tuple, i, v);
Yury Selivanovd2fd3592016-11-09 09:42:14 -050087 Py_DECREF(w);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030088 modified = 1;
89 }
90 }
91 Py_DECREF(tmp);
92 }
93 }
94 return modified;
95}
96
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097
98PyCodeObject *
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010099PyCode_New(int argcount, int posonlyargcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 int nlocals, int stacksize, int flags,
101 PyObject *code, PyObject *consts, PyObject *names,
102 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
103 PyObject *filename, PyObject *name, int firstlineno,
104 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200107 Py_ssize_t *cell2arg = NULL;
Serhiy Storchakabd473842018-07-16 09:10:19 +0300108 Py_ssize_t i, n_cellvars, n_varnames, total_args;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 /* Check argument types */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100111 if (argcount < 0 || posonlyargcount < 0 || kwonlyargcount < 0 ||
112 nlocals < 0 || code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 consts == NULL || !PyTuple_Check(consts) ||
114 names == NULL || !PyTuple_Check(names) ||
115 varnames == NULL || !PyTuple_Check(varnames) ||
116 freevars == NULL || !PyTuple_Check(freevars) ||
117 cellvars == NULL || !PyTuple_Check(cellvars) ||
118 name == NULL || !PyUnicode_Check(name) ||
119 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200120 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 PyErr_BadInternalCall();
122 return NULL;
123 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200124
125 /* Ensure that the filename is a ready Unicode string */
126 if (PyUnicode_READY(filename) < 0)
127 return NULL;
128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 intern_strings(names);
130 intern_strings(varnames);
131 intern_strings(freevars);
132 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300133 intern_string_constants(consts);
Nick Coghlan078f1812017-12-03 11:12:20 +1000134
135 /* Check for any inner or outer closure references */
136 n_cellvars = PyTuple_GET_SIZE(cellvars);
137 if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
138 flags |= CO_NOFREE;
139 } else {
140 flags &= ~CO_NOFREE;
141 }
142
Serhiy Storchakabd473842018-07-16 09:10:19 +0300143 n_varnames = PyTuple_GET_SIZE(varnames);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100144 if (posonlyargcount + argcount <= n_varnames
145 && kwonlyargcount <= n_varnames) {
Serhiy Storchakabd473842018-07-16 09:10:19 +0300146 /* Never overflows. */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100147 total_args = (Py_ssize_t)posonlyargcount + (Py_ssize_t)argcount
148 + (Py_ssize_t)kwonlyargcount +
149 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
Serhiy Storchakabd473842018-07-16 09:10:19 +0300150 }
151 else {
152 total_args = n_varnames + 1;
153 }
154 if (total_args > n_varnames) {
155 PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
156 return NULL;
157 }
158
Benjamin Peterson90037602011-06-25 22:54:45 -0500159 /* Create mapping between cells and arguments if needed. */
160 if (n_cellvars) {
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700161 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200162 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
163 if (cell2arg == NULL) {
164 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500165 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200166 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500167 /* Find cells which are also arguments. */
168 for (i = 0; i < n_cellvars; i++) {
169 Py_ssize_t j;
170 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200171 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500172 for (j = 0; j < total_args; j++) {
173 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200174 int cmp = PyUnicode_Compare(cell, arg);
175 if (cmp == -1 && PyErr_Occurred()) {
176 PyMem_FREE(cell2arg);
177 return NULL;
178 }
179 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500180 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700181 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500182 break;
183 }
184 }
185 }
186 if (!used_cell2arg) {
187 PyMem_FREE(cell2arg);
188 cell2arg = NULL;
189 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500191 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
192 if (co == NULL) {
193 if (cell2arg)
194 PyMem_FREE(cell2arg);
195 return NULL;
196 }
197 co->co_argcount = argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100198 co->co_posonlyargcount = posonlyargcount;
Benjamin Peterson90037602011-06-25 22:54:45 -0500199 co->co_kwonlyargcount = kwonlyargcount;
200 co->co_nlocals = nlocals;
201 co->co_stacksize = stacksize;
202 co->co_flags = flags;
203 Py_INCREF(code);
204 co->co_code = code;
205 Py_INCREF(consts);
206 co->co_consts = consts;
207 Py_INCREF(names);
208 co->co_names = names;
209 Py_INCREF(varnames);
210 co->co_varnames = varnames;
211 Py_INCREF(freevars);
212 co->co_freevars = freevars;
213 Py_INCREF(cellvars);
214 co->co_cellvars = cellvars;
215 co->co_cell2arg = cell2arg;
216 Py_INCREF(filename);
217 co->co_filename = filename;
218 Py_INCREF(name);
219 co->co_name = name;
220 co->co_firstlineno = firstlineno;
221 Py_INCREF(lnotab);
222 co->co_lnotab = lnotab;
223 co->co_zombieframe = NULL;
224 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700225 co->co_extra = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227}
228
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000229PyCodeObject *
230PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 static PyObject *emptystring = NULL;
233 static PyObject *nulltuple = NULL;
234 PyObject *filename_ob = NULL;
235 PyObject *funcname_ob = NULL;
236 PyCodeObject *result = NULL;
237 if (emptystring == NULL) {
238 emptystring = PyBytes_FromString("");
239 if (emptystring == NULL)
240 goto failed;
241 }
242 if (nulltuple == NULL) {
243 nulltuple = PyTuple_New(0);
244 if (nulltuple == NULL)
245 goto failed;
246 }
247 funcname_ob = PyUnicode_FromString(funcname);
248 if (funcname_ob == NULL)
249 goto failed;
250 filename_ob = PyUnicode_DecodeFSDefault(filename);
251 if (filename_ob == NULL)
252 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 result = PyCode_New(0, /* argcount */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100255 0, /* posonlyargcount */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 0, /* kwonlyargcount */
257 0, /* nlocals */
258 0, /* stacksize */
259 0, /* flags */
260 emptystring, /* code */
261 nulltuple, /* consts */
262 nulltuple, /* names */
263 nulltuple, /* varnames */
264 nulltuple, /* freevars */
265 nulltuple, /* cellvars */
266 filename_ob, /* filename */
267 funcname_ob, /* name */
268 firstlineno, /* firstlineno */
269 emptystring /* lnotab */
270 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000271
272failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 Py_XDECREF(funcname_ob);
274 Py_XDECREF(filename_ob);
275 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000276}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277
278#define OFF(x) offsetof(PyCodeObject, x)
279
280static PyMemberDef code_memberlist[] = {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100281 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
282 {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
283 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
284 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
285 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
286 {"co_flags", T_INT, OFF(co_flags), READONLY},
287 {"co_code", T_OBJECT, OFF(co_code), READONLY},
288 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
289 {"co_names", T_OBJECT, OFF(co_names), READONLY},
290 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
291 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
292 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
293 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
294 {"co_name", T_OBJECT, OFF(co_name), READONLY},
295 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
296 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298};
299
300/* Helper for code_new: return a shallow copy of a tuple that is
301 guaranteed to contain exact strings, by converting string subclasses
302 to exact strings and complaining if a non-string is found. */
303static PyObject*
304validate_and_copy_tuple(PyObject *tup)
305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 PyObject *newtuple;
307 PyObject *item;
308 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 len = PyTuple_GET_SIZE(tup);
311 newtuple = PyTuple_New(len);
312 if (newtuple == NULL)
313 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 for (i = 0; i < len; i++) {
316 item = PyTuple_GET_ITEM(tup, i);
317 if (PyUnicode_CheckExact(item)) {
318 Py_INCREF(item);
319 }
320 else if (!PyUnicode_Check(item)) {
321 PyErr_Format(
322 PyExc_TypeError,
323 "name tuples must contain only "
324 "strings, not '%.500s'",
325 item->ob_type->tp_name);
326 Py_DECREF(newtuple);
327 return NULL;
328 }
329 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100330 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 if (item == NULL) {
332 Py_DECREF(newtuple);
333 return NULL;
334 }
335 }
336 PyTuple_SET_ITEM(newtuple, i, item);
337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340}
341
342PyDoc_STRVAR(code_doc,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100343"code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
344 flags, codestring, constants, names, varnames, filename, name,\n\
345 firstlineno, lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346\n\
347Create a code object. Not for the faint of heart.");
348
349static PyObject *
350code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 int argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100353 int posonlyargcount;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 int kwonlyargcount;
355 int nlocals;
356 int stacksize;
357 int flags;
358 PyObject *co = NULL;
359 PyObject *code;
360 PyObject *consts;
361 PyObject *names, *ournames = NULL;
362 PyObject *varnames, *ourvarnames = NULL;
363 PyObject *freevars = NULL, *ourfreevars = NULL;
364 PyObject *cellvars = NULL, *ourcellvars = NULL;
365 PyObject *filename;
366 PyObject *name;
367 int firstlineno;
368 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100370 if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
371 &argcount, &posonlyargcount, &kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 &nlocals, &stacksize, &flags,
373 &code,
374 &PyTuple_Type, &consts,
375 &PyTuple_Type, &names,
376 &PyTuple_Type, &varnames,
377 &filename, &name,
378 &firstlineno, &lnotab,
379 &PyTuple_Type, &freevars,
380 &PyTuple_Type, &cellvars))
381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 if (argcount < 0) {
384 PyErr_SetString(
385 PyExc_ValueError,
386 "code: argcount must not be negative");
387 goto cleanup;
388 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100390 if (posonlyargcount < 0) {
391 PyErr_SetString(
392 PyExc_ValueError,
393 "code: posonlyargcount must not be negative");
394 goto cleanup;
395 }
396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (kwonlyargcount < 0) {
398 PyErr_SetString(
399 PyExc_ValueError,
400 "code: kwonlyargcount must not be negative");
401 goto cleanup;
402 }
403 if (nlocals < 0) {
404 PyErr_SetString(
405 PyExc_ValueError,
406 "code: nlocals must not be negative");
407 goto cleanup;
408 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 ournames = validate_and_copy_tuple(names);
411 if (ournames == NULL)
412 goto cleanup;
413 ourvarnames = validate_and_copy_tuple(varnames);
414 if (ourvarnames == NULL)
415 goto cleanup;
416 if (freevars)
417 ourfreevars = validate_and_copy_tuple(freevars);
418 else
419 ourfreevars = PyTuple_New(0);
420 if (ourfreevars == NULL)
421 goto cleanup;
422 if (cellvars)
423 ourcellvars = validate_and_copy_tuple(cellvars);
424 else
425 ourcellvars = PyTuple_New(0);
426 if (ourcellvars == NULL)
427 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100429 co = (PyObject *)PyCode_New(argcount, posonlyargcount, kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 nlocals, stacksize, flags,
431 code, consts, ournames, ourvarnames,
432 ourfreevars, ourcellvars, filename,
433 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 Py_XDECREF(ournames);
436 Py_XDECREF(ourvarnames);
437 Py_XDECREF(ourfreevars);
438 Py_XDECREF(ourcellvars);
439 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440}
441
442static void
443code_dealloc(PyCodeObject *co)
444{
Brett Cannon5c4de282016-09-07 11:16:41 -0700445 if (co->co_extra != NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200446 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannond0600ed2016-09-07 14:30:39 -0700447 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700448
Brett Cannond0600ed2016-09-07 14:30:39 -0700449 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700450 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700451
452 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700453 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700454 }
455 }
456
Victor Stinner23e79442017-06-28 02:12:00 +0200457 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700458 }
459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 Py_XDECREF(co->co_code);
461 Py_XDECREF(co->co_consts);
462 Py_XDECREF(co->co_names);
463 Py_XDECREF(co->co_varnames);
464 Py_XDECREF(co->co_freevars);
465 Py_XDECREF(co->co_cellvars);
466 Py_XDECREF(co->co_filename);
467 Py_XDECREF(co->co_name);
468 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500469 if (co->co_cell2arg != NULL)
470 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 if (co->co_zombieframe != NULL)
472 PyObject_GC_Del(co->co_zombieframe);
473 if (co->co_weakreflist != NULL)
474 PyObject_ClearWeakRefs((PyObject*)co);
475 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476}
477
478static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200479code_sizeof(PyCodeObject *co, void *unused)
480{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900481 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
482 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200483
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300484 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200485 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300486 }
487 if (co_extra != NULL) {
488 res += sizeof(_PyCodeObjectExtra) +
489 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
490 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200491 return PyLong_FromSsize_t(res);
492}
493
494static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495code_repr(PyCodeObject *co)
496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 int lineno;
498 if (co->co_firstlineno != 0)
499 lineno = co->co_firstlineno;
500 else
501 lineno = -1;
502 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
503 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000504 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 co->co_name, co, co->co_filename, lineno);
506 } else {
507 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000508 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 co->co_name, co, lineno);
510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511}
512
Victor Stinnerefb24132016-01-22 12:33:12 +0100513PyObject*
514_PyCode_ConstantKey(PyObject *op)
515{
516 PyObject *key;
517
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300518 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100519 if (op == Py_None || op == Py_Ellipsis
520 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100521 || PyUnicode_CheckExact(op)
522 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300523 || PyCode_Check(op))
524 {
525 /* Objects of these types are always different from object of other
526 * type and from tuples. */
527 Py_INCREF(op);
528 key = op;
529 }
530 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
531 /* Make booleans different from integers 0 and 1.
532 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100533 key = PyTuple_Pack(2, Py_TYPE(op), op);
534 }
535 else if (PyFloat_CheckExact(op)) {
536 double d = PyFloat_AS_DOUBLE(op);
537 /* all we need is to make the tuple different in either the 0.0
538 * or -0.0 case from all others, just to avoid the "coercion".
539 */
540 if (d == 0.0 && copysign(1.0, d) < 0.0)
541 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
542 else
543 key = PyTuple_Pack(2, Py_TYPE(op), op);
544 }
545 else if (PyComplex_CheckExact(op)) {
546 Py_complex z;
547 int real_negzero, imag_negzero;
548 /* For the complex case we must make complex(x, 0.)
549 different from complex(x, -0.) and complex(0., y)
550 different from complex(-0., y), for any x and y.
551 All four complex zeros must be distinguished.*/
552 z = PyComplex_AsCComplex(op);
553 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
554 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
555 /* use True, False and None singleton as tags for the real and imag
556 * sign, to make tuples different */
557 if (real_negzero && imag_negzero) {
558 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
559 }
560 else if (imag_negzero) {
561 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
562 }
563 else if (real_negzero) {
564 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
565 }
566 else {
567 key = PyTuple_Pack(2, Py_TYPE(op), op);
568 }
569 }
570 else if (PyTuple_CheckExact(op)) {
571 Py_ssize_t i, len;
572 PyObject *tuple;
573
574 len = PyTuple_GET_SIZE(op);
575 tuple = PyTuple_New(len);
576 if (tuple == NULL)
577 return NULL;
578
579 for (i=0; i < len; i++) {
580 PyObject *item, *item_key;
581
582 item = PyTuple_GET_ITEM(op, i);
583 item_key = _PyCode_ConstantKey(item);
584 if (item_key == NULL) {
585 Py_DECREF(tuple);
586 return NULL;
587 }
588
589 PyTuple_SET_ITEM(tuple, i, item_key);
590 }
591
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200592 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100593 Py_DECREF(tuple);
594 }
595 else if (PyFrozenSet_CheckExact(op)) {
596 Py_ssize_t pos = 0;
597 PyObject *item;
598 Py_hash_t hash;
599 Py_ssize_t i, len;
600 PyObject *tuple, *set;
601
602 len = PySet_GET_SIZE(op);
603 tuple = PyTuple_New(len);
604 if (tuple == NULL)
605 return NULL;
606
607 i = 0;
608 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
609 PyObject *item_key;
610
611 item_key = _PyCode_ConstantKey(item);
612 if (item_key == NULL) {
613 Py_DECREF(tuple);
614 return NULL;
615 }
616
617 assert(i < len);
618 PyTuple_SET_ITEM(tuple, i, item_key);
619 i++;
620 }
621 set = PyFrozenSet_New(tuple);
622 Py_DECREF(tuple);
623 if (set == NULL)
624 return NULL;
625
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200626 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100627 Py_DECREF(set);
628 return key;
629 }
630 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000631 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100632 * to ensure that they are seen as unequal. */
633 PyObject *obj_id = PyLong_FromVoidPtr(op);
634 if (obj_id == NULL)
635 return NULL;
636
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200637 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100638 Py_DECREF(obj_id);
639 }
640 return key;
641}
642
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000643static PyObject *
644code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 PyCodeObject *co, *cp;
647 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100648 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 if ((op != Py_EQ && op != Py_NE) ||
652 !PyCode_Check(self) ||
653 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500654 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 co = (PyCodeObject *)self;
658 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100661 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 eq = co->co_argcount == cp->co_argcount;
663 if (!eq) goto unequal;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100664 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
665 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
667 if (!eq) goto unequal;
668 eq = co->co_nlocals == cp->co_nlocals;
669 if (!eq) goto unequal;
670 eq = co->co_flags == cp->co_flags;
671 if (!eq) goto unequal;
672 eq = co->co_firstlineno == cp->co_firstlineno;
673 if (!eq) goto unequal;
674 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
675 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100676
677 /* compare constants */
678 consts1 = _PyCode_ConstantKey(co->co_consts);
679 if (!consts1)
680 return NULL;
681 consts2 = _PyCode_ConstantKey(cp->co_consts);
682 if (!consts2) {
683 Py_DECREF(consts1);
684 return NULL;
685 }
686 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
687 Py_DECREF(consts1);
688 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
692 if (eq <= 0) goto unequal;
693 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
694 if (eq <= 0) goto unequal;
695 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
696 if (eq <= 0) goto unequal;
697 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
698 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (op == Py_EQ)
701 res = Py_True;
702 else
703 res = Py_False;
704 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000705
706 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 if (eq < 0)
708 return NULL;
709 if (op == Py_NE)
710 res = Py_True;
711 else
712 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000713
714 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 Py_INCREF(res);
716 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717}
718
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000719static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720code_hash(PyCodeObject *co)
721{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000722 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 h0 = PyObject_Hash(co->co_name);
724 if (h0 == -1) return -1;
725 h1 = PyObject_Hash(co->co_code);
726 if (h1 == -1) return -1;
727 h2 = PyObject_Hash(co->co_consts);
728 if (h2 == -1) return -1;
729 h3 = PyObject_Hash(co->co_names);
730 if (h3 == -1) return -1;
731 h4 = PyObject_Hash(co->co_varnames);
732 if (h4 == -1) return -1;
733 h5 = PyObject_Hash(co->co_freevars);
734 if (h5 == -1) return -1;
735 h6 = PyObject_Hash(co->co_cellvars);
736 if (h6 == -1) return -1;
737 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100738 co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 co->co_nlocals ^ co->co_flags;
740 if (h == -1) h = -2;
741 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742}
743
744/* XXX code objects need to participate in GC? */
745
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200746static struct PyMethodDef code_methods[] = {
747 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
748 {NULL, NULL} /* sentinel */
749};
750
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PyVarObject_HEAD_INIT(&PyType_Type, 0)
753 "code",
754 sizeof(PyCodeObject),
755 0,
756 (destructor)code_dealloc, /* tp_dealloc */
757 0, /* tp_print */
758 0, /* tp_getattr */
759 0, /* tp_setattr */
760 0, /* tp_reserved */
761 (reprfunc)code_repr, /* tp_repr */
762 0, /* tp_as_number */
763 0, /* tp_as_sequence */
764 0, /* tp_as_mapping */
765 (hashfunc)code_hash, /* tp_hash */
766 0, /* tp_call */
767 0, /* tp_str */
768 PyObject_GenericGetAttr, /* tp_getattro */
769 0, /* tp_setattro */
770 0, /* tp_as_buffer */
771 Py_TPFLAGS_DEFAULT, /* tp_flags */
772 code_doc, /* tp_doc */
773 0, /* tp_traverse */
774 0, /* tp_clear */
775 code_richcompare, /* tp_richcompare */
776 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
777 0, /* tp_iter */
778 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200779 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 code_memberlist, /* tp_members */
781 0, /* tp_getset */
782 0, /* tp_base */
783 0, /* tp_dict */
784 0, /* tp_descr_get */
785 0, /* tp_descr_set */
786 0, /* tp_dictoffset */
787 0, /* tp_init */
788 0, /* tp_alloc */
789 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790};
791
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000792/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
793 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794*/
795
796int
797PyCode_Addr2Line(PyCodeObject *co, int addrq)
798{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000799 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
801 int line = co->co_firstlineno;
802 int addr = 0;
803 while (--size >= 0) {
804 addr += *p++;
805 if (addr > addrq)
806 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100807 line += (signed char)*p;
808 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 }
810 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000812
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000813/* Update *bounds to describe the first and one-past-the-last instructions in
814 the same line as lasti. Return the number of that line. */
815int
816_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000817{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000818 Py_ssize_t size;
819 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
823 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 addr = 0;
826 line = co->co_firstlineno;
827 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 /* possible optimization: if f->f_lasti == instr_ub
830 (likely to be a common case) then we already know
831 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700832 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 /* See lnotab_notes.txt for the description of
835 co_lnotab. A point to remember: increments to p
836 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 bounds->ap_lower = 0;
839 while (size > 0) {
840 if (addr + *p > lasti)
841 break;
842 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100843 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100845 line += (signed char)*p;
846 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 --size;
848 }
849
850 if (size > 0) {
851 while (--size >= 0) {
852 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100853 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100855 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000856 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 bounds->ap_upper = addr;
858 }
859 else {
860 bounds->ap_upper = INT_MAX;
861 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864}
Brett Cannon5c4de282016-09-07 11:16:41 -0700865
866
867int
868_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
869{
Brett Cannon5c4de282016-09-07 11:16:41 -0700870 if (!PyCode_Check(code)) {
871 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700872 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700873 }
874
Brett Cannond0600ed2016-09-07 14:30:39 -0700875 PyCodeObject *o = (PyCodeObject*) code;
876 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700877
Brett Cannond0600ed2016-09-07 14:30:39 -0700878 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700879 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700880 return 0;
881 }
882
Brett Cannond0600ed2016-09-07 14:30:39 -0700883 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700884 return 0;
885}
886
887
888int
889_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
890{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200891 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -0700892
893 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700894 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700895 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700896 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700897 }
898
Brett Cannond0600ed2016-09-07 14:30:39 -0700899 PyCodeObject *o = (PyCodeObject*) code;
900 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700901
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300902 if (co_extra == NULL || co_extra->ce_size <= index) {
903 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
904 co_extra = PyMem_Realloc(
905 co_extra,
906 sizeof(_PyCodeObjectExtra) +
907 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +0000908 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700909 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700910 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300911 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700912 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700913 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700914 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300915 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700916 }
917
918 if (co_extra->ce_extras[index] != NULL) {
919 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700920 if (free != NULL) {
921 free(co_extra->ce_extras[index]);
922 }
Brett Cannon5c4de282016-09-07 11:16:41 -0700923 }
924
Brett Cannond0600ed2016-09-07 14:30:39 -0700925 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700926 return 0;
927}