blob: 39bf6fc6f228e7fddc1b88da9a5375b09485a763 [file] [log] [blame]
Benjamin Peterson1bf494b2016-09-07 11:28:35 -07001#include <stdbool.h>
2
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003#include "Python.h"
4#include "code.h"
Inada Naoki91234a12019-06-03 21:30:58 +09005#include "opcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "structmember.h"
Inada Naoki91234a12019-06-03 21:30:58 +09007#include "pycore_code.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01008#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +01009#include "pycore_tupleobject.h"
Victor Stinnera9f05d62019-05-24 23:57:23 +020010#include "clinic/codeobject.c.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
Brett Cannond0600ed2016-09-07 14:30:39 -070012/* Holder for co_extra information */
13typedef struct {
14 Py_ssize_t ce_size;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +030015 void *ce_extras[1];
Brett Cannond0600ed2016-09-07 14:30:39 -070016} _PyCodeObjectExtra;
17
Victor Stinnera9f05d62019-05-24 23:57:23 +020018/*[clinic input]
19class code "PyCodeObject *" "&PyCode_Type"
20[clinic start generated code]*/
21/*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
22
Benjamin Peterson8e0ad462017-09-07 23:35:53 -070023/* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020025all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026{
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030027 const unsigned char *s, *e;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020028
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030029 if (!PyUnicode_IS_ASCII(o))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020030 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000031
Serhiy Storchaka09f3d082016-10-04 18:17:22 +030032 s = PyUnicode_1BYTE_DATA(o);
33 e = s + PyUnicode_GET_LENGTH(o);
Benjamin Peterson9020ac72017-09-07 18:06:23 -070034 for (; s != e; s++) {
Benjamin Peterson2b7953d2017-09-08 10:35:49 -070035 if (!Py_ISALNUM(*s) && *s != '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 return 0;
37 }
38 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039}
40
41static void
42intern_strings(PyObject *tuple)
43{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
47 PyObject *v = PyTuple_GET_ITEM(tuple, i);
48 if (v == NULL || !PyUnicode_CheckExact(v)) {
49 Py_FatalError("non-string found in code slot");
50 }
Victor Stinnerd17a6932018-11-09 16:56:48 +010051 PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053}
54
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030055/* Intern selected string constants */
56static int
57intern_string_constants(PyObject *tuple)
58{
59 int modified = 0;
60 Py_ssize_t i;
61
62 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
63 PyObject *v = PyTuple_GET_ITEM(tuple, i);
64 if (PyUnicode_CheckExact(v)) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030065 if (PyUnicode_READY(v) == -1) {
66 PyErr_Clear();
67 continue;
68 }
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030069 if (all_name_chars(v)) {
70 PyObject *w = v;
71 PyUnicode_InternInPlace(&v);
72 if (w != v) {
73 PyTuple_SET_ITEM(tuple, i, v);
74 modified = 1;
75 }
76 }
77 }
78 else if (PyTuple_CheckExact(v)) {
79 intern_string_constants(v);
80 }
81 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050082 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030083 PyObject *tmp = PySequence_Tuple(v);
84 if (tmp == NULL) {
85 PyErr_Clear();
86 continue;
87 }
88 if (intern_string_constants(tmp)) {
89 v = PyFrozenSet_New(tmp);
90 if (v == NULL) {
91 PyErr_Clear();
92 }
93 else {
94 PyTuple_SET_ITEM(tuple, i, v);
Yury Selivanovd2fd3592016-11-09 09:42:14 -050095 Py_DECREF(w);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030096 modified = 1;
97 }
98 }
99 Py_DECREF(tmp);
100 }
101 }
102 return modified;
103}
104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105PyCodeObject *
Miss Islington (bot)cb083f72019-07-01 04:29:14 -0700106PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
107 int nlocals, int stacksize, int flags,
108 PyObject *code, PyObject *consts, PyObject *names,
109 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
110 PyObject *filename, PyObject *name, int firstlineno,
111 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200114 Py_ssize_t *cell2arg = NULL;
Serhiy Storchakabd473842018-07-16 09:10:19 +0300115 Py_ssize_t i, n_cellvars, n_varnames, total_args;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 /* Check argument types */
Pablo Galindocd74e662019-06-01 18:08:04 +0100118 if (argcount < posonlyargcount || posonlyargcount < 0 ||
119 kwonlyargcount < 0 || nlocals < 0 ||
120 stacksize < 0 || flags < 0 ||
Victor Stinnera9f05d62019-05-24 23:57:23 +0200121 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 consts == NULL || !PyTuple_Check(consts) ||
123 names == NULL || !PyTuple_Check(names) ||
124 varnames == NULL || !PyTuple_Check(varnames) ||
125 freevars == NULL || !PyTuple_Check(freevars) ||
126 cellvars == NULL || !PyTuple_Check(cellvars) ||
127 name == NULL || !PyUnicode_Check(name) ||
128 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200129 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 PyErr_BadInternalCall();
131 return NULL;
132 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200133
Victor Stinnera9f05d62019-05-24 23:57:23 +0200134 /* Ensure that strings are ready Unicode string */
135 if (PyUnicode_READY(name) < 0) {
Victor Stinner7c74de42013-10-10 15:55:14 +0200136 return NULL;
Victor Stinnera9f05d62019-05-24 23:57:23 +0200137 }
138 if (PyUnicode_READY(filename) < 0) {
139 return NULL;
140 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 intern_strings(names);
143 intern_strings(varnames);
144 intern_strings(freevars);
145 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300146 intern_string_constants(consts);
Nick Coghlan078f1812017-12-03 11:12:20 +1000147
148 /* Check for any inner or outer closure references */
149 n_cellvars = PyTuple_GET_SIZE(cellvars);
150 if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
151 flags |= CO_NOFREE;
152 } else {
153 flags &= ~CO_NOFREE;
154 }
155
Serhiy Storchakabd473842018-07-16 09:10:19 +0300156 n_varnames = PyTuple_GET_SIZE(varnames);
Pablo Galindocd74e662019-06-01 18:08:04 +0100157 if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
Serhiy Storchakabd473842018-07-16 09:10:19 +0300158 /* Never overflows. */
Pablo Galindocd74e662019-06-01 18:08:04 +0100159 total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100160 ((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;
Inada Naoki91234a12019-06-03 21:30:58 +0900237
238 co->co_opcache_map = NULL;
239 co->co_opcache = NULL;
240 co->co_opcache_flag = 0;
241 co->co_opcache_size = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243}
244
Miss Islington (bot)cb083f72019-07-01 04:29:14 -0700245PyCodeObject *
246PyCode_New(int argcount, int kwonlyargcount,
247 int nlocals, int stacksize, int flags,
248 PyObject *code, PyObject *consts, PyObject *names,
249 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
250 PyObject *filename, PyObject *name, int firstlineno,
251 PyObject *lnotab)
252{
253 return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
254 stacksize, flags, code, consts, names,
255 varnames, freevars, cellvars, filename,
256 name, firstlineno, lnotab);
257}
258
Inada Naoki91234a12019-06-03 21:30:58 +0900259int
260_PyCode_InitOpcache(PyCodeObject *co)
261{
262 Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);
263 co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);
264 if (co->co_opcache_map == NULL) {
265 return -1;
266 }
267
268 _Py_CODEUNIT *opcodes = (_Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);
269 Py_ssize_t opts = 0;
270
271 for (Py_ssize_t i = 0; i < co_size;) {
272 unsigned char opcode = _Py_OPCODE(opcodes[i]);
273 i++; // 'i' is now aligned to (next_instr - first_instr)
274
275 // TODO: LOAD_METHOD, LOAD_ATTR
276 if (opcode == LOAD_GLOBAL) {
Victor Stinnerea9f1682019-06-04 17:08:24 +0200277 opts++;
278 co->co_opcache_map[i] = (unsigned char)opts;
Inada Naoki91234a12019-06-03 21:30:58 +0900279 if (opts > 254) {
280 break;
281 }
282 }
283 }
284
285 if (opts) {
286 co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));
287 if (co->co_opcache == NULL) {
288 PyMem_FREE(co->co_opcache_map);
289 return -1;
290 }
291 }
292 else {
293 PyMem_FREE(co->co_opcache_map);
294 co->co_opcache_map = NULL;
295 co->co_opcache = NULL;
296 }
297
Miss Islington (bot)996e5262019-06-12 03:14:43 -0700298 co->co_opcache_size = (unsigned char)opts;
Inada Naoki91234a12019-06-03 21:30:58 +0900299 return 0;
300}
301
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000302PyCodeObject *
303PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 static PyObject *emptystring = NULL;
306 static PyObject *nulltuple = NULL;
307 PyObject *filename_ob = NULL;
308 PyObject *funcname_ob = NULL;
309 PyCodeObject *result = NULL;
310 if (emptystring == NULL) {
311 emptystring = PyBytes_FromString("");
312 if (emptystring == NULL)
313 goto failed;
314 }
315 if (nulltuple == NULL) {
316 nulltuple = PyTuple_New(0);
317 if (nulltuple == NULL)
318 goto failed;
319 }
320 funcname_ob = PyUnicode_FromString(funcname);
321 if (funcname_ob == NULL)
322 goto failed;
323 filename_ob = PyUnicode_DecodeFSDefault(filename);
324 if (filename_ob == NULL)
325 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000326
Miss Islington (bot)cb083f72019-07-01 04:29:14 -0700327 result = PyCode_NewWithPosOnlyArgs(
328 0, /* argcount */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100329 0, /* posonlyargcount */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 0, /* kwonlyargcount */
331 0, /* nlocals */
332 0, /* stacksize */
333 0, /* flags */
334 emptystring, /* code */
335 nulltuple, /* consts */
336 nulltuple, /* names */
337 nulltuple, /* varnames */
338 nulltuple, /* freevars */
339 nulltuple, /* cellvars */
340 filename_ob, /* filename */
341 funcname_ob, /* name */
342 firstlineno, /* firstlineno */
343 emptystring /* lnotab */
344 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000345
346failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 Py_XDECREF(funcname_ob);
348 Py_XDECREF(filename_ob);
349 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000350}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351
352#define OFF(x) offsetof(PyCodeObject, x)
353
354static PyMemberDef code_memberlist[] = {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100355 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
356 {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
357 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
358 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
359 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
360 {"co_flags", T_INT, OFF(co_flags), READONLY},
361 {"co_code", T_OBJECT, OFF(co_code), READONLY},
362 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
363 {"co_names", T_OBJECT, OFF(co_names), READONLY},
364 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
365 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
366 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
367 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
368 {"co_name", T_OBJECT, OFF(co_name), READONLY},
369 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
370 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372};
373
374/* Helper for code_new: return a shallow copy of a tuple that is
375 guaranteed to contain exact strings, by converting string subclasses
376 to exact strings and complaining if a non-string is found. */
377static PyObject*
378validate_and_copy_tuple(PyObject *tup)
379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 PyObject *newtuple;
381 PyObject *item;
382 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 len = PyTuple_GET_SIZE(tup);
385 newtuple = PyTuple_New(len);
386 if (newtuple == NULL)
387 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 for (i = 0; i < len; i++) {
390 item = PyTuple_GET_ITEM(tup, i);
391 if (PyUnicode_CheckExact(item)) {
392 Py_INCREF(item);
393 }
394 else if (!PyUnicode_Check(item)) {
395 PyErr_Format(
396 PyExc_TypeError,
397 "name tuples must contain only "
398 "strings, not '%.500s'",
399 item->ob_type->tp_name);
400 Py_DECREF(newtuple);
401 return NULL;
402 }
403 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100404 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (item == NULL) {
406 Py_DECREF(newtuple);
407 return NULL;
408 }
409 }
410 PyTuple_SET_ITEM(newtuple, i, item);
411 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414}
415
416PyDoc_STRVAR(code_doc,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100417"code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
418 flags, codestring, constants, names, varnames, filename, name,\n\
419 firstlineno, lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420\n\
421Create a code object. Not for the faint of heart.");
422
423static PyObject *
424code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 int argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100427 int posonlyargcount;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 int kwonlyargcount;
429 int nlocals;
430 int stacksize;
431 int flags;
432 PyObject *co = NULL;
433 PyObject *code;
434 PyObject *consts;
435 PyObject *names, *ournames = NULL;
436 PyObject *varnames, *ourvarnames = NULL;
437 PyObject *freevars = NULL, *ourfreevars = NULL;
438 PyObject *cellvars = NULL, *ourcellvars = NULL;
439 PyObject *filename;
440 PyObject *name;
441 int firstlineno;
442 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000443
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100444 if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
445 &argcount, &posonlyargcount, &kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 &nlocals, &stacksize, &flags,
447 &code,
448 &PyTuple_Type, &consts,
449 &PyTuple_Type, &names,
450 &PyTuple_Type, &varnames,
451 &filename, &name,
452 &firstlineno, &lnotab,
453 &PyTuple_Type, &freevars,
454 &PyTuple_Type, &cellvars))
455 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456
Pablo Galindo3b57f502019-06-01 21:18:48 +0100457 if (PySys_Audit("code.__new__", "OOOiiiiii",
458 code, filename, name, argcount, posonlyargcount,
459 kwonlyargcount, nlocals, stacksize, flags) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -0700460 goto cleanup;
461 }
462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (argcount < 0) {
464 PyErr_SetString(
465 PyExc_ValueError,
466 "code: argcount must not be negative");
467 goto cleanup;
468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100470 if (posonlyargcount < 0) {
471 PyErr_SetString(
472 PyExc_ValueError,
473 "code: posonlyargcount must not be negative");
474 goto cleanup;
475 }
476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (kwonlyargcount < 0) {
478 PyErr_SetString(
479 PyExc_ValueError,
480 "code: kwonlyargcount must not be negative");
481 goto cleanup;
482 }
483 if (nlocals < 0) {
484 PyErr_SetString(
485 PyExc_ValueError,
486 "code: nlocals must not be negative");
487 goto cleanup;
488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 ournames = validate_and_copy_tuple(names);
491 if (ournames == NULL)
492 goto cleanup;
493 ourvarnames = validate_and_copy_tuple(varnames);
494 if (ourvarnames == NULL)
495 goto cleanup;
496 if (freevars)
497 ourfreevars = validate_and_copy_tuple(freevars);
498 else
499 ourfreevars = PyTuple_New(0);
500 if (ourfreevars == NULL)
501 goto cleanup;
502 if (cellvars)
503 ourcellvars = validate_and_copy_tuple(cellvars);
504 else
505 ourcellvars = PyTuple_New(0);
506 if (ourcellvars == NULL)
507 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508
Miss Islington (bot)cb083f72019-07-01 04:29:14 -0700509 co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
510 kwonlyargcount,
511 nlocals, stacksize, flags,
512 code, consts, ournames,
513 ourvarnames, ourfreevars,
514 ourcellvars, filename,
515 name, firstlineno, lnotab);
516 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 Py_XDECREF(ournames);
518 Py_XDECREF(ourvarnames);
519 Py_XDECREF(ourfreevars);
520 Py_XDECREF(ourcellvars);
521 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522}
523
524static void
525code_dealloc(PyCodeObject *co)
526{
Inada Naoki91234a12019-06-03 21:30:58 +0900527 if (co->co_opcache != NULL) {
528 PyMem_FREE(co->co_opcache);
529 }
530 if (co->co_opcache_map != NULL) {
531 PyMem_FREE(co->co_opcache_map);
532 }
533 co->co_opcache_flag = 0;
534 co->co_opcache_size = 0;
535
Brett Cannon5c4de282016-09-07 11:16:41 -0700536 if (co->co_extra != NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200537 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannond0600ed2016-09-07 14:30:39 -0700538 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700539
Brett Cannond0600ed2016-09-07 14:30:39 -0700540 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700541 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700542
543 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700544 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700545 }
546 }
547
Victor Stinner23e79442017-06-28 02:12:00 +0200548 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700549 }
550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 Py_XDECREF(co->co_code);
552 Py_XDECREF(co->co_consts);
553 Py_XDECREF(co->co_names);
554 Py_XDECREF(co->co_varnames);
555 Py_XDECREF(co->co_freevars);
556 Py_XDECREF(co->co_cellvars);
557 Py_XDECREF(co->co_filename);
558 Py_XDECREF(co->co_name);
559 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500560 if (co->co_cell2arg != NULL)
561 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 if (co->co_zombieframe != NULL)
563 PyObject_GC_Del(co->co_zombieframe);
564 if (co->co_weakreflist != NULL)
565 PyObject_ClearWeakRefs((PyObject*)co);
566 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567}
568
569static PyObject *
Victor Stinnera9f05d62019-05-24 23:57:23 +0200570code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200571{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900572 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
573 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200574
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300575 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200576 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300577 }
578 if (co_extra != NULL) {
579 res += sizeof(_PyCodeObjectExtra) +
580 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
581 }
Inada Naoki91234a12019-06-03 21:30:58 +0900582 if (co->co_opcache != NULL) {
583 assert(co->co_opcache_map != NULL);
584 // co_opcache_map
585 res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);
586 // co_opcache
587 res += co->co_opcache_size * sizeof(_PyOpcache);
588 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200589 return PyLong_FromSsize_t(res);
590}
591
Victor Stinnera9f05d62019-05-24 23:57:23 +0200592/*[clinic input]
593code.replace
594
595 *
596 co_argcount: int(c_default="self->co_argcount") = -1
597 co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
598 co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
599 co_nlocals: int(c_default="self->co_nlocals") = -1
600 co_stacksize: int(c_default="self->co_stacksize") = -1
601 co_flags: int(c_default="self->co_flags") = -1
602 co_firstlineno: int(c_default="self->co_firstlineno") = -1
603 co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
604 co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
605 co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
606 co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
607 co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
608 co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
609 co_filename: unicode(c_default="self->co_filename") = None
610 co_name: unicode(c_default="self->co_name") = None
611 co_lnotab: PyBytesObject(c_default="(PyBytesObject *)self->co_lnotab") = None
612
613Return a new code object with new specified fields.
614[clinic start generated code]*/
615
616static PyObject *
617code_replace_impl(PyCodeObject *self, int co_argcount,
618 int co_posonlyargcount, int co_kwonlyargcount,
619 int co_nlocals, int co_stacksize, int co_flags,
620 int co_firstlineno, PyBytesObject *co_code,
621 PyObject *co_consts, PyObject *co_names,
622 PyObject *co_varnames, PyObject *co_freevars,
623 PyObject *co_cellvars, PyObject *co_filename,
624 PyObject *co_name, PyBytesObject *co_lnotab)
625/*[clinic end generated code: output=25c8e303913bcace input=77189e46579ec426]*/
626{
627#define CHECK_INT_ARG(ARG) \
628 if (ARG < 0) { \
629 PyErr_SetString(PyExc_ValueError, \
630 #ARG " must be a positive integer"); \
631 return NULL; \
632 }
633
634 CHECK_INT_ARG(co_argcount);
635 CHECK_INT_ARG(co_posonlyargcount);
636 CHECK_INT_ARG(co_kwonlyargcount);
637 CHECK_INT_ARG(co_nlocals);
638 CHECK_INT_ARG(co_stacksize);
639 CHECK_INT_ARG(co_flags);
640 CHECK_INT_ARG(co_firstlineno);
641
642#undef CHECK_INT_ARG
643
Miss Islington (bot)cb083f72019-07-01 04:29:14 -0700644 return (PyObject *)PyCode_NewWithPosOnlyArgs(
Victor Stinnera9f05d62019-05-24 23:57:23 +0200645 co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
646 co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
647 co_varnames, co_freevars, co_cellvars, co_filename, co_name,
648 co_firstlineno, (PyObject*)co_lnotab);
649}
650
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200651static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652code_repr(PyCodeObject *co)
653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 int lineno;
655 if (co->co_firstlineno != 0)
656 lineno = co->co_firstlineno;
657 else
658 lineno = -1;
659 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
660 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000661 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 co->co_name, co, co->co_filename, lineno);
663 } else {
664 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000665 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 co->co_name, co, lineno);
667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668}
669
Victor Stinnerefb24132016-01-22 12:33:12 +0100670PyObject*
671_PyCode_ConstantKey(PyObject *op)
672{
673 PyObject *key;
674
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300675 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100676 if (op == Py_None || op == Py_Ellipsis
677 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100678 || PyUnicode_CheckExact(op)
679 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300680 || PyCode_Check(op))
681 {
682 /* Objects of these types are always different from object of other
683 * type and from tuples. */
684 Py_INCREF(op);
685 key = op;
686 }
687 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
688 /* Make booleans different from integers 0 and 1.
689 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100690 key = PyTuple_Pack(2, Py_TYPE(op), op);
691 }
692 else if (PyFloat_CheckExact(op)) {
693 double d = PyFloat_AS_DOUBLE(op);
694 /* all we need is to make the tuple different in either the 0.0
695 * or -0.0 case from all others, just to avoid the "coercion".
696 */
697 if (d == 0.0 && copysign(1.0, d) < 0.0)
698 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
699 else
700 key = PyTuple_Pack(2, Py_TYPE(op), op);
701 }
702 else if (PyComplex_CheckExact(op)) {
703 Py_complex z;
704 int real_negzero, imag_negzero;
705 /* For the complex case we must make complex(x, 0.)
706 different from complex(x, -0.) and complex(0., y)
707 different from complex(-0., y), for any x and y.
708 All four complex zeros must be distinguished.*/
709 z = PyComplex_AsCComplex(op);
710 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
711 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
712 /* use True, False and None singleton as tags for the real and imag
713 * sign, to make tuples different */
714 if (real_negzero && imag_negzero) {
715 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
716 }
717 else if (imag_negzero) {
718 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
719 }
720 else if (real_negzero) {
721 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
722 }
723 else {
724 key = PyTuple_Pack(2, Py_TYPE(op), op);
725 }
726 }
727 else if (PyTuple_CheckExact(op)) {
728 Py_ssize_t i, len;
729 PyObject *tuple;
730
731 len = PyTuple_GET_SIZE(op);
732 tuple = PyTuple_New(len);
733 if (tuple == NULL)
734 return NULL;
735
736 for (i=0; i < len; i++) {
737 PyObject *item, *item_key;
738
739 item = PyTuple_GET_ITEM(op, i);
740 item_key = _PyCode_ConstantKey(item);
741 if (item_key == NULL) {
742 Py_DECREF(tuple);
743 return NULL;
744 }
745
746 PyTuple_SET_ITEM(tuple, i, item_key);
747 }
748
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200749 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100750 Py_DECREF(tuple);
751 }
752 else if (PyFrozenSet_CheckExact(op)) {
753 Py_ssize_t pos = 0;
754 PyObject *item;
755 Py_hash_t hash;
756 Py_ssize_t i, len;
757 PyObject *tuple, *set;
758
759 len = PySet_GET_SIZE(op);
760 tuple = PyTuple_New(len);
761 if (tuple == NULL)
762 return NULL;
763
764 i = 0;
765 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
766 PyObject *item_key;
767
768 item_key = _PyCode_ConstantKey(item);
769 if (item_key == NULL) {
770 Py_DECREF(tuple);
771 return NULL;
772 }
773
774 assert(i < len);
775 PyTuple_SET_ITEM(tuple, i, item_key);
776 i++;
777 }
778 set = PyFrozenSet_New(tuple);
779 Py_DECREF(tuple);
780 if (set == NULL)
781 return NULL;
782
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200783 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100784 Py_DECREF(set);
785 return key;
786 }
787 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000788 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100789 * to ensure that they are seen as unequal. */
790 PyObject *obj_id = PyLong_FromVoidPtr(op);
791 if (obj_id == NULL)
792 return NULL;
793
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200794 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100795 Py_DECREF(obj_id);
796 }
797 return key;
798}
799
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000800static PyObject *
801code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 PyCodeObject *co, *cp;
804 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100805 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 if ((op != Py_EQ && op != Py_NE) ||
809 !PyCode_Check(self) ||
810 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500811 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 co = (PyCodeObject *)self;
815 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100818 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 eq = co->co_argcount == cp->co_argcount;
820 if (!eq) goto unequal;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100821 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
822 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
824 if (!eq) goto unequal;
825 eq = co->co_nlocals == cp->co_nlocals;
826 if (!eq) goto unequal;
827 eq = co->co_flags == cp->co_flags;
828 if (!eq) goto unequal;
829 eq = co->co_firstlineno == cp->co_firstlineno;
830 if (!eq) goto unequal;
831 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
832 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100833
834 /* compare constants */
835 consts1 = _PyCode_ConstantKey(co->co_consts);
836 if (!consts1)
837 return NULL;
838 consts2 = _PyCode_ConstantKey(cp->co_consts);
839 if (!consts2) {
840 Py_DECREF(consts1);
841 return NULL;
842 }
843 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
844 Py_DECREF(consts1);
845 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
849 if (eq <= 0) goto unequal;
850 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
851 if (eq <= 0) goto unequal;
852 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
853 if (eq <= 0) goto unequal;
854 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
855 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (op == Py_EQ)
858 res = Py_True;
859 else
860 res = Py_False;
861 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000862
863 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 if (eq < 0)
865 return NULL;
866 if (op == Py_NE)
867 res = Py_True;
868 else
869 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000870
871 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 Py_INCREF(res);
873 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874}
875
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000876static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877code_hash(PyCodeObject *co)
878{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000879 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 h0 = PyObject_Hash(co->co_name);
881 if (h0 == -1) return -1;
882 h1 = PyObject_Hash(co->co_code);
883 if (h1 == -1) return -1;
884 h2 = PyObject_Hash(co->co_consts);
885 if (h2 == -1) return -1;
886 h3 = PyObject_Hash(co->co_names);
887 if (h3 == -1) return -1;
888 h4 = PyObject_Hash(co->co_varnames);
889 if (h4 == -1) return -1;
890 h5 = PyObject_Hash(co->co_freevars);
891 if (h5 == -1) return -1;
892 h6 = PyObject_Hash(co->co_cellvars);
893 if (h6 == -1) return -1;
894 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100895 co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 co->co_nlocals ^ co->co_flags;
897 if (h == -1) h = -2;
898 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899}
900
901/* XXX code objects need to participate in GC? */
902
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200903static struct PyMethodDef code_methods[] = {
904 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
Victor Stinnera9f05d62019-05-24 23:57:23 +0200905 CODE_REPLACE_METHODDEF
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200906 {NULL, NULL} /* sentinel */
907};
908
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 PyVarObject_HEAD_INIT(&PyType_Type, 0)
911 "code",
912 sizeof(PyCodeObject),
913 0,
914 (destructor)code_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200915 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 0, /* tp_getattr */
917 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200918 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 (reprfunc)code_repr, /* tp_repr */
920 0, /* tp_as_number */
921 0, /* tp_as_sequence */
922 0, /* tp_as_mapping */
923 (hashfunc)code_hash, /* tp_hash */
924 0, /* tp_call */
925 0, /* tp_str */
926 PyObject_GenericGetAttr, /* tp_getattro */
927 0, /* tp_setattro */
928 0, /* tp_as_buffer */
929 Py_TPFLAGS_DEFAULT, /* tp_flags */
930 code_doc, /* tp_doc */
931 0, /* tp_traverse */
932 0, /* tp_clear */
933 code_richcompare, /* tp_richcompare */
934 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
935 0, /* tp_iter */
936 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200937 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 code_memberlist, /* tp_members */
939 0, /* tp_getset */
940 0, /* tp_base */
941 0, /* tp_dict */
942 0, /* tp_descr_get */
943 0, /* tp_descr_set */
944 0, /* tp_dictoffset */
945 0, /* tp_init */
946 0, /* tp_alloc */
947 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948};
949
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000950/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
951 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952*/
953
954int
955PyCode_Addr2Line(PyCodeObject *co, int addrq)
956{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000957 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
959 int line = co->co_firstlineno;
960 int addr = 0;
961 while (--size >= 0) {
962 addr += *p++;
963 if (addr > addrq)
964 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100965 line += (signed char)*p;
966 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 }
968 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000970
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000971/* Update *bounds to describe the first and one-past-the-last instructions in
972 the same line as lasti. Return the number of that line. */
973int
974_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000975{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000976 Py_ssize_t size;
977 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
981 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 addr = 0;
984 line = co->co_firstlineno;
985 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 /* possible optimization: if f->f_lasti == instr_ub
988 (likely to be a common case) then we already know
989 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700990 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 /* See lnotab_notes.txt for the description of
993 co_lnotab. A point to remember: increments to p
994 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 bounds->ap_lower = 0;
997 while (size > 0) {
998 if (addr + *p > lasti)
999 break;
1000 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01001001 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01001003 line += (signed char)*p;
1004 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 --size;
1006 }
1007
1008 if (size > 0) {
1009 while (--size >= 0) {
1010 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01001011 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01001013 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001014 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 bounds->ap_upper = addr;
1016 }
1017 else {
1018 bounds->ap_upper = INT_MAX;
1019 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001022}
Brett Cannon5c4de282016-09-07 11:16:41 -07001023
1024
1025int
1026_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
1027{
Brett Cannon5c4de282016-09-07 11:16:41 -07001028 if (!PyCode_Check(code)) {
1029 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -07001030 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001031 }
1032
Brett Cannond0600ed2016-09-07 14:30:39 -07001033 PyCodeObject *o = (PyCodeObject*) code;
1034 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001035
Brett Cannond0600ed2016-09-07 14:30:39 -07001036 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001037 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -07001038 return 0;
1039 }
1040
Brett Cannond0600ed2016-09-07 14:30:39 -07001041 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -07001042 return 0;
1043}
1044
1045
1046int
1047_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
1048{
Victor Stinnercaba55b2018-08-03 15:33:52 +02001049 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07001050
1051 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001052 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -07001053 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -07001054 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001055 }
1056
Brett Cannond0600ed2016-09-07 14:30:39 -07001057 PyCodeObject *o = (PyCodeObject*) code;
1058 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001059
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001060 if (co_extra == NULL || co_extra->ce_size <= index) {
1061 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
1062 co_extra = PyMem_Realloc(
1063 co_extra,
1064 sizeof(_PyCodeObjectExtra) +
1065 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +00001066 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -07001067 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001068 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001069 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -07001070 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -07001071 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001072 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001073 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001074 }
1075
1076 if (co_extra->ce_extras[index] != NULL) {
1077 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001078 if (free != NULL) {
1079 free(co_extra->ce_extras[index]);
1080 }
Brett Cannon5c4de282016-09-07 11:16:41 -07001081 }
1082
Brett Cannond0600ed2016-09-07 14:30:39 -07001083 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001084 return 0;
1085}