1 #include <stdbool.h>
2
3 #include "Python.h"
4 #include "code.h"
5 #include "opcode.h"
6 #include "structmember.h" // PyMemberDef
7 #include "pycore_code.h"
8 #include "pycore_interp.h" // PyInterpreterState.co_extra_freefuncs
9 #include "pycore_pystate.h" // _PyInterpreterState_GET()
10 #include "pycore_tupleobject.h"
11 #include "clinic/codeobject.c.h"
12
13 /* Holder for co_extra information */
14 typedef struct {
15 Py_ssize_t ce_size;
16 void *ce_extras[1];
17 } _PyCodeObjectExtra;
18
19 /*[clinic input]
20 class code "PyCodeObject *" "&PyCode_Type"
21 [clinic start generated code]*/
22 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
23
24 /* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
25 static int
all_name_chars(PyObject * o)26 all_name_chars(PyObject *o)
27 {
28 const unsigned char *s, *e;
29
30 if (!PyUnicode_IS_ASCII(o))
31 return 0;
32
33 s = PyUnicode_1BYTE_DATA(o);
34 e = s + PyUnicode_GET_LENGTH(o);
35 for (; s != e; s++) {
36 if (!Py_ISALNUM(*s) && *s != '_')
37 return 0;
38 }
39 return 1;
40 }
41
42 static int
intern_strings(PyObject * tuple)43 intern_strings(PyObject *tuple)
44 {
45 Py_ssize_t i;
46
47 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
48 PyObject *v = PyTuple_GET_ITEM(tuple, i);
49 if (v == NULL || !PyUnicode_CheckExact(v)) {
50 PyErr_SetString(PyExc_SystemError,
51 "non-string found in code slot");
52 return -1;
53 }
54 PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
55 }
56 return 0;
57 }
58
59 /* Intern selected string constants */
60 static int
intern_string_constants(PyObject * tuple,int * modified)61 intern_string_constants(PyObject *tuple, int *modified)
62 {
63 for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
64 PyObject *v = PyTuple_GET_ITEM(tuple, i);
65 if (PyUnicode_CheckExact(v)) {
66 if (PyUnicode_READY(v) == -1) {
67 return -1;
68 }
69
70 if (all_name_chars(v)) {
71 PyObject *w = v;
72 PyUnicode_InternInPlace(&v);
73 if (w != v) {
74 PyTuple_SET_ITEM(tuple, i, v);
75 if (modified) {
76 *modified = 1;
77 }
78 }
79 }
80 }
81 else if (PyTuple_CheckExact(v)) {
82 if (intern_string_constants(v, NULL) < 0) {
83 return -1;
84 }
85 }
86 else if (PyFrozenSet_CheckExact(v)) {
87 PyObject *w = v;
88 PyObject *tmp = PySequence_Tuple(v);
89 if (tmp == NULL) {
90 return -1;
91 }
92 int tmp_modified = 0;
93 if (intern_string_constants(tmp, &tmp_modified) < 0) {
94 Py_DECREF(tmp);
95 return -1;
96 }
97 if (tmp_modified) {
98 v = PyFrozenSet_New(tmp);
99 if (v == NULL) {
100 Py_DECREF(tmp);
101 return -1;
102 }
103
104 PyTuple_SET_ITEM(tuple, i, v);
105 Py_DECREF(w);
106 if (modified) {
107 *modified = 1;
108 }
109 }
110 Py_DECREF(tmp);
111 }
112 }
113 return 0;
114 }
115
116 PyCodeObject *
PyCode_NewWithPosOnlyArgs(int argcount,int posonlyargcount,int kwonlyargcount,int nlocals,int stacksize,int flags,PyObject * code,PyObject * consts,PyObject * names,PyObject * varnames,PyObject * freevars,PyObject * cellvars,PyObject * filename,PyObject * name,int firstlineno,PyObject * lnotab)117 PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
118 int nlocals, int stacksize, int flags,
119 PyObject *code, PyObject *consts, PyObject *names,
120 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
121 PyObject *filename, PyObject *name, int firstlineno,
122 PyObject *lnotab)
123 {
124 PyCodeObject *co;
125 Py_ssize_t *cell2arg = NULL;
126 Py_ssize_t i, n_cellvars, n_varnames, total_args;
127
128 /* Check argument types */
129 if (argcount < posonlyargcount || posonlyargcount < 0 ||
130 kwonlyargcount < 0 || nlocals < 0 ||
131 stacksize < 0 || flags < 0 ||
132 code == NULL || !PyBytes_Check(code) ||
133 consts == NULL || !PyTuple_Check(consts) ||
134 names == NULL || !PyTuple_Check(names) ||
135 varnames == NULL || !PyTuple_Check(varnames) ||
136 freevars == NULL || !PyTuple_Check(freevars) ||
137 cellvars == NULL || !PyTuple_Check(cellvars) ||
138 name == NULL || !PyUnicode_Check(name) ||
139 filename == NULL || !PyUnicode_Check(filename) ||
140 lnotab == NULL || !PyBytes_Check(lnotab)) {
141 PyErr_BadInternalCall();
142 return NULL;
143 }
144
145 /* Ensure that strings are ready Unicode string */
146 if (PyUnicode_READY(name) < 0) {
147 return NULL;
148 }
149 if (PyUnicode_READY(filename) < 0) {
150 return NULL;
151 }
152
153 if (intern_strings(names) < 0) {
154 return NULL;
155 }
156 if (intern_strings(varnames) < 0) {
157 return NULL;
158 }
159 if (intern_strings(freevars) < 0) {
160 return NULL;
161 }
162 if (intern_strings(cellvars) < 0) {
163 return NULL;
164 }
165 if (intern_string_constants(consts, NULL) < 0) {
166 return NULL;
167 }
168
169 /* Check for any inner or outer closure references */
170 n_cellvars = PyTuple_GET_SIZE(cellvars);
171 if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) {
172 flags |= CO_NOFREE;
173 } else {
174 flags &= ~CO_NOFREE;
175 }
176
177 n_varnames = PyTuple_GET_SIZE(varnames);
178 if (argcount <= n_varnames && kwonlyargcount <= n_varnames) {
179 /* Never overflows. */
180 total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount +
181 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
182 }
183 else {
184 total_args = n_varnames + 1;
185 }
186 if (total_args > n_varnames) {
187 PyErr_SetString(PyExc_ValueError, "code: varnames is too small");
188 return NULL;
189 }
190
191 /* Create mapping between cells and arguments if needed. */
192 if (n_cellvars) {
193 bool used_cell2arg = false;
194 cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars);
195 if (cell2arg == NULL) {
196 PyErr_NoMemory();
197 return NULL;
198 }
199 /* Find cells which are also arguments. */
200 for (i = 0; i < n_cellvars; i++) {
201 Py_ssize_t j;
202 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
203 cell2arg[i] = CO_CELL_NOT_AN_ARG;
204 for (j = 0; j < total_args; j++) {
205 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
206 int cmp = PyUnicode_Compare(cell, arg);
207 if (cmp == -1 && PyErr_Occurred()) {
208 PyMem_FREE(cell2arg);
209 return NULL;
210 }
211 if (cmp == 0) {
212 cell2arg[i] = j;
213 used_cell2arg = true;
214 break;
215 }
216 }
217 }
218 if (!used_cell2arg) {
219 PyMem_FREE(cell2arg);
220 cell2arg = NULL;
221 }
222 }
223 co = PyObject_New(PyCodeObject, &PyCode_Type);
224 if (co == NULL) {
225 if (cell2arg)
226 PyMem_FREE(cell2arg);
227 return NULL;
228 }
229 co->co_argcount = argcount;
230 co->co_posonlyargcount = posonlyargcount;
231 co->co_kwonlyargcount = kwonlyargcount;
232 co->co_nlocals = nlocals;
233 co->co_stacksize = stacksize;
234 co->co_flags = flags;
235 Py_INCREF(code);
236 co->co_code = code;
237 Py_INCREF(consts);
238 co->co_consts = consts;
239 Py_INCREF(names);
240 co->co_names = names;
241 Py_INCREF(varnames);
242 co->co_varnames = varnames;
243 Py_INCREF(freevars);
244 co->co_freevars = freevars;
245 Py_INCREF(cellvars);
246 co->co_cellvars = cellvars;
247 co->co_cell2arg = cell2arg;
248 Py_INCREF(filename);
249 co->co_filename = filename;
250 Py_INCREF(name);
251 co->co_name = name;
252 co->co_firstlineno = firstlineno;
253 Py_INCREF(lnotab);
254 co->co_lnotab = lnotab;
255 co->co_zombieframe = NULL;
256 co->co_weakreflist = NULL;
257 co->co_extra = NULL;
258
259 co->co_opcache_map = NULL;
260 co->co_opcache = NULL;
261 co->co_opcache_flag = 0;
262 co->co_opcache_size = 0;
263 return co;
264 }
265
266 PyCodeObject *
PyCode_New(int argcount,int kwonlyargcount,int nlocals,int stacksize,int flags,PyObject * code,PyObject * consts,PyObject * names,PyObject * varnames,PyObject * freevars,PyObject * cellvars,PyObject * filename,PyObject * name,int firstlineno,PyObject * lnotab)267 PyCode_New(int argcount, int kwonlyargcount,
268 int nlocals, int stacksize, int flags,
269 PyObject *code, PyObject *consts, PyObject *names,
270 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
271 PyObject *filename, PyObject *name, int firstlineno,
272 PyObject *lnotab)
273 {
274 return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
275 stacksize, flags, code, consts, names,
276 varnames, freevars, cellvars, filename,
277 name, firstlineno, lnotab);
278 }
279
280 int
_PyCode_InitOpcache(PyCodeObject * co)281 _PyCode_InitOpcache(PyCodeObject *co)
282 {
283 Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);
284 co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);
285 if (co->co_opcache_map == NULL) {
286 return -1;
287 }
288
289 _Py_CODEUNIT *opcodes = (_Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);
290 Py_ssize_t opts = 0;
291
292 for (Py_ssize_t i = 0; i < co_size;) {
293 unsigned char opcode = _Py_OPCODE(opcodes[i]);
294 i++; // 'i' is now aligned to (next_instr - first_instr)
295
296 // TODO: LOAD_METHOD, LOAD_ATTR
297 if (opcode == LOAD_GLOBAL) {
298 opts++;
299 co->co_opcache_map[i] = (unsigned char)opts;
300 if (opts > 254) {
301 break;
302 }
303 }
304 }
305
306 if (opts) {
307 co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));
308 if (co->co_opcache == NULL) {
309 PyMem_FREE(co->co_opcache_map);
310 return -1;
311 }
312 }
313 else {
314 PyMem_FREE(co->co_opcache_map);
315 co->co_opcache_map = NULL;
316 co->co_opcache = NULL;
317 }
318
319 co->co_opcache_size = (unsigned char)opts;
320 return 0;
321 }
322
323 PyCodeObject *
PyCode_NewEmpty(const char * filename,const char * funcname,int firstlineno)324 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
325 {
326 static PyObject *emptystring = NULL;
327 static PyObject *nulltuple = NULL;
328 PyObject *filename_ob = NULL;
329 PyObject *funcname_ob = NULL;
330 PyCodeObject *result = NULL;
331 if (emptystring == NULL) {
332 emptystring = PyBytes_FromString("");
333 if (emptystring == NULL)
334 goto failed;
335 }
336 if (nulltuple == NULL) {
337 nulltuple = PyTuple_New(0);
338 if (nulltuple == NULL)
339 goto failed;
340 }
341 funcname_ob = PyUnicode_FromString(funcname);
342 if (funcname_ob == NULL)
343 goto failed;
344 filename_ob = PyUnicode_DecodeFSDefault(filename);
345 if (filename_ob == NULL)
346 goto failed;
347
348 result = PyCode_NewWithPosOnlyArgs(
349 0, /* argcount */
350 0, /* posonlyargcount */
351 0, /* kwonlyargcount */
352 0, /* nlocals */
353 0, /* stacksize */
354 0, /* flags */
355 emptystring, /* code */
356 nulltuple, /* consts */
357 nulltuple, /* names */
358 nulltuple, /* varnames */
359 nulltuple, /* freevars */
360 nulltuple, /* cellvars */
361 filename_ob, /* filename */
362 funcname_ob, /* name */
363 firstlineno, /* firstlineno */
364 emptystring /* lnotab */
365 );
366
367 failed:
368 Py_XDECREF(funcname_ob);
369 Py_XDECREF(filename_ob);
370 return result;
371 }
372
373 #define OFF(x) offsetof(PyCodeObject, x)
374
375 static PyMemberDef code_memberlist[] = {
376 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
377 {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
378 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
379 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
380 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
381 {"co_flags", T_INT, OFF(co_flags), READONLY},
382 {"co_code", T_OBJECT, OFF(co_code), READONLY},
383 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
384 {"co_names", T_OBJECT, OFF(co_names), READONLY},
385 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
386 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
387 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
388 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
389 {"co_name", T_OBJECT, OFF(co_name), READONLY},
390 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
391 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
392 {NULL} /* Sentinel */
393 };
394
395 /* Helper for code_new: return a shallow copy of a tuple that is
396 guaranteed to contain exact strings, by converting string subclasses
397 to exact strings and complaining if a non-string is found. */
398 static PyObject*
validate_and_copy_tuple(PyObject * tup)399 validate_and_copy_tuple(PyObject *tup)
400 {
401 PyObject *newtuple;
402 PyObject *item;
403 Py_ssize_t i, len;
404
405 len = PyTuple_GET_SIZE(tup);
406 newtuple = PyTuple_New(len);
407 if (newtuple == NULL)
408 return NULL;
409
410 for (i = 0; i < len; i++) {
411 item = PyTuple_GET_ITEM(tup, i);
412 if (PyUnicode_CheckExact(item)) {
413 Py_INCREF(item);
414 }
415 else if (!PyUnicode_Check(item)) {
416 PyErr_Format(
417 PyExc_TypeError,
418 "name tuples must contain only "
419 "strings, not '%.500s'",
420 Py_TYPE(item)->tp_name);
421 Py_DECREF(newtuple);
422 return NULL;
423 }
424 else {
425 item = _PyUnicode_Copy(item);
426 if (item == NULL) {
427 Py_DECREF(newtuple);
428 return NULL;
429 }
430 }
431 PyTuple_SET_ITEM(newtuple, i, item);
432 }
433
434 return newtuple;
435 }
436
437 PyDoc_STRVAR(code_doc,
438 "code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n\
439 flags, codestring, constants, names, varnames, filename, name,\n\
440 firstlineno, lnotab[, freevars[, cellvars]])\n\
441 \n\
442 Create a code object. Not for the faint of heart.");
443
444 static PyObject *
code_new(PyTypeObject * type,PyObject * args,PyObject * kw)445 code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
446 {
447 int argcount;
448 int posonlyargcount;
449 int kwonlyargcount;
450 int nlocals;
451 int stacksize;
452 int flags;
453 PyObject *co = NULL;
454 PyObject *code;
455 PyObject *consts;
456 PyObject *names, *ournames = NULL;
457 PyObject *varnames, *ourvarnames = NULL;
458 PyObject *freevars = NULL, *ourfreevars = NULL;
459 PyObject *cellvars = NULL, *ourcellvars = NULL;
460 PyObject *filename;
461 PyObject *name;
462 int firstlineno;
463 PyObject *lnotab;
464
465 if (!PyArg_ParseTuple(args, "iiiiiiSO!O!O!UUiS|O!O!:code",
466 &argcount, &posonlyargcount, &kwonlyargcount,
467 &nlocals, &stacksize, &flags,
468 &code,
469 &PyTuple_Type, &consts,
470 &PyTuple_Type, &names,
471 &PyTuple_Type, &varnames,
472 &filename, &name,
473 &firstlineno, &lnotab,
474 &PyTuple_Type, &freevars,
475 &PyTuple_Type, &cellvars))
476 return NULL;
477
478 if (PySys_Audit("code.__new__", "OOOiiiiii",
479 code, filename, name, argcount, posonlyargcount,
480 kwonlyargcount, nlocals, stacksize, flags) < 0) {
481 goto cleanup;
482 }
483
484 if (argcount < 0) {
485 PyErr_SetString(
486 PyExc_ValueError,
487 "code: argcount must not be negative");
488 goto cleanup;
489 }
490
491 if (posonlyargcount < 0) {
492 PyErr_SetString(
493 PyExc_ValueError,
494 "code: posonlyargcount must not be negative");
495 goto cleanup;
496 }
497
498 if (kwonlyargcount < 0) {
499 PyErr_SetString(
500 PyExc_ValueError,
501 "code: kwonlyargcount must not be negative");
502 goto cleanup;
503 }
504 if (nlocals < 0) {
505 PyErr_SetString(
506 PyExc_ValueError,
507 "code: nlocals must not be negative");
508 goto cleanup;
509 }
510
511 ournames = validate_and_copy_tuple(names);
512 if (ournames == NULL)
513 goto cleanup;
514 ourvarnames = validate_and_copy_tuple(varnames);
515 if (ourvarnames == NULL)
516 goto cleanup;
517 if (freevars)
518 ourfreevars = validate_and_copy_tuple(freevars);
519 else
520 ourfreevars = PyTuple_New(0);
521 if (ourfreevars == NULL)
522 goto cleanup;
523 if (cellvars)
524 ourcellvars = validate_and_copy_tuple(cellvars);
525 else
526 ourcellvars = PyTuple_New(0);
527 if (ourcellvars == NULL)
528 goto cleanup;
529
530 co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
531 kwonlyargcount,
532 nlocals, stacksize, flags,
533 code, consts, ournames,
534 ourvarnames, ourfreevars,
535 ourcellvars, filename,
536 name, firstlineno, lnotab);
537 cleanup:
538 Py_XDECREF(ournames);
539 Py_XDECREF(ourvarnames);
540 Py_XDECREF(ourfreevars);
541 Py_XDECREF(ourcellvars);
542 return co;
543 }
544
545 static void
code_dealloc(PyCodeObject * co)546 code_dealloc(PyCodeObject *co)
547 {
548 if (co->co_opcache != NULL) {
549 PyMem_FREE(co->co_opcache);
550 }
551 if (co->co_opcache_map != NULL) {
552 PyMem_FREE(co->co_opcache_map);
553 }
554 co->co_opcache_flag = 0;
555 co->co_opcache_size = 0;
556
557 if (co->co_extra != NULL) {
558 PyInterpreterState *interp = _PyInterpreterState_GET();
559 _PyCodeObjectExtra *co_extra = co->co_extra;
560
561 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
562 freefunc free_extra = interp->co_extra_freefuncs[i];
563
564 if (free_extra != NULL) {
565 free_extra(co_extra->ce_extras[i]);
566 }
567 }
568
569 PyMem_Free(co_extra);
570 }
571
572 Py_XDECREF(co->co_code);
573 Py_XDECREF(co->co_consts);
574 Py_XDECREF(co->co_names);
575 Py_XDECREF(co->co_varnames);
576 Py_XDECREF(co->co_freevars);
577 Py_XDECREF(co->co_cellvars);
578 Py_XDECREF(co->co_filename);
579 Py_XDECREF(co->co_name);
580 Py_XDECREF(co->co_lnotab);
581 if (co->co_cell2arg != NULL)
582 PyMem_FREE(co->co_cell2arg);
583 if (co->co_zombieframe != NULL)
584 PyObject_GC_Del(co->co_zombieframe);
585 if (co->co_weakreflist != NULL)
586 PyObject_ClearWeakRefs((PyObject*)co);
587 PyObject_DEL(co);
588 }
589
590 static PyObject *
code_sizeof(PyCodeObject * co,PyObject * Py_UNUSED (args))591 code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
592 {
593 Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
594 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
595
596 if (co->co_cell2arg != NULL && co->co_cellvars != NULL) {
597 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
598 }
599 if (co_extra != NULL) {
600 res += sizeof(_PyCodeObjectExtra) +
601 (co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
602 }
603 if (co->co_opcache != NULL) {
604 assert(co->co_opcache_map != NULL);
605 // co_opcache_map
606 res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);
607 // co_opcache
608 res += co->co_opcache_size * sizeof(_PyOpcache);
609 }
610 return PyLong_FromSsize_t(res);
611 }
612
613 /*[clinic input]
614 code.replace
615
616 *
617 co_argcount: int(c_default="self->co_argcount") = -1
618 co_posonlyargcount: int(c_default="self->co_posonlyargcount") = -1
619 co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = -1
620 co_nlocals: int(c_default="self->co_nlocals") = -1
621 co_stacksize: int(c_default="self->co_stacksize") = -1
622 co_flags: int(c_default="self->co_flags") = -1
623 co_firstlineno: int(c_default="self->co_firstlineno") = -1
624 co_code: PyBytesObject(c_default="(PyBytesObject *)self->co_code") = None
625 co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = None
626 co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = None
627 co_varnames: object(subclass_of="&PyTuple_Type", c_default="self->co_varnames") = None
628 co_freevars: object(subclass_of="&PyTuple_Type", c_default="self->co_freevars") = None
629 co_cellvars: object(subclass_of="&PyTuple_Type", c_default="self->co_cellvars") = None
630 co_filename: unicode(c_default="self->co_filename") = None
631 co_name: unicode(c_default="self->co_name") = None
632 co_lnotab: PyBytesObject(c_default="(PyBytesObject *)self->co_lnotab") = None
633
634 Return a copy of the code object with new values for the specified fields.
635 [clinic start generated code]*/
636
637 static PyObject *
code_replace_impl(PyCodeObject * self,int co_argcount,int co_posonlyargcount,int co_kwonlyargcount,int co_nlocals,int co_stacksize,int co_flags,int co_firstlineno,PyBytesObject * co_code,PyObject * co_consts,PyObject * co_names,PyObject * co_varnames,PyObject * co_freevars,PyObject * co_cellvars,PyObject * co_filename,PyObject * co_name,PyBytesObject * co_lnotab)638 code_replace_impl(PyCodeObject *self, int co_argcount,
639 int co_posonlyargcount, int co_kwonlyargcount,
640 int co_nlocals, int co_stacksize, int co_flags,
641 int co_firstlineno, PyBytesObject *co_code,
642 PyObject *co_consts, PyObject *co_names,
643 PyObject *co_varnames, PyObject *co_freevars,
644 PyObject *co_cellvars, PyObject *co_filename,
645 PyObject *co_name, PyBytesObject *co_lnotab)
646 /*[clinic end generated code: output=25c8e303913bcace input=d9051bc8f24e6b28]*/
647 {
648 #define CHECK_INT_ARG(ARG) \
649 if (ARG < 0) { \
650 PyErr_SetString(PyExc_ValueError, \
651 #ARG " must be a positive integer"); \
652 return NULL; \
653 }
654
655 CHECK_INT_ARG(co_argcount);
656 CHECK_INT_ARG(co_posonlyargcount);
657 CHECK_INT_ARG(co_kwonlyargcount);
658 CHECK_INT_ARG(co_nlocals);
659 CHECK_INT_ARG(co_stacksize);
660 CHECK_INT_ARG(co_flags);
661 CHECK_INT_ARG(co_firstlineno);
662
663 #undef CHECK_INT_ARG
664
665 if (PySys_Audit("code.__new__", "OOOiiiiii",
666 co_code, co_filename, co_name, co_argcount,
667 co_posonlyargcount, co_kwonlyargcount, co_nlocals,
668 co_stacksize, co_flags) < 0) {
669 return NULL;
670 }
671
672 return (PyObject *)PyCode_NewWithPosOnlyArgs(
673 co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
674 co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
675 co_varnames, co_freevars, co_cellvars, co_filename, co_name,
676 co_firstlineno, (PyObject*)co_lnotab);
677 }
678
679 static PyObject *
code_repr(PyCodeObject * co)680 code_repr(PyCodeObject *co)
681 {
682 int lineno;
683 if (co->co_firstlineno != 0)
684 lineno = co->co_firstlineno;
685 else
686 lineno = -1;
687 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
688 return PyUnicode_FromFormat(
689 "<code object %U at %p, file \"%U\", line %d>",
690 co->co_name, co, co->co_filename, lineno);
691 } else {
692 return PyUnicode_FromFormat(
693 "<code object %U at %p, file ???, line %d>",
694 co->co_name, co, lineno);
695 }
696 }
697
698 PyObject*
_PyCode_ConstantKey(PyObject * op)699 _PyCode_ConstantKey(PyObject *op)
700 {
701 PyObject *key;
702
703 /* Py_None and Py_Ellipsis are singletons. */
704 if (op == Py_None || op == Py_Ellipsis
705 || PyLong_CheckExact(op)
706 || PyUnicode_CheckExact(op)
707 /* code_richcompare() uses _PyCode_ConstantKey() internally */
708 || PyCode_Check(op))
709 {
710 /* Objects of these types are always different from object of other
711 * type and from tuples. */
712 Py_INCREF(op);
713 key = op;
714 }
715 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
716 /* Make booleans different from integers 0 and 1.
717 * Avoid BytesWarning from comparing bytes with strings. */
718 key = PyTuple_Pack(2, Py_TYPE(op), op);
719 }
720 else if (PyFloat_CheckExact(op)) {
721 double d = PyFloat_AS_DOUBLE(op);
722 /* all we need is to make the tuple different in either the 0.0
723 * or -0.0 case from all others, just to avoid the "coercion".
724 */
725 if (d == 0.0 && copysign(1.0, d) < 0.0)
726 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
727 else
728 key = PyTuple_Pack(2, Py_TYPE(op), op);
729 }
730 else if (PyComplex_CheckExact(op)) {
731 Py_complex z;
732 int real_negzero, imag_negzero;
733 /* For the complex case we must make complex(x, 0.)
734 different from complex(x, -0.) and complex(0., y)
735 different from complex(-0., y), for any x and y.
736 All four complex zeros must be distinguished.*/
737 z = PyComplex_AsCComplex(op);
738 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
739 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
740 /* use True, False and None singleton as tags for the real and imag
741 * sign, to make tuples different */
742 if (real_negzero && imag_negzero) {
743 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
744 }
745 else if (imag_negzero) {
746 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
747 }
748 else if (real_negzero) {
749 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
750 }
751 else {
752 key = PyTuple_Pack(2, Py_TYPE(op), op);
753 }
754 }
755 else if (PyTuple_CheckExact(op)) {
756 Py_ssize_t i, len;
757 PyObject *tuple;
758
759 len = PyTuple_GET_SIZE(op);
760 tuple = PyTuple_New(len);
761 if (tuple == NULL)
762 return NULL;
763
764 for (i=0; i < len; i++) {
765 PyObject *item, *item_key;
766
767 item = PyTuple_GET_ITEM(op, i);
768 item_key = _PyCode_ConstantKey(item);
769 if (item_key == NULL) {
770 Py_DECREF(tuple);
771 return NULL;
772 }
773
774 PyTuple_SET_ITEM(tuple, i, item_key);
775 }
776
777 key = PyTuple_Pack(2, tuple, op);
778 Py_DECREF(tuple);
779 }
780 else if (PyFrozenSet_CheckExact(op)) {
781 Py_ssize_t pos = 0;
782 PyObject *item;
783 Py_hash_t hash;
784 Py_ssize_t i, len;
785 PyObject *tuple, *set;
786
787 len = PySet_GET_SIZE(op);
788 tuple = PyTuple_New(len);
789 if (tuple == NULL)
790 return NULL;
791
792 i = 0;
793 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
794 PyObject *item_key;
795
796 item_key = _PyCode_ConstantKey(item);
797 if (item_key == NULL) {
798 Py_DECREF(tuple);
799 return NULL;
800 }
801
802 assert(i < len);
803 PyTuple_SET_ITEM(tuple, i, item_key);
804 i++;
805 }
806 set = PyFrozenSet_New(tuple);
807 Py_DECREF(tuple);
808 if (set == NULL)
809 return NULL;
810
811 key = PyTuple_Pack(2, set, op);
812 Py_DECREF(set);
813 return key;
814 }
815 else {
816 /* for other types, use the object identifier as a unique identifier
817 * to ensure that they are seen as unequal. */
818 PyObject *obj_id = PyLong_FromVoidPtr(op);
819 if (obj_id == NULL)
820 return NULL;
821
822 key = PyTuple_Pack(2, obj_id, op);
823 Py_DECREF(obj_id);
824 }
825 return key;
826 }
827
828 static PyObject *
code_richcompare(PyObject * self,PyObject * other,int op)829 code_richcompare(PyObject *self, PyObject *other, int op)
830 {
831 PyCodeObject *co, *cp;
832 int eq;
833 PyObject *consts1, *consts2;
834 PyObject *res;
835
836 if ((op != Py_EQ && op != Py_NE) ||
837 !PyCode_Check(self) ||
838 !PyCode_Check(other)) {
839 Py_RETURN_NOTIMPLEMENTED;
840 }
841
842 co = (PyCodeObject *)self;
843 cp = (PyCodeObject *)other;
844
845 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
846 if (!eq) goto unequal;
847 eq = co->co_argcount == cp->co_argcount;
848 if (!eq) goto unequal;
849 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
850 if (!eq) goto unequal;
851 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
852 if (!eq) goto unequal;
853 eq = co->co_nlocals == cp->co_nlocals;
854 if (!eq) goto unequal;
855 eq = co->co_flags == cp->co_flags;
856 if (!eq) goto unequal;
857 eq = co->co_firstlineno == cp->co_firstlineno;
858 if (!eq) goto unequal;
859 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
860 if (eq <= 0) goto unequal;
861
862 /* compare constants */
863 consts1 = _PyCode_ConstantKey(co->co_consts);
864 if (!consts1)
865 return NULL;
866 consts2 = _PyCode_ConstantKey(cp->co_consts);
867 if (!consts2) {
868 Py_DECREF(consts1);
869 return NULL;
870 }
871 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
872 Py_DECREF(consts1);
873 Py_DECREF(consts2);
874 if (eq <= 0) goto unequal;
875
876 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
877 if (eq <= 0) goto unequal;
878 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
879 if (eq <= 0) goto unequal;
880 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
881 if (eq <= 0) goto unequal;
882 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
883 if (eq <= 0) goto unequal;
884
885 if (op == Py_EQ)
886 res = Py_True;
887 else
888 res = Py_False;
889 goto done;
890
891 unequal:
892 if (eq < 0)
893 return NULL;
894 if (op == Py_NE)
895 res = Py_True;
896 else
897 res = Py_False;
898
899 done:
900 Py_INCREF(res);
901 return res;
902 }
903
904 static Py_hash_t
code_hash(PyCodeObject * co)905 code_hash(PyCodeObject *co)
906 {
907 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
908 h0 = PyObject_Hash(co->co_name);
909 if (h0 == -1) return -1;
910 h1 = PyObject_Hash(co->co_code);
911 if (h1 == -1) return -1;
912 h2 = PyObject_Hash(co->co_consts);
913 if (h2 == -1) return -1;
914 h3 = PyObject_Hash(co->co_names);
915 if (h3 == -1) return -1;
916 h4 = PyObject_Hash(co->co_varnames);
917 if (h4 == -1) return -1;
918 h5 = PyObject_Hash(co->co_freevars);
919 if (h5 == -1) return -1;
920 h6 = PyObject_Hash(co->co_cellvars);
921 if (h6 == -1) return -1;
922 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
923 co->co_argcount ^ co->co_posonlyargcount ^ co->co_kwonlyargcount ^
924 co->co_nlocals ^ co->co_flags;
925 if (h == -1) h = -2;
926 return h;
927 }
928
929 /* XXX code objects need to participate in GC? */
930
931 static struct PyMethodDef code_methods[] = {
932 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
933 CODE_REPLACE_METHODDEF
934 {NULL, NULL} /* sentinel */
935 };
936
937 PyTypeObject PyCode_Type = {
938 PyVarObject_HEAD_INIT(&PyType_Type, 0)
939 "code",
940 sizeof(PyCodeObject),
941 0,
942 (destructor)code_dealloc, /* tp_dealloc */
943 0, /* tp_vectorcall_offset */
944 0, /* tp_getattr */
945 0, /* tp_setattr */
946 0, /* tp_as_async */
947 (reprfunc)code_repr, /* tp_repr */
948 0, /* tp_as_number */
949 0, /* tp_as_sequence */
950 0, /* tp_as_mapping */
951 (hashfunc)code_hash, /* tp_hash */
952 0, /* tp_call */
953 0, /* tp_str */
954 PyObject_GenericGetAttr, /* tp_getattro */
955 0, /* tp_setattro */
956 0, /* tp_as_buffer */
957 Py_TPFLAGS_DEFAULT, /* tp_flags */
958 code_doc, /* tp_doc */
959 0, /* tp_traverse */
960 0, /* tp_clear */
961 code_richcompare, /* tp_richcompare */
962 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
963 0, /* tp_iter */
964 0, /* tp_iternext */
965 code_methods, /* tp_methods */
966 code_memberlist, /* tp_members */
967 0, /* tp_getset */
968 0, /* tp_base */
969 0, /* tp_dict */
970 0, /* tp_descr_get */
971 0, /* tp_descr_set */
972 0, /* tp_dictoffset */
973 0, /* tp_init */
974 0, /* tp_alloc */
975 code_new, /* tp_new */
976 };
977
978 /* Use co_lnotab to compute the line number from a bytecode index, addrq. See
979 lnotab_notes.txt for the details of the lnotab representation.
980 */
981
982 int
PyCode_Addr2Line(PyCodeObject * co,int addrq)983 PyCode_Addr2Line(PyCodeObject *co, int addrq)
984 {
985 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
986 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
987 int line = co->co_firstlineno;
988 int addr = 0;
989 while (--size >= 0) {
990 addr += *p++;
991 if (addr > addrq)
992 break;
993 line += (signed char)*p;
994 p++;
995 }
996 return line;
997 }
998
999 /* Update *bounds to describe the first and one-past-the-last instructions in
1000 the same line as lasti. Return the number of that line. */
1001 int
_PyCode_CheckLineNumber(PyCodeObject * co,int lasti,PyAddrPair * bounds)1002 _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
1003 {
1004 Py_ssize_t size;
1005 int addr, line;
1006 unsigned char* p;
1007
1008 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
1009 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
1010
1011 addr = 0;
1012 line = co->co_firstlineno;
1013 assert(line > 0);
1014
1015 /* possible optimization: if f->f_lasti == instr_ub
1016 (likely to be a common case) then we already know
1017 instr_lb -- if we stored the matching value of p
1018 somewhere we could skip the first while loop. */
1019
1020 /* See lnotab_notes.txt for the description of
1021 co_lnotab. A point to remember: increments to p
1022 come in (addr, line) pairs. */
1023
1024 bounds->ap_lower = 0;
1025 while (size > 0) {
1026 if (addr + *p > lasti)
1027 break;
1028 addr += *p++;
1029 if ((signed char)*p)
1030 bounds->ap_lower = addr;
1031 line += (signed char)*p;
1032 p++;
1033 --size;
1034 }
1035
1036 if (size > 0) {
1037 while (--size >= 0) {
1038 addr += *p++;
1039 if ((signed char)*p)
1040 break;
1041 p++;
1042 }
1043 bounds->ap_upper = addr;
1044 }
1045 else {
1046 bounds->ap_upper = INT_MAX;
1047 }
1048
1049 return line;
1050 }
1051
1052
1053 int
_PyCode_GetExtra(PyObject * code,Py_ssize_t index,void ** extra)1054 _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
1055 {
1056 if (!PyCode_Check(code)) {
1057 PyErr_BadInternalCall();
1058 return -1;
1059 }
1060
1061 PyCodeObject *o = (PyCodeObject*) code;
1062 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
1063
1064 if (co_extra == NULL || co_extra->ce_size <= index) {
1065 *extra = NULL;
1066 return 0;
1067 }
1068
1069 *extra = co_extra->ce_extras[index];
1070 return 0;
1071 }
1072
1073
1074 int
_PyCode_SetExtra(PyObject * code,Py_ssize_t index,void * extra)1075 _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
1076 {
1077 PyInterpreterState *interp = _PyInterpreterState_GET();
1078
1079 if (!PyCode_Check(code) || index < 0 ||
1080 index >= interp->co_extra_user_count) {
1081 PyErr_BadInternalCall();
1082 return -1;
1083 }
1084
1085 PyCodeObject *o = (PyCodeObject*) code;
1086 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
1087
1088 if (co_extra == NULL || co_extra->ce_size <= index) {
1089 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
1090 co_extra = PyMem_Realloc(
1091 co_extra,
1092 sizeof(_PyCodeObjectExtra) +
1093 (interp->co_extra_user_count-1) * sizeof(void*));
1094 if (co_extra == NULL) {
1095 return -1;
1096 }
1097 for (; i < interp->co_extra_user_count; i++) {
1098 co_extra->ce_extras[i] = NULL;
1099 }
1100 co_extra->ce_size = interp->co_extra_user_count;
1101 o->co_extra = co_extra;
1102 }
1103
1104 if (co_extra->ce_extras[index] != NULL) {
1105 freefunc free = interp->co_extra_freefuncs[index];
1106 if (free != NULL) {
1107 free(co_extra->ce_extras[index]);
1108 }
1109 }
1110
1111 co_extra->ce_extras[index] = extra;
1112 return 0;
1113 }
1114