blob: cedf11ee8af8a25262c12e74725e6c1e5c222dcb [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 Stinnercaba55b2018-08-03 15:33:52 +02006#include "internal/pystate.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007
Brett Cannond0600ed2016-09-07 14:30:39 -07008/* Holder for co_extra information */
9typedef struct {
10 Py_ssize_t ce_size;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +030011 void *ce_extras[1];
Brett Cannond0600ed2016-09-07 14:30:39 -070012} _PyCodeObjectExtra;
13
Benjamin Peterson8e0ad462017-09-07 23:35:53 -070014/* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020016all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017{
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030018 const unsigned char *s, *e;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020019
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030020 if (!PyUnicode_IS_ASCII(o))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020021 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030023 s = PyUnicode_1BYTE_DATA(o);
24 e = s + PyUnicode_GET_LENGTH(o);
Benjamin Peterson9020ac72017-09-07 18:06:23 -070025 for (; s != e; s++) {
Benjamin Peterson2b7953d2017-09-08 10:35:49 -070026 if (!Py_ISALNUM(*s) && *s != '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 return 0;
28 }
29 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000030}
31
32static void
33intern_strings(PyObject *tuple)
34{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
38 PyObject *v = PyTuple_GET_ITEM(tuple, i);
39 if (v == NULL || !PyUnicode_CheckExact(v)) {
40 Py_FatalError("non-string found in code slot");
41 }
42 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
43 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000044}
45
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030046/* Intern selected string constants */
47static int
48intern_string_constants(PyObject *tuple)
49{
50 int modified = 0;
51 Py_ssize_t i;
52
53 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
54 PyObject *v = PyTuple_GET_ITEM(tuple, i);
55 if (PyUnicode_CheckExact(v)) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030056 if (PyUnicode_READY(v) == -1) {
57 PyErr_Clear();
58 continue;
59 }
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030060 if (all_name_chars(v)) {
61 PyObject *w = v;
62 PyUnicode_InternInPlace(&v);
63 if (w != v) {
64 PyTuple_SET_ITEM(tuple, i, v);
65 modified = 1;
66 }
67 }
68 }
69 else if (PyTuple_CheckExact(v)) {
70 intern_string_constants(v);
71 }
72 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050073 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030074 PyObject *tmp = PySequence_Tuple(v);
75 if (tmp == NULL) {
76 PyErr_Clear();
77 continue;
78 }
79 if (intern_string_constants(tmp)) {
80 v = PyFrozenSet_New(tmp);
81 if (v == NULL) {
82 PyErr_Clear();
83 }
84 else {
85 PyTuple_SET_ITEM(tuple, i, v);
Yury Selivanovd2fd3592016-11-09 09:42:14 -050086 Py_DECREF(w);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030087 modified = 1;
88 }
89 }
90 Py_DECREF(tmp);
91 }
92 }
93 return modified;
94}
95
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096
97PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +000098PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 int nlocals, int stacksize, int flags,
100 PyObject *code, PyObject *consts, PyObject *names,
101 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
102 PyObject *filename, PyObject *name, int firstlineno,
103 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200106 Py_ssize_t *cell2arg = NULL;
Serhiy Storchakabd473842018-07-16 09:10:19 +0300107 Py_ssize_t i, n_cellvars, n_varnames, total_args;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 /* Check argument types */
110 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200111 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 consts == NULL || !PyTuple_Check(consts) ||
113 names == NULL || !PyTuple_Check(names) ||
114 varnames == NULL || !PyTuple_Check(varnames) ||
115 freevars == NULL || !PyTuple_Check(freevars) ||
116 cellvars == NULL || !PyTuple_Check(cellvars) ||
117 name == NULL || !PyUnicode_Check(name) ||
118 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200119 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyErr_BadInternalCall();
121 return NULL;
122 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200123
124 /* Ensure that the filename is a ready Unicode string */
125 if (PyUnicode_READY(filename) < 0)
126 return NULL;
127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 intern_strings(names);
129 intern_strings(varnames);
130 intern_strings(freevars);
131 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300132 intern_string_constants(consts);
Nick Coghlan078f1812017-12-03 11:12:20 +1000133
134 /* Check for any inner or outer closure references */
135 n_cellvars = PyTuple_GET_SIZE(cellvars);
136 if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
137 flags |= CO_NOFREE;
138 } else {
139 flags &= ~CO_NOFREE;
140 }
141
Serhiy Storchakabd473842018-07-16 09:10:19 +0300142 n_varnames = PyTuple_GET_SIZE(varnames);
143 if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
144 /* Never overflows. */
145 total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
146 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
147 }
148 else {
149 total_args = n_varnames + 1;
150 }
151 if (total_args > n_varnames) {
152 PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
153 return NULL;
154 }
155
Benjamin Peterson90037602011-06-25 22:54:45 -0500156 /* Create mapping between cells and arguments if needed. */
157 if (n_cellvars) {
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700158 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200159 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
160 if (cell2arg == NULL) {
161 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500162 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200163 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500164 /* Find cells which are also arguments. */
165 for (i = 0; i < n_cellvars; i++) {
166 Py_ssize_t j;
167 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200168 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500169 for (j = 0; j < total_args; j++) {
170 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200171 int cmp = PyUnicode_Compare(cell, arg);
172 if (cmp == -1 && PyErr_Occurred()) {
173 PyMem_FREE(cell2arg);
174 return NULL;
175 }
176 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500177 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700178 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500179 break;
180 }
181 }
182 }
183 if (!used_cell2arg) {
184 PyMem_FREE(cell2arg);
185 cell2arg = NULL;
186 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500188 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
189 if (co == NULL) {
190 if (cell2arg)
191 PyMem_FREE(cell2arg);
192 return NULL;
193 }
194 co->co_argcount = argcount;
195 co->co_kwonlyargcount = kwonlyargcount;
196 co->co_nlocals = nlocals;
197 co->co_stacksize = stacksize;
198 co->co_flags = flags;
199 Py_INCREF(code);
200 co->co_code = code;
201 Py_INCREF(consts);
202 co->co_consts = consts;
203 Py_INCREF(names);
204 co->co_names = names;
205 Py_INCREF(varnames);
206 co->co_varnames = varnames;
207 Py_INCREF(freevars);
208 co->co_freevars = freevars;
209 Py_INCREF(cellvars);
210 co->co_cellvars = cellvars;
211 co->co_cell2arg = cell2arg;
212 Py_INCREF(filename);
213 co->co_filename = filename;
214 Py_INCREF(name);
215 co->co_name = name;
216 co->co_firstlineno = firstlineno;
217 Py_INCREF(lnotab);
218 co->co_lnotab = lnotab;
219 co->co_zombieframe = NULL;
220 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700221 co->co_extra = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223}
224
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000225PyCodeObject *
226PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 static PyObject *emptystring = NULL;
229 static PyObject *nulltuple = NULL;
230 PyObject *filename_ob = NULL;
231 PyObject *funcname_ob = NULL;
232 PyCodeObject *result = NULL;
233 if (emptystring == NULL) {
234 emptystring = PyBytes_FromString("");
235 if (emptystring == NULL)
236 goto failed;
237 }
238 if (nulltuple == NULL) {
239 nulltuple = PyTuple_New(0);
240 if (nulltuple == NULL)
241 goto failed;
242 }
243 funcname_ob = PyUnicode_FromString(funcname);
244 if (funcname_ob == NULL)
245 goto failed;
246 filename_ob = PyUnicode_DecodeFSDefault(filename);
247 if (filename_ob == NULL)
248 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 result = PyCode_New(0, /* argcount */
251 0, /* kwonlyargcount */
252 0, /* nlocals */
253 0, /* stacksize */
254 0, /* flags */
255 emptystring, /* code */
256 nulltuple, /* consts */
257 nulltuple, /* names */
258 nulltuple, /* varnames */
259 nulltuple, /* freevars */
260 nulltuple, /* cellvars */
261 filename_ob, /* filename */
262 funcname_ob, /* name */
263 firstlineno, /* firstlineno */
264 emptystring /* lnotab */
265 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000266
267failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 Py_XDECREF(funcname_ob);
269 Py_XDECREF(filename_ob);
270 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000271}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272
273#define OFF(x) offsetof(PyCodeObject, x)
274
275static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
277 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
278 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
279 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
280 {"co_flags", T_INT, OFF(co_flags), READONLY},
281 {"co_code", T_OBJECT, OFF(co_code), READONLY},
282 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
283 {"co_names", T_OBJECT, OFF(co_names), READONLY},
284 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
285 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
286 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
287 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
288 {"co_name", T_OBJECT, OFF(co_name), READONLY},
289 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
290 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
291 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292};
293
294/* Helper for code_new: return a shallow copy of a tuple that is
295 guaranteed to contain exact strings, by converting string subclasses
296 to exact strings and complaining if a non-string is found. */
297static PyObject*
298validate_and_copy_tuple(PyObject *tup)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 PyObject *newtuple;
301 PyObject *item;
302 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 len = PyTuple_GET_SIZE(tup);
305 newtuple = PyTuple_New(len);
306 if (newtuple == NULL)
307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 for (i = 0; i < len; i++) {
310 item = PyTuple_GET_ITEM(tup, i);
311 if (PyUnicode_CheckExact(item)) {
312 Py_INCREF(item);
313 }
314 else if (!PyUnicode_Check(item)) {
315 PyErr_Format(
316 PyExc_TypeError,
317 "name tuples must contain only "
318 "strings, not '%.500s'",
319 item->ob_type->tp_name);
320 Py_DECREF(newtuple);
321 return NULL;
322 }
323 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100324 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (item == NULL) {
326 Py_DECREF(newtuple);
327 return NULL;
328 }
329 }
330 PyTuple_SET_ITEM(newtuple, i, item);
331 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334}
335
336PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000337"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000338 constants, names, varnames, filename, name, firstlineno,\n\
339 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340\n\
341Create a code object. Not for the faint of heart.");
342
343static PyObject *
344code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 int argcount;
347 int kwonlyargcount;
348 int nlocals;
349 int stacksize;
350 int flags;
351 PyObject *co = NULL;
352 PyObject *code;
353 PyObject *consts;
354 PyObject *names, *ournames = NULL;
355 PyObject *varnames, *ourvarnames = NULL;
356 PyObject *freevars = NULL, *ourfreevars = NULL;
357 PyObject *cellvars = NULL, *ourcellvars = NULL;
358 PyObject *filename;
359 PyObject *name;
360 int firstlineno;
361 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
364 &argcount, &kwonlyargcount,
365 &nlocals, &stacksize, &flags,
366 &code,
367 &PyTuple_Type, &consts,
368 &PyTuple_Type, &names,
369 &PyTuple_Type, &varnames,
370 &filename, &name,
371 &firstlineno, &lnotab,
372 &PyTuple_Type, &freevars,
373 &PyTuple_Type, &cellvars))
374 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (argcount < 0) {
377 PyErr_SetString(
378 PyExc_ValueError,
379 "code: argcount must not be negative");
380 goto cleanup;
381 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 if (kwonlyargcount < 0) {
384 PyErr_SetString(
385 PyExc_ValueError,
386 "code: kwonlyargcount must not be negative");
387 goto cleanup;
388 }
389 if (nlocals < 0) {
390 PyErr_SetString(
391 PyExc_ValueError,
392 "code: nlocals must not be negative");
393 goto cleanup;
394 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 ournames = validate_and_copy_tuple(names);
397 if (ournames == NULL)
398 goto cleanup;
399 ourvarnames = validate_and_copy_tuple(varnames);
400 if (ourvarnames == NULL)
401 goto cleanup;
402 if (freevars)
403 ourfreevars = validate_and_copy_tuple(freevars);
404 else
405 ourfreevars = PyTuple_New(0);
406 if (ourfreevars == NULL)
407 goto cleanup;
408 if (cellvars)
409 ourcellvars = validate_and_copy_tuple(cellvars);
410 else
411 ourcellvars = PyTuple_New(0);
412 if (ourcellvars == NULL)
413 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
416 nlocals, stacksize, flags,
417 code, consts, ournames, ourvarnames,
418 ourfreevars, ourcellvars, filename,
419 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 Py_XDECREF(ournames);
422 Py_XDECREF(ourvarnames);
423 Py_XDECREF(ourfreevars);
424 Py_XDECREF(ourcellvars);
425 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426}
427
428static void
429code_dealloc(PyCodeObject *co)
430{
Brett Cannon5c4de282016-09-07 11:16:41 -0700431 if (co->co_extra != NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200432 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannond0600ed2016-09-07 14:30:39 -0700433 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700434
Brett Cannond0600ed2016-09-07 14:30:39 -0700435 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700436 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700437
438 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700439 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700440 }
441 }
442
Victor Stinner23e79442017-06-28 02:12:00 +0200443 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700444 }
445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 Py_XDECREF(co->co_code);
447 Py_XDECREF(co->co_consts);
448 Py_XDECREF(co->co_names);
449 Py_XDECREF(co->co_varnames);
450 Py_XDECREF(co->co_freevars);
451 Py_XDECREF(co->co_cellvars);
452 Py_XDECREF(co->co_filename);
453 Py_XDECREF(co->co_name);
454 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500455 if (co->co_cell2arg != NULL)
456 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (co->co_zombieframe != NULL)
458 PyObject_GC_Del(co->co_zombieframe);
459 if (co->co_weakreflist != NULL)
460 PyObject_ClearWeakRefs((PyObject*)co);
461 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462}
463
464static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200465code_sizeof(PyCodeObject *co, void *unused)
466{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900467 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
468 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200469
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300470 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200471 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300472 }
473 if (co_extra != NULL) {
474 res += sizeof(_PyCodeObjectExtra) +
475 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
476 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200477 return PyLong_FromSsize_t(res);
478}
479
480static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481code_repr(PyCodeObject *co)
482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 int lineno;
484 if (co->co_firstlineno != 0)
485 lineno = co->co_firstlineno;
486 else
487 lineno = -1;
488 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
489 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000490 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 co->co_name, co, co->co_filename, lineno);
492 } else {
493 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000494 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 co->co_name, co, lineno);
496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497}
498
Victor Stinnerefb24132016-01-22 12:33:12 +0100499PyObject*
500_PyCode_ConstantKey(PyObject *op)
501{
502 PyObject *key;
503
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300504 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100505 if (op == Py_None || op == Py_Ellipsis
506 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100507 || PyUnicode_CheckExact(op)
508 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300509 || PyCode_Check(op))
510 {
511 /* Objects of these types are always different from object of other
512 * type and from tuples. */
513 Py_INCREF(op);
514 key = op;
515 }
516 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
517 /* Make booleans different from integers 0 and 1.
518 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100519 key = PyTuple_Pack(2, Py_TYPE(op), op);
520 }
521 else if (PyFloat_CheckExact(op)) {
522 double d = PyFloat_AS_DOUBLE(op);
523 /* all we need is to make the tuple different in either the 0.0
524 * or -0.0 case from all others, just to avoid the "coercion".
525 */
526 if (d == 0.0 && copysign(1.0, d) < 0.0)
527 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
528 else
529 key = PyTuple_Pack(2, Py_TYPE(op), op);
530 }
531 else if (PyComplex_CheckExact(op)) {
532 Py_complex z;
533 int real_negzero, imag_negzero;
534 /* For the complex case we must make complex(x, 0.)
535 different from complex(x, -0.) and complex(0., y)
536 different from complex(-0., y), for any x and y.
537 All four complex zeros must be distinguished.*/
538 z = PyComplex_AsCComplex(op);
539 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
540 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
541 /* use True, False and None singleton as tags for the real and imag
542 * sign, to make tuples different */
543 if (real_negzero && imag_negzero) {
544 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
545 }
546 else if (imag_negzero) {
547 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
548 }
549 else if (real_negzero) {
550 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
551 }
552 else {
553 key = PyTuple_Pack(2, Py_TYPE(op), op);
554 }
555 }
556 else if (PyTuple_CheckExact(op)) {
557 Py_ssize_t i, len;
558 PyObject *tuple;
559
560 len = PyTuple_GET_SIZE(op);
561 tuple = PyTuple_New(len);
562 if (tuple == NULL)
563 return NULL;
564
565 for (i=0; i < len; i++) {
566 PyObject *item, *item_key;
567
568 item = PyTuple_GET_ITEM(op, i);
569 item_key = _PyCode_ConstantKey(item);
570 if (item_key == NULL) {
571 Py_DECREF(tuple);
572 return NULL;
573 }
574
575 PyTuple_SET_ITEM(tuple, i, item_key);
576 }
577
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200578 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100579 Py_DECREF(tuple);
580 }
581 else if (PyFrozenSet_CheckExact(op)) {
582 Py_ssize_t pos = 0;
583 PyObject *item;
584 Py_hash_t hash;
585 Py_ssize_t i, len;
586 PyObject *tuple, *set;
587
588 len = PySet_GET_SIZE(op);
589 tuple = PyTuple_New(len);
590 if (tuple == NULL)
591 return NULL;
592
593 i = 0;
594 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
595 PyObject *item_key;
596
597 item_key = _PyCode_ConstantKey(item);
598 if (item_key == NULL) {
599 Py_DECREF(tuple);
600 return NULL;
601 }
602
603 assert(i < len);
604 PyTuple_SET_ITEM(tuple, i, item_key);
605 i++;
606 }
607 set = PyFrozenSet_New(tuple);
608 Py_DECREF(tuple);
609 if (set == NULL)
610 return NULL;
611
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200612 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100613 Py_DECREF(set);
614 return key;
615 }
616 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000617 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100618 * to ensure that they are seen as unequal. */
619 PyObject *obj_id = PyLong_FromVoidPtr(op);
620 if (obj_id == NULL)
621 return NULL;
622
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200623 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100624 Py_DECREF(obj_id);
625 }
626 return key;
627}
628
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000629static PyObject *
630code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 PyCodeObject *co, *cp;
633 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100634 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if ((op != Py_EQ && op != Py_NE) ||
638 !PyCode_Check(self) ||
639 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500640 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 co = (PyCodeObject *)self;
644 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
647 if (eq <= 0) goto unequal;
648 eq = co->co_argcount == cp->co_argcount;
649 if (!eq) goto unequal;
650 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
651 if (!eq) goto unequal;
652 eq = co->co_nlocals == cp->co_nlocals;
653 if (!eq) goto unequal;
654 eq = co->co_flags == cp->co_flags;
655 if (!eq) goto unequal;
656 eq = co->co_firstlineno == cp->co_firstlineno;
657 if (!eq) goto unequal;
658 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
659 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100660
661 /* compare constants */
662 consts1 = _PyCode_ConstantKey(co->co_consts);
663 if (!consts1)
664 return NULL;
665 consts2 = _PyCode_ConstantKey(cp->co_consts);
666 if (!consts2) {
667 Py_DECREF(consts1);
668 return NULL;
669 }
670 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
671 Py_DECREF(consts1);
672 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
676 if (eq <= 0) goto unequal;
677 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
678 if (eq <= 0) goto unequal;
679 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
680 if (eq <= 0) goto unequal;
681 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
682 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 if (op == Py_EQ)
685 res = Py_True;
686 else
687 res = Py_False;
688 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000689
690 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 if (eq < 0)
692 return NULL;
693 if (op == Py_NE)
694 res = Py_True;
695 else
696 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000697
698 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 Py_INCREF(res);
700 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701}
702
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000703static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704code_hash(PyCodeObject *co)
705{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000706 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 h0 = PyObject_Hash(co->co_name);
708 if (h0 == -1) return -1;
709 h1 = PyObject_Hash(co->co_code);
710 if (h1 == -1) return -1;
711 h2 = PyObject_Hash(co->co_consts);
712 if (h2 == -1) return -1;
713 h3 = PyObject_Hash(co->co_names);
714 if (h3 == -1) return -1;
715 h4 = PyObject_Hash(co->co_varnames);
716 if (h4 == -1) return -1;
717 h5 = PyObject_Hash(co->co_freevars);
718 if (h5 == -1) return -1;
719 h6 = PyObject_Hash(co->co_cellvars);
720 if (h6 == -1) return -1;
721 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
722 co->co_argcount ^ co->co_kwonlyargcount ^
723 co->co_nlocals ^ co->co_flags;
724 if (h == -1) h = -2;
725 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726}
727
728/* XXX code objects need to participate in GC? */
729
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200730static struct PyMethodDef code_methods[] = {
731 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
732 {NULL, NULL} /* sentinel */
733};
734
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 PyVarObject_HEAD_INIT(&PyType_Type, 0)
737 "code",
738 sizeof(PyCodeObject),
739 0,
740 (destructor)code_dealloc, /* tp_dealloc */
741 0, /* tp_print */
742 0, /* tp_getattr */
743 0, /* tp_setattr */
744 0, /* tp_reserved */
745 (reprfunc)code_repr, /* tp_repr */
746 0, /* tp_as_number */
747 0, /* tp_as_sequence */
748 0, /* tp_as_mapping */
749 (hashfunc)code_hash, /* tp_hash */
750 0, /* tp_call */
751 0, /* tp_str */
752 PyObject_GenericGetAttr, /* tp_getattro */
753 0, /* tp_setattro */
754 0, /* tp_as_buffer */
755 Py_TPFLAGS_DEFAULT, /* tp_flags */
756 code_doc, /* tp_doc */
757 0, /* tp_traverse */
758 0, /* tp_clear */
759 code_richcompare, /* tp_richcompare */
760 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
761 0, /* tp_iter */
762 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200763 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 code_memberlist, /* tp_members */
765 0, /* tp_getset */
766 0, /* tp_base */
767 0, /* tp_dict */
768 0, /* tp_descr_get */
769 0, /* tp_descr_set */
770 0, /* tp_dictoffset */
771 0, /* tp_init */
772 0, /* tp_alloc */
773 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774};
775
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000776/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
777 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778*/
779
780int
781PyCode_Addr2Line(PyCodeObject *co, int addrq)
782{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000783 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
785 int line = co->co_firstlineno;
786 int addr = 0;
787 while (--size >= 0) {
788 addr += *p++;
789 if (addr > addrq)
790 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100791 line += (signed char)*p;
792 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
794 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000796
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000797/* Update *bounds to describe the first and one-past-the-last instructions in
798 the same line as lasti. Return the number of that line. */
799int
800_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000801{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000802 Py_ssize_t size;
803 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
807 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 addr = 0;
810 line = co->co_firstlineno;
811 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 /* possible optimization: if f->f_lasti == instr_ub
814 (likely to be a common case) then we already know
815 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700816 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 /* See lnotab_notes.txt for the description of
819 co_lnotab. A point to remember: increments to p
820 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 bounds->ap_lower = 0;
823 while (size > 0) {
824 if (addr + *p > lasti)
825 break;
826 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100827 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100829 line += (signed char)*p;
830 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 --size;
832 }
833
834 if (size > 0) {
835 while (--size >= 0) {
836 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100837 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100839 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 bounds->ap_upper = addr;
842 }
843 else {
844 bounds->ap_upper = INT_MAX;
845 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848}
Brett Cannon5c4de282016-09-07 11:16:41 -0700849
850
851int
852_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
853{
Brett Cannon5c4de282016-09-07 11:16:41 -0700854 if (!PyCode_Check(code)) {
855 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700856 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700857 }
858
Brett Cannond0600ed2016-09-07 14:30:39 -0700859 PyCodeObject *o = (PyCodeObject*) code;
860 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700861
Brett Cannond0600ed2016-09-07 14:30:39 -0700862 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700863 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700864 return 0;
865 }
866
Brett Cannond0600ed2016-09-07 14:30:39 -0700867 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700868 return 0;
869}
870
871
872int
873_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
874{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200875 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -0700876
877 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700878 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700879 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700880 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700881 }
882
Brett Cannond0600ed2016-09-07 14:30:39 -0700883 PyCodeObject *o = (PyCodeObject*) code;
884 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700885
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300886 if (co_extra == NULL || co_extra->ce_size <= index) {
887 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
888 co_extra = PyMem_Realloc(
889 co_extra,
890 sizeof(_PyCodeObjectExtra) +
891 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +0000892 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700893 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700894 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300895 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700896 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700897 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700898 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300899 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700900 }
901
902 if (co_extra->ce_extras[index] != NULL) {
903 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700904 if (free != NULL) {
905 free(co_extra->ce_extras[index]);
906 }
Brett Cannon5c4de282016-09-07 11:16:41 -0700907 }
908
Brett Cannond0600ed2016-09-07 14:30:39 -0700909 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700910 return 0;
911}