blob: bf68e54f42ec1c581665487c3d33210282aa0ba2 [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 Galindocd74e662019-06-01 18:08:04 +0100117 if (argcount < posonlyargcount || posonlyargcount < 0 ||
118 kwonlyargcount < 0 || nlocals < 0 ||
119 stacksize < 0 || flags < 0 ||
Victor Stinnera9f05d62019-05-24 23:57:23 +0200120 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 consts == NULL || !PyTuple_Check(consts) ||
122 names == NULL || !PyTuple_Check(names) ||
123 varnames == NULL || !PyTuple_Check(varnames) ||
124 freevars == NULL || !PyTuple_Check(freevars) ||
125 cellvars == NULL || !PyTuple_Check(cellvars) ||
126 name == NULL || !PyUnicode_Check(name) ||
127 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200128 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 PyErr_BadInternalCall();
130 return NULL;
131 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200132
Victor Stinnera9f05d62019-05-24 23:57:23 +0200133 /* Ensure that strings are ready Unicode string */
134 if (PyUnicode_READY(name) < 0) {
Victor Stinner7c74de42013-10-10 15:55:14 +0200135 return NULL;
Victor Stinnera9f05d62019-05-24 23:57:23 +0200136 }
137 if (PyUnicode_READY(filename) < 0) {
138 return NULL;
139 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 intern_strings(names);
142 intern_strings(varnames);
143 intern_strings(freevars);
144 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300145 intern_string_constants(consts);
Nick Coghlan078f1812017-12-03 11:12:20 +1000146
147 /* Check for any inner or outer closure references */
148 n_cellvars = PyTuple_GET_SIZE(cellvars);
149 if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
150 flags |= CO_NOFREE;
151 } else {
152 flags &= ~CO_NOFREE;
153 }
154
Serhiy Storchakabd473842018-07-16 09:10:19 +0300155 n_varnames = PyTuple_GET_SIZE(varnames);
Pablo Galindocd74e662019-06-01 18:08:04 +0100156 if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
Serhiy Storchakabd473842018-07-16 09:10:19 +0300157 /* Never overflows. */
Pablo Galindocd74e662019-06-01 18:08:04 +0100158 total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100159 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
Serhiy Storchakabd473842018-07-16 09:10:19 +0300160 }
161 else {
162 total_args = n_varnames + 1;
163 }
164 if (total_args > n_varnames) {
165 PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
166 return NULL;
167 }
168
Benjamin Peterson90037602011-06-25 22:54:45 -0500169 /* Create mapping between cells and arguments if needed. */
170 if (n_cellvars) {
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700171 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200172 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
173 if (cell2arg == NULL) {
174 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500175 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200176 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500177 /* Find cells which are also arguments. */
178 for (i = 0; i < n_cellvars; i++) {
179 Py_ssize_t j;
180 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200181 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500182 for (j = 0; j < total_args; j++) {
183 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200184 int cmp = PyUnicode_Compare(cell, arg);
185 if (cmp == -1 && PyErr_Occurred()) {
186 PyMem_FREE(cell2arg);
187 return NULL;
188 }
189 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500190 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700191 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500192 break;
193 }
194 }
195 }
196 if (!used_cell2arg) {
197 PyMem_FREE(cell2arg);
198 cell2arg = NULL;
199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500201 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
202 if (co == NULL) {
203 if (cell2arg)
204 PyMem_FREE(cell2arg);
205 return NULL;
206 }
207 co->co_argcount = argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100208 co->co_posonlyargcount = posonlyargcount;
Benjamin Peterson90037602011-06-25 22:54:45 -0500209 co->co_kwonlyargcount = kwonlyargcount;
210 co->co_nlocals = nlocals;
211 co->co_stacksize = stacksize;
212 co->co_flags = flags;
213 Py_INCREF(code);
214 co->co_code = code;
215 Py_INCREF(consts);
216 co->co_consts = consts;
217 Py_INCREF(names);
218 co->co_names = names;
219 Py_INCREF(varnames);
220 co->co_varnames = varnames;
221 Py_INCREF(freevars);
222 co->co_freevars = freevars;
223 Py_INCREF(cellvars);
224 co->co_cellvars = cellvars;
225 co->co_cell2arg = cell2arg;
226 Py_INCREF(filename);
227 co->co_filename = filename;
228 Py_INCREF(name);
229 co->co_name = name;
230 co->co_firstlineno = firstlineno;
231 Py_INCREF(lnotab);
232 co->co_lnotab = lnotab;
233 co->co_zombieframe = NULL;
234 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700235 co->co_extra = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237}
238
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000239PyCodeObject *
240PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 static PyObject *emptystring = NULL;
243 static PyObject *nulltuple = NULL;
244 PyObject *filename_ob = NULL;
245 PyObject *funcname_ob = NULL;
246 PyCodeObject *result = NULL;
247 if (emptystring == NULL) {
248 emptystring = PyBytes_FromString("");
249 if (emptystring == NULL)
250 goto failed;
251 }
252 if (nulltuple == NULL) {
253 nulltuple = PyTuple_New(0);
254 if (nulltuple == NULL)
255 goto failed;
256 }
257 funcname_ob = PyUnicode_FromString(funcname);
258 if (funcname_ob == NULL)
259 goto failed;
260 filename_ob = PyUnicode_DecodeFSDefault(filename);
261 if (filename_ob == NULL)
262 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 result = PyCode_New(0, /* argcount */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100265 0, /* posonlyargcount */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 0, /* kwonlyargcount */
267 0, /* nlocals */
268 0, /* stacksize */
269 0, /* flags */
270 emptystring, /* code */
271 nulltuple, /* consts */
272 nulltuple, /* names */
273 nulltuple, /* varnames */
274 nulltuple, /* freevars */
275 nulltuple, /* cellvars */
276 filename_ob, /* filename */
277 funcname_ob, /* name */
278 firstlineno, /* firstlineno */
279 emptystring /* lnotab */
280 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000281
282failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_XDECREF(funcname_ob);
284 Py_XDECREF(filename_ob);
285 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000286}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287
288#define OFF(x) offsetof(PyCodeObject, x)
289
290static PyMemberDef code_memberlist[] = {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100291 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
292 {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
293 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
294 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
295 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
296 {"co_flags", T_INT, OFF(co_flags), READONLY},
297 {"co_code", T_OBJECT, OFF(co_code), READONLY},
298 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
299 {"co_names", T_OBJECT, OFF(co_names), READONLY},
300 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
301 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
302 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
303 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
304 {"co_name", T_OBJECT, OFF(co_name), READONLY},
305 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
306 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308};
309
310/* Helper for code_new: return a shallow copy of a tuple that is
311 guaranteed to contain exact strings, by converting string subclasses
312 to exact strings and complaining if a non-string is found. */
313static PyObject*
314validate_and_copy_tuple(PyObject *tup)
315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 PyObject *newtuple;
317 PyObject *item;
318 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 len = PyTuple_GET_SIZE(tup);
321 newtuple = PyTuple_New(len);
322 if (newtuple == NULL)
323 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 for (i = 0; i < len; i++) {
326 item = PyTuple_GET_ITEM(tup, i);
327 if (PyUnicode_CheckExact(item)) {
328 Py_INCREF(item);
329 }
330 else if (!PyUnicode_Check(item)) {
331 PyErr_Format(
332 PyExc_TypeError,
333 "name tuples must contain only "
334 "strings, not '%.500s'",
335 item->ob_type->tp_name);
336 Py_DECREF(newtuple);
337 return NULL;
338 }
339 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100340 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 if (item == NULL) {
342 Py_DECREF(newtuple);
343 return NULL;
344 }
345 }
346 PyTuple_SET_ITEM(newtuple, i, item);
347 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350}
351
352PyDoc_STRVAR(code_doc,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100353"code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
354 flags, codestring, constants, names, varnames, filename, name,\n\
355 firstlineno, lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356\n\
357Create a code object. Not for the faint of heart.");
358
359static PyObject *
360code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 int argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100363 int posonlyargcount;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 int kwonlyargcount;
365 int nlocals;
366 int stacksize;
367 int flags;
368 PyObject *co = NULL;
369 PyObject *code;
370 PyObject *consts;
371 PyObject *names, *ournames = NULL;
372 PyObject *varnames, *ourvarnames = NULL;
373 PyObject *freevars = NULL, *ourfreevars = NULL;
374 PyObject *cellvars = NULL, *ourcellvars = NULL;
375 PyObject *filename;
376 PyObject *name;
377 int firstlineno;
378 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100380 if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
381 &argcount, &posonlyargcount, &kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 &nlocals, &stacksize, &flags,
383 &code,
384 &PyTuple_Type, &consts,
385 &PyTuple_Type, &names,
386 &PyTuple_Type, &varnames,
387 &filename, &name,
388 &firstlineno, &lnotab,
389 &PyTuple_Type, &freevars,
390 &PyTuple_Type, &cellvars))
391 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392
Steve Dowerb82e17e2019-05-23 08:45:22 -0700393 if (PySys_Audit("code.__new__", "OOOiiiii",
394 code, filename, name, argcount, kwonlyargcount,
395 nlocals, stacksize, flags) < 0) {
396 goto cleanup;
397 }
398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (argcount < 0) {
400 PyErr_SetString(
401 PyExc_ValueError,
402 "code: argcount must not be negative");
403 goto cleanup;
404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100406 if (posonlyargcount < 0) {
407 PyErr_SetString(
408 PyExc_ValueError,
409 "code: posonlyargcount must not be negative");
410 goto cleanup;
411 }
412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (kwonlyargcount < 0) {
414 PyErr_SetString(
415 PyExc_ValueError,
416 "code: kwonlyargcount must not be negative");
417 goto cleanup;
418 }
419 if (nlocals < 0) {
420 PyErr_SetString(
421 PyExc_ValueError,
422 "code: nlocals must not be negative");
423 goto cleanup;
424 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 ournames = validate_and_copy_tuple(names);
427 if (ournames == NULL)
428 goto cleanup;
429 ourvarnames = validate_and_copy_tuple(varnames);
430 if (ourvarnames == NULL)
431 goto cleanup;
432 if (freevars)
433 ourfreevars = validate_and_copy_tuple(freevars);
434 else
435 ourfreevars = PyTuple_New(0);
436 if (ourfreevars == NULL)
437 goto cleanup;
438 if (cellvars)
439 ourcellvars = validate_and_copy_tuple(cellvars);
440 else
441 ourcellvars = PyTuple_New(0);
442 if (ourcellvars == NULL)
443 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100445 co = (PyObject *)PyCode_New(argcount, posonlyargcount, kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 nlocals, stacksize, flags,
447 code, consts, ournames, ourvarnames,
448 ourfreevars, ourcellvars, filename,
449 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 Py_XDECREF(ournames);
452 Py_XDECREF(ourvarnames);
453 Py_XDECREF(ourfreevars);
454 Py_XDECREF(ourcellvars);
455 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456}
457
458static void
459code_dealloc(PyCodeObject *co)
460{
Brett Cannon5c4de282016-09-07 11:16:41 -0700461 if (co->co_extra != NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200462 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannond0600ed2016-09-07 14:30:39 -0700463 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700464
Brett Cannond0600ed2016-09-07 14:30:39 -0700465 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700466 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700467
468 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700469 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700470 }
471 }
472
Victor Stinner23e79442017-06-28 02:12:00 +0200473 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700474 }
475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 Py_XDECREF(co->co_code);
477 Py_XDECREF(co->co_consts);
478 Py_XDECREF(co->co_names);
479 Py_XDECREF(co->co_varnames);
480 Py_XDECREF(co->co_freevars);
481 Py_XDECREF(co->co_cellvars);
482 Py_XDECREF(co->co_filename);
483 Py_XDECREF(co->co_name);
484 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500485 if (co->co_cell2arg != NULL)
486 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 if (co->co_zombieframe != NULL)
488 PyObject_GC_Del(co->co_zombieframe);
489 if (co->co_weakreflist != NULL)
490 PyObject_ClearWeakRefs((PyObject*)co);
491 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492}
493
494static PyObject *
Victor Stinnera9f05d62019-05-24 23:57:23 +0200495code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200496{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900497 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
498 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200499
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300500 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200501 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300502 }
503 if (co_extra != NULL) {
504 res += sizeof(_PyCodeObjectExtra) +
505 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
506 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200507 return PyLong_FromSsize_t(res);
508}
509
Victor Stinnera9f05d62019-05-24 23:57:23 +0200510/*[clinic input]
511code.replace
512
513 *
514 co_argcount: int(c_default="self->co_argcount") = -1
515 co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
516 co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
517 co_nlocals: int(c_default="self->co_nlocals") = -1
518 co_stacksize: int(c_default="self->co_stacksize") = -1
519 co_flags: int(c_default="self->co_flags") = -1
520 co_firstlineno: int(c_default="self->co_firstlineno") = -1
521 co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
522 co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
523 co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
524 co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
525 co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
526 co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
527 co_filename: unicode(c_default="self->co_filename") = None
528 co_name: unicode(c_default="self->co_name") = None
529 co_lnotab: PyBytesObject(c_default="(PyBytesObject *)self->co_lnotab") = None
530
531Return a new code object with new specified fields.
532[clinic start generated code]*/
533
534static PyObject *
535code_replace_impl(PyCodeObject *self, int co_argcount,
536 int co_posonlyargcount, int co_kwonlyargcount,
537 int co_nlocals, int co_stacksize, int co_flags,
538 int co_firstlineno, PyBytesObject *co_code,
539 PyObject *co_consts, PyObject *co_names,
540 PyObject *co_varnames, PyObject *co_freevars,
541 PyObject *co_cellvars, PyObject *co_filename,
542 PyObject *co_name, PyBytesObject *co_lnotab)
543/*[clinic end generated code: output=25c8e303913bcace input=77189e46579ec426]*/
544{
545#define CHECK_INT_ARG(ARG) \
546 if (ARG < 0) { \
547 PyErr_SetString(PyExc_ValueError, \
548 #ARG " must be a positive integer"); \
549 return NULL; \
550 }
551
552 CHECK_INT_ARG(co_argcount);
553 CHECK_INT_ARG(co_posonlyargcount);
554 CHECK_INT_ARG(co_kwonlyargcount);
555 CHECK_INT_ARG(co_nlocals);
556 CHECK_INT_ARG(co_stacksize);
557 CHECK_INT_ARG(co_flags);
558 CHECK_INT_ARG(co_firstlineno);
559
560#undef CHECK_INT_ARG
561
562 return (PyObject *)PyCode_New(
563 co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
564 co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
565 co_varnames, co_freevars, co_cellvars, co_filename, co_name,
566 co_firstlineno, (PyObject*)co_lnotab);
567}
568
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200569static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570code_repr(PyCodeObject *co)
571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 int lineno;
573 if (co->co_firstlineno != 0)
574 lineno = co->co_firstlineno;
575 else
576 lineno = -1;
577 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
578 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000579 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 co->co_name, co, co->co_filename, lineno);
581 } else {
582 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000583 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 co->co_name, co, lineno);
585 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586}
587
Victor Stinnerefb24132016-01-22 12:33:12 +0100588PyObject*
589_PyCode_ConstantKey(PyObject *op)
590{
591 PyObject *key;
592
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300593 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100594 if (op == Py_None || op == Py_Ellipsis
595 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100596 || PyUnicode_CheckExact(op)
597 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300598 || PyCode_Check(op))
599 {
600 /* Objects of these types are always different from object of other
601 * type and from tuples. */
602 Py_INCREF(op);
603 key = op;
604 }
605 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
606 /* Make booleans different from integers 0 and 1.
607 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100608 key = PyTuple_Pack(2, Py_TYPE(op), op);
609 }
610 else if (PyFloat_CheckExact(op)) {
611 double d = PyFloat_AS_DOUBLE(op);
612 /* all we need is to make the tuple different in either the 0.0
613 * or -0.0 case from all others, just to avoid the "coercion".
614 */
615 if (d == 0.0 && copysign(1.0, d) < 0.0)
616 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
617 else
618 key = PyTuple_Pack(2, Py_TYPE(op), op);
619 }
620 else if (PyComplex_CheckExact(op)) {
621 Py_complex z;
622 int real_negzero, imag_negzero;
623 /* For the complex case we must make complex(x, 0.)
624 different from complex(x, -0.) and complex(0., y)
625 different from complex(-0., y), for any x and y.
626 All four complex zeros must be distinguished.*/
627 z = PyComplex_AsCComplex(op);
628 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
629 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
630 /* use True, False and None singleton as tags for the real and imag
631 * sign, to make tuples different */
632 if (real_negzero && imag_negzero) {
633 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
634 }
635 else if (imag_negzero) {
636 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
637 }
638 else if (real_negzero) {
639 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
640 }
641 else {
642 key = PyTuple_Pack(2, Py_TYPE(op), op);
643 }
644 }
645 else if (PyTuple_CheckExact(op)) {
646 Py_ssize_t i, len;
647 PyObject *tuple;
648
649 len = PyTuple_GET_SIZE(op);
650 tuple = PyTuple_New(len);
651 if (tuple == NULL)
652 return NULL;
653
654 for (i=0; i < len; i++) {
655 PyObject *item, *item_key;
656
657 item = PyTuple_GET_ITEM(op, i);
658 item_key = _PyCode_ConstantKey(item);
659 if (item_key == NULL) {
660 Py_DECREF(tuple);
661 return NULL;
662 }
663
664 PyTuple_SET_ITEM(tuple, i, item_key);
665 }
666
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200667 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100668 Py_DECREF(tuple);
669 }
670 else if (PyFrozenSet_CheckExact(op)) {
671 Py_ssize_t pos = 0;
672 PyObject *item;
673 Py_hash_t hash;
674 Py_ssize_t i, len;
675 PyObject *tuple, *set;
676
677 len = PySet_GET_SIZE(op);
678 tuple = PyTuple_New(len);
679 if (tuple == NULL)
680 return NULL;
681
682 i = 0;
683 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
684 PyObject *item_key;
685
686 item_key = _PyCode_ConstantKey(item);
687 if (item_key == NULL) {
688 Py_DECREF(tuple);
689 return NULL;
690 }
691
692 assert(i < len);
693 PyTuple_SET_ITEM(tuple, i, item_key);
694 i++;
695 }
696 set = PyFrozenSet_New(tuple);
697 Py_DECREF(tuple);
698 if (set == NULL)
699 return NULL;
700
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200701 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100702 Py_DECREF(set);
703 return key;
704 }
705 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000706 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100707 * to ensure that they are seen as unequal. */
708 PyObject *obj_id = PyLong_FromVoidPtr(op);
709 if (obj_id == NULL)
710 return NULL;
711
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200712 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100713 Py_DECREF(obj_id);
714 }
715 return key;
716}
717
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000718static PyObject *
719code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 PyCodeObject *co, *cp;
722 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100723 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 if ((op != Py_EQ && op != Py_NE) ||
727 !PyCode_Check(self) ||
728 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500729 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 co = (PyCodeObject *)self;
733 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100736 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 eq = co->co_argcount == cp->co_argcount;
738 if (!eq) goto unequal;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100739 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
740 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
742 if (!eq) goto unequal;
743 eq = co->co_nlocals == cp->co_nlocals;
744 if (!eq) goto unequal;
745 eq = co->co_flags == cp->co_flags;
746 if (!eq) goto unequal;
747 eq = co->co_firstlineno == cp->co_firstlineno;
748 if (!eq) goto unequal;
749 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
750 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100751
752 /* compare constants */
753 consts1 = _PyCode_ConstantKey(co->co_consts);
754 if (!consts1)
755 return NULL;
756 consts2 = _PyCode_ConstantKey(cp->co_consts);
757 if (!consts2) {
758 Py_DECREF(consts1);
759 return NULL;
760 }
761 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
762 Py_DECREF(consts1);
763 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
767 if (eq <= 0) goto unequal;
768 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
769 if (eq <= 0) goto unequal;
770 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
771 if (eq <= 0) goto unequal;
772 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
773 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 if (op == Py_EQ)
776 res = Py_True;
777 else
778 res = Py_False;
779 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000780
781 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 if (eq < 0)
783 return NULL;
784 if (op == Py_NE)
785 res = Py_True;
786 else
787 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000788
789 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 Py_INCREF(res);
791 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792}
793
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000794static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795code_hash(PyCodeObject *co)
796{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000797 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 h0 = PyObject_Hash(co->co_name);
799 if (h0 == -1) return -1;
800 h1 = PyObject_Hash(co->co_code);
801 if (h1 == -1) return -1;
802 h2 = PyObject_Hash(co->co_consts);
803 if (h2 == -1) return -1;
804 h3 = PyObject_Hash(co->co_names);
805 if (h3 == -1) return -1;
806 h4 = PyObject_Hash(co->co_varnames);
807 if (h4 == -1) return -1;
808 h5 = PyObject_Hash(co->co_freevars);
809 if (h5 == -1) return -1;
810 h6 = PyObject_Hash(co->co_cellvars);
811 if (h6 == -1) return -1;
812 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100813 co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 co->co_nlocals ^ co->co_flags;
815 if (h == -1) h = -2;
816 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817}
818
819/* XXX code objects need to participate in GC? */
820
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200821static struct PyMethodDef code_methods[] = {
822 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
Victor Stinnera9f05d62019-05-24 23:57:23 +0200823 CODE_REPLACE_METHODDEF
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200824 {NULL, NULL} /* sentinel */
825};
826
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 PyVarObject_HEAD_INIT(&PyType_Type, 0)
829 "code",
830 sizeof(PyCodeObject),
831 0,
832 (destructor)code_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200833 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 0, /* tp_getattr */
835 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200836 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 (reprfunc)code_repr, /* tp_repr */
838 0, /* tp_as_number */
839 0, /* tp_as_sequence */
840 0, /* tp_as_mapping */
841 (hashfunc)code_hash, /* tp_hash */
842 0, /* tp_call */
843 0, /* tp_str */
844 PyObject_GenericGetAttr, /* tp_getattro */
845 0, /* tp_setattro */
846 0, /* tp_as_buffer */
847 Py_TPFLAGS_DEFAULT, /* tp_flags */
848 code_doc, /* tp_doc */
849 0, /* tp_traverse */
850 0, /* tp_clear */
851 code_richcompare, /* tp_richcompare */
852 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
853 0, /* tp_iter */
854 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200855 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 code_memberlist, /* tp_members */
857 0, /* tp_getset */
858 0, /* tp_base */
859 0, /* tp_dict */
860 0, /* tp_descr_get */
861 0, /* tp_descr_set */
862 0, /* tp_dictoffset */
863 0, /* tp_init */
864 0, /* tp_alloc */
865 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866};
867
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000868/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
869 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870*/
871
872int
873PyCode_Addr2Line(PyCodeObject *co, int addrq)
874{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000875 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
877 int line = co->co_firstlineno;
878 int addr = 0;
879 while (--size >= 0) {
880 addr += *p++;
881 if (addr > addrq)
882 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100883 line += (signed char)*p;
884 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 }
886 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000888
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000889/* Update *bounds to describe the first and one-past-the-last instructions in
890 the same line as lasti. Return the number of that line. */
891int
892_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000893{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000894 Py_ssize_t size;
895 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
899 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 addr = 0;
902 line = co->co_firstlineno;
903 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 /* possible optimization: if f->f_lasti == instr_ub
906 (likely to be a common case) then we already know
907 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700908 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 /* See lnotab_notes.txt for the description of
911 co_lnotab. A point to remember: increments to p
912 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 bounds->ap_lower = 0;
915 while (size > 0) {
916 if (addr + *p > lasti)
917 break;
918 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100919 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100921 line += (signed char)*p;
922 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 --size;
924 }
925
926 if (size > 0) {
927 while (--size >= 0) {
928 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100929 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100931 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 bounds->ap_upper = addr;
934 }
935 else {
936 bounds->ap_upper = INT_MAX;
937 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000940}
Brett Cannon5c4de282016-09-07 11:16:41 -0700941
942
943int
944_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
945{
Brett Cannon5c4de282016-09-07 11:16:41 -0700946 if (!PyCode_Check(code)) {
947 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700948 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700949 }
950
Brett Cannond0600ed2016-09-07 14:30:39 -0700951 PyCodeObject *o = (PyCodeObject*) code;
952 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700953
Brett Cannond0600ed2016-09-07 14:30:39 -0700954 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700955 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700956 return 0;
957 }
958
Brett Cannond0600ed2016-09-07 14:30:39 -0700959 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -0700960 return 0;
961}
962
963
964int
965_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
966{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200967 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -0700968
969 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700970 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -0700971 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -0700972 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700973 }
974
Brett Cannond0600ed2016-09-07 14:30:39 -0700975 PyCodeObject *o = (PyCodeObject*) code;
976 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700977
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300978 if (co_extra == NULL || co_extra->ce_size <= index) {
979 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
980 co_extra = PyMem_Realloc(
981 co_extra,
982 sizeof(_PyCodeObjectExtra) +
983 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +0000984 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -0700985 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -0700986 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300987 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700988 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700989 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700990 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300991 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700992 }
993
994 if (co_extra->ce_extras[index] != NULL) {
995 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700996 if (free != NULL) {
997 free(co_extra->ce_extras[index]);
998 }
Brett Cannon5c4de282016-09-07 11:16:41 -0700999 }
1000
Brett Cannond0600ed2016-09-07 14:30:39 -07001001 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001002 return 0;
1003}