blob: 9bb49f108be0c669e97f2710ff3b5235f90d2aee [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"
Inada Naoki91234a12019-06-03 21:30:58 +09005#include "opcode.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02006#include "structmember.h" // PyMemberDef
Victor Stinner384621c2020-06-22 17:27:35 +02007#include "pycore_code.h" // _PyOpcache
Victor Stinner4a3fe082020-04-14 14:26:24 +02008#include "pycore_interp.h" // PyInterpreterState.co_extra_freefuncs
Victor Stinner81a7be32020-04-14 15:14:01 +02009#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner384621c2020-06-22 17:27:35 +020010#include "pycore_tuple.h" // _PyTuple_ITEMS()
Victor Stinnera9f05d62019-05-24 23:57:23 +020011#include "clinic/codeobject.c.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
Brett Cannond0600ed2016-09-07 14:30:39 -070013/* Holder for co_extra information */
14typedef struct {
15 Py_ssize_t ce_size;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +030016 void *ce_extras[1];
Brett Cannond0600ed2016-09-07 14:30:39 -070017} _PyCodeObjectExtra;
18
Victor Stinnera9f05d62019-05-24 23:57:23 +020019/*[clinic input]
20class code "PyCodeObject *" "&PyCode_Type"
21[clinic start generated code]*/
22/*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
23
Benjamin Peterson8e0ad462017-09-07 23:35:53 -070024/* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000025static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020026all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027{
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++) {
Benjamin Peterson2b7953d2017-09-08 10:35:49 -070036 if (!Py_ISALNUM(*s) && *s != '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 return 0;
38 }
39 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040}
41
Victor Stinnera2783132020-01-27 23:24:13 +010042static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043intern_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)) {
Victor Stinnera2783132020-01-27 23:24:13 +010050 PyErr_SetString(PyExc_SystemError,
51 "non-string found in code slot");
52 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 }
Victor Stinnerd17a6932018-11-09 16:56:48 +010054 PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 }
Victor Stinnera2783132020-01-27 23:24:13 +010056 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057}
58
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030059/* Intern selected string constants */
60static int
Victor Stinnera2783132020-01-27 23:24:13 +010061intern_string_constants(PyObject *tuple, int *modified)
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030062{
Victor Stinnera2783132020-01-27 23:24:13 +010063 for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030064 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) {
Victor Stinnera2783132020-01-27 23:24:13 +010067 return -1;
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030068 }
Victor Stinnera2783132020-01-27 23:24:13 +010069
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);
Victor Stinnera2783132020-01-27 23:24:13 +010075 if (modified) {
76 *modified = 1;
77 }
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030078 }
79 }
80 }
81 else if (PyTuple_CheckExact(v)) {
Victor Stinnera2783132020-01-27 23:24:13 +010082 if (intern_string_constants(v, NULL) < 0) {
83 return -1;
84 }
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030085 }
86 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050087 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030088 PyObject *tmp = PySequence_Tuple(v);
89 if (tmp == NULL) {
Victor Stinnera2783132020-01-27 23:24:13 +010090 return -1;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030091 }
Victor Stinnera2783132020-01-27 23:24:13 +010092 int tmp_modified = 0;
93 if (intern_string_constants(tmp, &tmp_modified) < 0) {
94 Py_DECREF(tmp);
95 return -1;
96 }
97 if (tmp_modified) {
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030098 v = PyFrozenSet_New(tmp);
99 if (v == NULL) {
Victor Stinnera2783132020-01-27 23:24:13 +0100100 Py_DECREF(tmp);
101 return -1;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300102 }
Victor Stinnera2783132020-01-27 23:24:13 +0100103
104 PyTuple_SET_ITEM(tuple, i, v);
105 Py_DECREF(w);
106 if (modified) {
107 *modified = 1;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300108 }
109 }
110 Py_DECREF(tmp);
111 }
112 }
Victor Stinnera2783132020-01-27 23:24:13 +0100113 return 0;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300114}
115
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000116PyCodeObject *
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100117PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
118 int nlocals, int stacksize, int flags,
119 PyObject *code, PyObject *consts, PyObject *names,
120 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
121 PyObject *filename, PyObject *name, int firstlineno,
Mark Shannon877df852020-11-12 09:43:29 +0000122 PyObject *linetable)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200125 Py_ssize_t *cell2arg = NULL;
Serhiy Storchakabd473842018-07-16 09:10:19 +0300126 Py_ssize_t i, n_cellvars, n_varnames, total_args;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 /* Check argument types */
Pablo Galindocd74e662019-06-01 18:08:04 +0100129 if (argcount < posonlyargcount || posonlyargcount < 0 ||
130 kwonlyargcount < 0 || nlocals < 0 ||
131 stacksize < 0 || flags < 0 ||
Victor Stinnera9f05d62019-05-24 23:57:23 +0200132 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 consts == NULL || !PyTuple_Check(consts) ||
134 names == NULL || !PyTuple_Check(names) ||
135 varnames == NULL || !PyTuple_Check(varnames) ||
136 freevars == NULL || !PyTuple_Check(freevars) ||
137 cellvars == NULL || !PyTuple_Check(cellvars) ||
138 name == NULL || !PyUnicode_Check(name) ||
139 filename == NULL || !PyUnicode_Check(filename) ||
Mark Shannon877df852020-11-12 09:43:29 +0000140 linetable == NULL || !PyBytes_Check(linetable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 PyErr_BadInternalCall();
142 return NULL;
143 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200144
Victor Stinnera9f05d62019-05-24 23:57:23 +0200145 /* Ensure that strings are ready Unicode string */
146 if (PyUnicode_READY(name) < 0) {
Victor Stinner7c74de42013-10-10 15:55:14 +0200147 return NULL;
Victor Stinnera9f05d62019-05-24 23:57:23 +0200148 }
149 if (PyUnicode_READY(filename) < 0) {
150 return NULL;
151 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200152
Victor Stinnera2783132020-01-27 23:24:13 +0100153 if (intern_strings(names) < 0) {
154 return NULL;
155 }
156 if (intern_strings(varnames) < 0) {
157 return NULL;
158 }
159 if (intern_strings(freevars) < 0) {
160 return NULL;
161 }
162 if (intern_strings(cellvars) < 0) {
163 return NULL;
164 }
165 if (intern_string_constants(consts, NULL) < 0) {
166 return NULL;
167 }
Nick Coghlan078f1812017-12-03 11:12:20 +1000168
Ammar Askar3b3b83c2020-06-10 23:31:22 +0000169 /* Make sure that code is indexable with an int, this is
170 a long running assumption in ceval.c and many parts of
171 the interpreter. */
172 if (PyBytes_GET_SIZE(code) > INT_MAX) {
173 PyErr_SetString(PyExc_OverflowError, "co_code larger than INT_MAX");
174 return NULL;
175 }
176
Nick Coghlan078f1812017-12-03 11:12:20 +1000177 /* Check for any inner or outer closure references */
178 n_cellvars = PyTuple_GET_SIZE(cellvars);
179 if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
180 flags |= CO_NOFREE;
181 } else {
182 flags &= ~CO_NOFREE;
183 }
184
Serhiy Storchakabd473842018-07-16 09:10:19 +0300185 n_varnames = PyTuple_GET_SIZE(varnames);
Pablo Galindocd74e662019-06-01 18:08:04 +0100186 if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
Serhiy Storchakabd473842018-07-16 09:10:19 +0300187 /* Never overflows. */
Pablo Galindocd74e662019-06-01 18:08:04 +0100188 total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100189 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
Serhiy Storchakabd473842018-07-16 09:10:19 +0300190 }
191 else {
192 total_args = n_varnames + 1;
193 }
194 if (total_args > n_varnames) {
195 PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
196 return NULL;
197 }
198
Benjamin Peterson90037602011-06-25 22:54:45 -0500199 /* Create mapping between cells and arguments if needed. */
200 if (n_cellvars) {
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700201 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200202 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
203 if (cell2arg == NULL) {
204 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500205 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200206 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500207 /* Find cells which are also arguments. */
208 for (i = 0; i < n_cellvars; i++) {
209 Py_ssize_t j;
210 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200211 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500212 for (j = 0; j < total_args; j++) {
213 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200214 int cmp = PyUnicode_Compare(cell, arg);
215 if (cmp == -1 && PyErr_Occurred()) {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100216 PyMem_Free(cell2arg);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200217 return NULL;
218 }
219 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500220 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700221 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500222 break;
223 }
224 }
225 }
226 if (!used_cell2arg) {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100227 PyMem_Free(cell2arg);
Benjamin Peterson90037602011-06-25 22:54:45 -0500228 cell2arg = NULL;
229 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 }
Victor Stinner92055202020-04-08 00:38:15 +0200231 co = PyObject_New(PyCodeObject, &PyCode_Type);
Benjamin Peterson90037602011-06-25 22:54:45 -0500232 if (co == NULL) {
233 if (cell2arg)
Victor Stinner00d7abd2020-12-01 09:56:42 +0100234 PyMem_Free(cell2arg);
Benjamin Peterson90037602011-06-25 22:54:45 -0500235 return NULL;
236 }
237 co->co_argcount = argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100238 co->co_posonlyargcount = posonlyargcount;
Benjamin Peterson90037602011-06-25 22:54:45 -0500239 co->co_kwonlyargcount = kwonlyargcount;
240 co->co_nlocals = nlocals;
241 co->co_stacksize = stacksize;
242 co->co_flags = flags;
243 Py_INCREF(code);
244 co->co_code = code;
245 Py_INCREF(consts);
246 co->co_consts = consts;
247 Py_INCREF(names);
248 co->co_names = names;
249 Py_INCREF(varnames);
250 co->co_varnames = varnames;
251 Py_INCREF(freevars);
252 co->co_freevars = freevars;
253 Py_INCREF(cellvars);
254 co->co_cellvars = cellvars;
255 co->co_cell2arg = cell2arg;
256 Py_INCREF(filename);
257 co->co_filename = filename;
258 Py_INCREF(name);
259 co->co_name = name;
260 co->co_firstlineno = firstlineno;
Mark Shannon877df852020-11-12 09:43:29 +0000261 Py_INCREF(linetable);
262 co->co_linetable = linetable;
Benjamin Peterson90037602011-06-25 22:54:45 -0500263 co->co_zombieframe = NULL;
264 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700265 co->co_extra = NULL;
Inada Naoki91234a12019-06-03 21:30:58 +0900266
267 co->co_opcache_map = NULL;
268 co->co_opcache = NULL;
269 co->co_opcache_flag = 0;
270 co->co_opcache_size = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272}
273
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100274PyCodeObject *
275PyCode_New(int argcount, int kwonlyargcount,
276 int nlocals, int stacksize, int flags,
277 PyObject *code, PyObject *consts, PyObject *names,
278 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
279 PyObject *filename, PyObject *name, int firstlineno,
Mark Shannon877df852020-11-12 09:43:29 +0000280 PyObject *linetable)
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100281{
282 return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
283 stacksize, flags, code, consts, names,
284 varnames, freevars, cellvars, filename,
Mark Shannon877df852020-11-12 09:43:29 +0000285 name, firstlineno, linetable);
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100286}
287
Inada Naoki91234a12019-06-03 21:30:58 +0900288int
289_PyCode_InitOpcache(PyCodeObject *co)
290{
291 Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);
292 co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);
293 if (co->co_opcache_map == NULL) {
294 return -1;
295 }
296
297 _Py_CODEUNIT *opcodes = (_Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);
298 Py_ssize_t opts = 0;
299
300 for (Py_ssize_t i = 0; i < co_size;) {
301 unsigned char opcode = _Py_OPCODE(opcodes[i]);
302 i++; // 'i' is now aligned to (next_instr - first_instr)
303
Pablo Galindo109826c2020-10-20 06:22:44 +0100304 // TODO: LOAD_METHOD
305 if (opcode == LOAD_GLOBAL || opcode == LOAD_ATTR) {
Victor Stinnerea9f1682019-06-04 17:08:24 +0200306 opts++;
307 co->co_opcache_map[i] = (unsigned char)opts;
Inada Naoki91234a12019-06-03 21:30:58 +0900308 if (opts > 254) {
309 break;
310 }
311 }
312 }
313
314 if (opts) {
315 co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));
316 if (co->co_opcache == NULL) {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100317 PyMem_Free(co->co_opcache_map);
Inada Naoki91234a12019-06-03 21:30:58 +0900318 return -1;
319 }
320 }
321 else {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100322 PyMem_Free(co->co_opcache_map);
Inada Naoki91234a12019-06-03 21:30:58 +0900323 co->co_opcache_map = NULL;
324 co->co_opcache = NULL;
325 }
326
Victor Stinner376ce982019-06-12 04:41:16 +0200327 co->co_opcache_size = (unsigned char)opts;
Inada Naoki91234a12019-06-03 21:30:58 +0900328 return 0;
329}
330
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000331PyCodeObject *
332PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 static PyObject *emptystring = NULL;
335 static PyObject *nulltuple = NULL;
336 PyObject *filename_ob = NULL;
337 PyObject *funcname_ob = NULL;
338 PyCodeObject *result = NULL;
339 if (emptystring == NULL) {
340 emptystring = PyBytes_FromString("");
341 if (emptystring == NULL)
342 goto failed;
343 }
344 if (nulltuple == NULL) {
345 nulltuple = PyTuple_New(0);
346 if (nulltuple == NULL)
347 goto failed;
348 }
349 funcname_ob = PyUnicode_FromString(funcname);
350 if (funcname_ob == NULL)
351 goto failed;
352 filename_ob = PyUnicode_DecodeFSDefault(filename);
353 if (filename_ob == NULL)
354 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000355
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100356 result = PyCode_NewWithPosOnlyArgs(
357 0, /* argcount */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100358 0, /* posonlyargcount */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 0, /* kwonlyargcount */
360 0, /* nlocals */
361 0, /* stacksize */
362 0, /* flags */
363 emptystring, /* code */
364 nulltuple, /* consts */
365 nulltuple, /* names */
366 nulltuple, /* varnames */
367 nulltuple, /* freevars */
368 nulltuple, /* cellvars */
369 filename_ob, /* filename */
370 funcname_ob, /* name */
371 firstlineno, /* firstlineno */
Mark Shannon877df852020-11-12 09:43:29 +0000372 emptystring /* linetable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000374
375failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 Py_XDECREF(funcname_ob);
377 Py_XDECREF(filename_ob);
378 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000379}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380
381#define OFF(x) offsetof(PyCodeObject, x)
382
383static PyMemberDef code_memberlist[] = {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100384 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
385 {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
386 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
387 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
388 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
389 {"co_flags", T_INT, OFF(co_flags), READONLY},
390 {"co_code", T_OBJECT, OFF(co_code), READONLY},
391 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
392 {"co_names", T_OBJECT, OFF(co_names), READONLY},
393 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
394 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
395 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
396 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
397 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Mark Shannon877df852020-11-12 09:43:29 +0000398 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
399 {"co_linetable", T_OBJECT, OFF(co_linetable), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000401};
402
Mark Shannon877df852020-11-12 09:43:29 +0000403static int
404emit_pair(PyObject **bytes, int *offset, int a, int b)
405{
406 Py_ssize_t len = PyBytes_GET_SIZE(*bytes);
407 if (*offset + 2 >= len) {
408 if (_PyBytes_Resize(bytes, len * 2) < 0)
409 return 0;
410 }
Pablo Galindo86e322f2021-01-30 13:54:22 +0000411 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(*bytes);
412 lnotab += *offset;
Mark Shannon877df852020-11-12 09:43:29 +0000413 *lnotab++ = a;
414 *lnotab++ = b;
415 *offset += 2;
416 return 1;
417}
418
419static int
420emit_delta(PyObject **bytes, int bdelta, int ldelta, int *offset)
421{
422 while (bdelta > 255) {
423 if (!emit_pair(bytes, offset, 255, 0)) {
424 return 0;
425 }
426 bdelta -= 255;
427 }
428 while (ldelta > 127) {
429 if (!emit_pair(bytes, offset, bdelta, 127)) {
430 return 0;
431 }
432 bdelta = 0;
433 ldelta -= 127;
434 }
435 while (ldelta < -128) {
436 if (!emit_pair(bytes, offset, bdelta, -128)) {
437 return 0;
438 }
439 bdelta = 0;
440 ldelta += 128;
441 }
442 return emit_pair(bytes, offset, bdelta, ldelta);
443}
444
445static PyObject *
446code_getlnotab(PyCodeObject *code, void *closure)
447{
448 PyCodeAddressRange bounds;
449 PyObject *bytes;
450 int table_offset = 0;
451 int code_offset = 0;
452 int line = code->co_firstlineno;
453 bytes = PyBytes_FromStringAndSize(NULL, 64);
454 if (bytes == NULL) {
455 return NULL;
456 }
457 _PyCode_InitAddressRange(code, &bounds);
458 while (PyLineTable_NextAddressRange(&bounds)) {
459 if (bounds.ar_computed_line != line) {
460 int bdelta = bounds.ar_start - code_offset;
461 int ldelta = bounds.ar_computed_line - line;
462 if (!emit_delta(&bytes, bdelta, ldelta, &table_offset)) {
463 Py_DECREF(bytes);
464 return NULL;
465 }
466 code_offset = bounds.ar_start;
467 line = bounds.ar_computed_line;
468 }
469 }
470 _PyBytes_Resize(&bytes, table_offset);
471 return bytes;
472}
473
474
475static PyGetSetDef code_getsetlist[] = {
476 {"co_lnotab", (getter)code_getlnotab, NULL, NULL},
477 {0}
478};
479
480
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481/* Helper for code_new: return a shallow copy of a tuple that is
482 guaranteed to contain exact strings, by converting string subclasses
483 to exact strings and complaining if a non-string is found. */
484static PyObject*
485validate_and_copy_tuple(PyObject *tup)
486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 PyObject *newtuple;
488 PyObject *item;
489 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 len = PyTuple_GET_SIZE(tup);
492 newtuple = PyTuple_New(len);
493 if (newtuple == NULL)
494 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 for (i = 0; i < len; i++) {
497 item = PyTuple_GET_ITEM(tup, i);
498 if (PyUnicode_CheckExact(item)) {
499 Py_INCREF(item);
500 }
501 else if (!PyUnicode_Check(item)) {
502 PyErr_Format(
503 PyExc_TypeError,
504 "name tuples must contain only "
505 "strings, not '%.500s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100506 Py_TYPE(item)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_DECREF(newtuple);
508 return NULL;
509 }
510 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100511 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (item == NULL) {
513 Py_DECREF(newtuple);
514 return NULL;
515 }
516 }
517 PyTuple_SET_ITEM(newtuple, i, item);
518 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521}
522
Serhiy Storchaka0f9aa472020-07-10 10:12:04 +0300523/*[clinic input]
524@classmethod
525code.__new__ as code_new
526
527 argcount: int
528 posonlyargcount: int
529 kwonlyargcount: int
530 nlocals: int
531 stacksize: int
532 flags: int
533 codestring as code: object(subclass_of="&PyBytes_Type")
534 constants as consts: object(subclass_of="&PyTuple_Type")
535 names: object(subclass_of="&PyTuple_Type")
536 varnames: object(subclass_of="&PyTuple_Type")
537 filename: unicode
538 name: unicode
539 firstlineno: int
Mark Shannon877df852020-11-12 09:43:29 +0000540 linetable: object(subclass_of="&PyBytes_Type")
Serhiy Storchaka0f9aa472020-07-10 10:12:04 +0300541 freevars: object(subclass_of="&PyTuple_Type", c_default="NULL") = ()
542 cellvars: object(subclass_of="&PyTuple_Type", c_default="NULL") = ()
543 /
544
545Create a code object. Not for the faint of heart.
546[clinic start generated code]*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
548static PyObject *
Serhiy Storchaka0f9aa472020-07-10 10:12:04 +0300549code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,
550 int kwonlyargcount, int nlocals, int stacksize, int flags,
551 PyObject *code, PyObject *consts, PyObject *names,
552 PyObject *varnames, PyObject *filename, PyObject *name,
Mark Shannon877df852020-11-12 09:43:29 +0000553 int firstlineno, PyObject *linetable, PyObject *freevars,
Serhiy Storchaka0f9aa472020-07-10 10:12:04 +0300554 PyObject *cellvars)
Mark Shannon877df852020-11-12 09:43:29 +0000555/*[clinic end generated code: output=42c1839b082ba293 input=0ec80da632b99f57]*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 PyObject *co = NULL;
Serhiy Storchaka0f9aa472020-07-10 10:12:04 +0300558 PyObject *ournames = NULL;
559 PyObject *ourvarnames = NULL;
560 PyObject *ourfreevars = NULL;
561 PyObject *ourcellvars = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562
Pablo Galindo3b57f502019-06-01 21:18:48 +0100563 if (PySys_Audit("code.__new__", "OOOiiiiii",
564 code, filename, name, argcount, posonlyargcount,
565 kwonlyargcount, nlocals, stacksize, flags) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -0700566 goto cleanup;
567 }
568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 if (argcount < 0) {
570 PyErr_SetString(
571 PyExc_ValueError,
572 "code: argcount must not be negative");
573 goto cleanup;
574 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100576 if (posonlyargcount < 0) {
577 PyErr_SetString(
578 PyExc_ValueError,
579 "code: posonlyargcount must not be negative");
580 goto cleanup;
581 }
582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (kwonlyargcount < 0) {
584 PyErr_SetString(
585 PyExc_ValueError,
586 "code: kwonlyargcount must not be negative");
587 goto cleanup;
588 }
589 if (nlocals < 0) {
590 PyErr_SetString(
591 PyExc_ValueError,
592 "code: nlocals must not be negative");
593 goto cleanup;
594 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 ournames = validate_and_copy_tuple(names);
597 if (ournames == NULL)
598 goto cleanup;
599 ourvarnames = validate_and_copy_tuple(varnames);
600 if (ourvarnames == NULL)
601 goto cleanup;
602 if (freevars)
603 ourfreevars = validate_and_copy_tuple(freevars);
604 else
605 ourfreevars = PyTuple_New(0);
606 if (ourfreevars == NULL)
607 goto cleanup;
608 if (cellvars)
609 ourcellvars = validate_and_copy_tuple(cellvars);
610 else
611 ourcellvars = PyTuple_New(0);
612 if (ourcellvars == NULL)
613 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100615 co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
616 kwonlyargcount,
617 nlocals, stacksize, flags,
618 code, consts, ournames,
619 ourvarnames, ourfreevars,
620 ourcellvars, filename,
Mark Shannon877df852020-11-12 09:43:29 +0000621 name, firstlineno, linetable);
Victor Stinnera2783132020-01-27 23:24:13 +0100622 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 Py_XDECREF(ournames);
624 Py_XDECREF(ourvarnames);
625 Py_XDECREF(ourfreevars);
626 Py_XDECREF(ourcellvars);
627 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628}
629
630static void
631code_dealloc(PyCodeObject *co)
632{
Inada Naoki91234a12019-06-03 21:30:58 +0900633 if (co->co_opcache != NULL) {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100634 PyMem_Free(co->co_opcache);
Inada Naoki91234a12019-06-03 21:30:58 +0900635 }
636 if (co->co_opcache_map != NULL) {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100637 PyMem_Free(co->co_opcache_map);
Inada Naoki91234a12019-06-03 21:30:58 +0900638 }
639 co->co_opcache_flag = 0;
640 co->co_opcache_size = 0;
641
Brett Cannon5c4de282016-09-07 11:16:41 -0700642 if (co->co_extra != NULL) {
Victor Stinner81a7be32020-04-14 15:14:01 +0200643 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannond0600ed2016-09-07 14:30:39 -0700644 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700645
Brett Cannond0600ed2016-09-07 14:30:39 -0700646 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700647 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700648
649 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700650 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700651 }
652 }
653
Victor Stinner23e79442017-06-28 02:12:00 +0200654 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700655 }
656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 Py_XDECREF(co->co_code);
658 Py_XDECREF(co->co_consts);
659 Py_XDECREF(co->co_names);
660 Py_XDECREF(co->co_varnames);
661 Py_XDECREF(co->co_freevars);
662 Py_XDECREF(co->co_cellvars);
663 Py_XDECREF(co->co_filename);
664 Py_XDECREF(co->co_name);
Mark Shannon877df852020-11-12 09:43:29 +0000665 Py_XDECREF(co->co_linetable);
Benjamin Peterson90037602011-06-25 22:54:45 -0500666 if (co->co_cell2arg != NULL)
Victor Stinner00d7abd2020-12-01 09:56:42 +0100667 PyMem_Free(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (co->co_zombieframe != NULL)
669 PyObject_GC_Del(co->co_zombieframe);
670 if (co->co_weakreflist != NULL)
671 PyObject_ClearWeakRefs((PyObject*)co);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100672 PyObject_Free(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673}
674
675static PyObject *
Victor Stinnera9f05d62019-05-24 23:57:23 +0200676code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200677{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900678 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
679 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200680
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300681 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200682 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300683 }
684 if (co_extra != NULL) {
685 res += sizeof(_PyCodeObjectExtra) +
686 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
687 }
Inada Naoki91234a12019-06-03 21:30:58 +0900688 if (co->co_opcache != NULL) {
689 assert(co->co_opcache_map != NULL);
690 // co_opcache_map
691 res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);
692 // co_opcache
693 res += co->co_opcache_size * sizeof(_PyOpcache);
694 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200695 return PyLong_FromSsize_t(res);
696}
697
Victor Stinnera9f05d62019-05-24 23:57:23 +0200698/*[clinic input]
699code.replace
700
701 *
702 co_argcount: int(c_default="self->co_argcount") = -1
703 co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
704 co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
705 co_nlocals: int(c_default="self->co_nlocals") = -1
706 co_stacksize: int(c_default="self->co_stacksize") = -1
707 co_flags: int(c_default="self->co_flags") = -1
708 co_firstlineno: int(c_default="self->co_firstlineno") = -1
709 co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
710 co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
711 co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
712 co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
713 co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
714 co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
715 co_filename: unicode(c_default="self->co_filename") = None
716 co_name: unicode(c_default="self->co_name") = None
Mark Shannon877df852020-11-12 09:43:29 +0000717 co_linetable: PyBytesObject(c_default="(PyBytesObject *)self->co_linetable") = None
Victor Stinnera9f05d62019-05-24 23:57:23 +0200718
Anthony Sottile22424c02020-01-01 01:11:16 -0500719Return a copy of the code object with new values for the specified fields.
Victor Stinnera9f05d62019-05-24 23:57:23 +0200720[clinic start generated code]*/
721
722static PyObject *
723code_replace_impl(PyCodeObject *self, int co_argcount,
724 int co_posonlyargcount, int co_kwonlyargcount,
725 int co_nlocals, int co_stacksize, int co_flags,
726 int co_firstlineno, PyBytesObject *co_code,
727 PyObject *co_consts, PyObject *co_names,
728 PyObject *co_varnames, PyObject *co_freevars,
729 PyObject *co_cellvars, PyObject *co_filename,
Mark Shannon877df852020-11-12 09:43:29 +0000730 PyObject *co_name, PyBytesObject *co_linetable)
731/*[clinic end generated code: output=50d77e668d3b449b input=a5f997b173d7f636]*/
Victor Stinnera9f05d62019-05-24 23:57:23 +0200732{
733#define CHECK_INT_ARG(ARG) \
734 if (ARG < 0) { \
735 PyErr_SetString(PyExc_ValueError, \
736 #ARG " must be a positive integer"); \
737 return NULL; \
738 }
739
740 CHECK_INT_ARG(co_argcount);
741 CHECK_INT_ARG(co_posonlyargcount);
742 CHECK_INT_ARG(co_kwonlyargcount);
743 CHECK_INT_ARG(co_nlocals);
744 CHECK_INT_ARG(co_stacksize);
745 CHECK_INT_ARG(co_flags);
746 CHECK_INT_ARG(co_firstlineno);
747
748#undef CHECK_INT_ARG
749
Steve Dowerc7c01ab2019-11-26 16:27:50 -0800750 if (PySys_Audit("code.__new__", "OOOiiiiii",
751 co_code, co_filename, co_name, co_argcount,
752 co_posonlyargcount, co_kwonlyargcount, co_nlocals,
753 co_stacksize, co_flags) < 0) {
754 return NULL;
755 }
756
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100757 return (PyObject *)PyCode_NewWithPosOnlyArgs(
Victor Stinnera9f05d62019-05-24 23:57:23 +0200758 co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
759 co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
760 co_varnames, co_freevars, co_cellvars, co_filename, co_name,
Mark Shannon877df852020-11-12 09:43:29 +0000761 co_firstlineno, (PyObject*)co_linetable);
Victor Stinnera9f05d62019-05-24 23:57:23 +0200762}
763
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200764static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765code_repr(PyCodeObject *co)
766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 int lineno;
768 if (co->co_firstlineno != 0)
769 lineno = co->co_firstlineno;
770 else
771 lineno = -1;
772 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
773 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000774 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 co->co_name, co, co->co_filename, lineno);
776 } else {
777 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000778 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 co->co_name, co, lineno);
780 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781}
782
Victor Stinnerefb24132016-01-22 12:33:12 +0100783PyObject*
784_PyCode_ConstantKey(PyObject *op)
785{
786 PyObject *key;
787
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300788 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100789 if (op == Py_None || op == Py_Ellipsis
790 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100791 || PyUnicode_CheckExact(op)
792 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300793 || PyCode_Check(op))
794 {
795 /* Objects of these types are always different from object of other
796 * type and from tuples. */
797 Py_INCREF(op);
798 key = op;
799 }
800 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
801 /* Make booleans different from integers 0 and 1.
802 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100803 key = PyTuple_Pack(2, Py_TYPE(op), op);
804 }
805 else if (PyFloat_CheckExact(op)) {
806 double d = PyFloat_AS_DOUBLE(op);
807 /* all we need is to make the tuple different in either the 0.0
808 * or -0.0 case from all others, just to avoid the "coercion".
809 */
810 if (d == 0.0 && copysign(1.0, d) < 0.0)
811 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
812 else
813 key = PyTuple_Pack(2, Py_TYPE(op), op);
814 }
815 else if (PyComplex_CheckExact(op)) {
816 Py_complex z;
817 int real_negzero, imag_negzero;
818 /* For the complex case we must make complex(x, 0.)
819 different from complex(x, -0.) and complex(0., y)
820 different from complex(-0., y), for any x and y.
821 All four complex zeros must be distinguished.*/
822 z = PyComplex_AsCComplex(op);
823 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
824 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
825 /* use True, False and None singleton as tags for the real and imag
826 * sign, to make tuples different */
827 if (real_negzero && imag_negzero) {
828 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
829 }
830 else if (imag_negzero) {
831 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
832 }
833 else if (real_negzero) {
834 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
835 }
836 else {
837 key = PyTuple_Pack(2, Py_TYPE(op), op);
838 }
839 }
840 else if (PyTuple_CheckExact(op)) {
841 Py_ssize_t i, len;
842 PyObject *tuple;
843
844 len = PyTuple_GET_SIZE(op);
845 tuple = PyTuple_New(len);
846 if (tuple == NULL)
847 return NULL;
848
849 for (i=0; i < len; i++) {
850 PyObject *item, *item_key;
851
852 item = PyTuple_GET_ITEM(op, i);
853 item_key = _PyCode_ConstantKey(item);
854 if (item_key == NULL) {
855 Py_DECREF(tuple);
856 return NULL;
857 }
858
859 PyTuple_SET_ITEM(tuple, i, item_key);
860 }
861
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200862 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100863 Py_DECREF(tuple);
864 }
865 else if (PyFrozenSet_CheckExact(op)) {
866 Py_ssize_t pos = 0;
867 PyObject *item;
868 Py_hash_t hash;
869 Py_ssize_t i, len;
870 PyObject *tuple, *set;
871
872 len = PySet_GET_SIZE(op);
873 tuple = PyTuple_New(len);
874 if (tuple == NULL)
875 return NULL;
876
877 i = 0;
878 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
879 PyObject *item_key;
880
881 item_key = _PyCode_ConstantKey(item);
882 if (item_key == NULL) {
883 Py_DECREF(tuple);
884 return NULL;
885 }
886
887 assert(i < len);
888 PyTuple_SET_ITEM(tuple, i, item_key);
889 i++;
890 }
891 set = PyFrozenSet_New(tuple);
892 Py_DECREF(tuple);
893 if (set == NULL)
894 return NULL;
895
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200896 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100897 Py_DECREF(set);
898 return key;
899 }
900 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000901 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100902 * to ensure that they are seen as unequal. */
903 PyObject *obj_id = PyLong_FromVoidPtr(op);
904 if (obj_id == NULL)
905 return NULL;
906
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200907 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100908 Py_DECREF(obj_id);
909 }
910 return key;
911}
912
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000913static PyObject *
914code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PyCodeObject *co, *cp;
917 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100918 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if ((op != Py_EQ && op != Py_NE) ||
922 !PyCode_Check(self) ||
923 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500924 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 co = (PyCodeObject *)self;
928 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100931 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 eq = co->co_argcount == cp->co_argcount;
933 if (!eq) goto unequal;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100934 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
935 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
937 if (!eq) goto unequal;
938 eq = co->co_nlocals == cp->co_nlocals;
939 if (!eq) goto unequal;
940 eq = co->co_flags == cp->co_flags;
941 if (!eq) goto unequal;
942 eq = co->co_firstlineno == cp->co_firstlineno;
943 if (!eq) goto unequal;
944 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
945 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100946
947 /* compare constants */
948 consts1 = _PyCode_ConstantKey(co->co_consts);
949 if (!consts1)
950 return NULL;
951 consts2 = _PyCode_ConstantKey(cp->co_consts);
952 if (!consts2) {
953 Py_DECREF(consts1);
954 return NULL;
955 }
956 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
957 Py_DECREF(consts1);
958 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
962 if (eq <= 0) goto unequal;
963 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
964 if (eq <= 0) goto unequal;
965 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
966 if (eq <= 0) goto unequal;
967 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
968 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 if (op == Py_EQ)
971 res = Py_True;
972 else
973 res = Py_False;
974 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000975
976 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 if (eq < 0)
978 return NULL;
979 if (op == Py_NE)
980 res = Py_True;
981 else
982 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000983
984 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 Py_INCREF(res);
986 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987}
988
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000989static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990code_hash(PyCodeObject *co)
991{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000992 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 h0 = PyObject_Hash(co->co_name);
994 if (h0 == -1) return -1;
995 h1 = PyObject_Hash(co->co_code);
996 if (h1 == -1) return -1;
997 h2 = PyObject_Hash(co->co_consts);
998 if (h2 == -1) return -1;
999 h3 = PyObject_Hash(co->co_names);
1000 if (h3 == -1) return -1;
1001 h4 = PyObject_Hash(co->co_varnames);
1002 if (h4 == -1) return -1;
1003 h5 = PyObject_Hash(co->co_freevars);
1004 if (h5 == -1) return -1;
1005 h6 = PyObject_Hash(co->co_cellvars);
1006 if (h6 == -1) return -1;
1007 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001008 co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 co->co_nlocals ^ co->co_flags;
1010 if (h == -1) h = -2;
1011 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012}
1013
Mark Shannon877df852020-11-12 09:43:29 +00001014typedef struct {
1015 PyObject_HEAD
1016 PyCodeObject *li_code;
1017 PyCodeAddressRange li_line;
1018 char *li_end;
1019} lineiterator;
1020
1021
1022static void
1023lineiter_dealloc(lineiterator *li)
1024{
1025 Py_DECREF(li->li_code);
1026 Py_TYPE(li)->tp_free(li);
1027}
1028
1029static PyObject *
1030lineiter_next(lineiterator *li)
1031{
1032 PyCodeAddressRange *bounds = &li->li_line;
1033 if (!PyLineTable_NextAddressRange(bounds)) {
1034 return NULL;
1035 }
1036 PyObject *start = NULL;
1037 PyObject *end = NULL;
1038 PyObject *line = NULL;
1039 PyObject *result = PyTuple_New(3);
1040 start = PyLong_FromLong(bounds->ar_start);
1041 end = PyLong_FromLong(bounds->ar_end);
1042 if (bounds->ar_line < 0) {
1043 Py_INCREF(Py_None);
1044 line = Py_None;
1045 }
1046 else {
1047 line = PyLong_FromLong(bounds->ar_line);
1048 }
1049 if (result == NULL || start == NULL || end == NULL || line == NULL) {
1050 goto error;
1051 }
1052 PyTuple_SET_ITEM(result, 0, start);
1053 PyTuple_SET_ITEM(result, 1, end);
1054 PyTuple_SET_ITEM(result, 2, line);
1055 return result;
1056error:
1057 Py_XDECREF(start);
1058 Py_XDECREF(end);
1059 Py_XDECREF(line);
1060 Py_XDECREF(result);
1061 return result;
1062}
1063
1064static PyTypeObject LineIterator = {
1065 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1066 "line_iterator", /* tp_name */
1067 sizeof(lineiterator), /* tp_basicsize */
1068 0, /* tp_itemsize */
1069 /* methods */
1070 (destructor)lineiter_dealloc, /* tp_dealloc */
1071 0, /* tp_vectorcall_offset */
1072 0, /* tp_getattr */
1073 0, /* tp_setattr */
1074 0, /* tp_as_async */
1075 0, /* tp_repr */
1076 0, /* tp_as_number */
1077 0, /* tp_as_sequence */
1078 0, /* tp_as_mapping */
1079 0, /* tp_hash */
1080 0, /* tp_call */
1081 0, /* tp_str */
1082 0, /* tp_getattro */
1083 0, /* tp_setattro */
1084 0, /* tp_as_buffer */
1085 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1086 0, /* tp_doc */
1087 0, /* tp_traverse */
1088 0, /* tp_clear */
1089 0, /* tp_richcompare */
1090 0, /* tp_weaklistoffset */
1091 PyObject_SelfIter, /* tp_iter */
1092 (iternextfunc)lineiter_next, /* tp_iternext */
1093 0, /* tp_methods */
1094 0, /* tp_members */
1095 0, /* tp_getset */
1096 0, /* tp_base */
1097 0, /* tp_dict */
1098 0, /* tp_descr_get */
1099 0, /* tp_descr_set */
1100 0, /* tp_dictoffset */
1101 0, /* tp_init */
1102 0, /* tp_alloc */
1103 0, /* tp_new */
1104 PyObject_Del, /* tp_free */
1105};
1106
1107static PyObject *
1108code_linesiterator(PyCodeObject *code, PyObject *Py_UNUSED(args))
1109{
1110 lineiterator *li = (lineiterator *)PyType_GenericAlloc(&LineIterator, 0);
1111 if (li == NULL) {
1112 return NULL;
1113 }
1114 Py_INCREF(code);
1115 li->li_code = code;
1116 _PyCode_InitAddressRange(code, &li->li_line);
1117 return (PyObject *)li;
1118}
1119
1120static void
1121retreat(PyCodeAddressRange *bounds)
1122{
1123 int ldelta = ((signed char *)bounds->lo_next)[-1];
1124 if (ldelta == -128) {
1125 ldelta = 0;
1126 }
1127 bounds->ar_computed_line -= ldelta;
1128 bounds->lo_next -= 2;
1129 bounds->ar_end = bounds->ar_start;
1130 bounds->ar_start -= ((unsigned char *)bounds->lo_next)[-2];
1131 ldelta = ((signed char *)bounds->lo_next)[-1];
1132 if (ldelta == -128) {
1133 bounds->ar_line = -1;
1134 }
1135 else {
1136 bounds->ar_line = bounds->ar_computed_line;
1137 }
1138}
1139
1140static void
1141advance(PyCodeAddressRange *bounds)
1142{
1143 bounds->ar_start = bounds->ar_end;
1144 int delta = ((unsigned char *)bounds->lo_next)[0];
1145 assert (delta < 255);
1146 bounds->ar_end += delta;
1147 int ldelta = ((signed char *)bounds->lo_next)[1];
1148 bounds->lo_next += 2;
1149 if (ldelta == -128) {
1150 bounds->ar_line = -1;
1151 }
1152 else {
1153 bounds->ar_computed_line += ldelta;
1154 bounds->ar_line = bounds->ar_computed_line;
1155 }
1156}
1157
1158static inline int
1159at_end(PyCodeAddressRange *bounds) {
1160 return ((unsigned char *)bounds->lo_next)[0] == 255;
1161}
1162
1163int
1164PyLineTable_PreviousAddressRange(PyCodeAddressRange *range)
1165{
1166 if (range->ar_start <= 0) {
1167 return 0;
1168 }
1169 retreat(range);
1170 while (range->ar_start == range->ar_end) {
1171 assert(range->ar_start > 0);
1172 retreat(range);
1173 }
1174 return 1;
1175}
1176
1177int
1178PyLineTable_NextAddressRange(PyCodeAddressRange *range)
1179{
1180 if (at_end(range)) {
1181 return 0;
1182 }
1183 advance(range);
1184 while (range->ar_start == range->ar_end) {
1185 assert(!at_end(range));
1186 advance(range);
1187 }
1188 return 1;
1189}
1190
1191
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192/* XXX code objects need to participate in GC? */
1193
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +02001194static struct PyMethodDef code_methods[] = {
1195 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
Mark Shannon877df852020-11-12 09:43:29 +00001196 {"co_lines", (PyCFunction)code_linesiterator, METH_NOARGS},
Victor Stinnera9f05d62019-05-24 23:57:23 +02001197 CODE_REPLACE_METHODDEF
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +02001198 {NULL, NULL} /* sentinel */
1199};
1200
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1203 "code",
1204 sizeof(PyCodeObject),
1205 0,
1206 (destructor)code_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001207 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 0, /* tp_getattr */
1209 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001210 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 (reprfunc)code_repr, /* tp_repr */
1212 0, /* tp_as_number */
1213 0, /* tp_as_sequence */
1214 0, /* tp_as_mapping */
1215 (hashfunc)code_hash, /* tp_hash */
1216 0, /* tp_call */
1217 0, /* tp_str */
1218 PyObject_GenericGetAttr, /* tp_getattro */
1219 0, /* tp_setattro */
1220 0, /* tp_as_buffer */
1221 Py_TPFLAGS_DEFAULT, /* tp_flags */
Serhiy Storchaka0f9aa472020-07-10 10:12:04 +03001222 code_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 0, /* tp_traverse */
1224 0, /* tp_clear */
1225 code_richcompare, /* tp_richcompare */
1226 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
1227 0, /* tp_iter */
1228 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +02001229 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 code_memberlist, /* tp_members */
Mark Shannon877df852020-11-12 09:43:29 +00001231 code_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 0, /* tp_base */
1233 0, /* tp_dict */
1234 0, /* tp_descr_get */
1235 0, /* tp_descr_set */
1236 0, /* tp_dictoffset */
1237 0, /* tp_init */
1238 0, /* tp_alloc */
1239 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240};
1241
Mark Shannon877df852020-11-12 09:43:29 +00001242/* Use co_linetable to compute the line number from a bytecode index, addrq. See
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00001243 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244*/
1245
1246int
1247PyCode_Addr2Line(PyCodeObject *co, int addrq)
1248{
Mark Shannonfcb55c02021-04-01 16:00:31 +01001249 if (addrq < 0) {
Mark Shannon877df852020-11-12 09:43:29 +00001250 return co->co_firstlineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 }
Mark Shannon877df852020-11-12 09:43:29 +00001252 assert(addrq >= 0 && addrq < PyBytes_GET_SIZE(co->co_code));
1253 PyCodeAddressRange bounds;
1254 _PyCode_InitAddressRange(co, &bounds);
1255 return _PyCode_CheckLineNumber(addrq, &bounds);
1256}
1257
1258void
1259PyLineTable_InitAddressRange(char *linetable, int firstlineno, PyCodeAddressRange *range)
1260{
1261 range->lo_next = linetable;
1262 range->ar_start = -1;
1263 range->ar_end = 0;
Mark Shannonee9f98d2021-01-05 12:04:10 +00001264 range->ar_computed_line = firstlineno;
1265 range->ar_line = -1;
Mark Shannon877df852020-11-12 09:43:29 +00001266}
1267
1268int
1269_PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds)
1270{
1271 char *linetable = PyBytes_AS_STRING(co->co_linetable);
1272 PyLineTable_InitAddressRange(linetable, co->co_firstlineno, bounds);
1273 return bounds->ar_line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00001276/* Update *bounds to describe the first and one-past-the-last instructions in
Mark Shannon877df852020-11-12 09:43:29 +00001277 the same line as lasti. Return the number of that line, or -1 if lasti is out of bounds. */
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00001278int
Mark Shannon877df852020-11-12 09:43:29 +00001279_PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280{
Mark Shannon877df852020-11-12 09:43:29 +00001281 while (bounds->ar_end <= lasti) {
1282 if (!PyLineTable_NextAddressRange(bounds)) {
1283 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 }
Mark Shannon877df852020-11-12 09:43:29 +00001286 while (bounds->ar_start > lasti) {
1287 if (!PyLineTable_PreviousAddressRange(bounds)) {
1288 return -1;
1289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 }
Mark Shannon877df852020-11-12 09:43:29 +00001291 return bounds->ar_line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001292}
Brett Cannon5c4de282016-09-07 11:16:41 -07001293
1294
1295int
1296_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
1297{
Brett Cannon5c4de282016-09-07 11:16:41 -07001298 if (!PyCode_Check(code)) {
1299 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -07001300 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001301 }
1302
Brett Cannond0600ed2016-09-07 14:30:39 -07001303 PyCodeObject *o = (PyCodeObject*) code;
1304 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001305
Brett Cannond0600ed2016-09-07 14:30:39 -07001306 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001307 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -07001308 return 0;
1309 }
1310
Brett Cannond0600ed2016-09-07 14:30:39 -07001311 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -07001312 return 0;
1313}
1314
1315
1316int
1317_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
1318{
Victor Stinner81a7be32020-04-14 15:14:01 +02001319 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07001320
1321 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001322 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -07001323 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -07001324 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001325 }
1326
Brett Cannond0600ed2016-09-07 14:30:39 -07001327 PyCodeObject *o = (PyCodeObject*) code;
1328 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001329
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001330 if (co_extra == NULL || co_extra->ce_size <= index) {
1331 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
1332 co_extra = PyMem_Realloc(
1333 co_extra,
1334 sizeof(_PyCodeObjectExtra) +
1335 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +00001336 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -07001337 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001338 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001339 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -07001340 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -07001341 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001342 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001343 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001344 }
1345
1346 if (co_extra->ce_extras[index] != NULL) {
1347 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001348 if (free != NULL) {
1349 free(co_extra->ce_extras[index]);
1350 }
Brett Cannon5c4de282016-09-07 11:16:41 -07001351 }
1352
Brett Cannond0600ed2016-09-07 14:30:39 -07001353 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001354 return 0;
1355}