blob: 1e76f26d98fe67cf0d15e2b05d9d5fd381026790 [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"
Victor Stinnera9f05d62019-05-24 23:57:23 +02008#include "clinic/codeobject.c.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009
Brett Cannond0600ed2016-09-07 14:30:39 -070010/* Holder for co_extra information */
11typedef struct {
12 Py_ssize_t ce_size;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +030013 void *ce_extras[1];
Brett Cannond0600ed2016-09-07 14:30:39 -070014} _PyCodeObjectExtra;
15
Victor Stinnera9f05d62019-05-24 23:57:23 +020016/*[clinic input]
17class code "PyCodeObject *" "&PyCode_Type"
18[clinic start generated code]*/
19/*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
20
Benjamin Peterson8e0ad462017-09-07 23:35:53 -070021/* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020023all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024{
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030025 const unsigned char *s, *e;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020026
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030027 if (!PyUnicode_IS_ASCII(o))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020028 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030030 s = PyUnicode_1BYTE_DATA(o);
31 e = s + PyUnicode_GET_LENGTH(o);
Benjamin Peterson9020ac72017-09-07 18:06:23 -070032 for (; s != e; s++) {
Benjamin Peterson2b7953d2017-09-08 10:35:49 -070033 if (!Py_ISALNUM(*s) && *s != '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 return 0;
35 }
36 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037}
38
39static void
40intern_strings(PyObject *tuple)
41{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
45 PyObject *v = PyTuple_GET_ITEM(tuple, i);
46 if (v == NULL || !PyUnicode_CheckExact(v)) {
47 Py_FatalError("non-string found in code slot");
48 }
Victor Stinnerd17a6932018-11-09 16:56:48 +010049 PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000051}
52
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030053/* Intern selected string constants */
54static int
55intern_string_constants(PyObject *tuple)
56{
57 int modified = 0;
58 Py_ssize_t i;
59
60 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
61 PyObject *v = PyTuple_GET_ITEM(tuple, i);
62 if (PyUnicode_CheckExact(v)) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030063 if (PyUnicode_READY(v) == -1) {
64 PyErr_Clear();
65 continue;
66 }
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030067 if (all_name_chars(v)) {
68 PyObject *w = v;
69 PyUnicode_InternInPlace(&v);
70 if (w != v) {
71 PyTuple_SET_ITEM(tuple, i, v);
72 modified = 1;
73 }
74 }
75 }
76 else if (PyTuple_CheckExact(v)) {
77 intern_string_constants(v);
78 }
79 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050080 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030081 PyObject *tmp = PySequence_Tuple(v);
82 if (tmp == NULL) {
83 PyErr_Clear();
84 continue;
85 }
86 if (intern_string_constants(tmp)) {
87 v = PyFrozenSet_New(tmp);
88 if (v == NULL) {
89 PyErr_Clear();
90 }
91 else {
92 PyTuple_SET_ITEM(tuple, i, v);
Yury Selivanovd2fd3592016-11-09 09:42:14 -050093 Py_DECREF(w);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030094 modified = 1;
95 }
96 }
97 Py_DECREF(tmp);
98 }
99 }
100 return modified;
101}
102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103
104PyCodeObject *
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100105PyCode_New(int argcount, int posonlyargcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 int nlocals, int stacksize, int flags,
107 PyObject *code, PyObject *consts, PyObject *names,
108 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
109 PyObject *filename, PyObject *name, int firstlineno,
110 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200113 Py_ssize_t *cell2arg = NULL;
Serhiy Storchakabd473842018-07-16 09:10:19 +0300114 Py_ssize_t i, n_cellvars, n_varnames, total_args;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 /* Check argument types */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100117 if (argcount < 0 || posonlyargcount < 0 || kwonlyargcount < 0 ||
Victor Stinnera9f05d62019-05-24 23:57:23 +0200118 nlocals < 0 || stacksize < 0 || flags < 0 ||
119 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 consts == NULL || !PyTuple_Check(consts) ||
121 names == NULL || !PyTuple_Check(names) ||
122 varnames == NULL || !PyTuple_Check(varnames) ||
123 freevars == NULL || !PyTuple_Check(freevars) ||
124 cellvars == NULL || !PyTuple_Check(cellvars) ||
125 name == NULL || !PyUnicode_Check(name) ||
126 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200127 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 PyErr_BadInternalCall();
129 return NULL;
130 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200131
Victor Stinnera9f05d62019-05-24 23:57:23 +0200132 /* Ensure that strings are ready Unicode string */
133 if (PyUnicode_READY(name) < 0) {
Victor Stinner7c74de42013-10-10 15:55:14 +0200134 return NULL;
Victor Stinnera9f05d62019-05-24 23:57:23 +0200135 }
136 if (PyUnicode_READY(filename) < 0) {
137 return NULL;
138 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 intern_strings(names);
141 intern_strings(varnames);
142 intern_strings(freevars);
143 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300144 intern_string_constants(consts);
Nick Coghlan078f1812017-12-03 11:12:20 +1000145
146 /* Check for any inner or outer closure references */
147 n_cellvars = PyTuple_GET_SIZE(cellvars);
148 if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
149 flags |= CO_NOFREE;
150 } else {
151 flags &= ~CO_NOFREE;
152 }
153
Serhiy Storchakabd473842018-07-16 09:10:19 +0300154 n_varnames = PyTuple_GET_SIZE(varnames);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100155 if (posonlyargcount + argcount <= n_varnames
156 && kwonlyargcount <= n_varnames) {
Serhiy Storchakabd473842018-07-16 09:10:19 +0300157 /* Never overflows. */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100158 total_args = (Py_ssize_t)posonlyargcount + (Py_ssize_t)argcount
159 + (Py_ssize_t)kwonlyargcount +
160 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
Serhiy Storchakabd473842018-07-16 09:10:19 +0300161 }
162 else {
163 total_args = n_varnames + 1;
164 }
165 if (total_args > n_varnames) {
166 PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
167 return NULL;
168 }
169
Benjamin Peterson90037602011-06-25 22:54:45 -0500170 /* Create mapping between cells and arguments if needed. */
171 if (n_cellvars) {
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700172 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200173 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
174 if (cell2arg == NULL) {
175 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500176 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200177 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500178 /* Find cells which are also arguments. */
179 for (i = 0; i < n_cellvars; i++) {
180 Py_ssize_t j;
181 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200182 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500183 for (j = 0; j < total_args; j++) {
184 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200185 int cmp = PyUnicode_Compare(cell, arg);
186 if (cmp == -1 && PyErr_Occurred()) {
187 PyMem_FREE(cell2arg);
188 return NULL;
189 }
190 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500191 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700192 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500193 break;
194 }
195 }
196 }
197 if (!used_cell2arg) {
198 PyMem_FREE(cell2arg);
199 cell2arg = NULL;
200 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500202 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
203 if (co == NULL) {
204 if (cell2arg)
205 PyMem_FREE(cell2arg);
206 return NULL;
207 }
208 co->co_argcount = argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100209 co->co_posonlyargcount = posonlyargcount;
Benjamin Peterson90037602011-06-25 22:54:45 -0500210 co->co_kwonlyargcount = kwonlyargcount;
211 co->co_nlocals = nlocals;
212 co->co_stacksize = stacksize;
213 co->co_flags = flags;
214 Py_INCREF(code);
215 co->co_code = code;
216 Py_INCREF(consts);
217 co->co_consts = consts;
218 Py_INCREF(names);
219 co->co_names = names;
220 Py_INCREF(varnames);
221 co->co_varnames = varnames;
222 Py_INCREF(freevars);
223 co->co_freevars = freevars;
224 Py_INCREF(cellvars);
225 co->co_cellvars = cellvars;
226 co->co_cell2arg = cell2arg;
227 Py_INCREF(filename);
228 co->co_filename = filename;
229 Py_INCREF(name);
230 co->co_name = name;
231 co->co_firstlineno = firstlineno;
232 Py_INCREF(lnotab);
233 co->co_lnotab = lnotab;
234 co->co_zombieframe = NULL;
235 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700236 co->co_extra = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238}
239
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000240PyCodeObject *
241PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 static PyObject *emptystring = NULL;
244 static PyObject *nulltuple = NULL;
245 PyObject *filename_ob = NULL;
246 PyObject *funcname_ob = NULL;
247 PyCodeObject *result = NULL;
248 if (emptystring == NULL) {
249 emptystring = PyBytes_FromString("");
250 if (emptystring == NULL)
251 goto failed;
252 }
253 if (nulltuple == NULL) {
254 nulltuple = PyTuple_New(0);
255 if (nulltuple == NULL)
256 goto failed;
257 }
258 funcname_ob = PyUnicode_FromString(funcname);
259 if (funcname_ob == NULL)
260 goto failed;
261 filename_ob = PyUnicode_DecodeFSDefault(filename);
262 if (filename_ob == NULL)
263 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 result = PyCode_New(0, /* argcount */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100266 0, /* posonlyargcount */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 0, /* kwonlyargcount */
268 0, /* nlocals */
269 0, /* stacksize */
270 0, /* flags */
271 emptystring, /* code */
272 nulltuple, /* consts */
273 nulltuple, /* names */
274 nulltuple, /* varnames */
275 nulltuple, /* freevars */
276 nulltuple, /* cellvars */
277 filename_ob, /* filename */
278 funcname_ob, /* name */
279 firstlineno, /* firstlineno */
280 emptystring /* lnotab */
281 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000282
283failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 Py_XDECREF(funcname_ob);
285 Py_XDECREF(filename_ob);
286 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000287}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288
289#define OFF(x) offsetof(PyCodeObject, x)
290
291static PyMemberDef code_memberlist[] = {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100292 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
293 {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
294 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
295 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
296 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
297 {"co_flags", T_INT, OFF(co_flags), READONLY},
298 {"co_code", T_OBJECT, OFF(co_code), READONLY},
299 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
300 {"co_names", T_OBJECT, OFF(co_names), READONLY},
301 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
302 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
303 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
304 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
305 {"co_name", T_OBJECT, OFF(co_name), READONLY},
306 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
307 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309};
310
311/* Helper for code_new: return a shallow copy of a tuple that is
312 guaranteed to contain exact strings, by converting string subclasses
313 to exact strings and complaining if a non-string is found. */
314static PyObject*
315validate_and_copy_tuple(PyObject *tup)
316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 PyObject *newtuple;
318 PyObject *item;
319 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 len = PyTuple_GET_SIZE(tup);
322 newtuple = PyTuple_New(len);
323 if (newtuple == NULL)
324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 for (i = 0; i < len; i++) {
327 item = PyTuple_GET_ITEM(tup, i);
328 if (PyUnicode_CheckExact(item)) {
329 Py_INCREF(item);
330 }
331 else if (!PyUnicode_Check(item)) {
332 PyErr_Format(
333 PyExc_TypeError,
334 "name tuples must contain only "
335 "strings, not '%.500s'",
336 item->ob_type->tp_name);
337 Py_DECREF(newtuple);
338 return NULL;
339 }
340 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100341 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 if (item == NULL) {
343 Py_DECREF(newtuple);
344 return NULL;
345 }
346 }
347 PyTuple_SET_ITEM(newtuple, i, item);
348 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351}
352
353PyDoc_STRVAR(code_doc,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100354"code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
355 flags, codestring, constants, names, varnames, filename, name,\n\
356 firstlineno, lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357\n\
358Create a code object. Not for the faint of heart.");
359
360static PyObject *
361code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 int argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100364 int posonlyargcount;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 int kwonlyargcount;
366 int nlocals;
367 int stacksize;
368 int flags;
369 PyObject *co = NULL;
370 PyObject *code;
371 PyObject *consts;
372 PyObject *names, *ournames = NULL;
373 PyObject *varnames, *ourvarnames = NULL;
374 PyObject *freevars = NULL, *ourfreevars = NULL;
375 PyObject *cellvars = NULL, *ourcellvars = NULL;
376 PyObject *filename;
377 PyObject *name;
378 int firstlineno;
379 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100381 if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
382 &argcount, &posonlyargcount, &kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 &nlocals, &stacksize, &flags,
384 &code,
385 &PyTuple_Type, &consts,
386 &PyTuple_Type, &names,
387 &PyTuple_Type, &varnames,
388 &filename, &name,
389 &firstlineno, &lnotab,
390 &PyTuple_Type, &freevars,
391 &PyTuple_Type, &cellvars))
392 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393
Steve Dowerb82e17e2019-05-23 08:45:22 -0700394 if (PySys_Audit("code.__new__", "OOOiiiii",
395 code, filename, name, argcount, kwonlyargcount,
396 nlocals, stacksize, flags) < 0) {
397 goto cleanup;
398 }
399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (argcount < 0) {
401 PyErr_SetString(
402 PyExc_ValueError,
403 "code: argcount must not be negative");
404 goto cleanup;
405 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000406
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100407 if (posonlyargcount < 0) {
408 PyErr_SetString(
409 PyExc_ValueError,
410 "code: posonlyargcount must not be negative");
411 goto cleanup;
412 }
413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (kwonlyargcount < 0) {
415 PyErr_SetString(
416 PyExc_ValueError,
417 "code: kwonlyargcount must not be negative");
418 goto cleanup;
419 }
420 if (nlocals < 0) {
421 PyErr_SetString(
422 PyExc_ValueError,
423 "code: nlocals must not be negative");
424 goto cleanup;
425 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 ournames = validate_and_copy_tuple(names);
428 if (ournames == NULL)
429 goto cleanup;
430 ourvarnames = validate_and_copy_tuple(varnames);
431 if (ourvarnames == NULL)
432 goto cleanup;
433 if (freevars)
434 ourfreevars = validate_and_copy_tuple(freevars);
435 else
436 ourfreevars = PyTuple_New(0);
437 if (ourfreevars == NULL)
438 goto cleanup;
439 if (cellvars)
440 ourcellvars = validate_and_copy_tuple(cellvars);
441 else
442 ourcellvars = PyTuple_New(0);
443 if (ourcellvars == NULL)
444 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100446 co = (PyObject *)PyCode_New(argcount, posonlyargcount, kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 nlocals, stacksize, flags,
448 code, consts, ournames, ourvarnames,
449 ourfreevars, ourcellvars, filename,
450 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 Py_XDECREF(ournames);
453 Py_XDECREF(ourvarnames);
454 Py_XDECREF(ourfreevars);
455 Py_XDECREF(ourcellvars);
456 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457}
458
459static void
460code_dealloc(PyCodeObject *co)
461{
Brett Cannon5c4de282016-09-07 11:16:41 -0700462 if (co->co_extra != NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200463 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannond0600ed2016-09-07 14:30:39 -0700464 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700465
Brett Cannond0600ed2016-09-07 14:30:39 -0700466 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700467 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700468
469 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700470 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700471 }
472 }
473
Victor Stinner23e79442017-06-28 02:12:00 +0200474 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700475 }
476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 Py_XDECREF(co->co_code);
478 Py_XDECREF(co->co_consts);
479 Py_XDECREF(co->co_names);
480 Py_XDECREF(co->co_varnames);
481 Py_XDECREF(co->co_freevars);
482 Py_XDECREF(co->co_cellvars);
483 Py_XDECREF(co->co_filename);
484 Py_XDECREF(co->co_name);
485 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500486 if (co->co_cell2arg != NULL)
487 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (co->co_zombieframe != NULL)
489 PyObject_GC_Del(co->co_zombieframe);
490 if (co->co_weakreflist != NULL)
491 PyObject_ClearWeakRefs((PyObject*)co);
492 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493}
494
495static PyObject *
Victor Stinnera9f05d62019-05-24 23:57:23 +0200496code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200497{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900498 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
499 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200500
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300501 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200502 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300503 }
504 if (co_extra != NULL) {
505 res += sizeof(_PyCodeObjectExtra) +
506 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
507 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200508 return PyLong_FromSsize_t(res);
509}
510
Victor Stinnera9f05d62019-05-24 23:57:23 +0200511/*[clinic input]
512code.replace
513
514 *
515 co_argcount: int(c_default="self->co_argcount") = -1
516 co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
517 co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
518 co_nlocals: int(c_default="self->co_nlocals") = -1
519 co_stacksize: int(c_default="self->co_stacksize") = -1
520 co_flags: int(c_default="self->co_flags") = -1
521 co_firstlineno: int(c_default="self->co_firstlineno") = -1
522 co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
523 co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
524 co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
525 co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
526 co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
527 co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
528 co_filename: unicode(c_default="self->co_filename") = None
529 co_name: unicode(c_default="self->co_name") = None
530 co_lnotab: PyBytesObject(c_default="(PyBytesObject *)self->co_lnotab") = None
531
532Return a new code object with new specified fields.
533[clinic start generated code]*/
534
535static PyObject *
536code_replace_impl(PyCodeObject *self, int co_argcount,
537 int co_posonlyargcount, int co_kwonlyargcount,
538 int co_nlocals, int co_stacksize, int co_flags,
539 int co_firstlineno, PyBytesObject *co_code,
540 PyObject *co_consts, PyObject *co_names,
541 PyObject *co_varnames, PyObject *co_freevars,
542 PyObject *co_cellvars, PyObject *co_filename,
543 PyObject *co_name, PyBytesObject *co_lnotab)
544/*[clinic end generated code: output=25c8e303913bcace input=77189e46579ec426]*/
545{
546#define CHECK_INT_ARG(ARG) \
547 if (ARG < 0) { \
548 PyErr_SetString(PyExc_ValueError, \
549 #ARG " must be a positive integer"); \
550 return NULL; \
551 }
552
553 CHECK_INT_ARG(co_argcount);
554 CHECK_INT_ARG(co_posonlyargcount);
555 CHECK_INT_ARG(co_kwonlyargcount);
556 CHECK_INT_ARG(co_nlocals);
557 CHECK_INT_ARG(co_stacksize);
558 CHECK_INT_ARG(co_flags);
559 CHECK_INT_ARG(co_firstlineno);
560
561#undef CHECK_INT_ARG
562
563 return (PyObject *)PyCode_New(
564 co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
565 co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
566 co_varnames, co_freevars, co_cellvars, co_filename, co_name,
567 co_firstlineno, (PyObject*)co_lnotab);
568}
569
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200570static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571code_repr(PyCodeObject *co)
572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 int lineno;
574 if (co->co_firstlineno != 0)
575 lineno = co->co_firstlineno;
576 else
577 lineno = -1;
578 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
579 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000580 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 co->co_name, co, co->co_filename, lineno);
582 } else {
583 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000584 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 co->co_name, co, lineno);
586 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587}
588
Victor Stinnerefb24132016-01-22 12:33:12 +0100589PyObject*
590_PyCode_ConstantKey(PyObject *op)
591{
592 PyObject *key;
593
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300594 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100595 if (op == Py_None || op == Py_Ellipsis
596 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100597 || PyUnicode_CheckExact(op)
598 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300599 || PyCode_Check(op))
600 {
601 /* Objects of these types are always different from object of other
602 * type and from tuples. */
603 Py_INCREF(op);
604 key = op;
605 }
606 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
607 /* Make booleans different from integers 0 and 1.
608 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100609 key = PyTuple_Pack(2, Py_TYPE(op), op);
610 }
611 else if (PyFloat_CheckExact(op)) {
612 double d = PyFloat_AS_DOUBLE(op);
613 /* all we need is to make the tuple different in either the 0.0
614 * or -0.0 case from all others, just to avoid the "coercion".
615 */
616 if (d == 0.0 && copysign(1.0, d) < 0.0)
617 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
618 else
619 key = PyTuple_Pack(2, Py_TYPE(op), op);
620 }
621 else if (PyComplex_CheckExact(op)) {
622 Py_complex z;
623 int real_negzero, imag_negzero;
624 /* For the complex case we must make complex(x, 0.)
625 different from complex(x, -0.) and complex(0., y)
626 different from complex(-0., y), for any x and y.
627 All four complex zeros must be distinguished.*/
628 z = PyComplex_AsCComplex(op);
629 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
630 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
631 /* use True, False and None singleton as tags for the real and imag
632 * sign, to make tuples different */
633 if (real_negzero && imag_negzero) {
634 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
635 }
636 else if (imag_negzero) {
637 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
638 }
639 else if (real_negzero) {
640 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
641 }
642 else {
643 key = PyTuple_Pack(2, Py_TYPE(op), op);
644 }
645 }
646 else if (PyTuple_CheckExact(op)) {
647 Py_ssize_t i, len;
648 PyObject *tuple;
649
650 len = PyTuple_GET_SIZE(op);
651 tuple = PyTuple_New(len);
652 if (tuple == NULL)
653 return NULL;
654
655 for (i=0; i < len; i++) {
656 PyObject *item, *item_key;
657
658 item = PyTuple_GET_ITEM(op, i);
659 item_key = _PyCode_ConstantKey(item);
660 if (item_key == NULL) {
661 Py_DECREF(tuple);
662 return NULL;
663 }
664
665 PyTuple_SET_ITEM(tuple, i, item_key);
666 }
667
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200668 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100669 Py_DECREF(tuple);
670 }
671 else if (PyFrozenSet_CheckExact(op)) {
672 Py_ssize_t pos = 0;
673 PyObject *item;
674 Py_hash_t hash;
675 Py_ssize_t i, len;
676 PyObject *tuple, *set;
677
678 len = PySet_GET_SIZE(op);
679 tuple = PyTuple_New(len);
680 if (tuple == NULL)
681 return NULL;
682
683 i = 0;
684 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
685 PyObject *item_key;
686
687 item_key = _PyCode_ConstantKey(item);
688 if (item_key == NULL) {
689 Py_DECREF(tuple);
690 return NULL;
691 }
692
693 assert(i < len);
694 PyTuple_SET_ITEM(tuple, i, item_key);
695 i++;
696 }
697 set = PyFrozenSet_New(tuple);
698 Py_DECREF(tuple);
699 if (set == NULL)
700 return NULL;
701
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200702 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100703 Py_DECREF(set);
704 return key;
705 }
706 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000707 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100708 * to ensure that they are seen as unequal. */
709 PyObject *obj_id = PyLong_FromVoidPtr(op);
710 if (obj_id == NULL)
711 return NULL;
712
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200713 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100714 Py_DECREF(obj_id);
715 }
716 return key;
717}
718
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000719static PyObject *
720code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 PyCodeObject *co, *cp;
723 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100724 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 if ((op != Py_EQ && op != Py_NE) ||
728 !PyCode_Check(self) ||
729 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500730 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 co = (PyCodeObject *)self;
734 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100737 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 eq = co->co_argcount == cp->co_argcount;
739 if (!eq) goto unequal;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100740 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
741 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
743 if (!eq) goto unequal;
744 eq = co->co_nlocals == cp->co_nlocals;
745 if (!eq) goto unequal;
746 eq = co->co_flags == cp->co_flags;
747 if (!eq) goto unequal;
748 eq = co->co_firstlineno == cp->co_firstlineno;
749 if (!eq) goto unequal;
750 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
751 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100752
753 /* compare constants */
754 consts1 = _PyCode_ConstantKey(co->co_consts);
755 if (!consts1)
756 return NULL;
757 consts2 = _PyCode_ConstantKey(cp->co_consts);
758 if (!consts2) {
759 Py_DECREF(consts1);
760 return NULL;
761 }
762 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
763 Py_DECREF(consts1);
764 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
768 if (eq <= 0) goto unequal;
769 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
770 if (eq <= 0) goto unequal;
771 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
772 if (eq <= 0) goto unequal;
773 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
774 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 if (op == Py_EQ)
777 res = Py_True;
778 else
779 res = Py_False;
780 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000781
782 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 if (eq < 0)
784 return NULL;
785 if (op == Py_NE)
786 res = Py_True;
787 else
788 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000789
790 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 Py_INCREF(res);
792 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793}
794
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000795static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796code_hash(PyCodeObject *co)
797{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000798 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 h0 = PyObject_Hash(co->co_name);
800 if (h0 == -1) return -1;
801 h1 = PyObject_Hash(co->co_code);
802 if (h1 == -1) return -1;
803 h2 = PyObject_Hash(co->co_consts);
804 if (h2 == -1) return -1;
805 h3 = PyObject_Hash(co->co_names);
806 if (h3 == -1) return -1;
807 h4 = PyObject_Hash(co->co_varnames);
808 if (h4 == -1) return -1;
809 h5 = PyObject_Hash(co->co_freevars);
810 if (h5 == -1) return -1;
811 h6 = PyObject_Hash(co->co_cellvars);
812 if (h6 == -1) return -1;
813 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100814 co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 co->co_nlocals ^ co->co_flags;
816 if (h == -1) h = -2;
817 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818}
819
820/* XXX code objects need to participate in GC? */
821
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200822static struct PyMethodDef code_methods[] = {
823 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
Victor Stinnera9f05d62019-05-24 23:57:23 +0200824 CODE_REPLACE_METHODDEF
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200825 {NULL, NULL} /* sentinel */
826};
827
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 PyVarObject_HEAD_INIT(&PyType_Type, 0)
830 "code",
831 sizeof(PyCodeObject),
832 0,
833 (destructor)code_dealloc, /* tp_dealloc */
834 0, /* tp_print */
835 0, /* tp_getattr */
836 0, /* tp_setattr */
837 0, /* tp_reserved */
838 (reprfunc)code_repr, /* tp_repr */
839 0, /* tp_as_number */
840 0, /* tp_as_sequence */
841 0, /* tp_as_mapping */
842 (hashfunc)code_hash, /* tp_hash */
843 0, /* tp_call */
844 0, /* tp_str */
845 PyObject_GenericGetAttr, /* tp_getattro */
846 0, /* tp_setattro */
847 0, /* tp_as_buffer */
848 Py_TPFLAGS_DEFAULT, /* tp_flags */
849 code_doc, /* tp_doc */
850 0, /* tp_traverse */
851 0, /* tp_clear */
852 code_richcompare, /* tp_richcompare */
853 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
854 0, /* tp_iter */
855 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200856 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 code_memberlist, /* tp_members */
858 0, /* tp_getset */
859 0, /* tp_base */
860 0, /* tp_dict */
861 0, /* tp_descr_get */
862 0, /* tp_descr_set */
863 0, /* tp_dictoffset */
864 0, /* tp_init */
865 0, /* tp_alloc */
866 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867};
868
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000869/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
870 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871*/
872
873int
874PyCode_Addr2Line(PyCodeObject *co, int addrq)
875{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000876 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
878 int line = co->co_firstlineno;
879 int addr = 0;
880 while (--size >= 0) {
881 addr += *p++;
882 if (addr > addrq)
883 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100884 line += (signed char)*p;
885 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 }
887 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000889
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000890/* Update *bounds to describe the first and one-past-the-last instructions in
891 the same line as lasti. Return the number of that line. */
892int
893_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000894{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000895 Py_ssize_t size;
896 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
900 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 addr = 0;
903 line = co->co_firstlineno;
904 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* possible optimization: if f->f_lasti == instr_ub
907 (likely to be a common case) then we already know
908 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700909 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 /* See lnotab_notes.txt for the description of
912 co_lnotab. A point to remember: increments to p
913 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 bounds->ap_lower = 0;
916 while (size > 0) {
917 if (addr + *p > lasti)
918 break;
919 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100920 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100922 line += (signed char)*p;
923 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 --size;
925 }
926
927 if (size > 0) {
928 while (--size >= 0) {
929 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100930 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100932 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 bounds->ap_upper = addr;
935 }
936 else {
937 bounds->ap_upper = INT_MAX;
938 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000941}
Brett Cannon5c4de282016-09-07 11:16:41 -0700942
943
944int
945_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
946{
Brett Cannon5c4de282016-09-07 11:16:41 -0700947 if (!PyCode_Check(code)) {
948 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700949 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700950 }
951
Brett Cannond0600ed2016-09-07 14:30:39 -0700952 PyCodeObject *o = (PyCodeObject*) code;
953 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700954
Brett Cannond0600ed2016-09-07 14:30:39 -0700955 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700956 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700957 return 0;
958 }
959
Brett Cannond0600ed2016-09-07 14:30:39 -0700960 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700961 return 0;
962}
963
964
965int
966_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
967{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200968 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -0700969
970 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700971 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700972 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700973 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700974 }
975
Brett Cannond0600ed2016-09-07 14:30:39 -0700976 PyCodeObject *o = (PyCodeObject*) code;
977 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700978
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300979 if (co_extra == NULL || co_extra->ce_size <= index) {
980 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
981 co_extra = PyMem_Realloc(
982 co_extra,
983 sizeof(_PyCodeObjectExtra) +
984 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +0000985 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700986 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700987 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300988 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700989 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700990 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700991 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300992 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700993 }
994
995 if (co_extra->ce_extras[index] != NULL) {
996 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700997 if (free != NULL) {
998 free(co_extra->ce_extras[index]);
999 }
Brett Cannon5c4de282016-09-07 11:16:41 -07001000 }
1001
Brett Cannond0600ed2016-09-07 14:30:39 -07001002 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001003 return 0;
1004}