blob: 1333cc833e1e4b5194877ee11b4f8274248f5b18 [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 +0000105
106PyCodeObject *
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100107PyCode_New(int argcount, int posonlyargcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 int nlocals, int stacksize, int flags,
109 PyObject *code, PyObject *consts, PyObject *names,
110 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
111 PyObject *filename, PyObject *name, int firstlineno,
112 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200115 Py_ssize_t *cell2arg = NULL;
Serhiy Storchakabd473842018-07-16 09:10:19 +0300116 Py_ssize_t i, n_cellvars, n_varnames, total_args;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /* Check argument types */
Pablo Galindocd74e662019-06-01 18:08:04 +0100119 if (argcount < posonlyargcount || posonlyargcount < 0 ||
120 kwonlyargcount < 0 || nlocals < 0 ||
121 stacksize < 0 || flags < 0 ||
Victor Stinnera9f05d62019-05-24 23:57:23 +0200122 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 consts == NULL || !PyTuple_Check(consts) ||
124 names == NULL || !PyTuple_Check(names) ||
125 varnames == NULL || !PyTuple_Check(varnames) ||
126 freevars == NULL || !PyTuple_Check(freevars) ||
127 cellvars == NULL || !PyTuple_Check(cellvars) ||
128 name == NULL || !PyUnicode_Check(name) ||
129 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200130 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 PyErr_BadInternalCall();
132 return NULL;
133 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200134
Victor Stinnera9f05d62019-05-24 23:57:23 +0200135 /* Ensure that strings are ready Unicode string */
136 if (PyUnicode_READY(name) < 0) {
Victor Stinner7c74de42013-10-10 15:55:14 +0200137 return NULL;
Victor Stinnera9f05d62019-05-24 23:57:23 +0200138 }
139 if (PyUnicode_READY(filename) < 0) {
140 return NULL;
141 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 intern_strings(names);
144 intern_strings(varnames);
145 intern_strings(freevars);
146 intern_strings(cellvars);
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300147 intern_string_constants(consts);
Nick Coghlan078f1812017-12-03 11:12:20 +1000148
149 /* Check for any inner or outer closure references */
150 n_cellvars = PyTuple_GET_SIZE(cellvars);
151 if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
152 flags |= CO_NOFREE;
153 } else {
154 flags &= ~CO_NOFREE;
155 }
156
Serhiy Storchakabd473842018-07-16 09:10:19 +0300157 n_varnames = PyTuple_GET_SIZE(varnames);
Pablo Galindocd74e662019-06-01 18:08:04 +0100158 if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
Serhiy Storchakabd473842018-07-16 09:10:19 +0300159 /* Never overflows. */
Pablo Galindocd74e662019-06-01 18:08:04 +0100160 total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100161 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
Serhiy Storchakabd473842018-07-16 09:10:19 +0300162 }
163 else {
164 total_args = n_varnames + 1;
165 }
166 if (total_args > n_varnames) {
167 PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
168 return NULL;
169 }
170
Benjamin Peterson90037602011-06-25 22:54:45 -0500171 /* Create mapping between cells and arguments if needed. */
172 if (n_cellvars) {
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700173 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200174 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
175 if (cell2arg == NULL) {
176 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500177 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200178 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500179 /* Find cells which are also arguments. */
180 for (i = 0; i < n_cellvars; i++) {
181 Py_ssize_t j;
182 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200183 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500184 for (j = 0; j < total_args; j++) {
185 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200186 int cmp = PyUnicode_Compare(cell, arg);
187 if (cmp == -1 && PyErr_Occurred()) {
188 PyMem_FREE(cell2arg);
189 return NULL;
190 }
191 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500192 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700193 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500194 break;
195 }
196 }
197 }
198 if (!used_cell2arg) {
199 PyMem_FREE(cell2arg);
200 cell2arg = NULL;
201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500203 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
204 if (co == NULL) {
205 if (cell2arg)
206 PyMem_FREE(cell2arg);
207 return NULL;
208 }
209 co->co_argcount = argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100210 co->co_posonlyargcount = posonlyargcount;
Benjamin Peterson90037602011-06-25 22:54:45 -0500211 co->co_kwonlyargcount = kwonlyargcount;
212 co->co_nlocals = nlocals;
213 co->co_stacksize = stacksize;
214 co->co_flags = flags;
215 Py_INCREF(code);
216 co->co_code = code;
217 Py_INCREF(consts);
218 co->co_consts = consts;
219 Py_INCREF(names);
220 co->co_names = names;
221 Py_INCREF(varnames);
222 co->co_varnames = varnames;
223 Py_INCREF(freevars);
224 co->co_freevars = freevars;
225 Py_INCREF(cellvars);
226 co->co_cellvars = cellvars;
227 co->co_cell2arg = cell2arg;
228 Py_INCREF(filename);
229 co->co_filename = filename;
230 Py_INCREF(name);
231 co->co_name = name;
232 co->co_firstlineno = firstlineno;
233 Py_INCREF(lnotab);
234 co->co_lnotab = lnotab;
235 co->co_zombieframe = NULL;
236 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700237 co->co_extra = NULL;
Inada Naoki91234a12019-06-03 21:30:58 +0900238
239 co->co_opcache_map = NULL;
240 co->co_opcache = NULL;
241 co->co_opcache_flag = 0;
242 co->co_opcache_size = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244}
245
Inada Naoki91234a12019-06-03 21:30:58 +0900246int
247_PyCode_InitOpcache(PyCodeObject *co)
248{
249 Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);
250 co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);
251 if (co->co_opcache_map == NULL) {
252 return -1;
253 }
254
255 _Py_CODEUNIT *opcodes = (_Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);
256 Py_ssize_t opts = 0;
257
258 for (Py_ssize_t i = 0; i < co_size;) {
259 unsigned char opcode = _Py_OPCODE(opcodes[i]);
260 i++; // 'i' is now aligned to (next_instr - first_instr)
261
262 // TODO: LOAD_METHOD, LOAD_ATTR
263 if (opcode == LOAD_GLOBAL) {
Victor Stinnerea9f1682019-06-04 17:08:24 +0200264 opts++;
265 co->co_opcache_map[i] = (unsigned char)opts;
Inada Naoki91234a12019-06-03 21:30:58 +0900266 if (opts > 254) {
267 break;
268 }
269 }
270 }
271
272 if (opts) {
273 co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));
274 if (co->co_opcache == NULL) {
275 PyMem_FREE(co->co_opcache_map);
276 return -1;
277 }
278 }
279 else {
280 PyMem_FREE(co->co_opcache_map);
281 co->co_opcache_map = NULL;
282 co->co_opcache = NULL;
283 }
284
Miss Islington (bot)996e5262019-06-12 03:14:43 -0700285 co->co_opcache_size = (unsigned char)opts;
Inada Naoki91234a12019-06-03 21:30:58 +0900286 return 0;
287}
288
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000289PyCodeObject *
290PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 static PyObject *emptystring = NULL;
293 static PyObject *nulltuple = NULL;
294 PyObject *filename_ob = NULL;
295 PyObject *funcname_ob = NULL;
296 PyCodeObject *result = NULL;
297 if (emptystring == NULL) {
298 emptystring = PyBytes_FromString("");
299 if (emptystring == NULL)
300 goto failed;
301 }
302 if (nulltuple == NULL) {
303 nulltuple = PyTuple_New(0);
304 if (nulltuple == NULL)
305 goto failed;
306 }
307 funcname_ob = PyUnicode_FromString(funcname);
308 if (funcname_ob == NULL)
309 goto failed;
310 filename_ob = PyUnicode_DecodeFSDefault(filename);
311 if (filename_ob == NULL)
312 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 result = PyCode_New(0, /* argcount */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100315 0, /* posonlyargcount */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 0, /* kwonlyargcount */
317 0, /* nlocals */
318 0, /* stacksize */
319 0, /* flags */
320 emptystring, /* code */
321 nulltuple, /* consts */
322 nulltuple, /* names */
323 nulltuple, /* varnames */
324 nulltuple, /* freevars */
325 nulltuple, /* cellvars */
326 filename_ob, /* filename */
327 funcname_ob, /* name */
328 firstlineno, /* firstlineno */
329 emptystring /* lnotab */
330 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000331
332failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 Py_XDECREF(funcname_ob);
334 Py_XDECREF(filename_ob);
335 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000336}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337
338#define OFF(x) offsetof(PyCodeObject, x)
339
340static PyMemberDef code_memberlist[] = {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100341 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
342 {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
343 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
344 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
345 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
346 {"co_flags", T_INT, OFF(co_flags), READONLY},
347 {"co_code", T_OBJECT, OFF(co_code), READONLY},
348 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
349 {"co_names", T_OBJECT, OFF(co_names), READONLY},
350 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
351 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
352 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
353 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
354 {"co_name", T_OBJECT, OFF(co_name), READONLY},
355 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
356 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358};
359
360/* Helper for code_new: return a shallow copy of a tuple that is
361 guaranteed to contain exact strings, by converting string subclasses
362 to exact strings and complaining if a non-string is found. */
363static PyObject*
364validate_and_copy_tuple(PyObject *tup)
365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 PyObject *newtuple;
367 PyObject *item;
368 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 len = PyTuple_GET_SIZE(tup);
371 newtuple = PyTuple_New(len);
372 if (newtuple == NULL)
373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 for (i = 0; i < len; i++) {
376 item = PyTuple_GET_ITEM(tup, i);
377 if (PyUnicode_CheckExact(item)) {
378 Py_INCREF(item);
379 }
380 else if (!PyUnicode_Check(item)) {
381 PyErr_Format(
382 PyExc_TypeError,
383 "name tuples must contain only "
384 "strings, not '%.500s'",
385 item->ob_type->tp_name);
386 Py_DECREF(newtuple);
387 return NULL;
388 }
389 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100390 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (item == NULL) {
392 Py_DECREF(newtuple);
393 return NULL;
394 }
395 }
396 PyTuple_SET_ITEM(newtuple, i, item);
397 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400}
401
402PyDoc_STRVAR(code_doc,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100403"code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
404 flags, codestring, constants, names, varnames, filename, name,\n\
405 firstlineno, lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000406\n\
407Create a code object. Not for the faint of heart.");
408
409static PyObject *
410code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 int argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100413 int posonlyargcount;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 int kwonlyargcount;
415 int nlocals;
416 int stacksize;
417 int flags;
418 PyObject *co = NULL;
419 PyObject *code;
420 PyObject *consts;
421 PyObject *names, *ournames = NULL;
422 PyObject *varnames, *ourvarnames = NULL;
423 PyObject *freevars = NULL, *ourfreevars = NULL;
424 PyObject *cellvars = NULL, *ourcellvars = NULL;
425 PyObject *filename;
426 PyObject *name;
427 int firstlineno;
428 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100430 if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
431 &argcount, &posonlyargcount, &kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 &nlocals, &stacksize, &flags,
433 &code,
434 &PyTuple_Type, &consts,
435 &PyTuple_Type, &names,
436 &PyTuple_Type, &varnames,
437 &filename, &name,
438 &firstlineno, &lnotab,
439 &PyTuple_Type, &freevars,
440 &PyTuple_Type, &cellvars))
441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442
Pablo Galindo3b57f502019-06-01 21:18:48 +0100443 if (PySys_Audit("code.__new__", "OOOiiiiii",
444 code, filename, name, argcount, posonlyargcount,
445 kwonlyargcount, nlocals, stacksize, flags) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -0700446 goto cleanup;
447 }
448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (argcount < 0) {
450 PyErr_SetString(
451 PyExc_ValueError,
452 "code: argcount must not be negative");
453 goto cleanup;
454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100456 if (posonlyargcount < 0) {
457 PyErr_SetString(
458 PyExc_ValueError,
459 "code: posonlyargcount must not be negative");
460 goto cleanup;
461 }
462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (kwonlyargcount < 0) {
464 PyErr_SetString(
465 PyExc_ValueError,
466 "code: kwonlyargcount must not be negative");
467 goto cleanup;
468 }
469 if (nlocals < 0) {
470 PyErr_SetString(
471 PyExc_ValueError,
472 "code: nlocals must not be negative");
473 goto cleanup;
474 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 ournames = validate_and_copy_tuple(names);
477 if (ournames == NULL)
478 goto cleanup;
479 ourvarnames = validate_and_copy_tuple(varnames);
480 if (ourvarnames == NULL)
481 goto cleanup;
482 if (freevars)
483 ourfreevars = validate_and_copy_tuple(freevars);
484 else
485 ourfreevars = PyTuple_New(0);
486 if (ourfreevars == NULL)
487 goto cleanup;
488 if (cellvars)
489 ourcellvars = validate_and_copy_tuple(cellvars);
490 else
491 ourcellvars = PyTuple_New(0);
492 if (ourcellvars == NULL)
493 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100495 co = (PyObject *)PyCode_New(argcount, posonlyargcount, kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 nlocals, stacksize, flags,
497 code, consts, ournames, ourvarnames,
498 ourfreevars, ourcellvars, filename,
499 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 Py_XDECREF(ournames);
502 Py_XDECREF(ourvarnames);
503 Py_XDECREF(ourfreevars);
504 Py_XDECREF(ourcellvars);
505 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506}
507
508static void
509code_dealloc(PyCodeObject *co)
510{
Inada Naoki91234a12019-06-03 21:30:58 +0900511 if (co->co_opcache != NULL) {
512 PyMem_FREE(co->co_opcache);
513 }
514 if (co->co_opcache_map != NULL) {
515 PyMem_FREE(co->co_opcache_map);
516 }
517 co->co_opcache_flag = 0;
518 co->co_opcache_size = 0;
519
Brett Cannon5c4de282016-09-07 11:16:41 -0700520 if (co->co_extra != NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200521 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannond0600ed2016-09-07 14:30:39 -0700522 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700523
Brett Cannond0600ed2016-09-07 14:30:39 -0700524 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700525 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700526
527 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700528 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700529 }
530 }
531
Victor Stinner23e79442017-06-28 02:12:00 +0200532 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700533 }
534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 Py_XDECREF(co->co_code);
536 Py_XDECREF(co->co_consts);
537 Py_XDECREF(co->co_names);
538 Py_XDECREF(co->co_varnames);
539 Py_XDECREF(co->co_freevars);
540 Py_XDECREF(co->co_cellvars);
541 Py_XDECREF(co->co_filename);
542 Py_XDECREF(co->co_name);
543 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500544 if (co->co_cell2arg != NULL)
545 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 if (co->co_zombieframe != NULL)
547 PyObject_GC_Del(co->co_zombieframe);
548 if (co->co_weakreflist != NULL)
549 PyObject_ClearWeakRefs((PyObject*)co);
550 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551}
552
553static PyObject *
Victor Stinnera9f05d62019-05-24 23:57:23 +0200554code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200555{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900556 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
557 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200558
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300559 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200560 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300561 }
562 if (co_extra != NULL) {
563 res += sizeof(_PyCodeObjectExtra) +
564 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
565 }
Inada Naoki91234a12019-06-03 21:30:58 +0900566 if (co->co_opcache != NULL) {
567 assert(co->co_opcache_map != NULL);
568 // co_opcache_map
569 res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);
570 // co_opcache
571 res += co->co_opcache_size * sizeof(_PyOpcache);
572 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200573 return PyLong_FromSsize_t(res);
574}
575
Victor Stinnera9f05d62019-05-24 23:57:23 +0200576/*[clinic input]
577code.replace
578
579 *
580 co_argcount: int(c_default="self->co_argcount") = -1
581 co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
582 co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
583 co_nlocals: int(c_default="self->co_nlocals") = -1
584 co_stacksize: int(c_default="self->co_stacksize") = -1
585 co_flags: int(c_default="self->co_flags") = -1
586 co_firstlineno: int(c_default="self->co_firstlineno") = -1
587 co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
588 co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
589 co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
590 co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
591 co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
592 co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
593 co_filename: unicode(c_default="self->co_filename") = None
594 co_name: unicode(c_default="self->co_name") = None
595 co_lnotab: PyBytesObject(c_default="(PyBytesObject *)self->co_lnotab") = None
596
597Return a new code object with new specified fields.
598[clinic start generated code]*/
599
600static PyObject *
601code_replace_impl(PyCodeObject *self, int co_argcount,
602 int co_posonlyargcount, int co_kwonlyargcount,
603 int co_nlocals, int co_stacksize, int co_flags,
604 int co_firstlineno, PyBytesObject *co_code,
605 PyObject *co_consts, PyObject *co_names,
606 PyObject *co_varnames, PyObject *co_freevars,
607 PyObject *co_cellvars, PyObject *co_filename,
608 PyObject *co_name, PyBytesObject *co_lnotab)
609/*[clinic end generated code: output=25c8e303913bcace input=77189e46579ec426]*/
610{
611#define CHECK_INT_ARG(ARG) \
612 if (ARG < 0) { \
613 PyErr_SetString(PyExc_ValueError, \
614 #ARG " must be a positive integer"); \
615 return NULL; \
616 }
617
618 CHECK_INT_ARG(co_argcount);
619 CHECK_INT_ARG(co_posonlyargcount);
620 CHECK_INT_ARG(co_kwonlyargcount);
621 CHECK_INT_ARG(co_nlocals);
622 CHECK_INT_ARG(co_stacksize);
623 CHECK_INT_ARG(co_flags);
624 CHECK_INT_ARG(co_firstlineno);
625
626#undef CHECK_INT_ARG
627
628 return (PyObject *)PyCode_New(
629 co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
630 co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
631 co_varnames, co_freevars, co_cellvars, co_filename, co_name,
632 co_firstlineno, (PyObject*)co_lnotab);
633}
634
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200635static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636code_repr(PyCodeObject *co)
637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 int lineno;
639 if (co->co_firstlineno != 0)
640 lineno = co->co_firstlineno;
641 else
642 lineno = -1;
643 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
644 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000645 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 co->co_name, co, co->co_filename, lineno);
647 } else {
648 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000649 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 co->co_name, co, lineno);
651 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652}
653
Victor Stinnerefb24132016-01-22 12:33:12 +0100654PyObject*
655_PyCode_ConstantKey(PyObject *op)
656{
657 PyObject *key;
658
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300659 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100660 if (op == Py_None || op == Py_Ellipsis
661 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100662 || PyUnicode_CheckExact(op)
663 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300664 || PyCode_Check(op))
665 {
666 /* Objects of these types are always different from object of other
667 * type and from tuples. */
668 Py_INCREF(op);
669 key = op;
670 }
671 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
672 /* Make booleans different from integers 0 and 1.
673 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100674 key = PyTuple_Pack(2, Py_TYPE(op), op);
675 }
676 else if (PyFloat_CheckExact(op)) {
677 double d = PyFloat_AS_DOUBLE(op);
678 /* all we need is to make the tuple different in either the 0.0
679 * or -0.0 case from all others, just to avoid the "coercion".
680 */
681 if (d == 0.0 && copysign(1.0, d) < 0.0)
682 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
683 else
684 key = PyTuple_Pack(2, Py_TYPE(op), op);
685 }
686 else if (PyComplex_CheckExact(op)) {
687 Py_complex z;
688 int real_negzero, imag_negzero;
689 /* For the complex case we must make complex(x, 0.)
690 different from complex(x, -0.) and complex(0., y)
691 different from complex(-0., y), for any x and y.
692 All four complex zeros must be distinguished.*/
693 z = PyComplex_AsCComplex(op);
694 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
695 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
696 /* use True, False and None singleton as tags for the real and imag
697 * sign, to make tuples different */
698 if (real_negzero && imag_negzero) {
699 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
700 }
701 else if (imag_negzero) {
702 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
703 }
704 else if (real_negzero) {
705 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
706 }
707 else {
708 key = PyTuple_Pack(2, Py_TYPE(op), op);
709 }
710 }
711 else if (PyTuple_CheckExact(op)) {
712 Py_ssize_t i, len;
713 PyObject *tuple;
714
715 len = PyTuple_GET_SIZE(op);
716 tuple = PyTuple_New(len);
717 if (tuple == NULL)
718 return NULL;
719
720 for (i=0; i < len; i++) {
721 PyObject *item, *item_key;
722
723 item = PyTuple_GET_ITEM(op, i);
724 item_key = _PyCode_ConstantKey(item);
725 if (item_key == NULL) {
726 Py_DECREF(tuple);
727 return NULL;
728 }
729
730 PyTuple_SET_ITEM(tuple, i, item_key);
731 }
732
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200733 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100734 Py_DECREF(tuple);
735 }
736 else if (PyFrozenSet_CheckExact(op)) {
737 Py_ssize_t pos = 0;
738 PyObject *item;
739 Py_hash_t hash;
740 Py_ssize_t i, len;
741 PyObject *tuple, *set;
742
743 len = PySet_GET_SIZE(op);
744 tuple = PyTuple_New(len);
745 if (tuple == NULL)
746 return NULL;
747
748 i = 0;
749 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
750 PyObject *item_key;
751
752 item_key = _PyCode_ConstantKey(item);
753 if (item_key == NULL) {
754 Py_DECREF(tuple);
755 return NULL;
756 }
757
758 assert(i < len);
759 PyTuple_SET_ITEM(tuple, i, item_key);
760 i++;
761 }
762 set = PyFrozenSet_New(tuple);
763 Py_DECREF(tuple);
764 if (set == NULL)
765 return NULL;
766
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200767 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100768 Py_DECREF(set);
769 return key;
770 }
771 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000772 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100773 * to ensure that they are seen as unequal. */
774 PyObject *obj_id = PyLong_FromVoidPtr(op);
775 if (obj_id == NULL)
776 return NULL;
777
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200778 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100779 Py_DECREF(obj_id);
780 }
781 return key;
782}
783
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000784static PyObject *
785code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyCodeObject *co, *cp;
788 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100789 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 if ((op != Py_EQ && op != Py_NE) ||
793 !PyCode_Check(self) ||
794 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500795 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 co = (PyCodeObject *)self;
799 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100802 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 eq = co->co_argcount == cp->co_argcount;
804 if (!eq) goto unequal;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100805 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
806 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
808 if (!eq) goto unequal;
809 eq = co->co_nlocals == cp->co_nlocals;
810 if (!eq) goto unequal;
811 eq = co->co_flags == cp->co_flags;
812 if (!eq) goto unequal;
813 eq = co->co_firstlineno == cp->co_firstlineno;
814 if (!eq) goto unequal;
815 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
816 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100817
818 /* compare constants */
819 consts1 = _PyCode_ConstantKey(co->co_consts);
820 if (!consts1)
821 return NULL;
822 consts2 = _PyCode_ConstantKey(cp->co_consts);
823 if (!consts2) {
824 Py_DECREF(consts1);
825 return NULL;
826 }
827 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
828 Py_DECREF(consts1);
829 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
833 if (eq <= 0) goto unequal;
834 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
835 if (eq <= 0) goto unequal;
836 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
837 if (eq <= 0) goto unequal;
838 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
839 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 if (op == Py_EQ)
842 res = Py_True;
843 else
844 res = Py_False;
845 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000846
847 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 if (eq < 0)
849 return NULL;
850 if (op == Py_NE)
851 res = Py_True;
852 else
853 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000854
855 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 Py_INCREF(res);
857 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858}
859
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000860static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861code_hash(PyCodeObject *co)
862{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000863 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 h0 = PyObject_Hash(co->co_name);
865 if (h0 == -1) return -1;
866 h1 = PyObject_Hash(co->co_code);
867 if (h1 == -1) return -1;
868 h2 = PyObject_Hash(co->co_consts);
869 if (h2 == -1) return -1;
870 h3 = PyObject_Hash(co->co_names);
871 if (h3 == -1) return -1;
872 h4 = PyObject_Hash(co->co_varnames);
873 if (h4 == -1) return -1;
874 h5 = PyObject_Hash(co->co_freevars);
875 if (h5 == -1) return -1;
876 h6 = PyObject_Hash(co->co_cellvars);
877 if (h6 == -1) return -1;
878 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100879 co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 co->co_nlocals ^ co->co_flags;
881 if (h == -1) h = -2;
882 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883}
884
885/* XXX code objects need to participate in GC? */
886
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200887static struct PyMethodDef code_methods[] = {
888 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
Victor Stinnera9f05d62019-05-24 23:57:23 +0200889 CODE_REPLACE_METHODDEF
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200890 {NULL, NULL} /* sentinel */
891};
892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyVarObject_HEAD_INIT(&PyType_Type, 0)
895 "code",
896 sizeof(PyCodeObject),
897 0,
898 (destructor)code_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200899 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 0, /* tp_getattr */
901 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200902 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 (reprfunc)code_repr, /* tp_repr */
904 0, /* tp_as_number */
905 0, /* tp_as_sequence */
906 0, /* tp_as_mapping */
907 (hashfunc)code_hash, /* tp_hash */
908 0, /* tp_call */
909 0, /* tp_str */
910 PyObject_GenericGetAttr, /* tp_getattro */
911 0, /* tp_setattro */
912 0, /* tp_as_buffer */
913 Py_TPFLAGS_DEFAULT, /* tp_flags */
914 code_doc, /* tp_doc */
915 0, /* tp_traverse */
916 0, /* tp_clear */
917 code_richcompare, /* tp_richcompare */
918 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
919 0, /* tp_iter */
920 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200921 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 code_memberlist, /* tp_members */
923 0, /* tp_getset */
924 0, /* tp_base */
925 0, /* tp_dict */
926 0, /* tp_descr_get */
927 0, /* tp_descr_set */
928 0, /* tp_dictoffset */
929 0, /* tp_init */
930 0, /* tp_alloc */
931 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932};
933
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000934/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
935 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936*/
937
938int
939PyCode_Addr2Line(PyCodeObject *co, int addrq)
940{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000941 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
943 int line = co->co_firstlineno;
944 int addr = 0;
945 while (--size >= 0) {
946 addr += *p++;
947 if (addr > addrq)
948 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100949 line += (signed char)*p;
950 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 }
952 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000954
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000955/* Update *bounds to describe the first and one-past-the-last instructions in
956 the same line as lasti. Return the number of that line. */
957int
958_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000959{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000960 Py_ssize_t size;
961 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
965 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 addr = 0;
968 line = co->co_firstlineno;
969 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 /* possible optimization: if f->f_lasti == instr_ub
972 (likely to be a common case) then we already know
973 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700974 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 /* See lnotab_notes.txt for the description of
977 co_lnotab. A point to remember: increments to p
978 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 bounds->ap_lower = 0;
981 while (size > 0) {
982 if (addr + *p > lasti)
983 break;
984 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100985 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100987 line += (signed char)*p;
988 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 --size;
990 }
991
992 if (size > 0) {
993 while (--size >= 0) {
994 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100995 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100997 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 bounds->ap_upper = addr;
1000 }
1001 else {
1002 bounds->ap_upper = INT_MAX;
1003 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001006}
Brett Cannon5c4de282016-09-07 11:16:41 -07001007
1008
1009int
1010_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
1011{
Brett Cannon5c4de282016-09-07 11:16:41 -07001012 if (!PyCode_Check(code)) {
1013 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -07001014 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001015 }
1016
Brett Cannond0600ed2016-09-07 14:30:39 -07001017 PyCodeObject *o = (PyCodeObject*) code;
1018 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001019
Brett Cannond0600ed2016-09-07 14:30:39 -07001020 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001021 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -07001022 return 0;
1023 }
1024
Brett Cannond0600ed2016-09-07 14:30:39 -07001025 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -07001026 return 0;
1027}
1028
1029
1030int
1031_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
1032{
Victor Stinnercaba55b2018-08-03 15:33:52 +02001033 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07001034
1035 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001036 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -07001037 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -07001038 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001039 }
1040
Brett Cannond0600ed2016-09-07 14:30:39 -07001041 PyCodeObject *o = (PyCodeObject*) code;
1042 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001043
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001044 if (co_extra == NULL || co_extra->ce_size <= index) {
1045 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
1046 co_extra = PyMem_Realloc(
1047 co_extra,
1048 sizeof(_PyCodeObjectExtra) +
1049 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +00001050 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -07001051 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001052 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001053 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -07001054 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -07001055 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001056 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001057 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001058 }
1059
1060 if (co_extra->ce_extras[index] != NULL) {
1061 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001062 if (free != NULL) {
1063 free(co_extra->ce_extras[index]);
1064 }
Brett Cannon5c4de282016-09-07 11:16:41 -07001065 }
1066
Brett Cannond0600ed2016-09-07 14:30:39 -07001067 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001068 return 0;
1069}