1 #include <stdbool.h>
2
3 #include "Python.h"
4 #include "opcode.h"
5 #include "structmember.h" // PyMemberDef
6 #include "pycore_code.h" // _PyCodeConstructor
7 #include "pycore_frame.h" // FRAME_SPECIALS_SIZE
8 #include "pycore_interp.h" // PyInterpreterState.co_extra_freefuncs
9 #include "pycore_opcode.h" // _PyOpcode_Deopt
10 #include "pycore_pystate.h" // _PyInterpreterState_GET()
11 #include "pycore_tuple.h" // _PyTuple_ITEMS()
12 #include "clinic/codeobject.c.h"
13
14 static PyObject* code_repr(PyCodeObject *co);
15
16 static const char *
17 code_event_name(PyCodeEvent event) {
18 switch (event) {
19 #define CASE(op) \
20 case PY_CODE_EVENT_##op: \
21 return "PY_CODE_EVENT_" #op;
22 PY_FOREACH_CODE_EVENT(CASE)
23 #undef CASE
24 }
25 Py_UNREACHABLE();
26 }
27
28 static void
29 notify_code_watchers(PyCodeEvent event, PyCodeObject *co)
30 {
31 assert(Py_REFCNT(co) > 0);
32 PyInterpreterState *interp = _PyInterpreterState_GET();
33 assert(interp->_initialized);
34 uint8_t bits = interp->active_code_watchers;
35 int i = 0;
36 while (bits) {
37 assert(i < CODE_MAX_WATCHERS);
38 if (bits & 1) {
39 PyCode_WatchCallback cb = interp->code_watchers[i];
40 // callback must be non-null if the watcher bit is set
41 assert(cb != NULL);
42 if (cb(event, co) < 0) {
43 // Don't risk resurrecting the object if an unraisablehook keeps
44 // a reference; pass a string as context.
45 PyObject *context = NULL;
46 PyObject *repr = code_repr(co);
47 if (repr) {
48 context = PyUnicode_FromFormat(
49 "%s watcher callback for %U",
50 code_event_name(event), repr);
51 Py_DECREF(repr);
52 }
53 if (context == NULL) {
54 context = Py_NewRef(Py_None);
55 }
56 PyErr_WriteUnraisable(context);
57 Py_DECREF(context);
58 }
59 }
60 i++;
61 bits >>= 1;
62 }
63 }
64
65 int
66 PyCode_AddWatcher(PyCode_WatchCallback callback)
67 {
68 PyInterpreterState *interp = _PyInterpreterState_GET();
69 assert(interp->_initialized);
70
71 for (int i = 0; i < CODE_MAX_WATCHERS; i++) {
72 if (!interp->code_watchers[i]) {
73 interp->code_watchers[i] = callback;
74 interp->active_code_watchers |= (1 << i);
75 return i;
76 }
77 }
78
79 PyErr_SetString(PyExc_RuntimeError, "no more code watcher IDs available");
80 return -1;
81 }
82
83 static inline int
84 validate_watcher_id(PyInterpreterState *interp, int watcher_id)
85 {
86 if (watcher_id < 0 || watcher_id >= CODE_MAX_WATCHERS) {
87 PyErr_Format(PyExc_ValueError, "Invalid code watcher ID %d", watcher_id);
88 return -1;
89 }
90 if (!interp->code_watchers[watcher_id]) {
91 PyErr_Format(PyExc_ValueError, "No code watcher set for ID %d", watcher_id);
92 return -1;
93 }
94 return 0;
95 }
96
97 int
98 PyCode_ClearWatcher(int watcher_id)
99 {
100 PyInterpreterState *interp = _PyInterpreterState_GET();
101 assert(interp->_initialized);
102 if (validate_watcher_id(interp, watcher_id) < 0) {
103 return -1;
104 }
105 interp->code_watchers[watcher_id] = NULL;
106 interp->active_code_watchers &= ~(1 << watcher_id);
107 return 0;
108 }
109
110 /******************
111 * generic helpers
112 ******************/
113
114 /* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
115 static int
116 all_name_chars(PyObject *o)
117 {
118 const unsigned char *s, *e;
119
120 if (!PyUnicode_IS_ASCII(o))
121 return 0;
122
123 s = PyUnicode_1BYTE_DATA(o);
124 e = s + PyUnicode_GET_LENGTH(o);
125 for (; s != e; s++) {
126 if (!Py_ISALNUM(*s) && *s != '_')
127 return 0;
128 }
129 return 1;
130 }
131
132 static int
133 intern_strings(PyObject *tuple)
134 {
135 Py_ssize_t i;
136
137 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
138 PyObject *v = PyTuple_GET_ITEM(tuple, i);
139 if (v == NULL || !PyUnicode_CheckExact(v)) {
140 PyErr_SetString(PyExc_SystemError,
141 "non-string found in code slot");
142 return -1;
143 }
144 PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
145 }
146 return 0;
147 }
148
149 /* Intern selected string constants */
150 static int
151 intern_string_constants(PyObject *tuple, int *modified)
152 {
153 for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
154 PyObject *v = PyTuple_GET_ITEM(tuple, i);
155 if (PyUnicode_CheckExact(v)) {
156 if (PyUnicode_READY(v) == -1) {
157 return -1;
158 }
159
160 if (all_name_chars(v)) {
161 PyObject *w = v;
162 PyUnicode_InternInPlace(&v);
163 if (w != v) {
164 PyTuple_SET_ITEM(tuple, i, v);
165 if (modified) {
166 *modified = 1;
167 }
168 }
169 }
170 }
171 else if (PyTuple_CheckExact(v)) {
172 if (intern_string_constants(v, NULL) < 0) {
173 return -1;
174 }
175 }
176 else if (PyFrozenSet_CheckExact(v)) {
177 PyObject *w = v;
178 PyObject *tmp = PySequence_Tuple(v);
179 if (tmp == NULL) {
180 return -1;
181 }
182 int tmp_modified = 0;
183 if (intern_string_constants(tmp, &tmp_modified) < 0) {
184 Py_DECREF(tmp);
185 return -1;
186 }
187 if (tmp_modified) {
188 v = PyFrozenSet_New(tmp);
189 if (v == NULL) {
190 Py_DECREF(tmp);
191 return -1;
192 }
193
194 PyTuple_SET_ITEM(tuple, i, v);
195 Py_DECREF(w);
196 if (modified) {
197 *modified = 1;
198 }
199 }
200 Py_DECREF(tmp);
201 }
202 }
203 return 0;
204 }
205
206 /* Return a shallow copy of a tuple that is
207 guaranteed to contain exact strings, by converting string subclasses
208 to exact strings and complaining if a non-string is found. */
209 static PyObject*
210 validate_and_copy_tuple(PyObject *tup)
211 {
212 PyObject *newtuple;
213 PyObject *item;
214 Py_ssize_t i, len;
215
216 len = PyTuple_GET_SIZE(tup);
217 newtuple = PyTuple_New(len);
218 if (newtuple == NULL)
219 return NULL;
220
221 for (i = 0; i < len; i++) {
222 item = PyTuple_GET_ITEM(tup, i);
223 if (PyUnicode_CheckExact(item)) {
224 Py_INCREF(item);
225 }
226 else if (!PyUnicode_Check(item)) {
227 PyErr_Format(
228 PyExc_TypeError,
229 "name tuples must contain only "
230 "strings, not '%.500s'",
231 Py_TYPE(item)->tp_name);
232 Py_DECREF(newtuple);
233 return NULL;
234 }
235 else {
236 item = _PyUnicode_Copy(item);
237 if (item == NULL) {
238 Py_DECREF(newtuple);
239 return NULL;
240 }
241 }
242 PyTuple_SET_ITEM(newtuple, i, item);
243 }
244
245 return newtuple;
246 }
247
248 static int
249 init_co_cached(PyCodeObject *self) {
250 if (self->_co_cached == NULL) {
251 self->_co_cached = PyMem_New(_PyCoCached, 1);
252 if (self->_co_cached == NULL) {
253 PyErr_NoMemory();
254 return -1;
255 }
256 self->_co_cached->_co_code = NULL;
257 self->_co_cached->_co_cellvars = NULL;
258 self->_co_cached->_co_freevars = NULL;
259 self->_co_cached->_co_varnames = NULL;
260 }
261 return 0;
262
263 }
264 /******************
265 * _PyCode_New()
266 ******************/
267
268 // This is also used in compile.c.
269 void
270 _Py_set_localsplus_info(int offset, PyObject *name, _PyLocals_Kind kind,
271 PyObject *names, PyObject *kinds)
272 {
273 PyTuple_SET_ITEM(names, offset, Py_NewRef(name));
274 _PyLocals_SetKind(kinds, offset, kind);
275 }
276
277 static void
278 get_localsplus_counts(PyObject *names, PyObject *kinds,
279 int *pnlocals, int *pncellvars,
280 int *pnfreevars)
281 {
282 int nlocals = 0;
283 int ncellvars = 0;
284 int nfreevars = 0;
285 Py_ssize_t nlocalsplus = PyTuple_GET_SIZE(names);
286 for (int i = 0; i < nlocalsplus; i++) {
287 _PyLocals_Kind kind = _PyLocals_GetKind(kinds, i);
288 if (kind & CO_FAST_LOCAL) {
289 nlocals += 1;
290 if (kind & CO_FAST_CELL) {
291 ncellvars += 1;
292 }
293 }
294 else if (kind & CO_FAST_CELL) {
295 ncellvars += 1;
296 }
297 else if (kind & CO_FAST_FREE) {
298 nfreevars += 1;
299 }
300 }
301 if (pnlocals != NULL) {
302 *pnlocals = nlocals;
303 }
304 if (pncellvars != NULL) {
305 *pncellvars = ncellvars;
306 }
307 if (pnfreevars != NULL) {
308 *pnfreevars = nfreevars;
309 }
310 }
311
312 static PyObject *
313 get_localsplus_names(PyCodeObject *co, _PyLocals_Kind kind, int num)
314 {
315 PyObject *names = PyTuple_New(num);
316 if (names == NULL) {
317 return NULL;
318 }
319 int index = 0;
320 for (int offset = 0; offset < co->co_nlocalsplus; offset++) {
321 _PyLocals_Kind k = _PyLocals_GetKind(co->co_localspluskinds, offset);
322 if ((k & kind) == 0) {
323 continue;
324 }
325 assert(index < num);
326 PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, offset);
327 PyTuple_SET_ITEM(names, index, Py_NewRef(name));
328 index += 1;
329 }
330 assert(index == num);
331 return names;
332 }
333
334 int
335 _PyCode_Validate(struct _PyCodeConstructor *con)
336 {
337 /* Check argument types */
338 if (con->argcount < con->posonlyargcount || con->posonlyargcount < 0 ||
339 con->kwonlyargcount < 0 ||
340 con->stacksize < 0 || con->flags < 0 ||
341 con->code == NULL || !PyBytes_Check(con->code) ||
342 con->consts == NULL || !PyTuple_Check(con->consts) ||
343 con->names == NULL || !PyTuple_Check(con->names) ||
344 con->localsplusnames == NULL || !PyTuple_Check(con->localsplusnames) ||
345 con->localspluskinds == NULL || !PyBytes_Check(con->localspluskinds) ||
346 PyTuple_GET_SIZE(con->localsplusnames)
347 != PyBytes_GET_SIZE(con->localspluskinds) ||
348 con->name == NULL || !PyUnicode_Check(con->name) ||
349 con->qualname == NULL || !PyUnicode_Check(con->qualname) ||
350 con->filename == NULL || !PyUnicode_Check(con->filename) ||
351 con->linetable == NULL || !PyBytes_Check(con->linetable) ||
352 con->exceptiontable == NULL || !PyBytes_Check(con->exceptiontable)
353 ) {
354 PyErr_BadInternalCall();
355 return -1;
356 }
357
358 /* Make sure that code is indexable with an int, this is
359 a long running assumption in ceval.c and many parts of
360 the interpreter. */
361 if (PyBytes_GET_SIZE(con->code) > INT_MAX) {
362 PyErr_SetString(PyExc_OverflowError,
363 "code: co_code larger than INT_MAX");
364 return -1;
365 }
366 if (PyBytes_GET_SIZE(con->code) % sizeof(_Py_CODEUNIT) != 0 ||
367 !_Py_IS_ALIGNED(PyBytes_AS_STRING(con->code), sizeof(_Py_CODEUNIT))
368 ) {
369 PyErr_SetString(PyExc_ValueError, "code: co_code is malformed");
370 return -1;
371 }
372
373 /* Ensure that the co_varnames has enough names to cover the arg counts.
374 * Note that totalargs = nlocals - nplainlocals. We check nplainlocals
375 * here to avoid the possibility of overflow (however remote). */
376 int nlocals;
377 get_localsplus_counts(con->localsplusnames, con->localspluskinds,
378 &nlocals, NULL, NULL);
379 int nplainlocals = nlocals -
380 con->argcount -
381 con->kwonlyargcount -
382 ((con->flags & CO_VARARGS) != 0) -
383 ((con->flags & CO_VARKEYWORDS) != 0);
384 if (nplainlocals < 0) {
385 PyErr_SetString(PyExc_ValueError, "code: co_varnames is too small");
386 return -1;
387 }
388
389 return 0;
390 }
391
392 extern void _PyCode_Quicken(PyCodeObject *code);
393
394 static void
395 init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
396 {
397 int nlocalsplus = (int)PyTuple_GET_SIZE(con->localsplusnames);
398 int nlocals, ncellvars, nfreevars;
399 get_localsplus_counts(con->localsplusnames, con->localspluskinds,
400 &nlocals, &ncellvars, &nfreevars);
401
402 co->co_filename = Py_NewRef(con->filename);
403 co->co_name = Py_NewRef(con->name);
404 co->co_qualname = Py_NewRef(con->qualname);
405 co->co_flags = con->flags;
406
407 co->co_firstlineno = con->firstlineno;
408 co->co_linetable = Py_NewRef(con->linetable);
409
410 co->co_consts = Py_NewRef(con->consts);
411 co->co_names = Py_NewRef(con->names);
412
413 co->co_localsplusnames = Py_NewRef(con->localsplusnames);
414 co->co_localspluskinds = Py_NewRef(con->localspluskinds);
415
416 co->co_argcount = con->argcount;
417 co->co_posonlyargcount = con->posonlyargcount;
418 co->co_kwonlyargcount = con->kwonlyargcount;
419
420 co->co_stacksize = con->stacksize;
421
422 co->co_exceptiontable = Py_NewRef(con->exceptiontable);
423
424 /* derived values */
425 co->co_nlocalsplus = nlocalsplus;
426 co->co_nlocals = nlocals;
427 co->co_framesize = nlocalsplus + con->stacksize + FRAME_SPECIALS_SIZE;
428 co->co_ncellvars = ncellvars;
429 co->co_nfreevars = nfreevars;
430 co->co_version = _Py_next_func_version;
431 if (_Py_next_func_version != 0) {
432 _Py_next_func_version++;
433 }
434 co->_co_monitoring = NULL;
435 co->_co_instrumentation_version = 0;
436 /* not set */
437 co->co_weakreflist = NULL;
438 co->co_extra = NULL;
439 co->_co_cached = NULL;
440
441 memcpy(_PyCode_CODE(co), PyBytes_AS_STRING(con->code),
442 PyBytes_GET_SIZE(con->code));
443 int entry_point = 0;
444 while (entry_point < Py_SIZE(co) &&
445 _PyCode_CODE(co)[entry_point].op.code != RESUME) {
446 entry_point++;
447 }
448 co->_co_firsttraceable = entry_point;
449 _PyCode_Quicken(co);
450 notify_code_watchers(PY_CODE_EVENT_CREATE, co);
451 }
452
453 static int
454 scan_varint(const uint8_t *ptr)
455 {
456 unsigned int read = *ptr++;
457 unsigned int val = read & 63;
458 unsigned int shift = 0;
459 while (read & 64) {
460 read = *ptr++;
461 shift += 6;
462 val |= (read & 63) << shift;
463 }
464 return val;
465 }
466
467 static int
468 scan_signed_varint(const uint8_t *ptr)
469 {
470 unsigned int uval = scan_varint(ptr);
471 if (uval & 1) {
472 return -(int)(uval >> 1);
473 }
474 else {
475 return uval >> 1;
476 }
477 }
478
479 static int
480 get_line_delta(const uint8_t *ptr)
481 {
482 int code = ((*ptr) >> 3) & 15;
483 switch (code) {
484 case PY_CODE_LOCATION_INFO_NONE:
485 return 0;
486 case PY_CODE_LOCATION_INFO_NO_COLUMNS:
487 case PY_CODE_LOCATION_INFO_LONG:
488 return scan_signed_varint(ptr+1);
489 case PY_CODE_LOCATION_INFO_ONE_LINE0:
490 return 0;
491 case PY_CODE_LOCATION_INFO_ONE_LINE1:
492 return 1;
493 case PY_CODE_LOCATION_INFO_ONE_LINE2:
494 return 2;
495 default:
496 /* Same line */
497 return 0;
498 }
499 }
500
501 static PyObject *
502 remove_column_info(PyObject *locations)
503 {
504 int offset = 0;
505 const uint8_t *data = (const uint8_t *)PyBytes_AS_STRING(locations);
506 PyObject *res = PyBytes_FromStringAndSize(NULL, 32);
507 if (res == NULL) {
508 PyErr_NoMemory();
509 return NULL;
510 }
511 uint8_t *output = (uint8_t *)PyBytes_AS_STRING(res);
512 while (offset < PyBytes_GET_SIZE(locations)) {
513 Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res);
514 if (write_offset + 16 >= PyBytes_GET_SIZE(res)) {
515 if (_PyBytes_Resize(&res, PyBytes_GET_SIZE(res) * 2) < 0) {
516 return NULL;
517 }
518 output = (uint8_t *)PyBytes_AS_STRING(res) + write_offset;
519 }
520 int code = (data[offset] >> 3) & 15;
521 if (code == PY_CODE_LOCATION_INFO_NONE) {
522 *output++ = data[offset];
523 }
524 else {
525 int blength = (data[offset] & 7)+1;
526 output += write_location_entry_start(
527 output, PY_CODE_LOCATION_INFO_NO_COLUMNS, blength);
528 int ldelta = get_line_delta(&data[offset]);
529 output += write_signed_varint(output, ldelta);
530 }
531 offset++;
532 while (offset < PyBytes_GET_SIZE(locations) &&
533 (data[offset] & 128) == 0) {
534 offset++;
535 }
536 }
537 Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res);
538 if (_PyBytes_Resize(&res, write_offset)) {
539 return NULL;
540 }
541 return res;
542 }
543
544 /* The caller is responsible for ensuring that the given data is valid. */
545
546 PyCodeObject *
547 _PyCode_New(struct _PyCodeConstructor *con)
548 {
549 /* Ensure that strings are ready Unicode string */
550 if (PyUnicode_READY(con->name) < 0) {
551 return NULL;
552 }
553 if (PyUnicode_READY(con->qualname) < 0) {
554 return NULL;
555 }
556 if (PyUnicode_READY(con->filename) < 0) {
557 return NULL;
558 }
559
560 if (intern_strings(con->names) < 0) {
561 return NULL;
562 }
563 if (intern_string_constants(con->consts, NULL) < 0) {
564 return NULL;
565 }
566 if (intern_strings(con->localsplusnames) < 0) {
567 return NULL;
568 }
569
570 PyObject *replacement_locations = NULL;
571 // Compact the linetable if we are opted out of debug
572 // ranges.
573 if (!_Py_GetConfig()->code_debug_ranges) {
574 replacement_locations = remove_column_info(con->linetable);
575 if (replacement_locations == NULL) {
576 return NULL;
577 }
578 con->linetable = replacement_locations;
579 }
580
581 Py_ssize_t size = PyBytes_GET_SIZE(con->code) / sizeof(_Py_CODEUNIT);
582 PyCodeObject *co = PyObject_NewVar(PyCodeObject, &PyCode_Type, size);
583 if (co == NULL) {
584 Py_XDECREF(replacement_locations);
585 PyErr_NoMemory();
586 return NULL;
587 }
588 init_code(co, con);
589 Py_XDECREF(replacement_locations);
590 return co;
591 }
592
593
594 /******************
595 * the legacy "constructors"
596 ******************/
597
598 PyCodeObject *
599 PyUnstable_Code_NewWithPosOnlyArgs(
600 int argcount, int posonlyargcount, int kwonlyargcount,
601 int nlocals, int stacksize, int flags,
602 PyObject *code, PyObject *consts, PyObject *names,
603 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
604 PyObject *filename, PyObject *name,
605 PyObject *qualname, int firstlineno,
606 PyObject *linetable,
607 PyObject *exceptiontable)
608 {
609 PyCodeObject *co = NULL;
610 PyObject *localsplusnames = NULL;
611 PyObject *localspluskinds = NULL;
612
613 if (varnames == NULL || !PyTuple_Check(varnames) ||
614 cellvars == NULL || !PyTuple_Check(cellvars) ||
615 freevars == NULL || !PyTuple_Check(freevars)
616 ) {
617 PyErr_BadInternalCall();
618 return NULL;
619 }
620
621 // Set the "fast locals plus" info.
622 int nvarnames = (int)PyTuple_GET_SIZE(varnames);
623 int ncellvars = (int)PyTuple_GET_SIZE(cellvars);
624 int nfreevars = (int)PyTuple_GET_SIZE(freevars);
625 int nlocalsplus = nvarnames + ncellvars + nfreevars;
626 localsplusnames = PyTuple_New(nlocalsplus);
627 if (localsplusnames == NULL) {
628 goto error;
629 }
630 localspluskinds = PyBytes_FromStringAndSize(NULL, nlocalsplus);
631 if (localspluskinds == NULL) {
632 goto error;
633 }
634 int offset = 0;
635 for (int i = 0; i < nvarnames; i++, offset++) {
636 PyObject *name = PyTuple_GET_ITEM(varnames, i);
637 _Py_set_localsplus_info(offset, name, CO_FAST_LOCAL,
638 localsplusnames, localspluskinds);
639 }
640 for (int i = 0; i < ncellvars; i++, offset++) {
641 PyObject *name = PyTuple_GET_ITEM(cellvars, i);
642 int argoffset = -1;
643 for (int j = 0; j < nvarnames; j++) {
644 int cmp = PyUnicode_Compare(PyTuple_GET_ITEM(varnames, j),
645 name);
646 assert(!PyErr_Occurred());
647 if (cmp == 0) {
648 argoffset = j;
649 break;
650 }
651 }
652 if (argoffset >= 0) {
653 // Merge the localsplus indices.
654 nlocalsplus -= 1;
655 offset -= 1;
656 _PyLocals_Kind kind = _PyLocals_GetKind(localspluskinds, argoffset);
657 _PyLocals_SetKind(localspluskinds, argoffset, kind | CO_FAST_CELL);
658 continue;
659 }
660 _Py_set_localsplus_info(offset, name, CO_FAST_CELL,
661 localsplusnames, localspluskinds);
662 }
663 for (int i = 0; i < nfreevars; i++, offset++) {
664 PyObject *name = PyTuple_GET_ITEM(freevars, i);
665 _Py_set_localsplus_info(offset, name, CO_FAST_FREE,
666 localsplusnames, localspluskinds);
667 }
668 // If any cells were args then nlocalsplus will have shrunk.
669 if (nlocalsplus != PyTuple_GET_SIZE(localsplusnames)) {
670 if (_PyTuple_Resize(&localsplusnames, nlocalsplus) < 0
671 || _PyBytes_Resize(&localspluskinds, nlocalsplus) < 0) {
672 goto error;
673 }
674 }
675
676 struct _PyCodeConstructor con = {
677 .filename = filename,
678 .name = name,
679 .qualname = qualname,
680 .flags = flags,
681
682 .code = code,
683 .firstlineno = firstlineno,
684 .linetable = linetable,
685
686 .consts = consts,
687 .names = names,
688
689 .localsplusnames = localsplusnames,
690 .localspluskinds = localspluskinds,
691
692 .argcount = argcount,
693 .posonlyargcount = posonlyargcount,
694 .kwonlyargcount = kwonlyargcount,
695
696 .stacksize = stacksize,
697
698 .exceptiontable = exceptiontable,
699 };
700
701 if (_PyCode_Validate(&con) < 0) {
702 goto error;
703 }
704 assert(PyBytes_GET_SIZE(code) % sizeof(_Py_CODEUNIT) == 0);
705 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(code), sizeof(_Py_CODEUNIT)));
706 if (nlocals != PyTuple_GET_SIZE(varnames)) {
707 PyErr_SetString(PyExc_ValueError,
708 "code: co_nlocals != len(co_varnames)");
709 goto error;
710 }
711
712 co = _PyCode_New(&con);
713 if (co == NULL) {
714 goto error;
715 }
716
717 error:
718 Py_XDECREF(localsplusnames);
719 Py_XDECREF(localspluskinds);
720 return co;
721 }
722
723 PyCodeObject *
724 PyUnstable_Code_New(int argcount, int kwonlyargcount,
725 int nlocals, int stacksize, int flags,
726 PyObject *code, PyObject *consts, PyObject *names,
727 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
728 PyObject *filename, PyObject *name, PyObject *qualname,
729 int firstlineno,
730 PyObject *linetable,
731 PyObject *exceptiontable)
732 {
733 return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
734 stacksize, flags, code, consts, names,
735 varnames, freevars, cellvars, filename,
736 name, qualname, firstlineno,
737 linetable,
738 exceptiontable);
739 }
740
741 // NOTE: When modifying the construction of PyCode_NewEmpty, please also change
742 // test.test_code.CodeLocationTest.test_code_new_empty to keep it in sync!
743
744 static const uint8_t assert0[6] = {
745 RESUME, 0,
746 LOAD_ASSERTION_ERROR, 0,
747 RAISE_VARARGS, 1
748 };
749
750 static const uint8_t linetable[2] = {
751 (1 << 7) // New entry.
752 | (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3)
753 | (3 - 1), // Three code units.
754 0, // Offset from co_firstlineno.
755 };
756
757 PyCodeObject *
758 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
759 {
760 PyObject *nulltuple = NULL;
761 PyObject *filename_ob = NULL;
762 PyObject *funcname_ob = NULL;
763 PyObject *code_ob = NULL;
764 PyObject *linetable_ob = NULL;
765 PyCodeObject *result = NULL;
766
767 nulltuple = PyTuple_New(0);
768 if (nulltuple == NULL) {
769 goto failed;
770 }
771 funcname_ob = PyUnicode_FromString(funcname);
772 if (funcname_ob == NULL) {
773 goto failed;
774 }
775 filename_ob = PyUnicode_DecodeFSDefault(filename);
776 if (filename_ob == NULL) {
777 goto failed;
778 }
779 code_ob = PyBytes_FromStringAndSize((const char *)assert0, 6);
780 if (code_ob == NULL) {
781 goto failed;
782 }
783 linetable_ob = PyBytes_FromStringAndSize((const char *)linetable, 2);
784 if (linetable_ob == NULL) {
785 goto failed;
786 }
787
788 #define emptystring (PyObject *)&_Py_SINGLETON(bytes_empty)
789 struct _PyCodeConstructor con = {
790 .filename = filename_ob,
791 .name = funcname_ob,
792 .qualname = funcname_ob,
793 .code = code_ob,
794 .firstlineno = firstlineno,
795 .linetable = linetable_ob,
796 .consts = nulltuple,
797 .names = nulltuple,
798 .localsplusnames = nulltuple,
799 .localspluskinds = emptystring,
800 .exceptiontable = emptystring,
801 .stacksize = 1,
802 };
803 result = _PyCode_New(&con);
804
805 failed:
806 Py_XDECREF(nulltuple);
807 Py_XDECREF(funcname_ob);
808 Py_XDECREF(filename_ob);
809 Py_XDECREF(code_ob);
810 Py_XDECREF(linetable_ob);
811 return result;
812 }
813
814
815 /******************
816 * source location tracking (co_lines/co_positions)
817 ******************/
818
819 int
820 PyCode_Addr2Line(PyCodeObject *co, int addrq)
821 {
822 if (addrq < 0) {
823 return co->co_firstlineno;
824 }
825 assert(addrq >= 0 && addrq < _PyCode_NBYTES(co));
826 PyCodeAddressRange bounds;
827 _PyCode_InitAddressRange(co, &bounds);
828 return _PyCode_CheckLineNumber(addrq, &bounds);
829 }
830
831 void
832 _PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range)
833 {
834 range->opaque.lo_next = (const uint8_t *)linetable;
835 range->opaque.limit = range->opaque.lo_next + length;
836 range->ar_start = -1;
837 range->ar_end = 0;
838 range->opaque.computed_line = firstlineno;
839 range->ar_line = -1;
840 }
841
842 int
843 _PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds)
844 {
845 assert(co->co_linetable != NULL);
846 const char *linetable = PyBytes_AS_STRING(co->co_linetable);
847 Py_ssize_t length = PyBytes_GET_SIZE(co->co_linetable);
848 _PyLineTable_InitAddressRange(linetable, length, co->co_firstlineno, bounds);
849 return bounds->ar_line;
850 }
851
852 /* Update *bounds to describe the first and one-past-the-last instructions in
853 the same line as lasti. Return the number of that line, or -1 if lasti is out of bounds. */
854 int
855 _PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds)
856 {
857 while (bounds->ar_end <= lasti) {
858 if (!_PyLineTable_NextAddressRange(bounds)) {
859 return -1;
860 }
861 }
862 while (bounds->ar_start > lasti) {
863 if (!_PyLineTable_PreviousAddressRange(bounds)) {
864 return -1;
865 }
866 }
867 return bounds->ar_line;
868 }
869
870 static int
871 is_no_line_marker(uint8_t b)
872 {
873 return (b >> 3) == 0x1f;
874 }
875
876
877 #define ASSERT_VALID_BOUNDS(bounds) \
878 assert(bounds->opaque.lo_next <= bounds->opaque.limit && \
879 (bounds->ar_line == -1 || bounds->ar_line == bounds->opaque.computed_line) && \
880 (bounds->opaque.lo_next == bounds->opaque.limit || \
881 (*bounds->opaque.lo_next) & 128))
882
883 static int
884 next_code_delta(PyCodeAddressRange *bounds)
885 {
886 assert((*bounds->opaque.lo_next) & 128);
887 return (((*bounds->opaque.lo_next) & 7) + 1) * sizeof(_Py_CODEUNIT);
888 }
889
890 static int
891 previous_code_delta(PyCodeAddressRange *bounds)
892 {
893 if (bounds->ar_start == 0) {
894 // If we looking at the first entry, the
895 // "previous" entry has an implicit length of 1.
896 return 1;
897 }
898 const uint8_t *ptr = bounds->opaque.lo_next-1;
899 while (((*ptr) & 128) == 0) {
900 ptr--;
901 }
902 return (((*ptr) & 7) + 1) * sizeof(_Py_CODEUNIT);
903 }
904
905 static int
906 read_byte(PyCodeAddressRange *bounds)
907 {
908 return *bounds->opaque.lo_next++;
909 }
910
911 static int
912 read_varint(PyCodeAddressRange *bounds)
913 {
914 unsigned int read = read_byte(bounds);
915 unsigned int val = read & 63;
916 unsigned int shift = 0;
917 while (read & 64) {
918 read = read_byte(bounds);
919 shift += 6;
920 val |= (read & 63) << shift;
921 }
922 return val;
923 }
924
925 static int
926 read_signed_varint(PyCodeAddressRange *bounds)
927 {
928 unsigned int uval = read_varint(bounds);
929 if (uval & 1) {
930 return -(int)(uval >> 1);
931 }
932 else {
933 return uval >> 1;
934 }
935 }
936
937 static void
938 retreat(PyCodeAddressRange *bounds)
939 {
940 ASSERT_VALID_BOUNDS(bounds);
941 assert(bounds->ar_start >= 0);
942 do {
943 bounds->opaque.lo_next--;
944 } while (((*bounds->opaque.lo_next) & 128) == 0);
945 bounds->opaque.computed_line -= get_line_delta(bounds->opaque.lo_next);
946 bounds->ar_end = bounds->ar_start;
947 bounds->ar_start -= previous_code_delta(bounds);
948 if (is_no_line_marker(bounds->opaque.lo_next[-1])) {
949 bounds->ar_line = -1;
950 }
951 else {
952 bounds->ar_line = bounds->opaque.computed_line;
953 }
954 ASSERT_VALID_BOUNDS(bounds);
955 }
956
957 static void
958 advance(PyCodeAddressRange *bounds)
959 {
960 ASSERT_VALID_BOUNDS(bounds);
961 bounds->opaque.computed_line += get_line_delta(bounds->opaque.lo_next);
962 if (is_no_line_marker(*bounds->opaque.lo_next)) {
963 bounds->ar_line = -1;
964 }
965 else {
966 bounds->ar_line = bounds->opaque.computed_line;
967 }
968 bounds->ar_start = bounds->ar_end;
969 bounds->ar_end += next_code_delta(bounds);
970 do {
971 bounds->opaque.lo_next++;
972 } while (bounds->opaque.lo_next < bounds->opaque.limit &&
973 ((*bounds->opaque.lo_next) & 128) == 0);
974 ASSERT_VALID_BOUNDS(bounds);
975 }
976
977 static void
978 advance_with_locations(PyCodeAddressRange *bounds, int *endline, int *column, int *endcolumn)
979 {
980 ASSERT_VALID_BOUNDS(bounds);
981 int first_byte = read_byte(bounds);
982 int code = (first_byte >> 3) & 15;
983 bounds->ar_start = bounds->ar_end;
984 bounds->ar_end = bounds->ar_start + ((first_byte & 7) + 1) * sizeof(_Py_CODEUNIT);
985 switch(code) {
986 case PY_CODE_LOCATION_INFO_NONE:
987 bounds->ar_line = *endline = -1;
988 *column = *endcolumn = -1;
989 break;
990 case PY_CODE_LOCATION_INFO_LONG:
991 {
992 bounds->opaque.computed_line += read_signed_varint(bounds);
993 bounds->ar_line = bounds->opaque.computed_line;
994 *endline = bounds->ar_line + read_varint(bounds);
995 *column = read_varint(bounds)-1;
996 *endcolumn = read_varint(bounds)-1;
997 break;
998 }
999 case PY_CODE_LOCATION_INFO_NO_COLUMNS:
1000 {
1001 /* No column */
1002 bounds->opaque.computed_line += read_signed_varint(bounds);
1003 *endline = bounds->ar_line = bounds->opaque.computed_line;
1004 *column = *endcolumn = -1;
1005 break;
1006 }
1007 case PY_CODE_LOCATION_INFO_ONE_LINE0:
1008 case PY_CODE_LOCATION_INFO_ONE_LINE1:
1009 case PY_CODE_LOCATION_INFO_ONE_LINE2:
1010 {
1011 /* one line form */
1012 int line_delta = code - 10;
1013 bounds->opaque.computed_line += line_delta;
1014 *endline = bounds->ar_line = bounds->opaque.computed_line;
1015 *column = read_byte(bounds);
1016 *endcolumn = read_byte(bounds);
1017 break;
1018 }
1019 default:
1020 {
1021 /* Short forms */
1022 int second_byte = read_byte(bounds);
1023 assert((second_byte & 128) == 0);
1024 *endline = bounds->ar_line = bounds->opaque.computed_line;
1025 *column = code << 3 | (second_byte >> 4);
1026 *endcolumn = *column + (second_byte & 15);
1027 }
1028 }
1029 ASSERT_VALID_BOUNDS(bounds);
1030 }
1031 int
1032 PyCode_Addr2Location(PyCodeObject *co, int addrq,
1033 int *start_line, int *start_column,
1034 int *end_line, int *end_column)
1035 {
1036 if (addrq < 0) {
1037 *start_line = *end_line = co->co_firstlineno;
1038 *start_column = *end_column = 0;
1039 return 1;
1040 }
1041 assert(addrq >= 0 && addrq < _PyCode_NBYTES(co));
1042 PyCodeAddressRange bounds;
1043 _PyCode_InitAddressRange(co, &bounds);
1044 _PyCode_CheckLineNumber(addrq, &bounds);
1045 retreat(&bounds);
1046 advance_with_locations(&bounds, end_line, start_column, end_column);
1047 *start_line = bounds.ar_line;
1048 return 1;
1049 }
1050
1051
1052 static inline int
1053 at_end(PyCodeAddressRange *bounds) {
1054 return bounds->opaque.lo_next >= bounds->opaque.limit;
1055 }
1056
1057 int
1058 _PyLineTable_PreviousAddressRange(PyCodeAddressRange *range)
1059 {
1060 if (range->ar_start <= 0) {
1061 return 0;
1062 }
1063 retreat(range);
1064 assert(range->ar_end > range->ar_start);
1065 return 1;
1066 }
1067
1068 int
1069 _PyLineTable_NextAddressRange(PyCodeAddressRange *range)
1070 {
1071 if (at_end(range)) {
1072 return 0;
1073 }
1074 advance(range);
1075 assert(range->ar_end > range->ar_start);
1076 return 1;
1077 }
1078
1079 static int
1080 emit_pair(PyObject **bytes, int *offset, int a, int b)
1081 {
1082 Py_ssize_t len = PyBytes_GET_SIZE(*bytes);
1083 if (*offset + 2 >= len) {
1084 if (_PyBytes_Resize(bytes, len * 2) < 0)
1085 return 0;
1086 }
1087 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(*bytes);
1088 lnotab += *offset;
1089 *lnotab++ = a;
1090 *lnotab++ = b;
1091 *offset += 2;
1092 return 1;
1093 }
1094
1095 static int
1096 emit_delta(PyObject **bytes, int bdelta, int ldelta, int *offset)
1097 {
1098 while (bdelta > 255) {
1099 if (!emit_pair(bytes, offset, 255, 0)) {
1100 return 0;
1101 }
1102 bdelta -= 255;
1103 }
1104 while (ldelta > 127) {
1105 if (!emit_pair(bytes, offset, bdelta, 127)) {
1106 return 0;
1107 }
1108 bdelta = 0;
1109 ldelta -= 127;
1110 }
1111 while (ldelta < -128) {
1112 if (!emit_pair(bytes, offset, bdelta, -128)) {
1113 return 0;
1114 }
1115 bdelta = 0;
1116 ldelta += 128;
1117 }
1118 return emit_pair(bytes, offset, bdelta, ldelta);
1119 }
1120
1121 static PyObject *
1122 decode_linetable(PyCodeObject *code)
1123 {
1124 PyCodeAddressRange bounds;
1125 PyObject *bytes;
1126 int table_offset = 0;
1127 int code_offset = 0;
1128 int line = code->co_firstlineno;
1129 bytes = PyBytes_FromStringAndSize(NULL, 64);
1130 if (bytes == NULL) {
1131 return NULL;
1132 }
1133 _PyCode_InitAddressRange(code, &bounds);
1134 while (_PyLineTable_NextAddressRange(&bounds)) {
1135 if (bounds.opaque.computed_line != line) {
1136 int bdelta = bounds.ar_start - code_offset;
1137 int ldelta = bounds.opaque.computed_line - line;
1138 if (!emit_delta(&bytes, bdelta, ldelta, &table_offset)) {
1139 Py_DECREF(bytes);
1140 return NULL;
1141 }
1142 code_offset = bounds.ar_start;
1143 line = bounds.opaque.computed_line;
1144 }
1145 }
1146 _PyBytes_Resize(&bytes, table_offset);
1147 return bytes;
1148 }
1149
1150
1151 typedef struct {
1152 PyObject_HEAD
1153 PyCodeObject *li_code;
1154 PyCodeAddressRange li_line;
1155 } lineiterator;
1156
1157
1158 static void
1159 lineiter_dealloc(lineiterator *li)
1160 {
1161 Py_DECREF(li->li_code);
1162 Py_TYPE(li)->tp_free(li);
1163 }
1164
1165 static PyObject *
1166 _source_offset_converter(int *value) {
1167 if (*value == -1) {
1168 Py_RETURN_NONE;
1169 }
1170 return PyLong_FromLong(*value);
1171 }
1172
1173 static PyObject *
1174 lineiter_next(lineiterator *li)
1175 {
1176 PyCodeAddressRange *bounds = &li->li_line;
1177 if (!_PyLineTable_NextAddressRange(bounds)) {
1178 return NULL;
1179 }
1180 int start = bounds->ar_start;
1181 int line = bounds->ar_line;
1182 // Merge overlapping entries:
1183 while (_PyLineTable_NextAddressRange(bounds)) {
1184 if (bounds->ar_line != line) {
1185 _PyLineTable_PreviousAddressRange(bounds);
1186 break;
1187 }
1188 }
1189 return Py_BuildValue("iiO&", start, bounds->ar_end,
1190 _source_offset_converter, &line);
1191 }
1192
1193 PyTypeObject _PyLineIterator = {
1194 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1195 "line_iterator", /* tp_name */
1196 sizeof(lineiterator), /* tp_basicsize */
1197 0, /* tp_itemsize */
1198 /* methods */
1199 (destructor)lineiter_dealloc, /* tp_dealloc */
1200 0, /* tp_vectorcall_offset */
1201 0, /* tp_getattr */
1202 0, /* tp_setattr */
1203 0, /* tp_as_async */
1204 0, /* tp_repr */
1205 0, /* tp_as_number */
1206 0, /* tp_as_sequence */
1207 0, /* tp_as_mapping */
1208 0, /* tp_hash */
1209 0, /* tp_call */
1210 0, /* tp_str */
1211 0, /* tp_getattro */
1212 0, /* tp_setattro */
1213 0, /* tp_as_buffer */
1214 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1215 0, /* tp_doc */
1216 0, /* tp_traverse */
1217 0, /* tp_clear */
1218 0, /* tp_richcompare */
1219 0, /* tp_weaklistoffset */
1220 PyObject_SelfIter, /* tp_iter */
1221 (iternextfunc)lineiter_next, /* tp_iternext */
1222 0, /* tp_methods */
1223 0, /* tp_members */
1224 0, /* tp_getset */
1225 0, /* tp_base */
1226 0, /* tp_dict */
1227 0, /* tp_descr_get */
1228 0, /* tp_descr_set */
1229 0, /* tp_dictoffset */
1230 0, /* tp_init */
1231 0, /* tp_alloc */
1232 0, /* tp_new */
1233 PyObject_Del, /* tp_free */
1234 };
1235
1236 static lineiterator *
1237 new_linesiterator(PyCodeObject *code)
1238 {
1239 lineiterator *li = (lineiterator *)PyType_GenericAlloc(&_PyLineIterator, 0);
1240 if (li == NULL) {
1241 return NULL;
1242 }
1243 li->li_code = (PyCodeObject*)Py_NewRef(code);
1244 _PyCode_InitAddressRange(code, &li->li_line);
1245 return li;
1246 }
1247
1248 /* co_positions iterator object. */
1249 typedef struct {
1250 PyObject_HEAD
1251 PyCodeObject* pi_code;
1252 PyCodeAddressRange pi_range;
1253 int pi_offset;
1254 int pi_endline;
1255 int pi_column;
1256 int pi_endcolumn;
1257 } positionsiterator;
1258
1259 static void
1260 positionsiter_dealloc(positionsiterator* pi)
1261 {
1262 Py_DECREF(pi->pi_code);
1263 Py_TYPE(pi)->tp_free(pi);
1264 }
1265
1266 static PyObject*
1267 positionsiter_next(positionsiterator* pi)
1268 {
1269 if (pi->pi_offset >= pi->pi_range.ar_end) {
1270 assert(pi->pi_offset == pi->pi_range.ar_end);
1271 if (at_end(&pi->pi_range)) {
1272 return NULL;
1273 }
1274 advance_with_locations(&pi->pi_range, &pi->pi_endline, &pi->pi_column, &pi->pi_endcolumn);
1275 }
1276 pi->pi_offset += 2;
1277 return Py_BuildValue("(O&O&O&O&)",
1278 _source_offset_converter, &pi->pi_range.ar_line,
1279 _source_offset_converter, &pi->pi_endline,
1280 _source_offset_converter, &pi->pi_column,
1281 _source_offset_converter, &pi->pi_endcolumn);
1282 }
1283
1284 PyTypeObject _PyPositionsIterator = {
1285 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1286 "positions_iterator", /* tp_name */
1287 sizeof(positionsiterator), /* tp_basicsize */
1288 0, /* tp_itemsize */
1289 /* methods */
1290 (destructor)positionsiter_dealloc, /* tp_dealloc */
1291 0, /* tp_vectorcall_offset */
1292 0, /* tp_getattr */
1293 0, /* tp_setattr */
1294 0, /* tp_as_async */
1295 0, /* tp_repr */
1296 0, /* tp_as_number */
1297 0, /* tp_as_sequence */
1298 0, /* tp_as_mapping */
1299 0, /* tp_hash */
1300 0, /* tp_call */
1301 0, /* tp_str */
1302 0, /* tp_getattro */
1303 0, /* tp_setattro */
1304 0, /* tp_as_buffer */
1305 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1306 0, /* tp_doc */
1307 0, /* tp_traverse */
1308 0, /* tp_clear */
1309 0, /* tp_richcompare */
1310 0, /* tp_weaklistoffset */
1311 PyObject_SelfIter, /* tp_iter */
1312 (iternextfunc)positionsiter_next, /* tp_iternext */
1313 0, /* tp_methods */
1314 0, /* tp_members */
1315 0, /* tp_getset */
1316 0, /* tp_base */
1317 0, /* tp_dict */
1318 0, /* tp_descr_get */
1319 0, /* tp_descr_set */
1320 0, /* tp_dictoffset */
1321 0, /* tp_init */
1322 0, /* tp_alloc */
1323 0, /* tp_new */
1324 PyObject_Del, /* tp_free */
1325 };
1326
1327 static PyObject*
1328 code_positionsiterator(PyCodeObject* code, PyObject* Py_UNUSED(args))
1329 {
1330 positionsiterator* pi = (positionsiterator*)PyType_GenericAlloc(&_PyPositionsIterator, 0);
1331 if (pi == NULL) {
1332 return NULL;
1333 }
1334 pi->pi_code = (PyCodeObject*)Py_NewRef(code);
1335 _PyCode_InitAddressRange(code, &pi->pi_range);
1336 pi->pi_offset = pi->pi_range.ar_end;
1337 return (PyObject*)pi;
1338 }
1339
1340
1341 /******************
1342 * "extra" frame eval info (see PEP 523)
1343 ******************/
1344
1345 /* Holder for co_extra information */
1346 typedef struct {
1347 Py_ssize_t ce_size;
1348 void *ce_extras[1];
1349 } _PyCodeObjectExtra;
1350
1351
1352 int
1353 PyUnstable_Code_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
1354 {
1355 if (!PyCode_Check(code)) {
1356 PyErr_BadInternalCall();
1357 return -1;
1358 }
1359
1360 PyCodeObject *o = (PyCodeObject*) code;
1361 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) o->co_extra;
1362
1363 if (co_extra == NULL || index < 0 || co_extra->ce_size <= index) {
1364 *extra = NULL;
1365 return 0;
1366 }
1367
1368 *extra = co_extra->ce_extras[index];
1369 return 0;
1370 }
1371
1372
1373 int
1374 PyUnstable_Code_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
1375 {
1376 PyInterpreterState *interp = _PyInterpreterState_GET();
1377
1378 if (!PyCode_Check(code) || index < 0 ||
1379 index >= interp->co_extra_user_count) {
1380 PyErr_BadInternalCall();
1381 return -1;
1382 }
1383
1384 PyCodeObject *o = (PyCodeObject*) code;
1385 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
1386
1387 if (co_extra == NULL || co_extra->ce_size <= index) {
1388 Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
1389 co_extra = PyMem_Realloc(
1390 co_extra,
1391 sizeof(_PyCodeObjectExtra) +
1392 (interp->co_extra_user_count-1) * sizeof(void*));
1393 if (co_extra == NULL) {
1394 return -1;
1395 }
1396 for (; i < interp->co_extra_user_count; i++) {
1397 co_extra->ce_extras[i] = NULL;
1398 }
1399 co_extra->ce_size = interp->co_extra_user_count;
1400 o->co_extra = co_extra;
1401 }
1402
1403 if (co_extra->ce_extras[index] != NULL) {
1404 freefunc free = interp->co_extra_freefuncs[index];
1405 if (free != NULL) {
1406 free(co_extra->ce_extras[index]);
1407 }
1408 }
1409
1410 co_extra->ce_extras[index] = extra;
1411 return 0;
1412 }
1413
1414
1415 /******************
1416 * other PyCodeObject accessor functions
1417 ******************/
1418
1419 static PyObject *
1420 get_cached_locals(PyCodeObject *co, PyObject **cached_field,
1421 _PyLocals_Kind kind, int num)
1422 {
1423 assert(cached_field != NULL);
1424 assert(co->_co_cached != NULL);
1425 if (*cached_field != NULL) {
1426 return Py_NewRef(*cached_field);
1427 }
1428 assert(*cached_field == NULL);
1429 PyObject *varnames = get_localsplus_names(co, kind, num);
1430 if (varnames == NULL) {
1431 return NULL;
1432 }
1433 *cached_field = Py_NewRef(varnames);
1434 return varnames;
1435 }
1436
1437 PyObject *
1438 _PyCode_GetVarnames(PyCodeObject *co)
1439 {
1440 if (init_co_cached(co)) {
1441 return NULL;
1442 }
1443 return get_cached_locals(co, &co->_co_cached->_co_varnames, CO_FAST_LOCAL, co->co_nlocals);
1444 }
1445
1446 PyObject *
1447 PyCode_GetVarnames(PyCodeObject *code)
1448 {
1449 return _PyCode_GetVarnames(code);
1450 }
1451
1452 PyObject *
1453 _PyCode_GetCellvars(PyCodeObject *co)
1454 {
1455 if (init_co_cached(co)) {
1456 return NULL;
1457 }
1458 return get_cached_locals(co, &co->_co_cached->_co_cellvars, CO_FAST_CELL, co->co_ncellvars);
1459 }
1460
1461 PyObject *
1462 PyCode_GetCellvars(PyCodeObject *code)
1463 {
1464 return _PyCode_GetCellvars(code);
1465 }
1466
1467 PyObject *
1468 _PyCode_GetFreevars(PyCodeObject *co)
1469 {
1470 if (init_co_cached(co)) {
1471 return NULL;
1472 }
1473 return get_cached_locals(co, &co->_co_cached->_co_freevars, CO_FAST_FREE, co->co_nfreevars);
1474 }
1475
1476 PyObject *
1477 PyCode_GetFreevars(PyCodeObject *code)
1478 {
1479 return _PyCode_GetFreevars(code);
1480 }
1481
1482 static void
1483 deopt_code(PyCodeObject *code, _Py_CODEUNIT *instructions)
1484 {
1485 Py_ssize_t len = Py_SIZE(code);
1486 for (int i = 0; i < len; i++) {
1487 int opcode = _Py_GetBaseOpcode(code, i);
1488 int caches = _PyOpcode_Caches[opcode];
1489 instructions[i].op.code = opcode;
1490 for (int j = 1; j <= caches; j++) {
1491 instructions[i+j].cache = 0;
1492 }
1493 i += caches;
1494 }
1495 }
1496
1497 PyObject *
1498 _PyCode_GetCode(PyCodeObject *co)
1499 {
1500 if (init_co_cached(co)) {
1501 return NULL;
1502 }
1503 if (co->_co_cached->_co_code != NULL) {
1504 return Py_NewRef(co->_co_cached->_co_code);
1505 }
1506 PyObject *code = PyBytes_FromStringAndSize((const char *)_PyCode_CODE(co),
1507 _PyCode_NBYTES(co));
1508 if (code == NULL) {
1509 return NULL;
1510 }
1511 deopt_code(co, (_Py_CODEUNIT *)PyBytes_AS_STRING(code));
1512 assert(co->_co_cached->_co_code == NULL);
1513 co->_co_cached->_co_code = Py_NewRef(code);
1514 return code;
1515 }
1516
1517 PyObject *
1518 PyCode_GetCode(PyCodeObject *co)
1519 {
1520 return _PyCode_GetCode(co);
1521 }
1522
1523 /******************
1524 * PyCode_Type
1525 ******************/
1526
1527 /*[clinic input]
1528 class code "PyCodeObject *" "&PyCode_Type"
1529 [clinic start generated code]*/
1530 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=78aa5d576683bb4b]*/
1531
1532 /*[clinic input]
1533 @classmethod
1534 code.__new__ as code_new
1535
1536 argcount: int
1537 posonlyargcount: int
1538 kwonlyargcount: int
1539 nlocals: int
1540 stacksize: int
1541 flags: int
1542 codestring as code: object(subclass_of="&PyBytes_Type")
1543 constants as consts: object(subclass_of="&PyTuple_Type")
1544 names: object(subclass_of="&PyTuple_Type")
1545 varnames: object(subclass_of="&PyTuple_Type")
1546 filename: unicode
1547 name: unicode
1548 qualname: unicode
1549 firstlineno: int
1550 linetable: object(subclass_of="&PyBytes_Type")
1551 exceptiontable: object(subclass_of="&PyBytes_Type")
1552 freevars: object(subclass_of="&PyTuple_Type", c_default="NULL") = ()
1553 cellvars: object(subclass_of="&PyTuple_Type", c_default="NULL") = ()
1554 /
1555
1556 Create a code object. Not for the faint of heart.
1557 [clinic start generated code]*/
1558
1559 static PyObject *
1560 code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,
1561 int kwonlyargcount, int nlocals, int stacksize, int flags,
1562 PyObject *code, PyObject *consts, PyObject *names,
1563 PyObject *varnames, PyObject *filename, PyObject *name,
1564 PyObject *qualname, int firstlineno, PyObject *linetable,
1565 PyObject *exceptiontable, PyObject *freevars,
1566 PyObject *cellvars)
1567 /*[clinic end generated code: output=069fa20d299f9dda input=e31da3c41ad8064a]*/
1568 {
1569 PyObject *co = NULL;
1570 PyObject *ournames = NULL;
1571 PyObject *ourvarnames = NULL;
1572 PyObject *ourfreevars = NULL;
1573 PyObject *ourcellvars = NULL;
1574
1575 if (PySys_Audit("code.__new__", "OOOiiiiii",
1576 code, filename, name, argcount, posonlyargcount,
1577 kwonlyargcount, nlocals, stacksize, flags) < 0) {
1578 goto cleanup;
1579 }
1580
1581 if (argcount < 0) {
1582 PyErr_SetString(
1583 PyExc_ValueError,
1584 "code: argcount must not be negative");
1585 goto cleanup;
1586 }
1587
1588 if (posonlyargcount < 0) {
1589 PyErr_SetString(
1590 PyExc_ValueError,
1591 "code: posonlyargcount must not be negative");
1592 goto cleanup;
1593 }
1594
1595 if (kwonlyargcount < 0) {
1596 PyErr_SetString(
1597 PyExc_ValueError,
1598 "code: kwonlyargcount must not be negative");
1599 goto cleanup;
1600 }
1601 if (nlocals < 0) {
1602 PyErr_SetString(
1603 PyExc_ValueError,
1604 "code: nlocals must not be negative");
1605 goto cleanup;
1606 }
1607
1608 ournames = validate_and_copy_tuple(names);
1609 if (ournames == NULL)
1610 goto cleanup;
1611 ourvarnames = validate_and_copy_tuple(varnames);
1612 if (ourvarnames == NULL)
1613 goto cleanup;
1614 if (freevars)
1615 ourfreevars = validate_and_copy_tuple(freevars);
1616 else
1617 ourfreevars = PyTuple_New(0);
1618 if (ourfreevars == NULL)
1619 goto cleanup;
1620 if (cellvars)
1621 ourcellvars = validate_and_copy_tuple(cellvars);
1622 else
1623 ourcellvars = PyTuple_New(0);
1624 if (ourcellvars == NULL)
1625 goto cleanup;
1626
1627 co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
1628 kwonlyargcount,
1629 nlocals, stacksize, flags,
1630 code, consts, ournames,
1631 ourvarnames, ourfreevars,
1632 ourcellvars, filename,
1633 name, qualname, firstlineno,
1634 linetable,
1635 exceptiontable
1636 );
1637 cleanup:
1638 Py_XDECREF(ournames);
1639 Py_XDECREF(ourvarnames);
1640 Py_XDECREF(ourfreevars);
1641 Py_XDECREF(ourcellvars);
1642 return co;
1643 }
1644
1645 static void
1646 free_monitoring_data(_PyCoMonitoringData *data)
1647 {
1648 if (data == NULL) {
1649 return;
1650 }
1651 if (data->tools) {
1652 PyMem_Free(data->tools);
1653 }
1654 if (data->lines) {
1655 PyMem_Free(data->lines);
1656 }
1657 if (data->line_tools) {
1658 PyMem_Free(data->line_tools);
1659 }
1660 if (data->per_instruction_opcodes) {
1661 PyMem_Free(data->per_instruction_opcodes);
1662 }
1663 if (data->per_instruction_tools) {
1664 PyMem_Free(data->per_instruction_tools);
1665 }
1666 PyMem_Free(data);
1667 }
1668
1669 static void
1670 code_dealloc(PyCodeObject *co)
1671 {
1672 assert(Py_REFCNT(co) == 0);
1673 Py_SET_REFCNT(co, 1);
1674 notify_code_watchers(PY_CODE_EVENT_DESTROY, co);
1675 if (Py_REFCNT(co) > 1) {
1676 Py_SET_REFCNT(co, Py_REFCNT(co) - 1);
1677 return;
1678 }
1679 Py_SET_REFCNT(co, 0);
1680
1681 if (co->co_extra != NULL) {
1682 PyInterpreterState *interp = _PyInterpreterState_GET();
1683 _PyCodeObjectExtra *co_extra = co->co_extra;
1684
1685 for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
1686 freefunc free_extra = interp->co_extra_freefuncs[i];
1687
1688 if (free_extra != NULL) {
1689 free_extra(co_extra->ce_extras[i]);
1690 }
1691 }
1692
1693 PyMem_Free(co_extra);
1694 }
1695
1696 Py_XDECREF(co->co_consts);
1697 Py_XDECREF(co->co_names);
1698 Py_XDECREF(co->co_localsplusnames);
1699 Py_XDECREF(co->co_localspluskinds);
1700 Py_XDECREF(co->co_filename);
1701 Py_XDECREF(co->co_name);
1702 Py_XDECREF(co->co_qualname);
1703 Py_XDECREF(co->co_linetable);
1704 Py_XDECREF(co->co_exceptiontable);
1705 if (co->_co_cached != NULL) {
1706 Py_XDECREF(co->_co_cached->_co_code);
1707 Py_XDECREF(co->_co_cached->_co_cellvars);
1708 Py_XDECREF(co->_co_cached->_co_freevars);
1709 Py_XDECREF(co->_co_cached->_co_varnames);
1710 PyMem_Free(co->_co_cached);
1711 }
1712 if (co->co_weakreflist != NULL) {
1713 PyObject_ClearWeakRefs((PyObject*)co);
1714 }
1715 free_monitoring_data(co->_co_monitoring);
1716 PyObject_Free(co);
1717 }
1718
1719 static PyObject *
1720 code_repr(PyCodeObject *co)
1721 {
1722 int lineno;
1723 if (co->co_firstlineno != 0)
1724 lineno = co->co_firstlineno;
1725 else
1726 lineno = -1;
1727 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
1728 return PyUnicode_FromFormat(
1729 "<code object %U at %p, file \"%U\", line %d>",
1730 co->co_name, co, co->co_filename, lineno);
1731 } else {
1732 return PyUnicode_FromFormat(
1733 "<code object %U at %p, file ???, line %d>",
1734 co->co_name, co, lineno);
1735 }
1736 }
1737
1738 static PyObject *
1739 code_richcompare(PyObject *self, PyObject *other, int op)
1740 {
1741 PyCodeObject *co, *cp;
1742 int eq;
1743 PyObject *consts1, *consts2;
1744 PyObject *res;
1745
1746 if ((op != Py_EQ && op != Py_NE) ||
1747 !PyCode_Check(self) ||
1748 !PyCode_Check(other)) {
1749 Py_RETURN_NOTIMPLEMENTED;
1750 }
1751
1752 co = (PyCodeObject *)self;
1753 cp = (PyCodeObject *)other;
1754
1755 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
1756 if (!eq) goto unequal;
1757 eq = co->co_argcount == cp->co_argcount;
1758 if (!eq) goto unequal;
1759 eq = co->co_posonlyargcount == cp->co_posonlyargcount;
1760 if (!eq) goto unequal;
1761 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
1762 if (!eq) goto unequal;
1763 eq = co->co_flags == cp->co_flags;
1764 if (!eq) goto unequal;
1765 eq = co->co_firstlineno == cp->co_firstlineno;
1766 if (!eq) goto unequal;
1767 eq = Py_SIZE(co) == Py_SIZE(cp);
1768 if (!eq) {
1769 goto unequal;
1770 }
1771 for (int i = 0; i < Py_SIZE(co); i++) {
1772 _Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];
1773 _Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i];
1774 co_instr.op.code = _PyOpcode_Deopt[co_instr.op.code];
1775 cp_instr.op.code = _PyOpcode_Deopt[cp_instr.op.code];
1776 eq = co_instr.cache == cp_instr.cache;
1777 if (!eq) {
1778 goto unequal;
1779 }
1780 i += _PyOpcode_Caches[co_instr.op.code];
1781 }
1782
1783 /* compare constants */
1784 consts1 = _PyCode_ConstantKey(co->co_consts);
1785 if (!consts1)
1786 return NULL;
1787 consts2 = _PyCode_ConstantKey(cp->co_consts);
1788 if (!consts2) {
1789 Py_DECREF(consts1);
1790 return NULL;
1791 }
1792 eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
1793 Py_DECREF(consts1);
1794 Py_DECREF(consts2);
1795 if (eq <= 0) goto unequal;
1796
1797 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
1798 if (eq <= 0) goto unequal;
1799 eq = PyObject_RichCompareBool(co->co_localsplusnames,
1800 cp->co_localsplusnames, Py_EQ);
1801 if (eq <= 0) goto unequal;
1802 eq = PyObject_RichCompareBool(co->co_linetable, cp->co_linetable, Py_EQ);
1803 if (eq <= 0) {
1804 goto unequal;
1805 }
1806 eq = PyObject_RichCompareBool(co->co_exceptiontable,
1807 cp->co_exceptiontable, Py_EQ);
1808 if (eq <= 0) {
1809 goto unequal;
1810 }
1811
1812 if (op == Py_EQ)
1813 res = Py_True;
1814 else
1815 res = Py_False;
1816 goto done;
1817
1818 unequal:
1819 if (eq < 0)
1820 return NULL;
1821 if (op == Py_NE)
1822 res = Py_True;
1823 else
1824 res = Py_False;
1825
1826 done:
1827 return Py_NewRef(res);
1828 }
1829
1830 static Py_hash_t
1831 code_hash(PyCodeObject *co)
1832 {
1833 Py_uhash_t uhash = 20221211;
1834 #define SCRAMBLE_IN(H) do { \
1835 uhash ^= (Py_uhash_t)(H); \
1836 uhash *= _PyHASH_MULTIPLIER; \
1837 } while (0)
1838 #define SCRAMBLE_IN_HASH(EXPR) do { \
1839 Py_hash_t h = PyObject_Hash(EXPR); \
1840 if (h == -1) { \
1841 return -1; \
1842 } \
1843 SCRAMBLE_IN(h); \
1844 } while (0)
1845
1846 SCRAMBLE_IN_HASH(co->co_name);
1847 SCRAMBLE_IN_HASH(co->co_consts);
1848 SCRAMBLE_IN_HASH(co->co_names);
1849 SCRAMBLE_IN_HASH(co->co_localsplusnames);
1850 SCRAMBLE_IN_HASH(co->co_linetable);
1851 SCRAMBLE_IN_HASH(co->co_exceptiontable);
1852 SCRAMBLE_IN(co->co_argcount);
1853 SCRAMBLE_IN(co->co_posonlyargcount);
1854 SCRAMBLE_IN(co->co_kwonlyargcount);
1855 SCRAMBLE_IN(co->co_flags);
1856 SCRAMBLE_IN(co->co_firstlineno);
1857 SCRAMBLE_IN(Py_SIZE(co));
1858 for (int i = 0; i < Py_SIZE(co); i++) {
1859 int deop = _Py_GetBaseOpcode(co, i);
1860 SCRAMBLE_IN(deop);
1861 SCRAMBLE_IN(_PyCode_CODE(co)[i].op.arg);
1862 i += _PyOpcode_Caches[deop];
1863 }
1864 if ((Py_hash_t)uhash == -1) {
1865 return -2;
1866 }
1867 return (Py_hash_t)uhash;
1868 }
1869
1870
1871 #define OFF(x) offsetof(PyCodeObject, x)
1872
1873 static PyMemberDef code_memberlist[] = {
1874 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
1875 {"co_posonlyargcount", T_INT, OFF(co_posonlyargcount), READONLY},
1876 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
1877 {"co_stacksize", T_INT, OFF(co_stacksize), READONLY},
1878 {"co_flags", T_INT, OFF(co_flags), READONLY},
1879 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
1880 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
1881 {"co_names", T_OBJECT, OFF(co_names), READONLY},
1882 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
1883 {"co_name", T_OBJECT, OFF(co_name), READONLY},
1884 {"co_qualname", T_OBJECT, OFF(co_qualname), READONLY},
1885 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
1886 {"co_linetable", T_OBJECT, OFF(co_linetable), READONLY},
1887 {"co_exceptiontable", T_OBJECT, OFF(co_exceptiontable), READONLY},
1888 {NULL} /* Sentinel */
1889 };
1890
1891
1892 static PyObject *
1893 code_getlnotab(PyCodeObject *code, void *closure)
1894 {
1895 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1896 "co_lnotab is deprecated, use co_lines instead.",
1897 1) < 0) {
1898 return NULL;
1899 }
1900 return decode_linetable(code);
1901 }
1902
1903 static PyObject *
1904 code_getvarnames(PyCodeObject *code, void *closure)
1905 {
1906 return _PyCode_GetVarnames(code);
1907 }
1908
1909 static PyObject *
1910 code_getcellvars(PyCodeObject *code, void *closure)
1911 {
1912 return _PyCode_GetCellvars(code);
1913 }
1914
1915 static PyObject *
1916 code_getfreevars(PyCodeObject *code, void *closure)
1917 {
1918 return _PyCode_GetFreevars(code);
1919 }
1920
1921 static PyObject *
1922 code_getcodeadaptive(PyCodeObject *code, void *closure)
1923 {
1924 return PyBytes_FromStringAndSize(code->co_code_adaptive,
1925 _PyCode_NBYTES(code));
1926 }
1927
1928 static PyObject *
1929 code_getcode(PyCodeObject *code, void *closure)
1930 {
1931 return _PyCode_GetCode(code);
1932 }
1933
1934 static PyGetSetDef code_getsetlist[] = {
1935 {"co_lnotab", (getter)code_getlnotab, NULL, NULL},
1936 {"_co_code_adaptive", (getter)code_getcodeadaptive, NULL, NULL},
1937 // The following old names are kept for backward compatibility.
1938 {"co_varnames", (getter)code_getvarnames, NULL, NULL},
1939 {"co_cellvars", (getter)code_getcellvars, NULL, NULL},
1940 {"co_freevars", (getter)code_getfreevars, NULL, NULL},
1941 {"co_code", (getter)code_getcode, NULL, NULL},
1942 {0}
1943 };
1944
1945
1946 static PyObject *
1947 code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
1948 {
1949 size_t res = _PyObject_VAR_SIZE(Py_TYPE(co), Py_SIZE(co));
1950 _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
1951 if (co_extra != NULL) {
1952 res += sizeof(_PyCodeObjectExtra);
1953 res += ((size_t)co_extra->ce_size - 1) * sizeof(co_extra->ce_extras[0]);
1954 }
1955 return PyLong_FromSize_t(res);
1956 }
1957
1958 static PyObject *
1959 code_linesiterator(PyCodeObject *code, PyObject *Py_UNUSED(args))
1960 {
1961 return (PyObject *)new_linesiterator(code);
1962 }
1963
1964 /*[clinic input]
1965 @text_signature "($self, /, **changes)"
1966 code.replace
1967
1968 *
1969 co_argcount: int(c_default="self->co_argcount") = unchanged
1970 co_posonlyargcount: int(c_default="self->co_posonlyargcount") = unchanged
1971 co_kwonlyargcount: int(c_default="self->co_kwonlyargcount") = unchanged
1972 co_nlocals: int(c_default="self->co_nlocals") = unchanged
1973 co_stacksize: int(c_default="self->co_stacksize") = unchanged
1974 co_flags: int(c_default="self->co_flags") = unchanged
1975 co_firstlineno: int(c_default="self->co_firstlineno") = unchanged
1976 co_code: object(subclass_of="&PyBytes_Type", c_default="NULL") = unchanged
1977 co_consts: object(subclass_of="&PyTuple_Type", c_default="self->co_consts") = unchanged
1978 co_names: object(subclass_of="&PyTuple_Type", c_default="self->co_names") = unchanged
1979 co_varnames: object(subclass_of="&PyTuple_Type", c_default="NULL") = unchanged
1980 co_freevars: object(subclass_of="&PyTuple_Type", c_default="NULL") = unchanged
1981 co_cellvars: object(subclass_of="&PyTuple_Type", c_default="NULL") = unchanged
1982 co_filename: unicode(c_default="self->co_filename") = unchanged
1983 co_name: unicode(c_default="self->co_name") = unchanged
1984 co_qualname: unicode(c_default="self->co_qualname") = unchanged
1985 co_linetable: object(subclass_of="&PyBytes_Type", c_default="self->co_linetable") = unchanged
1986 co_exceptiontable: object(subclass_of="&PyBytes_Type", c_default="self->co_exceptiontable") = unchanged
1987
1988 Return a copy of the code object with new values for the specified fields.
1989 [clinic start generated code]*/
1990
1991 static PyObject *
1992 code_replace_impl(PyCodeObject *self, int co_argcount,
1993 int co_posonlyargcount, int co_kwonlyargcount,
1994 int co_nlocals, int co_stacksize, int co_flags,
1995 int co_firstlineno, PyObject *co_code, PyObject *co_consts,
1996 PyObject *co_names, PyObject *co_varnames,
1997 PyObject *co_freevars, PyObject *co_cellvars,
1998 PyObject *co_filename, PyObject *co_name,
1999 PyObject *co_qualname, PyObject *co_linetable,
2000 PyObject *co_exceptiontable)
2001 /*[clinic end generated code: output=e75c48a15def18b9 input=18e280e07846c122]*/
2002 {
2003 #define CHECK_INT_ARG(ARG) \
2004 if (ARG < 0) { \
2005 PyErr_SetString(PyExc_ValueError, \
2006 #ARG " must be a positive integer"); \
2007 return NULL; \
2008 }
2009
2010 CHECK_INT_ARG(co_argcount);
2011 CHECK_INT_ARG(co_posonlyargcount);
2012 CHECK_INT_ARG(co_kwonlyargcount);
2013 CHECK_INT_ARG(co_nlocals);
2014 CHECK_INT_ARG(co_stacksize);
2015 CHECK_INT_ARG(co_flags);
2016 CHECK_INT_ARG(co_firstlineno);
2017
2018 #undef CHECK_INT_ARG
2019
2020 PyObject *code = NULL;
2021 if (co_code == NULL) {
2022 code = _PyCode_GetCode(self);
2023 if (code == NULL) {
2024 return NULL;
2025 }
2026 co_code = code;
2027 }
2028
2029 if (PySys_Audit("code.__new__", "OOOiiiiii",
2030 co_code, co_filename, co_name, co_argcount,
2031 co_posonlyargcount, co_kwonlyargcount, co_nlocals,
2032 co_stacksize, co_flags) < 0) {
2033 Py_XDECREF(code);
2034 return NULL;
2035 }
2036
2037 PyCodeObject *co = NULL;
2038 PyObject *varnames = NULL;
2039 PyObject *cellvars = NULL;
2040 PyObject *freevars = NULL;
2041 if (co_varnames == NULL) {
2042 varnames = get_localsplus_names(self, CO_FAST_LOCAL, self->co_nlocals);
2043 if (varnames == NULL) {
2044 goto error;
2045 }
2046 co_varnames = varnames;
2047 }
2048 if (co_cellvars == NULL) {
2049 cellvars = get_localsplus_names(self, CO_FAST_CELL, self->co_ncellvars);
2050 if (cellvars == NULL) {
2051 goto error;
2052 }
2053 co_cellvars = cellvars;
2054 }
2055 if (co_freevars == NULL) {
2056 freevars = get_localsplus_names(self, CO_FAST_FREE, self->co_nfreevars);
2057 if (freevars == NULL) {
2058 goto error;
2059 }
2060 co_freevars = freevars;
2061 }
2062
2063 co = PyCode_NewWithPosOnlyArgs(
2064 co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
2065 co_stacksize, co_flags, co_code, co_consts, co_names,
2066 co_varnames, co_freevars, co_cellvars, co_filename, co_name,
2067 co_qualname, co_firstlineno,
2068 co_linetable, co_exceptiontable);
2069
2070 error:
2071 Py_XDECREF(code);
2072 Py_XDECREF(varnames);
2073 Py_XDECREF(cellvars);
2074 Py_XDECREF(freevars);
2075 return (PyObject *)co;
2076 }
2077
2078 /*[clinic input]
2079 code._varname_from_oparg
2080
2081 oparg: int
2082
2083 (internal-only) Return the local variable name for the given oparg.
2084
2085 WARNING: this method is for internal use only and may change or go away.
2086 [clinic start generated code]*/
2087
2088 static PyObject *
2089 code__varname_from_oparg_impl(PyCodeObject *self, int oparg)
2090 /*[clinic end generated code: output=1fd1130413184206 input=c5fa3ee9bac7d4ca]*/
2091 {
2092 PyObject *name = PyTuple_GetItem(self->co_localsplusnames, oparg);
2093 if (name == NULL) {
2094 return NULL;
2095 }
2096 return Py_NewRef(name);
2097 }
2098
2099 /* XXX code objects need to participate in GC? */
2100
2101 static struct PyMethodDef code_methods[] = {
2102 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
2103 {"co_lines", (PyCFunction)code_linesiterator, METH_NOARGS},
2104 {"co_positions", (PyCFunction)code_positionsiterator, METH_NOARGS},
2105 CODE_REPLACE_METHODDEF
2106 CODE__VARNAME_FROM_OPARG_METHODDEF
2107 {NULL, NULL} /* sentinel */
2108 };
2109
2110
2111 PyTypeObject PyCode_Type = {
2112 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2113 "code",
2114 offsetof(PyCodeObject, co_code_adaptive),
2115 sizeof(_Py_CODEUNIT),
2116 (destructor)code_dealloc, /* tp_dealloc */
2117 0, /* tp_vectorcall_offset */
2118 0, /* tp_getattr */
2119 0, /* tp_setattr */
2120 0, /* tp_as_async */
2121 (reprfunc)code_repr, /* tp_repr */
2122 0, /* tp_as_number */
2123 0, /* tp_as_sequence */
2124 0, /* tp_as_mapping */
2125 (hashfunc)code_hash, /* tp_hash */
2126 0, /* tp_call */
2127 0, /* tp_str */
2128 PyObject_GenericGetAttr, /* tp_getattro */
2129 0, /* tp_setattro */
2130 0, /* tp_as_buffer */
2131 Py_TPFLAGS_DEFAULT, /* tp_flags */
2132 code_new__doc__, /* tp_doc */
2133 0, /* tp_traverse */
2134 0, /* tp_clear */
2135 code_richcompare, /* tp_richcompare */
2136 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
2137 0, /* tp_iter */
2138 0, /* tp_iternext */
2139 code_methods, /* tp_methods */
2140 code_memberlist, /* tp_members */
2141 code_getsetlist, /* tp_getset */
2142 0, /* tp_base */
2143 0, /* tp_dict */
2144 0, /* tp_descr_get */
2145 0, /* tp_descr_set */
2146 0, /* tp_dictoffset */
2147 0, /* tp_init */
2148 0, /* tp_alloc */
2149 code_new, /* tp_new */
2150 };
2151
2152
2153 /******************
2154 * other API
2155 ******************/
2156
2157 PyObject*
2158 _PyCode_ConstantKey(PyObject *op)
2159 {
2160 PyObject *key;
2161
2162 /* Py_None and Py_Ellipsis are singletons. */
2163 if (op == Py_None || op == Py_Ellipsis
2164 || PyLong_CheckExact(op)
2165 || PyUnicode_CheckExact(op)
2166 /* code_richcompare() uses _PyCode_ConstantKey() internally */
2167 || PyCode_Check(op))
2168 {
2169 /* Objects of these types are always different from object of other
2170 * type and from tuples. */
2171 key = Py_NewRef(op);
2172 }
2173 else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
2174 /* Make booleans different from integers 0 and 1.
2175 * Avoid BytesWarning from comparing bytes with strings. */
2176 key = PyTuple_Pack(2, Py_TYPE(op), op);
2177 }
2178 else if (PyFloat_CheckExact(op)) {
2179 double d = PyFloat_AS_DOUBLE(op);
2180 /* all we need is to make the tuple different in either the 0.0
2181 * or -0.0 case from all others, just to avoid the "coercion".
2182 */
2183 if (d == 0.0 && copysign(1.0, d) < 0.0)
2184 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
2185 else
2186 key = PyTuple_Pack(2, Py_TYPE(op), op);
2187 }
2188 else if (PyComplex_CheckExact(op)) {
2189 Py_complex z;
2190 int real_negzero, imag_negzero;
2191 /* For the complex case we must make complex(x, 0.)
2192 different from complex(x, -0.) and complex(0., y)
2193 different from complex(-0., y), for any x and y.
2194 All four complex zeros must be distinguished.*/
2195 z = PyComplex_AsCComplex(op);
2196 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
2197 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
2198 /* use True, False and None singleton as tags for the real and imag
2199 * sign, to make tuples different */
2200 if (real_negzero && imag_negzero) {
2201 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
2202 }
2203 else if (imag_negzero) {
2204 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
2205 }
2206 else if (real_negzero) {
2207 key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
2208 }
2209 else {
2210 key = PyTuple_Pack(2, Py_TYPE(op), op);
2211 }
2212 }
2213 else if (PyTuple_CheckExact(op)) {
2214 Py_ssize_t i, len;
2215 PyObject *tuple;
2216
2217 len = PyTuple_GET_SIZE(op);
2218 tuple = PyTuple_New(len);
2219 if (tuple == NULL)
2220 return NULL;
2221
2222 for (i=0; i < len; i++) {
2223 PyObject *item, *item_key;
2224
2225 item = PyTuple_GET_ITEM(op, i);
2226 item_key = _PyCode_ConstantKey(item);
2227 if (item_key == NULL) {
2228 Py_DECREF(tuple);
2229 return NULL;
2230 }
2231
2232 PyTuple_SET_ITEM(tuple, i, item_key);
2233 }
2234
2235 key = PyTuple_Pack(2, tuple, op);
2236 Py_DECREF(tuple);
2237 }
2238 else if (PyFrozenSet_CheckExact(op)) {
2239 Py_ssize_t pos = 0;
2240 PyObject *item;
2241 Py_hash_t hash;
2242 Py_ssize_t i, len;
2243 PyObject *tuple, *set;
2244
2245 len = PySet_GET_SIZE(op);
2246 tuple = PyTuple_New(len);
2247 if (tuple == NULL)
2248 return NULL;
2249
2250 i = 0;
2251 while (_PySet_NextEntry(op, &pos, &item, &hash)) {
2252 PyObject *item_key;
2253
2254 item_key = _PyCode_ConstantKey(item);
2255 if (item_key == NULL) {
2256 Py_DECREF(tuple);
2257 return NULL;
2258 }
2259
2260 assert(i < len);
2261 PyTuple_SET_ITEM(tuple, i, item_key);
2262 i++;
2263 }
2264 set = PyFrozenSet_New(tuple);
2265 Py_DECREF(tuple);
2266 if (set == NULL)
2267 return NULL;
2268
2269 key = PyTuple_Pack(2, set, op);
2270 Py_DECREF(set);
2271 return key;
2272 }
2273 else {
2274 /* for other types, use the object identifier as a unique identifier
2275 * to ensure that they are seen as unequal. */
2276 PyObject *obj_id = PyLong_FromVoidPtr(op);
2277 if (obj_id == NULL)
2278 return NULL;
2279
2280 key = PyTuple_Pack(2, obj_id, op);
2281 Py_DECREF(obj_id);
2282 }
2283 return key;
2284 }
2285
2286 void
2287 _PyStaticCode_Fini(PyCodeObject *co)
2288 {
2289 deopt_code(co, _PyCode_CODE(co));
2290 PyMem_Free(co->co_extra);
2291 if (co->_co_cached != NULL) {
2292 Py_CLEAR(co->_co_cached->_co_code);
2293 Py_CLEAR(co->_co_cached->_co_cellvars);
2294 Py_CLEAR(co->_co_cached->_co_freevars);
2295 Py_CLEAR(co->_co_cached->_co_varnames);
2296 PyMem_Free(co->_co_cached);
2297 co->_co_cached = NULL;
2298 }
2299 co->co_extra = NULL;
2300 if (co->co_weakreflist != NULL) {
2301 PyObject_ClearWeakRefs((PyObject *)co);
2302 co->co_weakreflist = NULL;
2303 }
2304 free_monitoring_data(co->_co_monitoring);
2305 co->_co_monitoring = NULL;
2306 }
2307
2308 int
2309 _PyStaticCode_Init(PyCodeObject *co)
2310 {
2311 int res = intern_strings(co->co_names);
2312 if (res < 0) {
2313 return -1;
2314 }
2315 res = intern_string_constants(co->co_consts, NULL);
2316 if (res < 0) {
2317 return -1;
2318 }
2319 res = intern_strings(co->co_localsplusnames);
2320 if (res < 0) {
2321 return -1;
2322 }
2323 _PyCode_Quicken(co);
2324 return 0;
2325 }
2326
2327 #define MAX_CODE_UNITS_PER_LOC_ENTRY 8
2328
2329 PyCodeObject *
2330 _Py_MakeShimCode(const _PyShimCodeDef *codedef)
2331 {
2332 PyObject *name = NULL;
2333 PyObject *co_code = NULL;
2334 PyObject *lines = NULL;
2335 PyCodeObject *codeobj = NULL;
2336 uint8_t *loc_table = NULL;
2337
2338 name = _PyUnicode_FromASCII(codedef->cname, strlen(codedef->cname));
2339 if (name == NULL) {
2340 goto cleanup;
2341 }
2342 co_code = PyBytes_FromStringAndSize(
2343 (const char *)codedef->code, codedef->codelen);
2344 if (co_code == NULL) {
2345 goto cleanup;
2346 }
2347 int code_units = codedef->codelen / sizeof(_Py_CODEUNIT);
2348 int loc_entries = (code_units + MAX_CODE_UNITS_PER_LOC_ENTRY - 1) /
2349 MAX_CODE_UNITS_PER_LOC_ENTRY;
2350 loc_table = PyMem_Malloc(loc_entries);
2351 if (loc_table == NULL) {
2352 PyErr_NoMemory();
2353 goto cleanup;
2354 }
2355 for (int i = 0; i < loc_entries-1; i++) {
2356 loc_table[i] = 0x80 | (PY_CODE_LOCATION_INFO_NONE << 3) | 7;
2357 code_units -= MAX_CODE_UNITS_PER_LOC_ENTRY;
2358 }
2359 assert(loc_entries > 0);
2360 assert(code_units > 0 && code_units <= MAX_CODE_UNITS_PER_LOC_ENTRY);
2361 loc_table[loc_entries-1] = 0x80 |
2362 (PY_CODE_LOCATION_INFO_NONE << 3) | (code_units-1);
2363 lines = PyBytes_FromStringAndSize((const char *)loc_table, loc_entries);
2364 PyMem_Free(loc_table);
2365 if (lines == NULL) {
2366 goto cleanup;
2367 }
2368 _Py_DECLARE_STR(shim_name, "<shim>");
2369 struct _PyCodeConstructor con = {
2370 .filename = &_Py_STR(shim_name),
2371 .name = name,
2372 .qualname = name,
2373 .flags = CO_NEWLOCALS | CO_OPTIMIZED,
2374
2375 .code = co_code,
2376 .firstlineno = 1,
2377 .linetable = lines,
2378
2379 .consts = (PyObject *)&_Py_SINGLETON(tuple_empty),
2380 .names = (PyObject *)&_Py_SINGLETON(tuple_empty),
2381
2382 .localsplusnames = (PyObject *)&_Py_SINGLETON(tuple_empty),
2383 .localspluskinds = (PyObject *)&_Py_SINGLETON(bytes_empty),
2384
2385 .argcount = 0,
2386 .posonlyargcount = 0,
2387 .kwonlyargcount = 0,
2388
2389 .stacksize = codedef->stacksize,
2390
2391 .exceptiontable = (PyObject *)&_Py_SINGLETON(bytes_empty),
2392 };
2393
2394 codeobj = _PyCode_New(&con);
2395 cleanup:
2396 Py_XDECREF(name);
2397 Py_XDECREF(co_code);
2398 Py_XDECREF(lines);
2399 return codeobj;
2400 }