blob: 09182d61c24474f32ada82844ed80e871f903154 [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 *
Guido van Rossum4f72a782006-10-27 23:31:49 +000099PyCode_New(int argcount, 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 */
111 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200112 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);
144 if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
145 /* Never overflows. */
146 total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
147 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
148 }
149 else {
150 total_args = n_varnames + 1;
151 }
152 if (total_args > n_varnames) {
153 PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
154 return NULL;
155 }
156
Benjamin Peterson90037602011-06-25 22:54:45 -0500157 /* Create mapping between cells and arguments if needed. */
158 if (n_cellvars) {
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700159 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200160 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
161 if (cell2arg == NULL) {
162 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500163 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200164 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500165 /* Find cells which are also arguments. */
166 for (i = 0; i < n_cellvars; i++) {
167 Py_ssize_t j;
168 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200169 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500170 for (j = 0; j < total_args; j++) {
171 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200172 int cmp = PyUnicode_Compare(cell, arg);
173 if (cmp == -1 && PyErr_Occurred()) {
174 PyMem_FREE(cell2arg);
175 return NULL;
176 }
177 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500178 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700179 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500180 break;
181 }
182 }
183 }
184 if (!used_cell2arg) {
185 PyMem_FREE(cell2arg);
186 cell2arg = NULL;
187 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500189 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
190 if (co == NULL) {
191 if (cell2arg)
192 PyMem_FREE(cell2arg);
193 return NULL;
194 }
195 co->co_argcount = argcount;
196 co->co_kwonlyargcount = kwonlyargcount;
197 co->co_nlocals = nlocals;
198 co->co_stacksize = stacksize;
199 co->co_flags = flags;
200 Py_INCREF(code);
201 co->co_code = code;
202 Py_INCREF(consts);
203 co->co_consts = consts;
204 Py_INCREF(names);
205 co->co_names = names;
206 Py_INCREF(varnames);
207 co->co_varnames = varnames;
208 Py_INCREF(freevars);
209 co->co_freevars = freevars;
210 Py_INCREF(cellvars);
211 co->co_cellvars = cellvars;
212 co->co_cell2arg = cell2arg;
213 Py_INCREF(filename);
214 co->co_filename = filename;
215 Py_INCREF(name);
216 co->co_name = name;
217 co->co_firstlineno = firstlineno;
218 Py_INCREF(lnotab);
219 co->co_lnotab = lnotab;
220 co->co_zombieframe = NULL;
221 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700222 co->co_extra = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224}
225
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000226PyCodeObject *
227PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 static PyObject *emptystring = NULL;
230 static PyObject *nulltuple = NULL;
231 PyObject *filename_ob = NULL;
232 PyObject *funcname_ob = NULL;
233 PyCodeObject *result = NULL;
234 if (emptystring == NULL) {
235 emptystring = PyBytes_FromString("");
236 if (emptystring == NULL)
237 goto failed;
238 }
239 if (nulltuple == NULL) {
240 nulltuple = PyTuple_New(0);
241 if (nulltuple == NULL)
242 goto failed;
243 }
244 funcname_ob = PyUnicode_FromString(funcname);
245 if (funcname_ob == NULL)
246 goto failed;
247 filename_ob = PyUnicode_DecodeFSDefault(filename);
248 if (filename_ob == NULL)
249 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 result = PyCode_New(0, /* argcount */
252 0, /* kwonlyargcount */
253 0, /* nlocals */
254 0, /* stacksize */
255 0, /* flags */
256 emptystring, /* code */
257 nulltuple, /* consts */
258 nulltuple, /* names */
259 nulltuple, /* varnames */
260 nulltuple, /* freevars */
261 nulltuple, /* cellvars */
262 filename_ob, /* filename */
263 funcname_ob, /* name */
264 firstlineno, /* firstlineno */
265 emptystring /* lnotab */
266 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000267
268failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 Py_XDECREF(funcname_ob);
270 Py_XDECREF(filename_ob);
271 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000272}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273
274#define OFF(x) offsetof(PyCodeObject, x)
275
276static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
278 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
279 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
280 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
281 {"co_flags", T_INT, OFF(co_flags), READONLY},
282 {"co_code", T_OBJECT, OFF(co_code), READONLY},
283 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
284 {"co_names", T_OBJECT, OFF(co_names), READONLY},
285 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
286 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
287 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
288 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
289 {"co_name", T_OBJECT, OFF(co_name), READONLY},
290 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
291 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
292 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293};
294
295/* Helper for code_new: return a shallow copy of a tuple that is
296 guaranteed to contain exact strings, by converting string subclasses
297 to exact strings and complaining if a non-string is found. */
298static PyObject*
299validate_and_copy_tuple(PyObject *tup)
300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 PyObject *newtuple;
302 PyObject *item;
303 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 len = PyTuple_GET_SIZE(tup);
306 newtuple = PyTuple_New(len);
307 if (newtuple == NULL)
308 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 for (i = 0; i < len; i++) {
311 item = PyTuple_GET_ITEM(tup, i);
312 if (PyUnicode_CheckExact(item)) {
313 Py_INCREF(item);
314 }
315 else if (!PyUnicode_Check(item)) {
316 PyErr_Format(
317 PyExc_TypeError,
318 "name tuples must contain only "
319 "strings, not '%.500s'",
320 item->ob_type->tp_name);
321 Py_DECREF(newtuple);
322 return NULL;
323 }
324 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100325 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 if (item == NULL) {
327 Py_DECREF(newtuple);
328 return NULL;
329 }
330 }
331 PyTuple_SET_ITEM(newtuple, i, item);
332 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000335}
336
337PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000338"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000339 constants, names, varnames, filename, name, firstlineno,\n\
340 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341\n\
342Create a code object. Not for the faint of heart.");
343
344static PyObject *
345code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 int argcount;
348 int kwonlyargcount;
349 int nlocals;
350 int stacksize;
351 int flags;
352 PyObject *co = NULL;
353 PyObject *code;
354 PyObject *consts;
355 PyObject *names, *ournames = NULL;
356 PyObject *varnames, *ourvarnames = NULL;
357 PyObject *freevars = NULL, *ourfreevars = NULL;
358 PyObject *cellvars = NULL, *ourcellvars = NULL;
359 PyObject *filename;
360 PyObject *name;
361 int firstlineno;
362 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
365 &argcount, &kwonlyargcount,
366 &nlocals, &stacksize, &flags,
367 &code,
368 &PyTuple_Type, &consts,
369 &PyTuple_Type, &names,
370 &PyTuple_Type, &varnames,
371 &filename, &name,
372 &firstlineno, &lnotab,
373 &PyTuple_Type, &freevars,
374 &PyTuple_Type, &cellvars))
375 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (argcount < 0) {
378 PyErr_SetString(
379 PyExc_ValueError,
380 "code: argcount must not be negative");
381 goto cleanup;
382 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (kwonlyargcount < 0) {
385 PyErr_SetString(
386 PyExc_ValueError,
387 "code: kwonlyargcount must not be negative");
388 goto cleanup;
389 }
390 if (nlocals < 0) {
391 PyErr_SetString(
392 PyExc_ValueError,
393 "code: nlocals must not be negative");
394 goto cleanup;
395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 ournames = validate_and_copy_tuple(names);
398 if (ournames == NULL)
399 goto cleanup;
400 ourvarnames = validate_and_copy_tuple(varnames);
401 if (ourvarnames == NULL)
402 goto cleanup;
403 if (freevars)
404 ourfreevars = validate_and_copy_tuple(freevars);
405 else
406 ourfreevars = PyTuple_New(0);
407 if (ourfreevars == NULL)
408 goto cleanup;
409 if (cellvars)
410 ourcellvars = validate_and_copy_tuple(cellvars);
411 else
412 ourcellvars = PyTuple_New(0);
413 if (ourcellvars == NULL)
414 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
417 nlocals, stacksize, flags,
418 code, consts, ournames, ourvarnames,
419 ourfreevars, ourcellvars, filename,
420 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 Py_XDECREF(ournames);
423 Py_XDECREF(ourvarnames);
424 Py_XDECREF(ourfreevars);
425 Py_XDECREF(ourcellvars);
426 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427}
428
429static void
430code_dealloc(PyCodeObject *co)
431{
Brett Cannon5c4de282016-09-07 11:16:41 -0700432 if (co->co_extra != NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200433 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannond0600ed2016-09-07 14:30:39 -0700434 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700435
Brett Cannond0600ed2016-09-07 14:30:39 -0700436 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700437 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700438
439 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700440 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700441 }
442 }
443
Victor Stinner23e79442017-06-28 02:12:00 +0200444 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700445 }
446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 Py_XDECREF(co->co_code);
448 Py_XDECREF(co->co_consts);
449 Py_XDECREF(co->co_names);
450 Py_XDECREF(co->co_varnames);
451 Py_XDECREF(co->co_freevars);
452 Py_XDECREF(co->co_cellvars);
453 Py_XDECREF(co->co_filename);
454 Py_XDECREF(co->co_name);
455 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500456 if (co->co_cell2arg != NULL)
457 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (co->co_zombieframe != NULL)
459 PyObject_GC_Del(co->co_zombieframe);
460 if (co->co_weakreflist != NULL)
461 PyObject_ClearWeakRefs((PyObject*)co);
462 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463}
464
465static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200466code_sizeof(PyCodeObject *co, void *unused)
467{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900468 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
469 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200470
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300471 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200472 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300473 }
474 if (co_extra != NULL) {
475 res += sizeof(_PyCodeObjectExtra) +
476 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
477 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200478 return PyLong_FromSsize_t(res);
479}
480
481static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482code_repr(PyCodeObject *co)
483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 int lineno;
485 if (co->co_firstlineno != 0)
486 lineno = co->co_firstlineno;
487 else
488 lineno = -1;
489 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
490 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000491 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 co->co_name, co, co->co_filename, lineno);
493 } else {
494 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000495 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 co->co_name, co, lineno);
497 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498}
499
Victor Stinnerefb24132016-01-22 12:33:12 +0100500PyObject*
501_PyCode_ConstantKey(PyObject *op)
502{
503 PyObject *key;
504
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300505 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100506 if (op == Py_None || op == Py_Ellipsis
507 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100508 || PyUnicode_CheckExact(op)
509 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300510 || PyCode_Check(op))
511 {
512 /* Objects of these types are always different from object of other
513 * type and from tuples. */
514 Py_INCREF(op);
515 key = op;
516 }
517 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
518 /* Make booleans different from integers 0 and 1.
519 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100520 key = PyTuple_Pack(2, Py_TYPE(op), op);
521 }
522 else if (PyFloat_CheckExact(op)) {
523 double d = PyFloat_AS_DOUBLE(op);
524 /* all we need is to make the tuple different in either the 0.0
525 * or -0.0 case from all others, just to avoid the "coercion".
526 */
527 if (d == 0.0 && copysign(1.0, d) < 0.0)
528 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
529 else
530 key = PyTuple_Pack(2, Py_TYPE(op), op);
531 }
532 else if (PyComplex_CheckExact(op)) {
533 Py_complex z;
534 int real_negzero, imag_negzero;
535 /* For the complex case we must make complex(x, 0.)
536 different from complex(x, -0.) and complex(0., y)
537 different from complex(-0., y), for any x and y.
538 All four complex zeros must be distinguished.*/
539 z = PyComplex_AsCComplex(op);
540 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
541 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
542 /* use True, False and None singleton as tags for the real and imag
543 * sign, to make tuples different */
544 if (real_negzero && imag_negzero) {
545 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
546 }
547 else if (imag_negzero) {
548 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
549 }
550 else if (real_negzero) {
551 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
552 }
553 else {
554 key = PyTuple_Pack(2, Py_TYPE(op), op);
555 }
556 }
557 else if (PyTuple_CheckExact(op)) {
558 Py_ssize_t i, len;
559 PyObject *tuple;
560
561 len = PyTuple_GET_SIZE(op);
562 tuple = PyTuple_New(len);
563 if (tuple == NULL)
564 return NULL;
565
566 for (i=0; i < len; i++) {
567 PyObject *item, *item_key;
568
569 item = PyTuple_GET_ITEM(op, i);
570 item_key = _PyCode_ConstantKey(item);
571 if (item_key == NULL) {
572 Py_DECREF(tuple);
573 return NULL;
574 }
575
576 PyTuple_SET_ITEM(tuple, i, item_key);
577 }
578
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200579 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100580 Py_DECREF(tuple);
581 }
582 else if (PyFrozenSet_CheckExact(op)) {
583 Py_ssize_t pos = 0;
584 PyObject *item;
585 Py_hash_t hash;
586 Py_ssize_t i, len;
587 PyObject *tuple, *set;
588
589 len = PySet_GET_SIZE(op);
590 tuple = PyTuple_New(len);
591 if (tuple == NULL)
592 return NULL;
593
594 i = 0;
595 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
596 PyObject *item_key;
597
598 item_key = _PyCode_ConstantKey(item);
599 if (item_key == NULL) {
600 Py_DECREF(tuple);
601 return NULL;
602 }
603
604 assert(i < len);
605 PyTuple_SET_ITEM(tuple, i, item_key);
606 i++;
607 }
608 set = PyFrozenSet_New(tuple);
609 Py_DECREF(tuple);
610 if (set == NULL)
611 return NULL;
612
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200613 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100614 Py_DECREF(set);
615 return key;
616 }
617 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000618 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100619 * to ensure that they are seen as unequal. */
620 PyObject *obj_id = PyLong_FromVoidPtr(op);
621 if (obj_id == NULL)
622 return NULL;
623
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200624 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100625 Py_DECREF(obj_id);
626 }
627 return key;
628}
629
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000630static PyObject *
631code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 PyCodeObject *co, *cp;
634 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100635 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 if ((op != Py_EQ && op != Py_NE) ||
639 !PyCode_Check(self) ||
640 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500641 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 co = (PyCodeObject *)self;
645 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
648 if (eq <= 0) goto unequal;
649 eq = co->co_argcount == cp->co_argcount;
650 if (!eq) goto unequal;
651 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
652 if (!eq) goto unequal;
653 eq = co->co_nlocals == cp->co_nlocals;
654 if (!eq) goto unequal;
655 eq = co->co_flags == cp->co_flags;
656 if (!eq) goto unequal;
657 eq = co->co_firstlineno == cp->co_firstlineno;
658 if (!eq) goto unequal;
659 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
660 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100661
662 /* compare constants */
663 consts1 = _PyCode_ConstantKey(co->co_consts);
664 if (!consts1)
665 return NULL;
666 consts2 = _PyCode_ConstantKey(cp->co_consts);
667 if (!consts2) {
668 Py_DECREF(consts1);
669 return NULL;
670 }
671 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
672 Py_DECREF(consts1);
673 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
677 if (eq <= 0) goto unequal;
678 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
679 if (eq <= 0) goto unequal;
680 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
681 if (eq <= 0) goto unequal;
682 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
683 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 if (op == Py_EQ)
686 res = Py_True;
687 else
688 res = Py_False;
689 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000690
691 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (eq < 0)
693 return NULL;
694 if (op == Py_NE)
695 res = Py_True;
696 else
697 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000698
699 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 Py_INCREF(res);
701 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702}
703
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000704static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705code_hash(PyCodeObject *co)
706{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000707 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 h0 = PyObject_Hash(co->co_name);
709 if (h0 == -1) return -1;
710 h1 = PyObject_Hash(co->co_code);
711 if (h1 == -1) return -1;
712 h2 = PyObject_Hash(co->co_consts);
713 if (h2 == -1) return -1;
714 h3 = PyObject_Hash(co->co_names);
715 if (h3 == -1) return -1;
716 h4 = PyObject_Hash(co->co_varnames);
717 if (h4 == -1) return -1;
718 h5 = PyObject_Hash(co->co_freevars);
719 if (h5 == -1) return -1;
720 h6 = PyObject_Hash(co->co_cellvars);
721 if (h6 == -1) return -1;
722 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
723 co->co_argcount ^ co->co_kwonlyargcount ^
724 co->co_nlocals ^ co->co_flags;
725 if (h == -1) h = -2;
726 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727}
728
729/* XXX code objects need to participate in GC? */
730
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200731static struct PyMethodDef code_methods[] = {
732 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
733 {NULL, NULL} /* sentinel */
734};
735
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 PyVarObject_HEAD_INIT(&PyType_Type, 0)
738 "code",
739 sizeof(PyCodeObject),
740 0,
741 (destructor)code_dealloc, /* tp_dealloc */
742 0, /* tp_print */
743 0, /* tp_getattr */
744 0, /* tp_setattr */
745 0, /* tp_reserved */
746 (reprfunc)code_repr, /* tp_repr */
747 0, /* tp_as_number */
748 0, /* tp_as_sequence */
749 0, /* tp_as_mapping */
750 (hashfunc)code_hash, /* tp_hash */
751 0, /* tp_call */
752 0, /* tp_str */
753 PyObject_GenericGetAttr, /* tp_getattro */
754 0, /* tp_setattro */
755 0, /* tp_as_buffer */
756 Py_TPFLAGS_DEFAULT, /* tp_flags */
757 code_doc, /* tp_doc */
758 0, /* tp_traverse */
759 0, /* tp_clear */
760 code_richcompare, /* tp_richcompare */
761 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
762 0, /* tp_iter */
763 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200764 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 code_memberlist, /* tp_members */
766 0, /* tp_getset */
767 0, /* tp_base */
768 0, /* tp_dict */
769 0, /* tp_descr_get */
770 0, /* tp_descr_set */
771 0, /* tp_dictoffset */
772 0, /* tp_init */
773 0, /* tp_alloc */
774 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775};
776
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000777/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
778 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779*/
780
781int
782PyCode_Addr2Line(PyCodeObject *co, int addrq)
783{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000784 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
786 int line = co->co_firstlineno;
787 int addr = 0;
788 while (--size >= 0) {
789 addr += *p++;
790 if (addr > addrq)
791 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100792 line += (signed char)*p;
793 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 }
795 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000797
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000798/* Update *bounds to describe the first and one-past-the-last instructions in
799 the same line as lasti. Return the number of that line. */
800int
801_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000802{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000803 Py_ssize_t size;
804 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
808 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 addr = 0;
811 line = co->co_firstlineno;
812 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 /* possible optimization: if f->f_lasti == instr_ub
815 (likely to be a common case) then we already know
816 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700817 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 /* See lnotab_notes.txt for the description of
820 co_lnotab. A point to remember: increments to p
821 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 bounds->ap_lower = 0;
824 while (size > 0) {
825 if (addr + *p > lasti)
826 break;
827 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100828 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100830 line += (signed char)*p;
831 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 --size;
833 }
834
835 if (size > 0) {
836 while (--size >= 0) {
837 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100838 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100840 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000841 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 bounds->ap_upper = addr;
843 }
844 else {
845 bounds->ap_upper = INT_MAX;
846 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000849}
Brett Cannon5c4de282016-09-07 11:16:41 -0700850
851
852int
853_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
854{
Brett Cannon5c4de282016-09-07 11:16:41 -0700855 if (!PyCode_Check(code)) {
856 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700857 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700858 }
859
Brett Cannond0600ed2016-09-07 14:30:39 -0700860 PyCodeObject *o = (PyCodeObject*) code;
861 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700862
Brett Cannond0600ed2016-09-07 14:30:39 -0700863 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700864 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700865 return 0;
866 }
867
Brett Cannond0600ed2016-09-07 14:30:39 -0700868 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700869 return 0;
870}
871
872
873int
874_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
875{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200876 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -0700877
878 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700879 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700880 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700881 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700882 }
883
Brett Cannond0600ed2016-09-07 14:30:39 -0700884 PyCodeObject *o = (PyCodeObject*) code;
885 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700886
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300887 if (co_extra == NULL || co_extra->ce_size <= index) {
888 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
889 co_extra = PyMem_Realloc(
890 co_extra,
891 sizeof(_PyCodeObjectExtra) +
892 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +0000893 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700894 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700895 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300896 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700897 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700898 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700899 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300900 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700901 }
902
903 if (co_extra->ce_extras[index] != NULL) {
904 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700905 if (free != NULL) {
906 free(co_extra->ce_extras[index]);
907 }
Brett Cannon5c4de282016-09-07 11:16:41 -0700908 }
909
Brett Cannond0600ed2016-09-07 14:30:39 -0700910 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700911 return 0;
912}