blob: fd64393c235c54610828c5fd8a57a9cb7d5224a3 [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
Victor Stinnera2783132020-01-27 23:24:13 +010041static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042intern_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)) {
Victor Stinnera2783132020-01-27 23:24:13 +010049 PyErr_SetString(PyExc_SystemError,
50 "non-string found in code slot");
51 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 }
Victor Stinnerd17a6932018-11-09 16:56:48 +010053 PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 }
Victor Stinnera2783132020-01-27 23:24:13 +010055 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056}
57
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030058/* Intern selected string constants */
59static int
Victor Stinnera2783132020-01-27 23:24:13 +010060intern_string_constants(PyObject *tuple, int *modified)
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030061{
Victor Stinnera2783132020-01-27 23:24:13 +010062 for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030063 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) {
Victor Stinnera2783132020-01-27 23:24:13 +010066 return -1;
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +030067 }
Victor Stinnera2783132020-01-27 23:24:13 +010068
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);
Victor Stinnera2783132020-01-27 23:24:13 +010074 if (modified) {
75 *modified = 1;
76 }
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030077 }
78 }
79 }
80 else if (PyTuple_CheckExact(v)) {
Victor Stinnera2783132020-01-27 23:24:13 +010081 if (intern_string_constants(v, NULL) < 0) {
82 return -1;
83 }
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030084 }
85 else if (PyFrozenSet_CheckExact(v)) {
Yury Selivanovd2fd3592016-11-09 09:42:14 -050086 PyObject *w = v;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030087 PyObject *tmp = PySequence_Tuple(v);
88 if (tmp == NULL) {
Victor Stinnera2783132020-01-27 23:24:13 +010089 return -1;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030090 }
Victor Stinnera2783132020-01-27 23:24:13 +010091 int tmp_modified = 0;
92 if (intern_string_constants(tmp, &tmp_modified) < 0) {
93 Py_DECREF(tmp);
94 return -1;
95 }
96 if (tmp_modified) {
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +030097 v = PyFrozenSet_New(tmp);
98 if (v == NULL) {
Victor Stinnera2783132020-01-27 23:24:13 +010099 Py_DECREF(tmp);
100 return -1;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300101 }
Victor Stinnera2783132020-01-27 23:24:13 +0100102
103 PyTuple_SET_ITEM(tuple, i, v);
104 Py_DECREF(w);
105 if (modified) {
106 *modified = 1;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300107 }
108 }
109 Py_DECREF(tmp);
110 }
111 }
Victor Stinnera2783132020-01-27 23:24:13 +0100112 return 0;
Serhiy Storchaka00a0fc12016-09-30 10:07:26 +0300113}
114
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115PyCodeObject *
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100116PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
117 int nlocals, int stacksize, int flags,
118 PyObject *code, PyObject *consts, PyObject *names,
119 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
120 PyObject *filename, PyObject *name, int firstlineno,
121 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 PyCodeObject *co;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200124 Py_ssize_t *cell2arg = NULL;
Serhiy Storchakabd473842018-07-16 09:10:19 +0300125 Py_ssize_t i, n_cellvars, n_varnames, total_args;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 /* Check argument types */
Pablo Galindocd74e662019-06-01 18:08:04 +0100128 if (argcount < posonlyargcount || posonlyargcount < 0 ||
129 kwonlyargcount < 0 || nlocals < 0 ||
130 stacksize < 0 || flags < 0 ||
Victor Stinnera9f05d62019-05-24 23:57:23 +0200131 code == NULL || !PyBytes_Check(code) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 consts == NULL || !PyTuple_Check(consts) ||
133 names == NULL || !PyTuple_Check(names) ||
134 varnames == NULL || !PyTuple_Check(varnames) ||
135 freevars == NULL || !PyTuple_Check(freevars) ||
136 cellvars == NULL || !PyTuple_Check(cellvars) ||
137 name == NULL || !PyUnicode_Check(name) ||
138 filename == NULL || !PyUnicode_Check(filename) ||
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200139 lnotab == NULL || !PyBytes_Check(lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyErr_BadInternalCall();
141 return NULL;
142 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200143
Victor Stinnera9f05d62019-05-24 23:57:23 +0200144 /* Ensure that strings are ready Unicode string */
145 if (PyUnicode_READY(name) < 0) {
Victor Stinner7c74de42013-10-10 15:55:14 +0200146 return NULL;
Victor Stinnera9f05d62019-05-24 23:57:23 +0200147 }
148 if (PyUnicode_READY(filename) < 0) {
149 return NULL;
150 }
Victor Stinner7c74de42013-10-10 15:55:14 +0200151
Victor Stinnera2783132020-01-27 23:24:13 +0100152 if (intern_strings(names) < 0) {
153 return NULL;
154 }
155 if (intern_strings(varnames) < 0) {
156 return NULL;
157 }
158 if (intern_strings(freevars) < 0) {
159 return NULL;
160 }
161 if (intern_strings(cellvars) < 0) {
162 return NULL;
163 }
164 if (intern_string_constants(consts, NULL) < 0) {
165 return NULL;
166 }
Nick Coghlan078f1812017-12-03 11:12:20 +1000167
168 /* Check for any inner or outer closure references */
169 n_cellvars = PyTuple_GET_SIZE(cellvars);
170 if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
171 flags |= CO_NOFREE;
172 } else {
173 flags &= ~CO_NOFREE;
174 }
175
Serhiy Storchakabd473842018-07-16 09:10:19 +0300176 n_varnames = PyTuple_GET_SIZE(varnames);
Pablo Galindocd74e662019-06-01 18:08:04 +0100177 if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
Serhiy Storchakabd473842018-07-16 09:10:19 +0300178 /* Never overflows. */
Pablo Galindocd74e662019-06-01 18:08:04 +0100179 total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100180 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
Serhiy Storchakabd473842018-07-16 09:10:19 +0300181 }
182 else {
183 total_args = n_varnames + 1;
184 }
185 if (total_args > n_varnames) {
186 PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
187 return NULL;
188 }
189
Benjamin Peterson90037602011-06-25 22:54:45 -0500190 /* Create mapping between cells and arguments if needed. */
191 if (n_cellvars) {
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700192 bool used_cell2arg = false;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200193 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
194 if (cell2arg == NULL) {
195 PyErr_NoMemory();
Benjamin Peterson90037602011-06-25 22:54:45 -0500196 return NULL;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200197 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500198 /* Find cells which are also arguments. */
199 for (i = 0; i < n_cellvars; i++) {
200 Py_ssize_t j;
201 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200202 cell2arg[i] = CO_CELL_NOT_AN_ARG;
Benjamin Peterson90037602011-06-25 22:54:45 -0500203 for (j = 0; j < total_args; j++) {
204 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200205 int cmp = PyUnicode_Compare(cell, arg);
206 if (cmp == -1 && PyErr_Occurred()) {
207 PyMem_FREE(cell2arg);
208 return NULL;
209 }
210 if (cmp == 0) {
Benjamin Peterson90037602011-06-25 22:54:45 -0500211 cell2arg[i] = j;
Benjamin Peterson1bf494b2016-09-07 11:28:35 -0700212 used_cell2arg = true;
Benjamin Peterson90037602011-06-25 22:54:45 -0500213 break;
214 }
215 }
216 }
217 if (!used_cell2arg) {
218 PyMem_FREE(cell2arg);
219 cell2arg = NULL;
220 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500222 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
223 if (co == NULL) {
224 if (cell2arg)
225 PyMem_FREE(cell2arg);
226 return NULL;
227 }
228 co->co_argcount = argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100229 co->co_posonlyargcount = posonlyargcount;
Benjamin Peterson90037602011-06-25 22:54:45 -0500230 co->co_kwonlyargcount = kwonlyargcount;
231 co->co_nlocals = nlocals;
232 co->co_stacksize = stacksize;
233 co->co_flags = flags;
234 Py_INCREF(code);
235 co->co_code = code;
236 Py_INCREF(consts);
237 co->co_consts = consts;
238 Py_INCREF(names);
239 co->co_names = names;
240 Py_INCREF(varnames);
241 co->co_varnames = varnames;
242 Py_INCREF(freevars);
243 co->co_freevars = freevars;
244 Py_INCREF(cellvars);
245 co->co_cellvars = cellvars;
246 co->co_cell2arg = cell2arg;
247 Py_INCREF(filename);
248 co->co_filename = filename;
249 Py_INCREF(name);
250 co->co_name = name;
251 co->co_firstlineno = firstlineno;
252 Py_INCREF(lnotab);
253 co->co_lnotab = lnotab;
254 co->co_zombieframe = NULL;
255 co->co_weakreflist = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -0700256 co->co_extra = NULL;
Inada Naoki91234a12019-06-03 21:30:58 +0900257
258 co->co_opcache_map = NULL;
259 co->co_opcache = NULL;
260 co->co_opcache_flag = 0;
261 co->co_opcache_size = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263}
264
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100265PyCodeObject *
266PyCode_New(int argcount, int kwonlyargcount,
267 int nlocals, int stacksize, int flags,
268 PyObject *code, PyObject *consts, PyObject *names,
269 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
270 PyObject *filename, PyObject *name, int firstlineno,
271 PyObject *lnotab)
272{
273 return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
274 stacksize, flags, code, consts, names,
275 varnames, freevars, cellvars, filename,
276 name, firstlineno, lnotab);
277}
278
Inada Naoki91234a12019-06-03 21:30:58 +0900279int
280_PyCode_InitOpcache(PyCodeObject *co)
281{
282 Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);
283 co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);
284 if (co->co_opcache_map == NULL) {
285 return -1;
286 }
287
288 _Py_CODEUNIT *opcodes = (_Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);
289 Py_ssize_t opts = 0;
290
291 for (Py_ssize_t i = 0; i < co_size;) {
292 unsigned char opcode = _Py_OPCODE(opcodes[i]);
293 i++; // 'i' is now aligned to (next_instr - first_instr)
294
295 // TODO: LOAD_METHOD, LOAD_ATTR
296 if (opcode == LOAD_GLOBAL) {
Victor Stinnerea9f1682019-06-04 17:08:24 +0200297 opts++;
298 co->co_opcache_map[i] = (unsigned char)opts;
Inada Naoki91234a12019-06-03 21:30:58 +0900299 if (opts > 254) {
300 break;
301 }
302 }
303 }
304
305 if (opts) {
306 co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));
307 if (co->co_opcache == NULL) {
308 PyMem_FREE(co->co_opcache_map);
309 return -1;
310 }
311 }
312 else {
313 PyMem_FREE(co->co_opcache_map);
314 co->co_opcache_map = NULL;
315 co->co_opcache = NULL;
316 }
317
Victor Stinner376ce982019-06-12 04:41:16 +0200318 co->co_opcache_size = (unsigned char)opts;
Inada Naoki91234a12019-06-03 21:30:58 +0900319 return 0;
320}
321
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000322PyCodeObject *
323PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 static PyObject *emptystring = NULL;
326 static PyObject *nulltuple = NULL;
327 PyObject *filename_ob = NULL;
328 PyObject *funcname_ob = NULL;
329 PyCodeObject *result = NULL;
330 if (emptystring == NULL) {
331 emptystring = PyBytes_FromString("");
332 if (emptystring == NULL)
333 goto failed;
334 }
335 if (nulltuple == NULL) {
336 nulltuple = PyTuple_New(0);
337 if (nulltuple == NULL)
338 goto failed;
339 }
340 funcname_ob = PyUnicode_FromString(funcname);
341 if (funcname_ob == NULL)
342 goto failed;
343 filename_ob = PyUnicode_DecodeFSDefault(filename);
344 if (filename_ob == NULL)
345 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000346
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100347 result = PyCode_NewWithPosOnlyArgs(
348 0, /* argcount */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100349 0, /* posonlyargcount */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 0, /* kwonlyargcount */
351 0, /* nlocals */
352 0, /* stacksize */
353 0, /* flags */
354 emptystring, /* code */
355 nulltuple, /* consts */
356 nulltuple, /* names */
357 nulltuple, /* varnames */
358 nulltuple, /* freevars */
359 nulltuple, /* cellvars */
360 filename_ob, /* filename */
361 funcname_ob, /* name */
362 firstlineno, /* firstlineno */
363 emptystring /* lnotab */
364 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000365
366failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 Py_XDECREF(funcname_ob);
368 Py_XDECREF(filename_ob);
369 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000370}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371
372#define OFF(x) offsetof(PyCodeObject, x)
373
374static PyMemberDef code_memberlist[] = {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100375 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
376 {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
377 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
378 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
379 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
380 {"co_flags", T_INT, OFF(co_flags), READONLY},
381 {"co_code", T_OBJECT, OFF(co_code), READONLY},
382 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
383 {"co_names", T_OBJECT, OFF(co_names), READONLY},
384 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
385 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
386 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
387 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
388 {"co_name", T_OBJECT, OFF(co_name), READONLY},
389 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
390 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392};
393
394/* Helper for code_new: return a shallow copy of a tuple that is
395 guaranteed to contain exact strings, by converting string subclasses
396 to exact strings and complaining if a non-string is found. */
397static PyObject*
398validate_and_copy_tuple(PyObject *tup)
399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 PyObject *newtuple;
401 PyObject *item;
402 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 len = PyTuple_GET_SIZE(tup);
405 newtuple = PyTuple_New(len);
406 if (newtuple == NULL)
407 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 for (i = 0; i < len; i++) {
410 item = PyTuple_GET_ITEM(tup, i);
411 if (PyUnicode_CheckExact(item)) {
412 Py_INCREF(item);
413 }
414 else if (!PyUnicode_Check(item)) {
415 PyErr_Format(
416 PyExc_TypeError,
417 "name tuples must contain only "
418 "strings, not '%.500s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100419 Py_TYPE(item)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 Py_DECREF(newtuple);
421 return NULL;
422 }
423 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100424 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 if (item == NULL) {
426 Py_DECREF(newtuple);
427 return NULL;
428 }
429 }
430 PyTuple_SET_ITEM(newtuple, i, item);
431 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434}
435
436PyDoc_STRVAR(code_doc,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100437"code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
438 flags, codestring, constants, names, varnames, filename, name,\n\
439 firstlineno, lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440\n\
441Create a code object. Not for the faint of heart.");
442
443static PyObject *
444code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 int argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100447 int posonlyargcount;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 int kwonlyargcount;
449 int nlocals;
450 int stacksize;
451 int flags;
452 PyObject *co = NULL;
453 PyObject *code;
454 PyObject *consts;
455 PyObject *names, *ournames = NULL;
456 PyObject *varnames, *ourvarnames = NULL;
457 PyObject *freevars = NULL, *ourfreevars = NULL;
458 PyObject *cellvars = NULL, *ourcellvars = NULL;
459 PyObject *filename;
460 PyObject *name;
461 int firstlineno;
462 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100464 if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
465 &argcount, &posonlyargcount, &kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 &nlocals, &stacksize, &flags,
467 &code,
468 &PyTuple_Type, &consts,
469 &PyTuple_Type, &names,
470 &PyTuple_Type, &varnames,
471 &filename, &name,
472 &firstlineno, &lnotab,
473 &PyTuple_Type, &freevars,
474 &PyTuple_Type, &cellvars))
475 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476
Pablo Galindo3b57f502019-06-01 21:18:48 +0100477 if (PySys_Audit("code.__new__", "OOOiiiiii",
478 code, filename, name, argcount, posonlyargcount,
479 kwonlyargcount, nlocals, stacksize, flags) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -0700480 goto cleanup;
481 }
482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (argcount < 0) {
484 PyErr_SetString(
485 PyExc_ValueError,
486 "code: argcount must not be negative");
487 goto cleanup;
488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100490 if (posonlyargcount < 0) {
491 PyErr_SetString(
492 PyExc_ValueError,
493 "code: posonlyargcount must not be negative");
494 goto cleanup;
495 }
496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (kwonlyargcount < 0) {
498 PyErr_SetString(
499 PyExc_ValueError,
500 "code: kwonlyargcount must not be negative");
501 goto cleanup;
502 }
503 if (nlocals < 0) {
504 PyErr_SetString(
505 PyExc_ValueError,
506 "code: nlocals must not be negative");
507 goto cleanup;
508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 ournames = validate_and_copy_tuple(names);
511 if (ournames == NULL)
512 goto cleanup;
513 ourvarnames = validate_and_copy_tuple(varnames);
514 if (ourvarnames == NULL)
515 goto cleanup;
516 if (freevars)
517 ourfreevars = validate_and_copy_tuple(freevars);
518 else
519 ourfreevars = PyTuple_New(0);
520 if (ourfreevars == NULL)
521 goto cleanup;
522 if (cellvars)
523 ourcellvars = validate_and_copy_tuple(cellvars);
524 else
525 ourcellvars = PyTuple_New(0);
526 if (ourcellvars == NULL)
527 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100529 co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
530 kwonlyargcount,
531 nlocals, stacksize, flags,
532 code, consts, ournames,
533 ourvarnames, ourfreevars,
534 ourcellvars, filename,
535 name, firstlineno, lnotab);
Victor Stinnera2783132020-01-27 23:24:13 +0100536 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 Py_XDECREF(ournames);
538 Py_XDECREF(ourvarnames);
539 Py_XDECREF(ourfreevars);
540 Py_XDECREF(ourcellvars);
541 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542}
543
544static void
545code_dealloc(PyCodeObject *co)
546{
Inada Naoki91234a12019-06-03 21:30:58 +0900547 if (co->co_opcache != NULL) {
548 PyMem_FREE(co->co_opcache);
549 }
550 if (co->co_opcache_map != NULL) {
551 PyMem_FREE(co->co_opcache_map);
552 }
553 co->co_opcache_flag = 0;
554 co->co_opcache_size = 0;
555
Brett Cannon5c4de282016-09-07 11:16:41 -0700556 if (co->co_extra != NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200557 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannond0600ed2016-09-07 14:30:39 -0700558 _PyCodeObjectExtra *co_extra = co->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -0700559
Brett Cannond0600ed2016-09-07 14:30:39 -0700560 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700561 freefunc free_extra = interp->co_extra_freefuncs[i];
Brett Cannon5c4de282016-09-07 11:16:41 -0700562
563 if (free_extra != NULL) {
Brett Cannond0600ed2016-09-07 14:30:39 -0700564 free_extra(co_extra->ce_extras[i]);
Brett Cannon5c4de282016-09-07 11:16:41 -0700565 }
566 }
567
Victor Stinner23e79442017-06-28 02:12:00 +0200568 PyMem_Free(co_extra);
Brett Cannon5c4de282016-09-07 11:16:41 -0700569 }
570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 Py_XDECREF(co->co_code);
572 Py_XDECREF(co->co_consts);
573 Py_XDECREF(co->co_names);
574 Py_XDECREF(co->co_varnames);
575 Py_XDECREF(co->co_freevars);
576 Py_XDECREF(co->co_cellvars);
577 Py_XDECREF(co->co_filename);
578 Py_XDECREF(co->co_name);
579 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500580 if (co->co_cell2arg != NULL)
581 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 if (co->co_zombieframe != NULL)
583 PyObject_GC_Del(co->co_zombieframe);
584 if (co->co_weakreflist != NULL)
585 PyObject_ClearWeakRefs((PyObject*)co);
586 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587}
588
589static PyObject *
Victor Stinnera9f05d62019-05-24 23:57:23 +0200590code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200591{
Dong-hee Nab4dc6af2017-04-20 16:31:17 +0900592 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
593 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200594
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300595 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200596 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
Serhiy Storchaka378ebb62017-07-04 15:06:16 +0300597 }
598 if (co_extra != NULL) {
599 res += sizeof(_PyCodeObjectExtra) +
600 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
601 }
Inada Naoki91234a12019-06-03 21:30:58 +0900602 if (co->co_opcache != NULL) {
603 assert(co->co_opcache_map != NULL);
604 // co_opcache_map
605 res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);
606 // co_opcache
607 res += co->co_opcache_size * sizeof(_PyOpcache);
608 }
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200609 return PyLong_FromSsize_t(res);
610}
611
Victor Stinnera9f05d62019-05-24 23:57:23 +0200612/*[clinic input]
613code.replace
614
615 *
616 co_argcount: int(c_default="self->co_argcount") = -1
617 co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
618 co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
619 co_nlocals: int(c_default="self->co_nlocals") = -1
620 co_stacksize: int(c_default="self->co_stacksize") = -1
621 co_flags: int(c_default="self->co_flags") = -1
622 co_firstlineno: int(c_default="self->co_firstlineno") = -1
623 co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
624 co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
625 co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
626 co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
627 co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
628 co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
629 co_filename: unicode(c_default="self->co_filename") = None
630 co_name: unicode(c_default="self->co_name") = None
631 co_lnotab: PyBytesObject(c_default="(PyBytesObject *)self->co_lnotab") = None
632
Anthony Sottile22424c02020-01-01 01:11:16 -0500633Return a copy of the code object with new values for the specified fields.
Victor Stinnera9f05d62019-05-24 23:57:23 +0200634[clinic start generated code]*/
635
636static PyObject *
637code_replace_impl(PyCodeObject *self, int co_argcount,
638 int co_posonlyargcount, int co_kwonlyargcount,
639 int co_nlocals, int co_stacksize, int co_flags,
640 int co_firstlineno, PyBytesObject *co_code,
641 PyObject *co_consts, PyObject *co_names,
642 PyObject *co_varnames, PyObject *co_freevars,
643 PyObject *co_cellvars, PyObject *co_filename,
644 PyObject *co_name, PyBytesObject *co_lnotab)
Anthony Sottile22424c02020-01-01 01:11:16 -0500645/*[clinic end generated code: output=25c8e303913bcace input=d9051bc8f24e6b28]*/
Victor Stinnera9f05d62019-05-24 23:57:23 +0200646{
647#define CHECK_INT_ARG(ARG) \
648 if (ARG < 0) { \
649 PyErr_SetString(PyExc_ValueError, \
650 #ARG " must be a positive integer"); \
651 return NULL; \
652 }
653
654 CHECK_INT_ARG(co_argcount);
655 CHECK_INT_ARG(co_posonlyargcount);
656 CHECK_INT_ARG(co_kwonlyargcount);
657 CHECK_INT_ARG(co_nlocals);
658 CHECK_INT_ARG(co_stacksize);
659 CHECK_INT_ARG(co_flags);
660 CHECK_INT_ARG(co_firstlineno);
661
662#undef CHECK_INT_ARG
663
Steve Dowerc7c01ab2019-11-26 16:27:50 -0800664 if (PySys_Audit("code.__new__", "OOOiiiiii",
665 co_code, co_filename, co_name, co_argcount,
666 co_posonlyargcount, co_kwonlyargcount, co_nlocals,
667 co_stacksize, co_flags) < 0) {
668 return NULL;
669 }
670
Pablo Galindo4a2edc32019-07-01 11:35:05 +0100671 return (PyObject *)PyCode_NewWithPosOnlyArgs(
Victor Stinnera9f05d62019-05-24 23:57:23 +0200672 co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
673 co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
674 co_varnames, co_freevars, co_cellvars, co_filename, co_name,
675 co_firstlineno, (PyObject*)co_lnotab);
676}
677
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200678static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679code_repr(PyCodeObject *co)
680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 int lineno;
682 if (co->co_firstlineno != 0)
683 lineno = co->co_firstlineno;
684 else
685 lineno = -1;
686 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
687 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000688 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 co->co_name, co, co->co_filename, lineno);
690 } else {
691 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000692 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 co->co_name, co, lineno);
694 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695}
696
Victor Stinnerefb24132016-01-22 12:33:12 +0100697PyObject*
698_PyCode_ConstantKey(PyObject *op)
699{
700 PyObject *key;
701
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300702 /* Py_None and Py_Ellipsis are singletons. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100703 if (op == Py_None || op == Py_Ellipsis
704 || PyLong_CheckExact(op)
Victor Stinnerefb24132016-01-22 12:33:12 +0100705 || PyUnicode_CheckExact(op)
706 /* code_richcompare() uses _PyCode_ConstantKey() internally */
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +0300707 || PyCode_Check(op))
708 {
709 /* Objects of these types are always different from object of other
710 * type and from tuples. */
711 Py_INCREF(op);
712 key = op;
713 }
714 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
715 /* Make booleans different from integers 0 and 1.
716 * Avoid BytesWarning from comparing bytes with strings. */
Victor Stinnerefb24132016-01-22 12:33:12 +0100717 key = PyTuple_Pack(2, Py_TYPE(op), op);
718 }
719 else if (PyFloat_CheckExact(op)) {
720 double d = PyFloat_AS_DOUBLE(op);
721 /* all we need is to make the tuple different in either the 0.0
722 * or -0.0 case from all others, just to avoid the "coercion".
723 */
724 if (d == 0.0 && copysign(1.0, d) < 0.0)
725 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
726 else
727 key = PyTuple_Pack(2, Py_TYPE(op), op);
728 }
729 else if (PyComplex_CheckExact(op)) {
730 Py_complex z;
731 int real_negzero, imag_negzero;
732 /* For the complex case we must make complex(x, 0.)
733 different from complex(x, -0.) and complex(0., y)
734 different from complex(-0., y), for any x and y.
735 All four complex zeros must be distinguished.*/
736 z = PyComplex_AsCComplex(op);
737 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
738 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
739 /* use True, False and None singleton as tags for the real and imag
740 * sign, to make tuples different */
741 if (real_negzero && imag_negzero) {
742 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
743 }
744 else if (imag_negzero) {
745 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
746 }
747 else if (real_negzero) {
748 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
749 }
750 else {
751 key = PyTuple_Pack(2, Py_TYPE(op), op);
752 }
753 }
754 else if (PyTuple_CheckExact(op)) {
755 Py_ssize_t i, len;
756 PyObject *tuple;
757
758 len = PyTuple_GET_SIZE(op);
759 tuple = PyTuple_New(len);
760 if (tuple == NULL)
761 return NULL;
762
763 for (i=0; i < len; i++) {
764 PyObject *item, *item_key;
765
766 item = PyTuple_GET_ITEM(op, i);
767 item_key = _PyCode_ConstantKey(item);
768 if (item_key == NULL) {
769 Py_DECREF(tuple);
770 return NULL;
771 }
772
773 PyTuple_SET_ITEM(tuple, i, item_key);
774 }
775
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200776 key = PyTuple_Pack(2, tuple, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100777 Py_DECREF(tuple);
778 }
779 else if (PyFrozenSet_CheckExact(op)) {
780 Py_ssize_t pos = 0;
781 PyObject *item;
782 Py_hash_t hash;
783 Py_ssize_t i, len;
784 PyObject *tuple, *set;
785
786 len = PySet_GET_SIZE(op);
787 tuple = PyTuple_New(len);
788 if (tuple == NULL)
789 return NULL;
790
791 i = 0;
792 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
793 PyObject *item_key;
794
795 item_key = _PyCode_ConstantKey(item);
796 if (item_key == NULL) {
797 Py_DECREF(tuple);
798 return NULL;
799 }
800
801 assert(i < len);
802 PyTuple_SET_ITEM(tuple, i, item_key);
803 i++;
804 }
805 set = PyFrozenSet_New(tuple);
806 Py_DECREF(tuple);
807 if (set == NULL)
808 return NULL;
809
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200810 key = PyTuple_Pack(2, set, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100811 Py_DECREF(set);
812 return key;
813 }
814 else {
Martin Panter6245cb32016-04-15 02:14:19 +0000815 /* for other types, use the object identifier as a unique identifier
Victor Stinnerefb24132016-01-22 12:33:12 +0100816 * to ensure that they are seen as unequal. */
817 PyObject *obj_id = PyLong_FromVoidPtr(op);
818 if (obj_id == NULL)
819 return NULL;
820
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200821 key = PyTuple_Pack(2, obj_id, op);
Victor Stinnerefb24132016-01-22 12:33:12 +0100822 Py_DECREF(obj_id);
823 }
824 return key;
825}
826
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000827static PyObject *
828code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 PyCodeObject *co, *cp;
831 int eq;
Victor Stinnerefb24132016-01-22 12:33:12 +0100832 PyObject *consts1, *consts2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 if ((op != Py_EQ && op != Py_NE) ||
836 !PyCode_Check(self) ||
837 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500838 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 co = (PyCodeObject *)self;
842 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100845 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 eq = co->co_argcount == cp->co_argcount;
847 if (!eq) goto unequal;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100848 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
849 if (!eq) goto unequal;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
851 if (!eq) goto unequal;
852 eq = co->co_nlocals == cp->co_nlocals;
853 if (!eq) goto unequal;
854 eq = co->co_flags == cp->co_flags;
855 if (!eq) goto unequal;
856 eq = co->co_firstlineno == cp->co_firstlineno;
857 if (!eq) goto unequal;
858 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
859 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100860
861 /* compare constants */
862 consts1 = _PyCode_ConstantKey(co->co_consts);
863 if (!consts1)
864 return NULL;
865 consts2 = _PyCode_ConstantKey(cp->co_consts);
866 if (!consts2) {
867 Py_DECREF(consts1);
868 return NULL;
869 }
870 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
871 Py_DECREF(consts1);
872 Py_DECREF(consts2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (eq <= 0) goto unequal;
Victor Stinnerefb24132016-01-22 12:33:12 +0100874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
876 if (eq <= 0) goto unequal;
877 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
878 if (eq <= 0) goto unequal;
879 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
880 if (eq <= 0) goto unequal;
881 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
882 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 if (op == Py_EQ)
885 res = Py_True;
886 else
887 res = Py_False;
888 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000889
890 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 if (eq < 0)
892 return NULL;
893 if (op == Py_NE)
894 res = Py_True;
895 else
896 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000897
898 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 Py_INCREF(res);
900 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901}
902
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000903static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904code_hash(PyCodeObject *co)
905{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000906 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 h0 = PyObject_Hash(co->co_name);
908 if (h0 == -1) return -1;
909 h1 = PyObject_Hash(co->co_code);
910 if (h1 == -1) return -1;
911 h2 = PyObject_Hash(co->co_consts);
912 if (h2 == -1) return -1;
913 h3 = PyObject_Hash(co->co_names);
914 if (h3 == -1) return -1;
915 h4 = PyObject_Hash(co->co_varnames);
916 if (h4 == -1) return -1;
917 h5 = PyObject_Hash(co->co_freevars);
918 if (h5 == -1) return -1;
919 h6 = PyObject_Hash(co->co_cellvars);
920 if (h6 == -1) return -1;
921 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100922 co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 co->co_nlocals ^ co->co_flags;
924 if (h == -1) h = -2;
925 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926}
927
928/* XXX code objects need to participate in GC? */
929
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200930static struct PyMethodDef code_methods[] = {
931 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
Victor Stinnera9f05d62019-05-24 23:57:23 +0200932 CODE_REPLACE_METHODDEF
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200933 {NULL, NULL} /* sentinel */
934};
935
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 PyVarObject_HEAD_INIT(&PyType_Type, 0)
938 "code",
939 sizeof(PyCodeObject),
940 0,
941 (destructor)code_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200942 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 0, /* tp_getattr */
944 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200945 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 (reprfunc)code_repr, /* tp_repr */
947 0, /* tp_as_number */
948 0, /* tp_as_sequence */
949 0, /* tp_as_mapping */
950 (hashfunc)code_hash, /* tp_hash */
951 0, /* tp_call */
952 0, /* tp_str */
953 PyObject_GenericGetAttr, /* tp_getattro */
954 0, /* tp_setattro */
955 0, /* tp_as_buffer */
956 Py_TPFLAGS_DEFAULT, /* tp_flags */
957 code_doc, /* tp_doc */
958 0, /* tp_traverse */
959 0, /* tp_clear */
960 code_richcompare, /* tp_richcompare */
961 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
962 0, /* tp_iter */
963 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200964 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 code_memberlist, /* tp_members */
966 0, /* tp_getset */
967 0, /* tp_base */
968 0, /* tp_dict */
969 0, /* tp_descr_get */
970 0, /* tp_descr_set */
971 0, /* tp_dictoffset */
972 0, /* tp_init */
973 0, /* tp_alloc */
974 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975};
976
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000977/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
978 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979*/
980
981int
982PyCode_Addr2Line(PyCodeObject *co, int addrq)
983{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000984 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
986 int line = co->co_firstlineno;
987 int addr = 0;
988 while (--size >= 0) {
989 addr += *p++;
990 if (addr > addrq)
991 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100992 line += (signed char)*p;
993 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 }
995 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000997
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000998/* Update *bounds to describe the first and one-past-the-last instructions in
999 the same line as lasti. Return the number of that line. */
1000int
1001_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001002{
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001003 Py_ssize_t size;
1004 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
1008 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 addr = 0;
1011 line = co->co_firstlineno;
1012 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 /* possible optimization: if f->f_lasti == instr_ub
1015 (likely to be a common case) then we already know
1016 instr_lb -- if we stored the matching value of p
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001017 somewhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 /* See lnotab_notes.txt for the description of
1020 co_lnotab. A point to remember: increments to p
1021 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 bounds->ap_lower = 0;
1024 while (size > 0) {
1025 if (addr + *p > lasti)
1026 break;
1027 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01001028 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 bounds->ap_lower = addr;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01001030 line += (signed char)*p;
1031 p++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 --size;
1033 }
1034
1035 if (size > 0) {
1036 while (--size >= 0) {
1037 addr += *p++;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01001038 if ((signed char)*p)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 break;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01001040 p++;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001041 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 bounds->ap_upper = addr;
1043 }
1044 else {
1045 bounds->ap_upper = INT_MAX;
1046 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001049}
Brett Cannon5c4de282016-09-07 11:16:41 -07001050
1051
1052int
1053_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
1054{
Brett Cannon5c4de282016-09-07 11:16:41 -07001055 if (!PyCode_Check(code)) {
1056 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -07001057 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001058 }
1059
Brett Cannond0600ed2016-09-07 14:30:39 -07001060 PyCodeObject *o = (PyCodeObject*) code;
1061 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001062
Brett Cannond0600ed2016-09-07 14:30:39 -07001063 if (co_extra == NULL || co_extra->ce_size <= index) {
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001064 *extra = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -07001065 return 0;
1066 }
1067
Brett Cannond0600ed2016-09-07 14:30:39 -07001068 *extra = co_extra->ce_extras[index];
Brett Cannon5c4de282016-09-07 11:16:41 -07001069 return 0;
1070}
1071
1072
1073int
1074_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
1075{
Victor Stinnercaba55b2018-08-03 15:33:52 +02001076 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07001077
1078 if (!PyCode_Check(code) || index < 0 ||
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001079 index >= interp->co_extra_user_count) {
Brett Cannon5c4de282016-09-07 11:16:41 -07001080 PyErr_BadInternalCall();
Brett Cannon3788b852016-09-07 12:51:08 -07001081 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001082 }
1083
Brett Cannond0600ed2016-09-07 14:30:39 -07001084 PyCodeObject *o = (PyCodeObject*) code;
1085 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001086
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001087 if (co_extra == NULL || co_extra->ce_size <= index) {
1088 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
1089 co_extra = PyMem_Realloc(
1090 co_extra,
1091 sizeof(_PyCodeObjectExtra) +
1092 (interp->co_extra_user_count-1) * sizeof(void*));
Brian Coleman6a9122c2017-03-02 10:32:18 +00001093 if (co_extra == NULL) {
Brett Cannon3788b852016-09-07 12:51:08 -07001094 return -1;
Brett Cannon5c4de282016-09-07 11:16:41 -07001095 }
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001096 for (; i < interp->co_extra_user_count; i++) {
Brett Cannond0600ed2016-09-07 14:30:39 -07001097 co_extra->ce_extras[i] = NULL;
Brett Cannon5c4de282016-09-07 11:16:41 -07001098 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001099 co_extra->ce_size = interp->co_extra_user_count;
Serhiy Storchaka378ebb62017-07-04 15:06:16 +03001100 o->co_extra = co_extra;
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001101 }
1102
1103 if (co_extra->ce_extras[index] != NULL) {
1104 freefunc free = interp->co_extra_freefuncs[index];
Dino Viehlandf3cffd22017-06-21 14:44:36 -07001105 if (free != NULL) {
1106 free(co_extra->ce_extras[index]);
1107 }
Brett Cannon5c4de282016-09-07 11:16:41 -07001108 }
1109
Brett Cannond0600ed2016-09-07 14:30:39 -07001110 co_extra->ce_extras[index] = extra;
Brett Cannon5c4de282016-09-07 11:16:41 -07001111 return 0;
1112}