Debian Codegraph @

Lib/test/test_code.py

raw
1 """This module includes tests of the code object representation.
2
3 >>> def f(x):
4 ... def g(y):
5 ... return x + y
6 ... return g
7 ...
8
9 >>> dump(f.__code__)
10 name: f
11 argcount: 1
12 posonlyargcount: 0
13 kwonlyargcount: 0
14 names: ()
15 varnames: ('x', 'g')
16 cellvars: ('x',)
17 freevars: ()
18 nlocals: 2
19 flags: 3
20 consts: ('<code object g>',)
21
22 >>> dump(f(4).__code__)
23 name: g
24 argcount: 1
25 posonlyargcount: 0
26 kwonlyargcount: 0
27 names: ()
28 varnames: ('y',)
29 cellvars: ()
30 freevars: ('x',)
31 nlocals: 1
32 flags: 19
33 consts: ('None',)
34
35 >>> def h(x, y):
36 ... a = x + y
37 ... b = x - y
38 ... c = a * b
39 ... return c
40 ...
41
42 >>> dump(h.__code__)
43 name: h
44 argcount: 2
45 posonlyargcount: 0
46 kwonlyargcount: 0
47 names: ()
48 varnames: ('x', 'y', 'a', 'b', 'c')
49 cellvars: ()
50 freevars: ()
51 nlocals: 5
52 flags: 3
53 consts: ('None',)
54
55 >>> def attrs(obj):
56 ... print(obj.attr1)
57 ... print(obj.attr2)
58 ... print(obj.attr3)
59
60 >>> dump(attrs.__code__)
61 name: attrs
62 argcount: 1
63 posonlyargcount: 0
64 kwonlyargcount: 0
65 names: ('print', 'attr1', 'attr2', 'attr3')
66 varnames: ('obj',)
67 cellvars: ()
68 freevars: ()
69 nlocals: 1
70 flags: 3
71 consts: ('None',)
72
73 >>> def optimize_away():
74 ... 'doc string'
75 ... 'not a docstring'
76 ... 53
77 ... 0x53
78
79 >>> dump(optimize_away.__code__)
80 name: optimize_away
81 argcount: 0
82 posonlyargcount: 0
83 kwonlyargcount: 0
84 names: ()
85 varnames: ()
86 cellvars: ()
87 freevars: ()
88 nlocals: 0
89 flags: 67108867
90 consts: ("'doc string'", 'None')
91
92 >>> def keywordonly_args(a,b,*,k1):
93 ... return a,b,k1
94 ...
95
96 >>> dump(keywordonly_args.__code__)
97 name: keywordonly_args
98 argcount: 2
99 posonlyargcount: 0
100 kwonlyargcount: 1
101 names: ()
102 varnames: ('a', 'b', 'k1')
103 cellvars: ()
104 freevars: ()
105 nlocals: 3
106 flags: 3
107 consts: ('None',)
108
109 >>> def posonly_args(a,b,/,c):
110 ... return a,b,c
111 ...
112
113 >>> dump(posonly_args.__code__)
114 name: posonly_args
115 argcount: 3
116 posonlyargcount: 2
117 kwonlyargcount: 0
118 names: ()
119 varnames: ('a', 'b', 'c')
120 cellvars: ()
121 freevars: ()
122 nlocals: 3
123 flags: 3
124 consts: ('None',)
125
126 >>> def has_docstring(x: str):
127 ... 'This is a one-line doc string'
128 ... x += x
129 ... x += "hello world"
130 ... # co_flags should be 0x4000003 = 67108867
131 ... return x
132
133 >>> dump(has_docstring.__code__)
134 name: has_docstring
135 argcount: 1
136 posonlyargcount: 0
137 kwonlyargcount: 0
138 names: ()
139 varnames: ('x',)
140 cellvars: ()
141 freevars: ()
142 nlocals: 1
143 flags: 67108867
144 consts: ("'This is a one-line doc string'", "'hello world'")
145
146 >>> async def async_func_docstring(x: str, y: str):
147 ... "This is a docstring from async function"
148 ... import asyncio
149 ... await asyncio.sleep(1)
150 ... # co_flags should be 0x4000083 = 67108995
151 ... return x + y
152
153 >>> dump(async_func_docstring.__code__)
154 name: async_func_docstring
155 argcount: 2
156 posonlyargcount: 0
157 kwonlyargcount: 0
158 names: ('asyncio', 'sleep')
159 varnames: ('x', 'y', 'asyncio')
160 cellvars: ()
161 freevars: ()
162 nlocals: 3
163 flags: 67108995
164 consts: ("'This is a docstring from async function'", 'None')
165
166 >>> def no_docstring(x, y, z):
167 ... return x + "hello" + y + z + "world"
168
169 >>> dump(no_docstring.__code__)
170 name: no_docstring
171 argcount: 3
172 posonlyargcount: 0
173 kwonlyargcount: 0
174 names: ()
175 varnames: ('x', 'y', 'z')
176 cellvars: ()
177 freevars: ()
178 nlocals: 3
179 flags: 3
180 consts: ("'hello'", "'world'")
181
182 >>> class class_with_docstring:
183 ... '''This is a docstring for class'''
184 ... '''This line is not docstring'''
185 ... pass
186
187 >>> print(class_with_docstring.__doc__)
188 This is a docstring for class
189
190 >>> class class_without_docstring:
191 ... pass
192
193 >>> print(class_without_docstring.__doc__)
194 None
195 """
196
197 import copy
198 import inspect
199 import sys
200 import threading
201 import doctest
202 import unittest
203 import textwrap
204 import weakref
205 import dis
206
207 try:
208 import ctypes
209 except ImportError:
210 ctypes = None
211 from test.support import (cpython_only,
212 check_impl_detail, requires_debug_ranges,
213 gc_collect, Py_GIL_DISABLED, late_deletion)
214 from test.support.script_helper import assert_python_ok
215 from test.support import threading_helper, import_helper
216 from test.support.bytecode_helper import instructions_with_positions
217 from opcode import opmap, opname
218 from _testcapi import code_offset_to_line
219 try:
220 import _testinternalcapi
221 except ModuleNotFoundError:
222 _testinternalcapi = None
223 import test._code_definitions as defs
224
225 COPY_FREE_VARS = opmap['COPY_FREE_VARS']
226
227
228 def consts(t):
229 """Yield a doctest-safe sequence of object reprs."""
230 for elt in t:
231 r = repr(elt)
232 if r.startswith("<code object"):
233 yield "<code object %s>" % elt.co_name
234 else:
235 yield r
236
237 def dump(co):
238 """Print out a text representation of a code object."""
239 for attr in ["name", "argcount", "posonlyargcount",
240 "kwonlyargcount", "names", "varnames",
241 "cellvars", "freevars", "nlocals", "flags"]:
242 print("%s: %s" % (attr, getattr(co, "co_" + attr)))
243 print("consts:", tuple(consts(co.co_consts)))
244
245 # Needed for test_closure_injection below
246 # Defined at global scope to avoid implicitly closing over __class__
247 def external_getitem(self, i):
248 return f"Foreign getitem: {super().__getitem__(i)}"
249
250
251 class CodeTest(unittest.TestCase):
252
253 @cpython_only
254 def test_newempty(self):
255 _testcapi = import_helper.import_module("_testcapi")
256 co = _testcapi.code_newempty("filename", "funcname", 15)
257 self.assertEqual(co.co_filename, "filename")
258 self.assertEqual(co.co_name, "funcname")
259 self.assertEqual(co.co_firstlineno, 15)
260 #Empty code object should raise, but not crash the VM
261 with self.assertRaises(Exception):
262 exec(co)
263
264 @cpython_only
265 def test_closure_injection(self):
266 # From https://bugs.python.org/issue32176
267 from types import FunctionType
268
269 def create_closure(__class__):
270 return (lambda: __class__).__closure__
271
272 def new_code(c):
273 '''A new code object with a __class__ cell added to freevars'''
274 return c.replace(co_freevars=c.co_freevars + ('__class__',), co_code=bytes([COPY_FREE_VARS, 1])+c.co_code)
275
276 def add_foreign_method(cls, name, f):
277 code = new_code(f.__code__)
278 assert not f.__closure__
279 closure = create_closure(cls)
280 defaults = f.__defaults__
281 setattr(cls, name, FunctionType(code, globals(), name, defaults, closure))
282
283 class List(list):
284 pass
285
286 add_foreign_method(List, "__getitem__", external_getitem)
287
288 # Ensure the closure injection actually worked
289 function = List.__getitem__
290 class_ref = function.__closure__[0].cell_contents
291 self.assertIs(class_ref, List)
292
293 # Ensure the zero-arg super() call in the injected method works
294 obj = List([1, 2, 3])
295 self.assertEqual(obj[0], "Foreign getitem: 1")
296
297 def test_constructor(self):
298 def func(): pass
299 co = func.__code__
300 CodeType = type(co)
301
302 # test code constructor
303 CodeType(co.co_argcount,
304 co.co_posonlyargcount,
305 co.co_kwonlyargcount,
306 co.co_nlocals,
307 co.co_stacksize,
308 co.co_flags,
309 co.co_code,
310 co.co_consts,
311 co.co_names,
312 co.co_varnames,
313 co.co_filename,
314 co.co_name,
315 co.co_qualname,
316 co.co_firstlineno,
317 co.co_linetable,
318 co.co_exceptiontable,
319 co.co_freevars,
320 co.co_cellvars)
321
322 def test_qualname(self):
323 self.assertEqual(
324 CodeTest.test_qualname.__code__.co_qualname,
325 CodeTest.test_qualname.__qualname__
326 )
327
328 def test_replace(self):
329 def func():
330 x = 1
331 return x
332 code = func.__code__
333
334 # Different co_name, co_varnames, co_consts.
335 # Must have the same number of constants and
336 # variables or we get crashes.
337 def func2():
338 y = 2
339 return y
340 code2 = func2.__code__
341
342 for attr, value in (
343 ("co_argcount", 0),
344 ("co_posonlyargcount", 0),
345 ("co_kwonlyargcount", 0),
346 ("co_nlocals", 1),
347 ("co_stacksize", 1),
348 ("co_flags", code.co_flags | inspect.CO_COROUTINE),
349 ("co_firstlineno", 100),
350 ("co_code", code2.co_code),
351 ("co_consts", code2.co_consts),
352 ("co_names", ("myname",)),
353 ("co_varnames", ('spam',)),
354 ("co_freevars", ("freevar",)),
355 ("co_cellvars", ("cellvar",)),
356 ("co_filename", "newfilename"),
357 ("co_name", "newname"),
358 ("co_linetable", code2.co_linetable),
359 ):
360 with self.subTest(attr=attr, value=value):
361 new_code = code.replace(**{attr: value})
362 self.assertEqual(getattr(new_code, attr), value)
363 new_code = copy.replace(code, **{attr: value})
364 self.assertEqual(getattr(new_code, attr), value)
365
366 new_code = code.replace(co_varnames=code2.co_varnames,
367 co_nlocals=code2.co_nlocals)
368 self.assertEqual(new_code.co_varnames, code2.co_varnames)
369 self.assertEqual(new_code.co_nlocals, code2.co_nlocals)
370 new_code = copy.replace(code, co_varnames=code2.co_varnames,
371 co_nlocals=code2.co_nlocals)
372 self.assertEqual(new_code.co_varnames, code2.co_varnames)
373 self.assertEqual(new_code.co_nlocals, code2.co_nlocals)
374
375 def test_nlocals_mismatch(self):
376 def func():
377 x = 1
378 return x
379 co = func.__code__
380 assert co.co_nlocals > 0;
381
382 # First we try the constructor.
383 CodeType = type(co)
384 for diff in (-1, 1):
385 with self.assertRaises(ValueError):
386 CodeType(co.co_argcount,
387 co.co_posonlyargcount,
388 co.co_kwonlyargcount,
389 # This is the only change.
390 co.co_nlocals + diff,
391 co.co_stacksize,
392 co.co_flags,
393 co.co_code,
394 co.co_consts,
395 co.co_names,
396 co.co_varnames,
397 co.co_filename,
398 co.co_name,
399 co.co_qualname,
400 co.co_firstlineno,
401 co.co_linetable,
402 co.co_exceptiontable,
403 co.co_freevars,
404 co.co_cellvars,
405 )
406 # Then we try the replace method.
407 with self.assertRaises(ValueError):
408 co.replace(co_nlocals=co.co_nlocals - 1)
409 with self.assertRaises(ValueError):
410 co.replace(co_nlocals=co.co_nlocals + 1)
411
412 def test_shrinking_localsplus(self):
413 # Check that PyCode_NewWithPosOnlyArgs resizes both
414 # localsplusnames and localspluskinds, if an argument is a cell.
415 def func(arg):
416 return lambda: arg
417 code = func.__code__
418 newcode = code.replace(co_name="func") # Should not raise SystemError
419 self.assertEqual(code, newcode)
420
421 def test_empty_linetable(self):
422 def func():
423 pass
424 new_code = code = func.__code__.replace(co_linetable=b'')
425 self.assertEqual(list(new_code.co_lines()), [])
426
427 def test_co_lnotab_is_deprecated(self): # TODO: remove in 3.14
428 def func():
429 pass
430
431 with self.assertWarns(DeprecationWarning):
432 func.__code__.co_lnotab
433
434 @unittest.skipIf(_testinternalcapi is None, '_testinternalcapi is missing')
435 def test_returns_only_none(self):
436 value = True
437
438 def spam1():
439 pass
440 def spam2():
441 return
442 def spam3():
443 return None
444 def spam4():
445 if not value:
446 return
447 ...
448 def spam5():
449 if not value:
450 return None
451 ...
452 lambda1 = (lambda: None)
453 for func in [
454 spam1,
455 spam2,
456 spam3,
457 spam4,
458 spam5,
459 lambda1,
460 ]:
461 with self.subTest(func):
462 res = _testinternalcapi.code_returns_only_none(func.__code__)
463 self.assertTrue(res)
464
465 def spam6():
466 return True
467 def spam7():
468 return value
469 def spam8():
470 if value:
471 return None
472 return True
473 def spam9():
474 if value:
475 return True
476 return None
477 lambda2 = (lambda: True)
478 for func in [
479 spam6,
480 spam7,
481 spam8,
482 spam9,
483 lambda2,
484 ]:
485 with self.subTest(func):
486 res = _testinternalcapi.code_returns_only_none(func.__code__)
487 self.assertFalse(res)
488
489 def test_invalid_bytecode(self):
490 def foo():
491 pass
492
493 # assert that opcode 127 is invalid
494 self.assertEqual(opname[127], '<127>')
495
496 # change first opcode to 0x7f (=127)
497 foo.__code__ = foo.__code__.replace(
498 co_code=b'\x7f' + foo.__code__.co_code[1:])
499
500 msg = "unknown opcode 127"
501 with self.assertRaisesRegex(SystemError, msg):
502 foo()
503
504 @requires_debug_ranges()
505 def test_co_positions_artificial_instructions(self):
506 import dis
507
508 namespace = {}
509 exec(textwrap.dedent("""\
510 try:
511 1/0
512 except Exception as e:
513 exc = e
514 """), namespace)
515
516 exc = namespace['exc']
517 traceback = exc.__traceback__
518 code = traceback.tb_frame.f_code
519
520 artificial_instructions = []
521 for instr, positions in instructions_with_positions(
522 dis.get_instructions(code), code.co_positions()
523 ):
524 # If any of the positions is None, then all have to
525 # be None as well for the case above. There are still
526 # some places in the compiler, where the artificial instructions
527 # get assigned the first_lineno but they don't have other positions.
528 # There is no easy way of inferring them at that stage, so for now
529 # we don't support it.
530 self.assertIn(positions.count(None), [0, 3, 4])
531
532 if not any(positions):
533 artificial_instructions.append(instr)
534
535 self.assertEqual(
536 [
537 (instruction.opname, instruction.argval)
538 for instruction in artificial_instructions
539 ],
540 [
541 ("PUSH_EXC_INFO", None),
542 ("LOAD_CONST", None), # artificial 'None'
543 ("STORE_NAME", "e"), # XX: we know the location for this
544 ("DELETE_NAME", "e"),
545 ("RERAISE", 1),
546 ("COPY", 3),
547 ("POP_EXCEPT", None),
548 ("RERAISE", 1)
549 ]
550 )
551
552 def test_endline_and_columntable_none_when_no_debug_ranges(self):
553 # Make sure that if `-X no_debug_ranges` is used, there is
554 # minimal debug info
555 code = textwrap.dedent("""
556 def f():
557 pass
558
559 positions = f.__code__.co_positions()
560 for line, end_line, column, end_column in positions:
561 assert line == end_line
562 assert column is None
563 assert end_column is None
564 """)
565 assert_python_ok('-X', 'no_debug_ranges', '-c', code)
566
567 def test_endline_and_columntable_none_when_no_debug_ranges_env(self):
568 # Same as above but using the environment variable opt out.
569 code = textwrap.dedent("""
570 def f():
571 pass
572
573 positions = f.__code__.co_positions()
574 for line, end_line, column, end_column in positions:
575 assert line == end_line
576 assert column is None
577 assert end_column is None
578 """)
579 assert_python_ok('-c', code, PYTHONNODEBUGRANGES='1')
580
581 # co_positions behavior when info is missing.
582
583 @requires_debug_ranges()
584 def test_co_positions_empty_linetable(self):
585 def func():
586 x = 1
587 new_code = func.__code__.replace(co_linetable=b'')
588 positions = new_code.co_positions()
589 for line, end_line, column, end_column in positions:
590 self.assertIsNone(line)
591 self.assertEqual(end_line, new_code.co_firstlineno + 1)
592
593 def test_code_equality(self):
594 def f():
595 try:
596 a()
597 except:
598 b()
599 else:
600 c()
601 finally:
602 d()
603 code_a = f.__code__
604 code_b = code_a.replace(co_linetable=b"")
605 code_c = code_a.replace(co_exceptiontable=b"")
606 code_d = code_b.replace(co_exceptiontable=b"")
607 self.assertNotEqual(code_a, code_b)
608 self.assertNotEqual(code_a, code_c)
609 self.assertNotEqual(code_a, code_d)
610 self.assertNotEqual(code_b, code_c)
611 self.assertNotEqual(code_b, code_d)
612 self.assertNotEqual(code_c, code_d)
613
614 def test_code_hash_uses_firstlineno(self):
615 c1 = (lambda: 1).__code__
616 c2 = (lambda: 1).__code__
617 self.assertNotEqual(c1, c2)
618 self.assertNotEqual(hash(c1), hash(c2))
619 c3 = c1.replace(co_firstlineno=17)
620 self.assertNotEqual(c1, c3)
621 self.assertNotEqual(hash(c1), hash(c3))
622
623 def test_code_hash_uses_order(self):
624 # Swapping posonlyargcount and kwonlyargcount should change the hash.
625 c = (lambda x, y, *, z=1, w=1: 1).__code__
626 self.assertEqual(c.co_argcount, 2)
627 self.assertEqual(c.co_posonlyargcount, 0)
628 self.assertEqual(c.co_kwonlyargcount, 2)
629 swapped = c.replace(co_posonlyargcount=2, co_kwonlyargcount=0)
630 self.assertNotEqual(c, swapped)
631 self.assertNotEqual(hash(c), hash(swapped))
632
633 def test_code_hash_uses_bytecode(self):
634 c = (lambda x, y: x + y).__code__
635 d = (lambda x, y: x * y).__code__
636 c1 = c.replace(co_code=d.co_code)
637 self.assertNotEqual(c, c1)
638 self.assertNotEqual(hash(c), hash(c1))
639
640 @cpython_only
641 def test_code_equal_with_instrumentation(self):
642 """ GH-109052
643
644 Make sure the instrumentation doesn't affect the code equality
645 The validity of this test relies on the fact that "x is x" and
646 "x in x" have only one different instruction and the instructions
647 have the same argument.
648
649 """
650 code1 = compile("x is x", "example.py", "eval")
651 code2 = compile("x in x", "example.py", "eval")
652 sys._getframe().f_trace_opcodes = True
653 sys.settrace(lambda *args: None)
654 exec(code1, {'x': []})
655 exec(code2, {'x': []})
656 self.assertNotEqual(code1, code2)
657 sys.settrace(None)
658
659 @unittest.skipIf(_testinternalcapi is None, "missing _testinternalcapi")
660 def test_local_kinds(self):
661 CO_FAST_ARG_POS = 0x02
662 CO_FAST_ARG_KW = 0x04
663 CO_FAST_ARG_VAR = 0x08
664 CO_FAST_HIDDEN = 0x10
665 CO_FAST_LOCAL = 0x20
666 CO_FAST_CELL = 0x40
667 CO_FAST_FREE = 0x80
668
669 POSONLY = CO_FAST_LOCAL | CO_FAST_ARG_POS
670 POSORKW = CO_FAST_LOCAL | CO_FAST_ARG_POS | CO_FAST_ARG_KW
671 KWONLY = CO_FAST_LOCAL | CO_FAST_ARG_KW
672 VARARGS = CO_FAST_LOCAL | CO_FAST_ARG_VAR | CO_FAST_ARG_POS
673 VARKWARGS = CO_FAST_LOCAL | CO_FAST_ARG_VAR | CO_FAST_ARG_KW
674
675 funcs = {
676 defs.simple_script: {},
677 defs.complex_script: {
678 'obj': CO_FAST_LOCAL,
679 'pickle': CO_FAST_LOCAL,
680 'spam_minimal': CO_FAST_LOCAL,
681 'data': CO_FAST_LOCAL,
682 'res': CO_FAST_LOCAL,
683 },
684 defs.script_with_globals: {
685 'obj1': CO_FAST_LOCAL,
686 'obj2': CO_FAST_LOCAL,
687 },
688 defs.script_with_explicit_empty_return: {},
689 defs.script_with_return: {},
690 defs.spam_minimal: {},
691 defs.spam_with_builtins: {
692 'x': CO_FAST_LOCAL,
693 'values': CO_FAST_LOCAL,
694 'checks': CO_FAST_LOCAL,
695 'res': CO_FAST_LOCAL,
696 },
697 defs.spam_with_globals_and_builtins: {
698 'func1': CO_FAST_LOCAL,
699 'func2': CO_FAST_LOCAL,
700 'funcs': CO_FAST_LOCAL,
701 'checks': CO_FAST_LOCAL,
702 'res': CO_FAST_LOCAL,
703 },
704 defs.spam_with_global_and_attr_same_name: {},
705 defs.spam_full_args: {
706 'a': POSONLY,
707 'b': POSONLY,
708 'c': POSORKW,
709 'd': POSORKW,
710 'e': KWONLY,
711 'f': KWONLY,
712 'args': VARARGS,
713 'kwargs': VARKWARGS,
714 },
715 defs.spam_full_args_with_defaults: {
716 'a': POSONLY,
717 'b': POSONLY,
718 'c': POSORKW,
719 'd': POSORKW,
720 'e': KWONLY,
721 'f': KWONLY,
722 'args': VARARGS,
723 'kwargs': VARKWARGS,
724 },
725 defs.spam_args_attrs_and_builtins: {
726 'a': POSONLY,
727 'b': POSONLY,
728 'c': POSORKW,
729 'd': POSORKW,
730 'e': KWONLY,
731 'f': KWONLY,
732 'args': VARARGS,
733 'kwargs': VARKWARGS,
734 },
735 defs.spam_returns_arg: {
736 'x': POSORKW,
737 },
738 defs.spam_raises: {},
739 defs.spam_with_inner_not_closure: {
740 'eggs': CO_FAST_LOCAL,
741 },
742 defs.spam_with_inner_closure: {
743 'x': CO_FAST_CELL,
744 'eggs': CO_FAST_LOCAL,
745 },
746 defs.spam_annotated: {
747 'a': POSORKW,
748 'b': POSORKW,
749 'c': POSORKW,
750 },
751 defs.spam_full: {
752 'a': POSONLY,
753 'b': POSONLY,
754 'c': POSORKW,
755 'd': POSORKW,
756 'e': KWONLY,
757 'f': KWONLY,
758 'args': VARARGS,
759 'kwargs': VARKWARGS,
760 'x': CO_FAST_LOCAL,
761 'y': CO_FAST_LOCAL,
762 'z': CO_FAST_LOCAL,
763 'extras': CO_FAST_LOCAL,
764 },
765 defs.spam: {
766 'x': POSORKW,
767 },
768 defs.spam_N: {
769 'x': POSORKW,
770 'eggs_nested': CO_FAST_LOCAL,
771 },
772 defs.spam_C: {
773 'x': POSORKW | CO_FAST_CELL,
774 'a': CO_FAST_CELL,
775 'eggs_closure': CO_FAST_LOCAL,
776 },
777 defs.spam_NN: {
778 'x': POSORKW,
779 'eggs_nested_N': CO_FAST_LOCAL,
780 },
781 defs.spam_NC: {
782 'x': POSORKW | CO_FAST_CELL,
783 'a': CO_FAST_CELL,
784 'eggs_nested_C': CO_FAST_LOCAL,
785 },
786 defs.spam_CN: {
787 'x': POSORKW | CO_FAST_CELL,
788 'a': CO_FAST_CELL,
789 'eggs_closure_N': CO_FAST_LOCAL,
790 },
791 defs.spam_CC: {
792 'x': POSORKW | CO_FAST_CELL,
793 'a': CO_FAST_CELL,
794 'eggs_closure_C': CO_FAST_LOCAL,
795 },
796 defs.eggs_nested: {
797 'y': POSORKW,
798 },
799 defs.eggs_closure: {
800 'y': POSORKW,
801 'x': CO_FAST_FREE,
802 'a': CO_FAST_FREE,
803 },
804 defs.eggs_nested_N: {
805 'y': POSORKW,
806 'ham_nested': CO_FAST_LOCAL,
807 },
808 defs.eggs_nested_C: {
809 'y': POSORKW | CO_FAST_CELL,
810 'x': CO_FAST_FREE,
811 'a': CO_FAST_FREE,
812 'ham_closure': CO_FAST_LOCAL,
813 },
814 defs.eggs_closure_N: {
815 'y': POSORKW,
816 'x': CO_FAST_FREE,
817 'a': CO_FAST_FREE,
818 'ham_C_nested': CO_FAST_LOCAL,
819 },
820 defs.eggs_closure_C: {
821 'y': POSORKW | CO_FAST_CELL,
822 'b': CO_FAST_CELL,
823 'x': CO_FAST_FREE,
824 'a': CO_FAST_FREE,
825 'ham_C_closure': CO_FAST_LOCAL,
826 },
827 defs.ham_nested: {
828 'z': POSORKW,
829 },
830 defs.ham_closure: {
831 'z': POSORKW,
832 'y': CO_FAST_FREE,
833 'x': CO_FAST_FREE,
834 'a': CO_FAST_FREE,
835 },
836 defs.ham_C_nested: {
837 'z': POSORKW,
838 },
839 defs.ham_C_closure: {
840 'z': POSORKW,
841 'y': CO_FAST_FREE,
842 'b': CO_FAST_FREE,
843 'x': CO_FAST_FREE,
844 'a': CO_FAST_FREE,
845 },
846 }
847 assert len(funcs) == len(defs.FUNCTIONS)
848 for func in defs.FUNCTIONS:
849 with self.subTest(func):
850 expected = funcs[func]
851 kinds = _testinternalcapi.get_co_localskinds(func.__code__)
852 self.assertEqual(kinds, expected)
853
854 @unittest.skipIf(_testinternalcapi is None, "missing _testinternalcapi")
855 def test_var_counts(self):
856 self.maxDiff = None
857 def new_var_counts(*,
858 posonly=0,
859 posorkw=0,
860 kwonly=0,
861 varargs=0,
862 varkwargs=0,
863 purelocals=0,
864 argcells=0,
865 othercells=0,
866 freevars=0,
867 globalvars=0,
868 attrs=0,
869 unknown=0,
870 ):
871 nargvars = posonly + posorkw + kwonly + varargs + varkwargs
872 nlocals = nargvars + purelocals + othercells
873 if isinstance(globalvars, int):
874 globalvars = {
875 'total': globalvars,
876 'numglobal': 0,
877 'numbuiltin': 0,
878 'numunknown': globalvars,
879 }
880 else:
881 g_numunknown = 0
882 if isinstance(globalvars, dict):
883 numglobal = globalvars['numglobal']
884 numbuiltin = globalvars['numbuiltin']
885 size = 2
886 if 'numunknown' in globalvars:
887 g_numunknown = globalvars['numunknown']
888 size += 1
889 assert len(globalvars) == size, globalvars
890 else:
891 assert not isinstance(globalvars, str), repr(globalvars)
892 try:
893 numglobal, numbuiltin = globalvars
894 except ValueError:
895 numglobal, numbuiltin, g_numunknown = globalvars
896 globalvars = {
897 'total': numglobal + numbuiltin + g_numunknown,
898 'numglobal': numglobal,
899 'numbuiltin': numbuiltin,
900 'numunknown': g_numunknown,
901 }
902 unbound = globalvars['total'] + attrs + unknown
903 return {
904 'total': nlocals + freevars + unbound,
905 'locals': {
906 'total': nlocals,
907 'args': {
908 'total': nargvars,
909 'numposonly': posonly,
910 'numposorkw': posorkw,
911 'numkwonly': kwonly,
912 'varargs': varargs,
913 'varkwargs': varkwargs,
914 },
915 'numpure': purelocals,
916 'cells': {
917 'total': argcells + othercells,
918 'numargs': argcells,
919 'numothers': othercells,
920 },
921 'hidden': {
922 'total': 0,
923 'numpure': 0,
924 'numcells': 0,
925 },
926 },
927 'numfree': freevars,
928 'unbound': {
929 'total': unbound,
930 'globals': globalvars,
931 'numattrs': attrs,
932 'numunknown': unknown,
933 },
934 }
935
936 funcs = {
937 defs.simple_script: new_var_counts(),
938 defs.complex_script: new_var_counts(
939 purelocals=5,
940 globalvars=1,
941 attrs=2,
942 ),
943 defs.script_with_globals: new_var_counts(
944 purelocals=2,
945 globalvars=1,
946 ),
947 defs.script_with_explicit_empty_return: new_var_counts(),
948 defs.script_with_return: new_var_counts(),
949 defs.spam_minimal: new_var_counts(),
950 defs.spam_minimal: new_var_counts(),
951 defs.spam_with_builtins: new_var_counts(
952 purelocals=4,
953 globalvars=4,
954 ),
955 defs.spam_with_globals_and_builtins: new_var_counts(
956 purelocals=5,
957 globalvars=6,
958 ),
959 defs.spam_with_global_and_attr_same_name: new_var_counts(
960 globalvars=2,
961 attrs=1,
962 ),
963 defs.spam_full_args: new_var_counts(
964 posonly=2,
965 posorkw=2,
966 kwonly=2,
967 varargs=1,
968 varkwargs=1,
969 ),
970 defs.spam_full_args_with_defaults: new_var_counts(
971 posonly=2,
972 posorkw=2,
973 kwonly=2,
974 varargs=1,
975 varkwargs=1,
976 ),
977 defs.spam_args_attrs_and_builtins: new_var_counts(
978 posonly=2,
979 posorkw=2,
980 kwonly=2,
981 varargs=1,
982 varkwargs=1,
983 attrs=1,
984 ),
985 defs.spam_returns_arg: new_var_counts(
986 posorkw=1,
987 ),
988 defs.spam_raises: new_var_counts(
989 globalvars=1,
990 ),
991 defs.spam_with_inner_not_closure: new_var_counts(
992 purelocals=1,
993 ),
994 defs.spam_with_inner_closure: new_var_counts(
995 othercells=1,
996 purelocals=1,
997 ),
998 defs.spam_annotated: new_var_counts(
999 posorkw=3,
1000 ),
1001 defs.spam_full: new_var_counts(
1002 posonly=2,
1003 posorkw=2,
1004 kwonly=2,
1005 varargs=1,
1006 varkwargs=1,
1007 purelocals=4,
1008 globalvars=3,
1009 attrs=1,
1010 ),
1011 defs.spam: new_var_counts(
1012 posorkw=1,
1013 ),
1014 defs.spam_N: new_var_counts(
1015 posorkw=1,
1016 purelocals=1,
1017 ),
1018 defs.spam_C: new_var_counts(
1019 posorkw=1,
1020 purelocals=1,
1021 argcells=1,
1022 othercells=1,
1023 ),
1024 defs.spam_NN: new_var_counts(
1025 posorkw=1,
1026 purelocals=1,
1027 ),
1028 defs.spam_NC: new_var_counts(
1029 posorkw=1,
1030 purelocals=1,
1031 argcells=1,
1032 othercells=1,
1033 ),
1034 defs.spam_CN: new_var_counts(
1035 posorkw=1,
1036 purelocals=1,
1037 argcells=1,
1038 othercells=1,
1039 ),
1040 defs.spam_CC: new_var_counts(
1041 posorkw=1,
1042 purelocals=1,
1043 argcells=1,
1044 othercells=1,
1045 ),
1046 defs.eggs_nested: new_var_counts(
1047 posorkw=1,
1048 ),
1049 defs.eggs_closure: new_var_counts(
1050 posorkw=1,
1051 freevars=2,
1052 ),
1053 defs.eggs_nested_N: new_var_counts(
1054 posorkw=1,
1055 purelocals=1,
1056 ),
1057 defs.eggs_nested_C: new_var_counts(
1058 posorkw=1,
1059 purelocals=1,
1060 argcells=1,
1061 freevars=2,
1062 ),
1063 defs.eggs_closure_N: new_var_counts(
1064 posorkw=1,
1065 purelocals=1,
1066 freevars=2,
1067 ),
1068 defs.eggs_closure_C: new_var_counts(
1069 posorkw=1,
1070 purelocals=1,
1071 argcells=1,
1072 othercells=1,
1073 freevars=2,
1074 ),
1075 defs.ham_nested: new_var_counts(
1076 posorkw=1,
1077 ),
1078 defs.ham_closure: new_var_counts(
1079 posorkw=1,
1080 freevars=3,
1081 ),
1082 defs.ham_C_nested: new_var_counts(
1083 posorkw=1,
1084 ),
1085 defs.ham_C_closure: new_var_counts(
1086 posorkw=1,
1087 freevars=4,
1088 ),
1089 }
1090 assert len(funcs) == len(defs.FUNCTIONS), (len(funcs), len(defs.FUNCTIONS))
1091 for func in defs.FUNCTIONS:
1092 with self.subTest(func):
1093 expected = funcs[func]
1094 counts = _testinternalcapi.get_code_var_counts(func.__code__)
1095 self.assertEqual(counts, expected)
1096
1097 func = defs.spam_with_globals_and_builtins
1098 with self.subTest(f'{func} code'):
1099 expected = new_var_counts(
1100 purelocals=5,
1101 globalvars=6,
1102 )
1103 counts = _testinternalcapi.get_code_var_counts(func.__code__)
1104 self.assertEqual(counts, expected)
1105
1106 with self.subTest(f'{func} with own globals and builtins'):
1107 expected = new_var_counts(
1108 purelocals=5,
1109 globalvars=(2, 4),
1110 )
1111 counts = _testinternalcapi.get_code_var_counts(func)
1112 self.assertEqual(counts, expected)
1113
1114 with self.subTest(f'{func} without globals'):
1115 expected = new_var_counts(
1116 purelocals=5,
1117 globalvars=(0, 4, 2),
1118 )
1119 counts = _testinternalcapi.get_code_var_counts(func, globalsns={})
1120 self.assertEqual(counts, expected)
1121
1122 with self.subTest(f'{func} without both'):
1123 expected = new_var_counts(
1124 purelocals=5,
1125 globalvars=6,
1126 )
1127 counts = _testinternalcapi.get_code_var_counts(func, globalsns={},
1128 builtinsns={})
1129 self.assertEqual(counts, expected)
1130
1131 with self.subTest(f'{func} without builtins'):
1132 expected = new_var_counts(
1133 purelocals=5,
1134 globalvars=(2, 0, 4),
1135 )
1136 counts = _testinternalcapi.get_code_var_counts(func, builtinsns={})
1137 self.assertEqual(counts, expected)
1138
1139 @unittest.skipIf(_testinternalcapi is None, "missing _testinternalcapi")
1140 def test_stateless(self):
1141 self.maxDiff = None
1142
1143 STATELESS_FUNCTIONS = [
1144 *defs.STATELESS_FUNCTIONS,
1145 # stateless with defaults
1146 defs.spam_full_args_with_defaults,
1147 ]
1148
1149 for func in defs.STATELESS_CODE:
1150 with self.subTest((func, '(code)')):
1151 _testinternalcapi.verify_stateless_code(func.__code__)
1152 for func in STATELESS_FUNCTIONS:
1153 with self.subTest((func, '(func)')):
1154 _testinternalcapi.verify_stateless_code(func)
1155
1156 for func in defs.FUNCTIONS:
1157 if func not in defs.STATELESS_CODE:
1158 with self.subTest((func, '(code)')):
1159 with self.assertRaises(Exception):
1160 _testinternalcapi.verify_stateless_code(func.__code__)
1161
1162 if func not in STATELESS_FUNCTIONS:
1163 with self.subTest((func, '(func)')):
1164 with self.assertRaises(Exception):
1165 _testinternalcapi.verify_stateless_code(func)
1166
1167
1168 def isinterned(s):
1169 return s is sys.intern(('_' + s + '_')[1:-1])
1170
1171 class CodeConstsTest(unittest.TestCase):
1172
1173 def find_const(self, consts, value):
1174 for v in consts:
1175 if v == value:
1176 return v
1177 self.assertIn(value, consts) # raises an exception
1178 self.fail('Should never be reached')
1179
1180 def assertIsInterned(self, s):
1181 if not isinterned(s):
1182 self.fail('String %r is not interned' % (s,))
1183
1184 def assertIsNotInterned(self, s):
1185 if isinterned(s):
1186 self.fail('String %r is interned' % (s,))
1187
1188 @cpython_only
1189 def test_interned_string(self):
1190 co = compile('res = "str_value"', '?', 'exec')
1191 v = self.find_const(co.co_consts, 'str_value')
1192 self.assertIsInterned(v)
1193
1194 @cpython_only
1195 def test_interned_string_in_tuple(self):
1196 co = compile('res = ("str_value",)', '?', 'exec')
1197 v = self.find_const(co.co_consts, ('str_value',))
1198 self.assertIsInterned(v[0])
1199
1200 @cpython_only
1201 def test_interned_string_in_frozenset(self):
1202 co = compile('res = a in {"str_value"}', '?', 'exec')
1203 v = self.find_const(co.co_consts, frozenset(('str_value',)))
1204 self.assertIsInterned(tuple(v)[0])
1205
1206 @cpython_only
1207 def test_interned_string_default(self):
1208 def f(a='str_value'):
1209 return a
1210 self.assertIsInterned(f())
1211
1212 @cpython_only
1213 @unittest.skipIf(Py_GIL_DISABLED, "free-threaded build interns all string constants")
1214 def test_interned_string_with_null(self):
1215 co = compile(r'res = "str\0value!"', '?', 'exec')
1216 v = self.find_const(co.co_consts, 'str\0value!')
1217 self.assertIsNotInterned(v)
1218
1219 @cpython_only
1220 @unittest.skipUnless(Py_GIL_DISABLED, "does not intern all constants")
1221 def test_interned_constants(self):
1222 # compile separately to avoid compile time de-duping
1223
1224 globals = {}
1225 exec(textwrap.dedent("""
1226 def func1():
1227 return (0.0, (1, 2, "hello"))
1228 """), globals)
1229
1230 exec(textwrap.dedent("""
1231 def func2():
1232 return (0.0, (1, 2, "hello"))
1233 """), globals)
1234
1235 self.assertTrue(globals["func1"]() is globals["func2"]())
1236
1237 @cpython_only
1238 def test_unusual_constants(self):
1239 # gh-130851: Code objects constructed with constants that are not
1240 # types generated by the bytecode compiler should not crash the
1241 # interpreter.
1242 class Unhashable:
1243 def __hash__(self):
1244 raise TypeError("unhashable type")
1245
1246 class MyInt(int):
1247 pass
1248
1249 code = compile("a = 1", "<string>", "exec")
1250 code = code.replace(co_consts=(1, Unhashable(), MyInt(1), MyInt(1)))
1251 self.assertIsInstance(code.co_consts[1], Unhashable)
1252 self.assertEqual(code.co_consts[2], code.co_consts[3])
1253
1254
1255 class CodeWeakRefTest(unittest.TestCase):
1256
1257 def test_basic(self):
1258 # Create a code object in a clean environment so that we know we have
1259 # the only reference to it left.
1260 namespace = {}
1261 exec("def f(): pass", globals(), namespace)
1262 f = namespace["f"]
1263 del namespace
1264
1265 self.called = False
1266 def callback(code):
1267 self.called = True
1268
1269 # f is now the last reference to the function, and through it, the code
1270 # object. While we hold it, check that we can create a weakref and
1271 # deref it. Then delete it, and check that the callback gets called and
1272 # the reference dies.
1273 coderef = weakref.ref(f.__code__, callback)
1274 self.assertTrue(bool(coderef()))
1275 del f
1276 gc_collect() # For PyPy or other GCs.
1277 self.assertFalse(bool(coderef()))
1278 self.assertTrue(self.called)
1279
1280 # Python implementation of location table parsing algorithm
1281 def read(it):
1282 return next(it)
1283
1284 def read_varint(it):
1285 b = read(it)
1286 val = b & 63;
1287 shift = 0;
1288 while b & 64:
1289 b = read(it)
1290 shift += 6
1291 val |= (b&63) << shift
1292 return val
1293
1294 def read_signed_varint(it):
1295 uval = read_varint(it)
1296 if uval & 1:
1297 return -(uval >> 1)
1298 else:
1299 return uval >> 1
1300
1301 def parse_location_table(code):
1302 line = code.co_firstlineno
1303 it = iter(code.co_linetable)
1304 while True:
1305 try:
1306 first_byte = read(it)
1307 except StopIteration:
1308 return
1309 code = (first_byte >> 3) & 15
1310 length = (first_byte & 7) + 1
1311 if code == 15:
1312 yield (code, length, None, None, None, None)
1313 elif code == 14:
1314 line_delta = read_signed_varint(it)
1315 line += line_delta
1316 end_line = line + read_varint(it)
1317 col = read_varint(it)
1318 if col == 0:
1319 col = None
1320 else:
1321 col -= 1
1322 end_col = read_varint(it)
1323 if end_col == 0:
1324 end_col = None
1325 else:
1326 end_col -= 1
1327 yield (code, length, line, end_line, col, end_col)
1328 elif code == 13: # No column
1329 line_delta = read_signed_varint(it)
1330 line += line_delta
1331 yield (code, length, line, line, None, None)
1332 elif code in (10, 11, 12): # new line
1333 line_delta = code - 10
1334 line += line_delta
1335 column = read(it)
1336 end_column = read(it)
1337 yield (code, length, line, line, column, end_column)
1338 else:
1339 assert (0 <= code < 10)
1340 second_byte = read(it)
1341 column = code << 3 | (second_byte >> 4)
1342 yield (code, length, line, line, column, column + (second_byte & 15))
1343
1344 def positions_from_location_table(code):
1345 for _, length, line, end_line, col, end_col in parse_location_table(code):
1346 for _ in range(length):
1347 yield (line, end_line, col, end_col)
1348
1349 def dedup(lst, prev=object()):
1350 for item in lst:
1351 if item != prev:
1352 yield item
1353 prev = item
1354
1355 def lines_from_postions(positions):
1356 return dedup(l for (l, _, _, _) in positions)
1357
1358 def misshappen():
1359 """
1360
1361
1362
1363
1364
1365 """
1366 x = (
1367
1368
1369 4
1370
1371 +
1372
1373 y
1374
1375 )
1376 y = (
1377 a
1378 +
1379 b
1380 +
1381
1382 d
1383 )
1384 return q if (
1385
1386 x
1387
1388 ) else p
1389
1390 def bug93662():
1391 example_report_generation_message= (
1392 """
1393 """
1394 ).strip()
1395 raise ValueError()
1396
1397
1398 class CodeLocationTest(unittest.TestCase):
1399
1400 def check_positions(self, func):
1401 pos1 = list(func.__code__.co_positions())
1402 pos2 = list(positions_from_location_table(func.__code__))
1403 for l1, l2 in zip(pos1, pos2):
1404 self.assertEqual(l1, l2)
1405 self.assertEqual(len(pos1), len(pos2))
1406
1407 def test_positions(self):
1408 self.check_positions(parse_location_table)
1409 self.check_positions(misshappen)
1410 self.check_positions(bug93662)
1411
1412 def check_lines(self, func):
1413 co = func.__code__
1414 lines1 = [line for _, _, line in co.co_lines()]
1415 self.assertEqual(lines1, list(dedup(lines1)))
1416 lines2 = list(lines_from_postions(positions_from_location_table(co)))
1417 for l1, l2 in zip(lines1, lines2):
1418 self.assertEqual(l1, l2)
1419 self.assertEqual(len(lines1), len(lines2))
1420
1421 def test_lines(self):
1422 self.check_lines(parse_location_table)
1423 self.check_lines(misshappen)
1424 self.check_lines(bug93662)
1425
1426 @cpython_only
1427 def test_code_new_empty(self):
1428 # If this test fails, it means that the construction of PyCode_NewEmpty
1429 # needs to be modified! Please update this test *and* PyCode_NewEmpty,
1430 # so that they both stay in sync.
1431 def f():
1432 pass
1433 PY_CODE_LOCATION_INFO_NO_COLUMNS = 13
1434 f.__code__ = f.__code__.replace(
1435 co_stacksize=1,
1436 co_firstlineno=42,
1437 co_code=bytes(
1438 [
1439 dis.opmap["RESUME"], 0,
1440 dis.opmap["LOAD_COMMON_CONSTANT"], 0,
1441 dis.opmap["RAISE_VARARGS"], 1,
1442 ]
1443 ),
1444 co_linetable=bytes(
1445 [
1446 (1 << 7)
1447 | (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3)
1448 | (3 - 1),
1449 0,
1450 ]
1451 ),
1452 )
1453 self.assertRaises(AssertionError, f)
1454 self.assertEqual(
1455 list(f.__code__.co_positions()),
1456 3 * [(42, 42, None, None)],
1457 )
1458
1459 @cpython_only
1460 def test_docstring_under_o2(self):
1461 code = textwrap.dedent('''
1462 def has_docstring(x, y):
1463 """This is a first-line doc string"""
1464 """This is a second-line doc string"""
1465 a = x + y
1466 b = x - y
1467 return a, b
1468
1469
1470 def no_docstring(x):
1471 def g(y):
1472 return x + y
1473 return g
1474
1475
1476 async def async_func():
1477 """asynf function doc string"""
1478 pass
1479
1480
1481 for func in [has_docstring, no_docstring(4), async_func]:
1482 assert(func.__doc__ is None)
1483 ''')
1484
1485 rc, out, err = assert_python_ok('-OO', '-c', code)
1486
1487 def test_co_branches(self):
1488
1489 def get_line_branches(func):
1490 code = func.__code__
1491 base = code.co_firstlineno
1492 return [
1493 (
1494 code_offset_to_line(code, src) - base,
1495 code_offset_to_line(code, left) - base,
1496 code_offset_to_line(code, right) - base
1497 ) for (src, left, right) in
1498 code.co_branches()
1499 ]
1500
1501 def simple(x):
1502 if x:
1503 A
1504 else:
1505 B
1506
1507 self.assertEqual(
1508 get_line_branches(simple),
1509 [(1,2,4)])
1510
1511 def with_extended_args(x):
1512 if x:
1513 A.x; A.x; A.x; A.x; A.x; A.x;
1514 A.x; A.x; A.x; A.x; A.x; A.x;
1515 A.x; A.x; A.x; A.x; A.x; A.x;
1516 A.x; A.x; A.x; A.x; A.x; A.x;
1517 A.x; A.x; A.x; A.x; A.x; A.x;
1518 else:
1519 B
1520
1521 self.assertEqual(
1522 get_line_branches(with_extended_args),
1523 [(1,2,8)])
1524
1525 async def afunc():
1526 async for letter in async_iter1:
1527 2
1528 3
1529
1530 self.assertEqual(
1531 get_line_branches(afunc),
1532 [(1,1,3)])
1533
1534 if check_impl_detail(cpython=True) and ctypes is not None:
1535 py = ctypes.pythonapi
1536 freefunc = ctypes.CFUNCTYPE(None,ctypes.c_voidp)
1537
1538 RequestCodeExtraIndex = py.PyUnstable_Eval_RequestCodeExtraIndex
1539 RequestCodeExtraIndex.argtypes = (freefunc,)
1540 RequestCodeExtraIndex.restype = ctypes.c_ssize_t
1541
1542 SetExtra = py.PyUnstable_Code_SetExtra
1543 SetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t, ctypes.c_voidp)
1544 SetExtra.restype = ctypes.c_int
1545
1546 GetExtra = py.PyUnstable_Code_GetExtra
1547 GetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t,
1548 ctypes.POINTER(ctypes.c_voidp))
1549 GetExtra.restype = ctypes.c_int
1550
1551 LAST_FREED = None
1552 def myfree(ptr):
1553 global LAST_FREED
1554 LAST_FREED = ptr
1555
1556 FREE_FUNC = freefunc(myfree)
1557 FREE_INDEX = RequestCodeExtraIndex(FREE_FUNC)
1558 # Make sure myfree sticks around at least as long as the interpreter,
1559 # since we (currently) can't unregister the function and leaving a
1560 # dangling pointer will cause a crash on deallocation of code objects if
1561 # something else uses co_extras, like test_capi.test_misc.
1562 late_deletion(myfree)
1563
1564 class CoExtra(unittest.TestCase):
1565 def get_func(self):
1566 # Defining a function causes the containing function to have a
1567 # reference to the code object. We need the code objects to go
1568 # away, so we eval a lambda.
1569 return eval('lambda:42')
1570
1571 def test_get_non_code(self):
1572 f = self.get_func()
1573
1574 self.assertRaises(SystemError, SetExtra, 42, FREE_INDEX,
1575 ctypes.c_voidp(100))
1576 self.assertRaises(SystemError, GetExtra, 42, FREE_INDEX,
1577 ctypes.c_voidp(100))
1578
1579 def test_bad_index(self):
1580 f = self.get_func()
1581 self.assertRaises(SystemError, SetExtra, f.__code__,
1582 FREE_INDEX+100, ctypes.c_voidp(100))
1583 self.assertEqual(GetExtra(f.__code__, FREE_INDEX+100,
1584 ctypes.c_voidp(100)), 0)
1585
1586 def test_free_called(self):
1587 # Verify that the provided free function gets invoked
1588 # when the code object is cleaned up.
1589 f = self.get_func()
1590
1591 SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(100))
1592 del f
1593 gc_collect() # For free-threaded build
1594 self.assertEqual(LAST_FREED, 100)
1595
1596 def test_get_set(self):
1597 # Test basic get/set round tripping.
1598 f = self.get_func()
1599
1600 extra = ctypes.c_voidp()
1601
1602 SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(200))
1603 # reset should free...
1604 SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(300))
1605 self.assertEqual(LAST_FREED, 200)
1606
1607 extra = ctypes.c_voidp()
1608 GetExtra(f.__code__, FREE_INDEX, extra)
1609 self.assertEqual(extra.value, 300)
1610 del f
1611
1612 @threading_helper.requires_working_threading()
1613 def test_free_different_thread(self):
1614 # Freeing a code object on a different thread then
1615 # where the co_extra was set should be safe.
1616 f = self.get_func()
1617 class ThreadTest(threading.Thread):
1618 def __init__(self, f, test):
1619 super().__init__()
1620 self.f = f
1621 self.test = test
1622 def run(self):
1623 del self.f
1624 gc_collect()
1625 # gh-117683: In the free-threaded build, the code object's
1626 # destructor may still be running concurrently in the main
1627 # thread.
1628 if not Py_GIL_DISABLED:
1629 self.test.assertEqual(LAST_FREED, 500)
1630
1631 SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(500))
1632 tt = ThreadTest(f, self)
1633 del f
1634 tt.start()
1635 tt.join()
1636 gc_collect() # For free-threaded build
1637 self.assertEqual(LAST_FREED, 500)
1638
1639
1640 def load_tests(loader, tests, pattern):
1641 tests.addTest(doctest.DocTestSuite())
1642 return tests
1643
1644
1645 if __name__ == "__main__":
1646 unittest.main()