blob: f4e48a9757e5879bb510e031ca118fefb0cdbd59 [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
Steve Dowerb82e17e2019-05-23 08:45:22 -0700383 if (PySys_Audit("code.__new__", "OOOiiiii",
384 code, filename, name, argcount, kwonlyargcount,
385 nlocals, stacksize, flags) < 0) {
386 goto cleanup;
387 }
388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (argcount < 0) {
390 PyErr_SetString(
391 PyExc_ValueError,
392 "code: argcount must not be negative");
393 goto cleanup;
394 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100396 if (posonlyargcount < 0) {
397 PyErr_SetString(
398 PyExc_ValueError,
399 "code: posonlyargcount must not be negative");
400 goto cleanup;
401 }
402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (kwonlyargcount < 0) {
404 PyErr_SetString(
405 PyExc_ValueError,
406 "code: kwonlyargcount must not be negative");
407 goto cleanup;
408 }
409 if (nlocals < 0) {
410 PyErr_SetString(
411 PyExc_ValueError,
412 "code: nlocals must not be negative");
413 goto cleanup;
414 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 ournames = validate_and_copy_tuple(names);
417 if (ournames == NULL)
418 goto cleanup;
419 ourvarnames = validate_and_copy_tuple(varnames);
420 if (ourvarnames == NULL)
421 goto cleanup;
422 if (freevars)
423 ourfreevars = validate_and_copy_tuple(freevars);
424 else
425 ourfreevars = PyTuple_New(0);
426 if (ourfreevars == NULL)
427 goto cleanup;
428 if (cellvars)
429 ourcellvars = validate_and_copy_tuple(cellvars);
430 else
431 ourcellvars = PyTuple_New(0);
432 if (ourcellvars == NULL)
433 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100435 co = (PyObject *)PyCode_New(argcount, posonlyargcount, kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 nlocals, stacksize, flags,
437 code, consts, ournames, ourvarnames,
438 ourfreevars, ourcellvars, filename,
439 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 Py_XDECREF(ournames);
442 Py_XDECREF(ourvarnames);
443 Py_XDECREF(ourfreevars);
444 Py_XDECREF(ourcellvars);
445 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446}
447
448static void
449code_dealloc(PyCodeObject *co)
450{
Brett Cannon5c4de282016-09-07 11:16:41 -0700451 if (co->co_extra != NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200452 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannond0600ed2016-09-07 14:30:39 -0700453 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700454
Brett Cannond0600ed2016-09-07 14:30:39 -0700455 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700456 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700457
458 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700459 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700460 }
461 }
462
Victor Stinner23e79442017-06-28 02:12:00 +0200463 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700464 }
465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 Py_XDECREF(co->co_code);
467 Py_XDECREF(co->co_consts);
468 Py_XDECREF(co->co_names);
469 Py_XDECREF(co->co_varnames);
470 Py_XDECREF(co->co_freevars);
471 Py_XDECREF(co->co_cellvars);
472 Py_XDECREF(co->co_filename);
473 Py_XDECREF(co->co_name);
474 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500475 if (co->co_cell2arg != NULL)
476 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (co->co_zombieframe != NULL)
478 PyObject_GC_Del(co->co_zombieframe);
479 if (co->co_weakreflist != NULL)
480 PyObject_ClearWeakRefs((PyObject*)co);
481 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482}
483
484static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200485code_sizeof(PyCodeObject *co, void *unused)
486{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900487 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
488 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200489
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300490 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200491 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300492 }
493 if (co_extra != NULL) {
494 res += sizeof(_PyCodeObjectExtra) +
495 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
496 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200497 return PyLong_FromSsize_t(res);
498}
499
500static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501code_repr(PyCodeObject *co)
502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 int lineno;
504 if (co->co_firstlineno != 0)
505 lineno = co->co_firstlineno;
506 else
507 lineno = -1;
508 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
509 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000510 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 co->co_name, co, co->co_filename, lineno);
512 } else {
513 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000514 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 co->co_name, co, lineno);
516 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517}
518
Victor Stinnerefb24132016-01-22 12:33:12 +0100519PyObject*
520_PyCode_ConstantKey(PyObject *op)
521{
522 PyObject *key;
523
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300524 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100525 if (op == Py_None || op == Py_Ellipsis
526 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100527 || PyUnicode_CheckExact(op)
528 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300529 || PyCode_Check(op))
530 {
531 /* Objects of these types are always different from object of other
532 * type and from tuples. */
533 Py_INCREF(op);
534 key = op;
535 }
536 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
537 /* Make booleans different from integers 0 and 1.
538 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100539 key = PyTuple_Pack(2, Py_TYPE(op), op);
540 }
541 else if (PyFloat_CheckExact(op)) {
542 double d = PyFloat_AS_DOUBLE(op);
543 /* all we need is to make the tuple different in either the 0.0
544 * or -0.0 case from all others, just to avoid the "coercion".
545 */
546 if (d == 0.0 && copysign(1.0, d) < 0.0)
547 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
548 else
549 key = PyTuple_Pack(2, Py_TYPE(op), op);
550 }
551 else if (PyComplex_CheckExact(op)) {
552 Py_complex z;
553 int real_negzero, imag_negzero;
554 /* For the complex case we must make complex(x, 0.)
555 different from complex(x, -0.) and complex(0., y)
556 different from complex(-0., y), for any x and y.
557 All four complex zeros must be distinguished.*/
558 z = PyComplex_AsCComplex(op);
559 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
560 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
561 /* use True, False and None singleton as tags for the real and imag
562 * sign, to make tuples different */
563 if (real_negzero && imag_negzero) {
564 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
565 }
566 else if (imag_negzero) {
567 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
568 }
569 else if (real_negzero) {
570 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
571 }
572 else {
573 key = PyTuple_Pack(2, Py_TYPE(op), op);
574 }
575 }
576 else if (PyTuple_CheckExact(op)) {
577 Py_ssize_t i, len;
578 PyObject *tuple;
579
580 len = PyTuple_GET_SIZE(op);
581 tuple = PyTuple_New(len);
582 if (tuple == NULL)
583 return NULL;
584
585 for (i=0; i < len; i++) {
586 PyObject *item, *item_key;
587
588 item = PyTuple_GET_ITEM(op, i);
589 item_key = _PyCode_ConstantKey(item);
590 if (item_key == NULL) {
591 Py_DECREF(tuple);
592 return NULL;
593 }
594
595 PyTuple_SET_ITEM(tuple, i, item_key);
596 }
597
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200598 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100599 Py_DECREF(tuple);
600 }
601 else if (PyFrozenSet_CheckExact(op)) {
602 Py_ssize_t pos = 0;
603 PyObject *item;
604 Py_hash_t hash;
605 Py_ssize_t i, len;
606 PyObject *tuple, *set;
607
608 len = PySet_GET_SIZE(op);
609 tuple = PyTuple_New(len);
610 if (tuple == NULL)
611 return NULL;
612
613 i = 0;
614 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
615 PyObject *item_key;
616
617 item_key = _PyCode_ConstantKey(item);
618 if (item_key == NULL) {
619 Py_DECREF(tuple);
620 return NULL;
621 }
622
623 assert(i < len);
624 PyTuple_SET_ITEM(tuple, i, item_key);
625 i++;
626 }
627 set = PyFrozenSet_New(tuple);
628 Py_DECREF(tuple);
629 if (set == NULL)
630 return NULL;
631
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200632 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100633 Py_DECREF(set);
634 return key;
635 }
636 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000637 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100638 * to ensure that they are seen as unequal. */
639 PyObject *obj_id = PyLong_FromVoidPtr(op);
640 if (obj_id == NULL)
641 return NULL;
642
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200643 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100644 Py_DECREF(obj_id);
645 }
646 return key;
647}
648
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000649static PyObject *
650code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyCodeObject *co, *cp;
653 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100654 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 if ((op != Py_EQ && op != Py_NE) ||
658 !PyCode_Check(self) ||
659 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500660 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 co = (PyCodeObject *)self;
664 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100667 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 eq = co->co_argcount == cp->co_argcount;
669 if (!eq) goto unequal;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100670 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
671 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
673 if (!eq) goto unequal;
674 eq = co->co_nlocals == cp->co_nlocals;
675 if (!eq) goto unequal;
676 eq = co->co_flags == cp->co_flags;
677 if (!eq) goto unequal;
678 eq = co->co_firstlineno == cp->co_firstlineno;
679 if (!eq) goto unequal;
680 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
681 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100682
683 /* compare constants */
684 consts1 = _PyCode_ConstantKey(co->co_consts);
685 if (!consts1)
686 return NULL;
687 consts2 = _PyCode_ConstantKey(cp->co_consts);
688 if (!consts2) {
689 Py_DECREF(consts1);
690 return NULL;
691 }
692 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
693 Py_DECREF(consts1);
694 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
698 if (eq <= 0) goto unequal;
699 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
700 if (eq <= 0) goto unequal;
701 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
702 if (eq <= 0) goto unequal;
703 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
704 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (op == Py_EQ)
707 res = Py_True;
708 else
709 res = Py_False;
710 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000711
712 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (eq < 0)
714 return NULL;
715 if (op == Py_NE)
716 res = Py_True;
717 else
718 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000719
720 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 Py_INCREF(res);
722 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723}
724
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000725static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726code_hash(PyCodeObject *co)
727{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000728 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 h0 = PyObject_Hash(co->co_name);
730 if (h0 == -1) return -1;
731 h1 = PyObject_Hash(co->co_code);
732 if (h1 == -1) return -1;
733 h2 = PyObject_Hash(co->co_consts);
734 if (h2 == -1) return -1;
735 h3 = PyObject_Hash(co->co_names);
736 if (h3 == -1) return -1;
737 h4 = PyObject_Hash(co->co_varnames);
738 if (h4 == -1) return -1;
739 h5 = PyObject_Hash(co->co_freevars);
740 if (h5 == -1) return -1;
741 h6 = PyObject_Hash(co->co_cellvars);
742 if (h6 == -1) return -1;
743 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100744 co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 co->co_nlocals ^ co->co_flags;
746 if (h == -1) h = -2;
747 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748}
749
750/* XXX code objects need to participate in GC? */
751
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200752static struct PyMethodDef code_methods[] = {
753 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
754 {NULL, NULL} /* sentinel */
755};
756
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 PyVarObject_HEAD_INIT(&PyType_Type, 0)
759 "code",
760 sizeof(PyCodeObject),
761 0,
762 (destructor)code_dealloc, /* tp_dealloc */
763 0, /* tp_print */
764 0, /* tp_getattr */
765 0, /* tp_setattr */
766 0, /* tp_reserved */
767 (reprfunc)code_repr, /* tp_repr */
768 0, /* tp_as_number */
769 0, /* tp_as_sequence */
770 0, /* tp_as_mapping */
771 (hashfunc)code_hash, /* tp_hash */
772 0, /* tp_call */
773 0, /* tp_str */
774 PyObject_GenericGetAttr, /* tp_getattro */
775 0, /* tp_setattro */
776 0, /* tp_as_buffer */
777 Py_TPFLAGS_DEFAULT, /* tp_flags */
778 code_doc, /* tp_doc */
779 0, /* tp_traverse */
780 0, /* tp_clear */
781 code_richcompare, /* tp_richcompare */
782 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
783 0, /* tp_iter */
784 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200785 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 code_memberlist, /* tp_members */
787 0, /* tp_getset */
788 0, /* tp_base */
789 0, /* tp_dict */
790 0, /* tp_descr_get */
791 0, /* tp_descr_set */
792 0, /* tp_dictoffset */
793 0, /* tp_init */
794 0, /* tp_alloc */
795 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796};
797
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000798/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
799 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800*/
801
802int
803PyCode_Addr2Line(PyCodeObject *co, int addrq)
804{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000805 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
807 int line = co->co_firstlineno;
808 int addr = 0;
809 while (--size >= 0) {
810 addr += *p++;
811 if (addr > addrq)
812 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100813 line += (signed char)*p;
814 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 }
816 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000819/* Update *bounds to describe the first and one-past-the-last instructions in
820 the same line as lasti. Return the number of that line. */
821int
822_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000823{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000824 Py_ssize_t size;
825 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
829 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 addr = 0;
832 line = co->co_firstlineno;
833 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 /* possible optimization: if f->f_lasti == instr_ub
836 (likely to be a common case) then we already know
837 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700838 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 /* See lnotab_notes.txt for the description of
841 co_lnotab. A point to remember: increments to p
842 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 bounds->ap_lower = 0;
845 while (size > 0) {
846 if (addr + *p > lasti)
847 break;
848 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100849 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100851 line += (signed char)*p;
852 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 --size;
854 }
855
856 if (size > 0) {
857 while (--size >= 0) {
858 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100859 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100861 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 bounds->ap_upper = addr;
864 }
865 else {
866 bounds->ap_upper = INT_MAX;
867 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000870}
Brett Cannon5c4de282016-09-07 11:16:41 -0700871
872
873int
874_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
875{
Brett Cannon5c4de282016-09-07 11:16:41 -0700876 if (!PyCode_Check(code)) {
877 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700878 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700879 }
880
Brett Cannond0600ed2016-09-07 14:30:39 -0700881 PyCodeObject *o = (PyCodeObject*) code;
882 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700883
Brett Cannond0600ed2016-09-07 14:30:39 -0700884 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700885 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700886 return 0;
887 }
888
Brett Cannond0600ed2016-09-07 14:30:39 -0700889 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700890 return 0;
891}
892
893
894int
895_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
896{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200897 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -0700898
899 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700900 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700901 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700902 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700903 }
904
Brett Cannond0600ed2016-09-07 14:30:39 -0700905 PyCodeObject *o = (PyCodeObject*) code;
906 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700907
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300908 if (co_extra == NULL || co_extra->ce_size <= index) {
909 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
910 co_extra = PyMem_Realloc(
911 co_extra,
912 sizeof(_PyCodeObjectExtra) +
913 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +0000914 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700915 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700916 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300917 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700918 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700919 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700920 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300921 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700922 }
923
924 if (co_extra->ce_extras[index] != NULL) {
925 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700926 if (free != NULL) {
927 free(co_extra->ce_extras[index]);
928 }
Brett Cannon5c4de282016-09-07 11:16:41 -0700929 }
930
Brett Cannond0600ed2016-09-07 14:30:39 -0700931 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700932 return 0;
933}