Debian Codegraph @

Lib/test/test_capi/test_misc.py

raw
1 # Run the _testcapi module tests (tests for the Python/C API): by defn,
2 # these are all functions _testcapi exports whose name begins with 'test_'.
3
4 import _thread
5 from collections import deque
6 import contextlib
7 import importlib.machinery
8 import importlib.util
9 import json
10 import os
11 import pickle
12 import queue
13 import random
14 import sys
15 import textwrap
16 import threading
17 import time
18 import types
19 import unittest
20 import weakref
21 import operator
22 from test import support
23 from test.support import MISSING_C_DOCSTRINGS
24 from test.support import import_helper
25 from test.support import threading_helper
26 from test.support import warnings_helper
27 from test.support import requires_limited_api
28 from test.support import expected_failure_if_gil_disabled
29 from test.support import Py_GIL_DISABLED
30 from test.support.script_helper import assert_python_failure, assert_python_ok, run_python_until_end
31 try:
32 import _posixsubprocess
33 except ImportError:
34 _posixsubprocess = None
35 try:
36 import _testmultiphase
37 except ImportError:
38 _testmultiphase = None
39 try:
40 import _testsinglephase
41 except ImportError:
42 _testsinglephase = None
43 try:
44 import _interpreters
45 except ModuleNotFoundError:
46 _interpreters = None
47
48 # Skip this test if the _testcapi module isn't available.
49 _testcapi = import_helper.import_module('_testcapi')
50
51 from _testcapi import HeapCTypeSubclass, HeapCTypeSubclassWithFinalizer
52
53 import _testlimitedcapi
54 import _testinternalcapi
55
56
57 NULL = None
58
59 def decode_stderr(err):
60 return err.decode('utf-8', 'replace').replace('\r', '')
61
62
63 def requires_subinterpreters(meth):
64 """Decorator to skip a test if subinterpreters are not supported."""
65 return unittest.skipIf(_interpreters is None,
66 'subinterpreters required')(meth)
67
68
69 def testfunction(self):
70 """some doc"""
71 return self
72
73
74 class InstanceMethod:
75 id = _testcapi.instancemethod(id)
76 testfunction = _testcapi.instancemethod(testfunction)
77
78
79 CURRENT_THREAD_REGEX = r'Current thread.*:\n' if not support.Py_GIL_DISABLED else r'Stack .*:\n'
80
81
82 @support.force_not_colorized_test_class
83 class CAPITest(unittest.TestCase):
84
85 def test_instancemethod(self):
86 inst = InstanceMethod()
87 self.assertEqual(id(inst), inst.id())
88 self.assertTrue(inst.testfunction() is inst)
89 self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__)
90 self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__)
91
92 InstanceMethod.testfunction.attribute = "test"
93 self.assertEqual(testfunction.attribute, "test")
94 self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
95
96 @support.requires_subprocess()
97 def test_no_FatalError_infinite_loop(self):
98 code = textwrap.dedent("""
99 import _testcapi
100 from test import support
101
102 with support.SuppressCrashReport():
103 _testcapi.crash_no_current_thread()
104 """)
105
106 run_result, _cmd_line = run_python_until_end('-c', code)
107 _rc, out, err = run_result
108 self.assertEqual(out, b'')
109 # This used to cause an infinite loop.
110 if not support.Py_GIL_DISABLED:
111 msg = ("Fatal Python error: PyThreadState_Get: "
112 "the function must be called with the GIL held, "
113 "after Python initialization and before Python finalization, "
114 "but the GIL is released "
115 "(the current Python thread state is NULL)").encode()
116 else:
117 msg = ("Fatal Python error: PyThreadState_Get: "
118 "the function must be called with an active thread state, "
119 "after Python initialization and before Python finalization, "
120 "but it was called without an active thread state. "
121 "Are you trying to call the C API inside of a Py_BEGIN_ALLOW_THREADS block?").encode()
122 self.assertStartsWith(err.rstrip(), msg)
123
124 def test_memoryview_from_NULL_pointer(self):
125 self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer)
126
127 @unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.')
128 def test_seq_bytes_to_charp_array(self):
129 # Issue #15732: crash in _PySequence_BytesToCharpArray()
130 class Z(object):
131 def __len__(self):
132 return 1
133 with self.assertRaisesRegex(TypeError, 'indexing'):
134 _posixsubprocess.fork_exec(
135 1,Z(),True,(1, 2),5,6,7,8,9,10,11,12,13,14,True,True,17,False,19,20,21,22)
136 # Issue #15736: overflow in _PySequence_BytesToCharpArray()
137 class Z(object):
138 def __len__(self):
139 return sys.maxsize
140 def __getitem__(self, i):
141 return b'x'
142 self.assertRaises(MemoryError, _posixsubprocess.fork_exec,
143 1,Z(),True,(1, 2),5,6,7,8,9,10,11,12,13,14,True,True,17,False,19,20,21,22)
144
145 @unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.')
146 def test_subprocess_fork_exec(self):
147 class Z(object):
148 def __len__(self):
149 return 1
150
151 # Issue #15738: crash in subprocess_fork_exec()
152 self.assertRaises(TypeError, _posixsubprocess.fork_exec,
153 Z(),[b'1'],True,(1, 2),5,6,7,8,9,10,11,12,13,14,True,True,17,False,19,20,21,22)
154
155 @unittest.skipIf(MISSING_C_DOCSTRINGS,
156 "Signature information for builtins requires docstrings")
157 def test_docstring_signature_parsing(self):
158
159 self.assertEqual(_testcapi.no_docstring.__doc__, None)
160 self.assertEqual(_testcapi.no_docstring.__text_signature__, None)
161
162 self.assertEqual(_testcapi.docstring_empty.__doc__, None)
163 self.assertEqual(_testcapi.docstring_empty.__text_signature__, None)
164
165 self.assertEqual(_testcapi.docstring_no_signature.__doc__,
166 "This docstring has no signature.")
167 self.assertEqual(_testcapi.docstring_no_signature.__text_signature__, None)
168
169 self.assertEqual(_testcapi.docstring_with_invalid_signature.__doc__,
170 "docstring_with_invalid_signature($module, /, boo)\n"
171 "\n"
172 "This docstring has an invalid signature."
173 )
174 self.assertEqual(_testcapi.docstring_with_invalid_signature.__text_signature__, None)
175
176 self.assertEqual(_testcapi.docstring_with_invalid_signature2.__doc__,
177 "docstring_with_invalid_signature2($module, /, boo)\n"
178 "\n"
179 "--\n"
180 "\n"
181 "This docstring also has an invalid signature."
182 )
183 self.assertEqual(_testcapi.docstring_with_invalid_signature2.__text_signature__, None)
184
185 self.assertEqual(_testcapi.docstring_with_signature.__doc__,
186 "This docstring has a valid signature.")
187 self.assertEqual(_testcapi.docstring_with_signature.__text_signature__, "($module, /, sig)")
188
189 self.assertEqual(_testcapi.docstring_with_signature_but_no_doc.__doc__, None)
190 self.assertEqual(_testcapi.docstring_with_signature_but_no_doc.__text_signature__,
191 "($module, /, sig)")
192
193 self.assertEqual(_testcapi.docstring_with_signature_and_extra_newlines.__doc__,
194 "\nThis docstring has a valid signature and some extra newlines.")
195 self.assertEqual(_testcapi.docstring_with_signature_and_extra_newlines.__text_signature__,
196 "($module, /, parameter)")
197
198 def test_c_type_with_matrix_multiplication(self):
199 M = _testcapi.matmulType
200 m1 = M()
201 m2 = M()
202 self.assertEqual(m1 @ m2, ("matmul", m1, m2))
203 self.assertEqual(m1 @ 42, ("matmul", m1, 42))
204 self.assertEqual(42 @ m1, ("matmul", 42, m1))
205 o = m1
206 o @= m2
207 self.assertEqual(o, ("imatmul", m1, m2))
208 o = m1
209 o @= 42
210 self.assertEqual(o, ("imatmul", m1, 42))
211 o = 42
212 o @= m1
213 self.assertEqual(o, ("matmul", 42, m1))
214
215 def test_c_type_with_ipow(self):
216 # When the __ipow__ method of a type was implemented in C, using the
217 # modulo param would cause segfaults.
218 o = _testcapi.ipowType()
219 self.assertEqual(o.__ipow__(1), (1, None))
220 self.assertEqual(o.__ipow__(2, 2), (2, 2))
221
222 def test_return_null_without_error(self):
223 # Issue #23571: A function must not return NULL without setting an
224 # error
225 if support.Py_DEBUG:
226 code = textwrap.dedent("""
227 import _testcapi
228 from test import support
229
230 with support.SuppressCrashReport():
231 _testcapi.return_null_without_error()
232 """)
233 rc, out, err = assert_python_failure('-c', code)
234 err = decode_stderr(err)
235 self.assertRegex(err,
236 r'Fatal Python error: _Py_CheckFunctionResult: '
237 r'a function returned NULL without setting an exception\n'
238 r'Python runtime state: initialized\n'
239 r'SystemError: <built-in function return_null_without_error> '
240 r'returned NULL without setting an exception\n'
241 r'\n' +
242 CURRENT_THREAD_REGEX +
243 r' File .*", line 6 in <module>\n')
244 else:
245 with self.assertRaises(SystemError) as cm:
246 _testcapi.return_null_without_error()
247 self.assertRegex(str(cm.exception),
248 'return_null_without_error.* '
249 'returned NULL without setting an exception')
250
251 def test_return_result_with_error(self):
252 # Issue #23571: A function must not return a result with an error set
253 if support.Py_DEBUG:
254 code = textwrap.dedent("""
255 import _testcapi
256 from test import support
257
258 with support.SuppressCrashReport():
259 _testcapi.return_result_with_error()
260 """)
261 rc, out, err = assert_python_failure('-c', code)
262 err = decode_stderr(err)
263 self.assertRegex(err,
264 r'Fatal Python error: _Py_CheckFunctionResult: '
265 r'a function returned a result with an exception set\n'
266 r'Python runtime state: initialized\n'
267 r'ValueError\n'
268 r'\n'
269 r'The above exception was the direct cause '
270 r'of the following exception:\n'
271 r'\n'
272 r'SystemError: <built-in '
273 r'function return_result_with_error> '
274 r'returned a result with an exception set\n'
275 r'\n' +
276 CURRENT_THREAD_REGEX +
277 r' File .*, line 6 in <module>\n')
278 else:
279 with self.assertRaises(SystemError) as cm:
280 _testcapi.return_result_with_error()
281 self.assertRegex(str(cm.exception),
282 'return_result_with_error.* '
283 'returned a result with an exception set')
284
285 def test_getitem_with_error(self):
286 # Test _Py_CheckSlotResult(). Raise an exception and then calls
287 # PyObject_GetItem(): check that the assertion catches the bug.
288 # PyObject_GetItem() must not be called with an exception set.
289 code = textwrap.dedent("""
290 import _testcapi
291 from test import support
292
293 with support.SuppressCrashReport():
294 _testcapi.getitem_with_error({1: 2}, 1)
295 """)
296 rc, out, err = assert_python_failure('-c', code)
297 err = decode_stderr(err)
298 if 'SystemError: ' not in err:
299 self.assertRegex(err,
300 r'Fatal Python error: _Py_CheckSlotResult: '
301 r'Slot __getitem__ of type dict succeeded '
302 r'with an exception set\n'
303 r'Python runtime state: initialized\n'
304 r'ValueError: bug\n'
305 r'\n' +
306 CURRENT_THREAD_REGEX +
307 r' File .*, line 6 in <module>\n'
308 r'\n'
309 r'Extension modules: _testcapi \(total: 1\)\n')
310 else:
311 # Python built with NDEBUG macro defined:
312 # test _Py_CheckFunctionResult() instead.
313 self.assertIn('returned a result with an exception set', err)
314
315 def test_buildvalue(self):
316 # Test Py_BuildValue() with object arguments
317 buildvalue = _testcapi.py_buildvalue
318 self.assertEqual(buildvalue(''), None)
319 self.assertEqual(buildvalue('()'), ())
320 self.assertEqual(buildvalue('[]'), [])
321 self.assertEqual(buildvalue('{}'), {})
322 self.assertEqual(buildvalue('()[]{}'), ((), [], {}))
323 self.assertEqual(buildvalue('O', 1), 1)
324 self.assertEqual(buildvalue('(O)', 1), (1,))
325 self.assertEqual(buildvalue('[O]', 1), [1])
326 self.assertRaises(SystemError, buildvalue, '{O}', 1)
327 self.assertEqual(buildvalue('OO', 1, 2), (1, 2))
328 self.assertEqual(buildvalue('(OO)', 1, 2), (1, 2))
329 self.assertEqual(buildvalue('[OO]', 1, 2), [1, 2])
330 self.assertEqual(buildvalue('{OO}', 1, 2), {1: 2})
331 self.assertEqual(buildvalue('{OOOO}', 1, 2, 3, 4), {1: 2, 3: 4})
332 self.assertEqual(buildvalue('((O))', 1), ((1,),))
333 self.assertEqual(buildvalue('((OO))', 1, 2), ((1, 2),))
334
335 self.assertEqual(buildvalue(' \t,:'), None)
336 self.assertEqual(buildvalue('O,', 1), 1)
337 self.assertEqual(buildvalue(' O ', 1), 1)
338 self.assertEqual(buildvalue('\tO\t', 1), 1)
339 self.assertEqual(buildvalue('O,O', 1, 2), (1, 2))
340 self.assertEqual(buildvalue('O, O', 1, 2), (1, 2))
341 self.assertEqual(buildvalue('O,\tO', 1, 2), (1, 2))
342 self.assertEqual(buildvalue('O O', 1, 2), (1, 2))
343 self.assertEqual(buildvalue('O\tO', 1, 2), (1, 2))
344 self.assertEqual(buildvalue('(O,O)', 1, 2), (1, 2))
345 self.assertEqual(buildvalue('(O, O,)', 1, 2), (1, 2))
346 self.assertEqual(buildvalue(' ( O O ) ', 1, 2), (1, 2))
347 self.assertEqual(buildvalue('\t(\tO\tO\t)\t', 1, 2), (1, 2))
348 self.assertEqual(buildvalue('[O,O]', 1, 2), [1, 2])
349 self.assertEqual(buildvalue('[O, O,]', 1, 2), [1, 2])
350 self.assertEqual(buildvalue(' [ O O ] ', 1, 2), [1, 2])
351 self.assertEqual(buildvalue(' [\tO\tO\t] ', 1, 2), [1, 2])
352 self.assertEqual(buildvalue('{O:O}', 1, 2), {1: 2})
353 self.assertEqual(buildvalue('{O:O,O:O}', 1, 2, 3, 4), {1: 2, 3: 4})
354 self.assertEqual(buildvalue('{O: O, O: O,}', 1, 2, 3, 4), {1: 2, 3: 4})
355 self.assertEqual(buildvalue(' { O O O O } ', 1, 2, 3, 4), {1: 2, 3: 4})
356 self.assertEqual(buildvalue('\t{\tO\tO\tO\tO\t}\t', 1, 2, 3, 4), {1: 2, 3: 4})
357
358 self.assertRaises(SystemError, buildvalue, 'O', NULL)
359 self.assertRaises(SystemError, buildvalue, '(O)', NULL)
360 self.assertRaises(SystemError, buildvalue, '[O]', NULL)
361 self.assertRaises(SystemError, buildvalue, '{O}', NULL)
362 self.assertRaises(SystemError, buildvalue, 'OO', 1, NULL)
363 self.assertRaises(SystemError, buildvalue, 'OO', NULL, 2)
364 self.assertRaises(SystemError, buildvalue, '(OO)', 1, NULL)
365 self.assertRaises(SystemError, buildvalue, '(OO)', NULL, 2)
366 self.assertRaises(SystemError, buildvalue, '[OO]', 1, NULL)
367 self.assertRaises(SystemError, buildvalue, '[OO]', NULL, 2)
368 self.assertRaises(SystemError, buildvalue, '{OO}', 1, NULL)
369 self.assertRaises(SystemError, buildvalue, '{OO}', NULL, 2)
370
371 def test_buildvalue_ints(self):
372 # Test Py_BuildValue() with integer arguments
373 buildvalue = _testcapi.py_buildvalue_ints
374 from _testcapi import SHRT_MIN, SHRT_MAX, USHRT_MAX, INT_MIN, INT_MAX, UINT_MAX
375 self.assertEqual(buildvalue('i', INT_MAX), INT_MAX)
376 self.assertEqual(buildvalue('i', INT_MIN), INT_MIN)
377 self.assertEqual(buildvalue('I', UINT_MAX), UINT_MAX)
378
379 self.assertEqual(buildvalue('h', SHRT_MAX), SHRT_MAX)
380 self.assertEqual(buildvalue('h', SHRT_MIN), SHRT_MIN)
381 self.assertEqual(buildvalue('H', USHRT_MAX), USHRT_MAX)
382
383 self.assertEqual(buildvalue('b', 127), 127)
384 self.assertEqual(buildvalue('b', -128), -128)
385 self.assertEqual(buildvalue('B', 255), 255)
386
387 self.assertEqual(buildvalue('c', ord('A')), b'A')
388 self.assertEqual(buildvalue('c', 255), b'\xff')
389 self.assertEqual(buildvalue('c', 256), b'\x00')
390 self.assertEqual(buildvalue('c', -1), b'\xff')
391
392 self.assertEqual(buildvalue('C', 255), chr(255))
393 self.assertEqual(buildvalue('C', 256), chr(256))
394 self.assertEqual(buildvalue('C', sys.maxunicode), chr(sys.maxunicode))
395 self.assertRaises(ValueError, buildvalue, 'C', -1)
396 self.assertRaises(ValueError, buildvalue, 'C', sys.maxunicode+1)
397
398 # gh-84489
399 self.assertRaises(ValueError, buildvalue, '(C )i', -1, 2)
400 self.assertRaises(ValueError, buildvalue, '[C ]i', -1, 2)
401 self.assertRaises(ValueError, buildvalue, '{Ci }i', -1, 2, 3)
402
403 def test_buildvalue_N(self):
404 _testcapi.test_buildvalue_N()
405
406 def test_trashcan_subclass(self):
407 # bpo-35983: Check that the trashcan mechanism for "list" is NOT
408 # activated when its tp_dealloc is being called by a subclass
409 from _testcapi import MyList
410 L = None
411 for i in range(100):
412 L = MyList((L,))
413
414 @support.requires_resource('cpu')
415 @support.skip_emscripten_stack_overflow()
416 @support.skip_wasi_stack_overflow()
417 def test_trashcan_python_class1(self):
418 self.do_test_trashcan_python_class(list)
419
420 @support.requires_resource('cpu')
421 @support.skip_emscripten_stack_overflow()
422 @support.skip_wasi_stack_overflow()
423 def test_trashcan_python_class2(self):
424 from _testcapi import MyList
425 self.do_test_trashcan_python_class(MyList)
426
427 def do_test_trashcan_python_class(self, base):
428 # Check that the trashcan mechanism works properly for a Python
429 # subclass of a class using the trashcan (this specific test assumes
430 # that the base class "base" behaves like list)
431 class PyList(base):
432 # Count the number of PyList instances to verify that there is
433 # no memory leak
434 num = 0
435 def __init__(self, *args):
436 __class__.num += 1
437 super().__init__(*args)
438 def __del__(self):
439 __class__.num -= 1
440
441 for parity in (0, 1):
442 L = None
443 # We need in the order of 2**20 iterations here such that a
444 # typical 8MB stack would overflow without the trashcan.
445 for i in range(2**20):
446 L = PyList((L,))
447 L.attr = i
448 if parity:
449 # Add one additional nesting layer
450 L = (L,)
451 self.assertGreater(PyList.num, 0)
452 del L
453 self.assertEqual(PyList.num, 0)
454
455 @unittest.skipIf(MISSING_C_DOCSTRINGS,
456 "Signature information for builtins requires docstrings")
457 def test_heap_ctype_doc_and_text_signature(self):
458 self.assertEqual(_testcapi.HeapDocCType.__doc__, "somedoc")
459 self.assertEqual(_testcapi.HeapDocCType.__text_signature__, "(arg1, arg2)")
460
461 def test_null_type_doc(self):
462 self.assertEqual(_testcapi.NullTpDocType.__doc__, None)
463
464 def test_subclass_of_heap_gc_ctype_with_tpdealloc_decrefs_once(self):
465 class HeapGcCTypeSubclass(_testcapi.HeapGcCType):
466 def __init__(self):
467 self.value2 = 20
468 super().__init__()
469
470 subclass_instance = HeapGcCTypeSubclass()
471 type_refcnt = sys.getrefcount(HeapGcCTypeSubclass)
472
473 # Test that subclass instance was fully created
474 self.assertEqual(subclass_instance.value, 10)
475 self.assertEqual(subclass_instance.value2, 20)
476
477 # Test that the type reference count is only decremented once
478 del subclass_instance
479 self.assertEqual(type_refcnt - 1, sys.getrefcount(HeapGcCTypeSubclass))
480
481 def test_subclass_of_heap_gc_ctype_with_del_modifying_dunder_class_only_decrefs_once(self):
482 class A(_testcapi.HeapGcCType):
483 def __init__(self):
484 self.value2 = 20
485 super().__init__()
486
487 class B(A):
488 def __init__(self):
489 super().__init__()
490
491 def __del__(self):
492 self.__class__ = A
493 A.refcnt_in_del = sys.getrefcount(A)
494 B.refcnt_in_del = sys.getrefcount(B)
495
496 subclass_instance = B()
497 type_refcnt = sys.getrefcount(B)
498 new_type_refcnt = sys.getrefcount(A)
499
500 # Test that subclass instance was fully created
501 self.assertEqual(subclass_instance.value, 10)
502 self.assertEqual(subclass_instance.value2, 20)
503
504 del subclass_instance
505
506 # Test that setting __class__ modified the reference counts of the types
507 if support.Py_DEBUG:
508 # gh-89373: In debug mode, _Py_Dealloc() keeps a strong reference
509 # to the type while calling tp_dealloc()
510 self.assertEqual(type_refcnt, B.refcnt_in_del)
511 else:
512 self.assertEqual(type_refcnt - 1, B.refcnt_in_del)
513 self.assertEqual(new_type_refcnt + 1, A.refcnt_in_del)
514
515 # Test that the original type already has decreased its refcnt
516 self.assertEqual(type_refcnt - 1, sys.getrefcount(B))
517
518 # Test that subtype_dealloc decref the newly assigned __class__ only once
519 self.assertEqual(new_type_refcnt, sys.getrefcount(A))
520
521 def test_heaptype_with_dict(self):
522 for cls in (
523 _testcapi.HeapCTypeWithDict,
524 _testlimitedcapi.HeapCTypeWithRelativeDict,
525 ):
526 with self.subTest(cls=cls):
527 inst = cls()
528 inst.foo = 42
529 self.assertEqual(inst.foo, 42)
530 self.assertEqual(inst.dictobj, inst.__dict__)
531 self.assertEqual(inst.dictobj, {"foo": 42})
532
533 inst = cls()
534 self.assertEqual({}, inst.__dict__)
535
536 def test_heaptype_with_managed_dict(self):
537 inst = _testcapi.HeapCTypeWithManagedDict()
538 inst.foo = 42
539 self.assertEqual(inst.foo, 42)
540 self.assertEqual(inst.__dict__, {"foo": 42})
541
542 inst = _testcapi.HeapCTypeWithManagedDict()
543 self.assertEqual({}, inst.__dict__)
544
545 a = _testcapi.HeapCTypeWithManagedDict()
546 b = _testcapi.HeapCTypeWithManagedDict()
547 a.b = b
548 b.a = a
549 del a, b
550
551 def test_sublclassing_managed_dict(self):
552
553 class C(_testcapi.HeapCTypeWithManagedDict):
554 pass
555
556 i = C()
557 i.spam = i
558 del i
559
560 def test_heaptype_with_negative_dict(self):
561 inst = _testcapi.HeapCTypeWithNegativeDict()
562 inst.foo = 42
563 self.assertEqual(inst.foo, 42)
564 self.assertEqual(inst.dictobj, inst.__dict__)
565 self.assertEqual(inst.dictobj, {"foo": 42})
566
567 inst = _testcapi.HeapCTypeWithNegativeDict()
568 self.assertEqual({}, inst.__dict__)
569
570 def test_heaptype_with_weakref(self):
571 for cls in (
572 _testcapi.HeapCTypeWithWeakref,
573 _testlimitedcapi.HeapCTypeWithRelativeWeakref,
574 ):
575 with self.subTest(cls=cls):
576 inst = cls()
577 ref = weakref.ref(inst)
578 self.assertEqual(ref(), inst)
579 self.assertEqual(inst.weakreflist, ref)
580
581 def test_heaptype_with_managed_weakref(self):
582 inst = _testcapi.HeapCTypeWithManagedWeakref()
583 ref = weakref.ref(inst)
584 self.assertEqual(ref(), inst)
585
586 def test_sublclassing_managed_weakref(self):
587
588 class C(_testcapi.HeapCTypeWithManagedWeakref):
589 pass
590
591 inst = C()
592 ref = weakref.ref(inst)
593 self.assertEqual(ref(), inst)
594
595 def test_sublclassing_managed_both(self):
596
597 class C1(_testcapi.HeapCTypeWithManagedWeakref, _testcapi.HeapCTypeWithManagedDict):
598 pass
599
600 class C2(_testcapi.HeapCTypeWithManagedDict, _testcapi.HeapCTypeWithManagedWeakref):
601 pass
602
603 for cls in (C1, C2):
604 inst = cls()
605 ref = weakref.ref(inst)
606 self.assertEqual(ref(), inst)
607 inst.spam = inst
608 del inst
609 ref = weakref.ref(cls())
610 self.assertIs(ref(), None)
611
612 def test_heaptype_with_buffer(self):
613 inst = _testcapi.HeapCTypeWithBuffer()
614 b = bytes(inst)
615 self.assertEqual(b, b"1234")
616
617 def test_c_subclass_of_heap_ctype_with_tpdealloc_decrefs_once(self):
618 subclass_instance = _testcapi.HeapCTypeSubclass()
619 type_refcnt = sys.getrefcount(_testcapi.HeapCTypeSubclass)
620
621 # Test that subclass instance was fully created
622 self.assertEqual(subclass_instance.value, 10)
623 self.assertEqual(subclass_instance.value2, 20)
624
625 # Test that the type reference count is only decremented once
626 del subclass_instance
627 self.assertEqual(type_refcnt - 1, sys.getrefcount(_testcapi.HeapCTypeSubclass))
628
629 def test_c_subclass_of_heap_ctype_with_del_modifying_dunder_class_only_decrefs_once(self):
630 subclass_instance = HeapCTypeSubclassWithFinalizer()
631 type_refcnt = sys.getrefcount(HeapCTypeSubclassWithFinalizer)
632 new_type_refcnt = sys.getrefcount(HeapCTypeSubclass)
633
634 # Test that subclass instance was fully created
635 self.assertEqual(subclass_instance.value, 10)
636 self.assertEqual(subclass_instance.value2, 20)
637
638 # The tp_finalize slot will set __class__ to HeapCTypeSubclass
639 del subclass_instance
640
641 # Test that setting __class__ modified the reference counts of the types
642 #
643 # This is highly sensitive to implementation details and may break in the future.
644 #
645 # We expect the refcount on the old type, HeapCTypeSubclassWithFinalizer, to
646 # remain the same: the finalizer gets a strong reference (+1) when it gets the
647 # type from the module and setting __class__ decrements the refcount (-1).
648 #
649 # We expect the refcount on the new type, HeapCTypeSubclass, to increase by 2:
650 # the finalizer get a strong reference (+1) when it gets the type from the
651 # module and setting __class__ increments the refcount (+1).
652 expected_type_refcnt = type_refcnt
653 expected_new_type_refcnt = new_type_refcnt + 2
654
655 if not Py_GIL_DISABLED:
656 # In default builds the result returned from sys.getrefcount
657 # includes a temporary reference that is created by the interpreter
658 # when it pushes its argument on the operand stack. This temporary
659 # reference is not included in the result returned by Py_REFCNT, which
660 # is used in the finalizer.
661 #
662 # In free-threaded builds the result returned from sys.getrefcount
663 # does not include the temporary reference. Types use deferred
664 # refcounting and the interpreter will not create a new reference
665 # for deferred values on the operand stack.
666 expected_type_refcnt -= 1
667 expected_new_type_refcnt -= 1
668
669 if support.Py_DEBUG:
670 # gh-89373: In debug mode, _Py_Dealloc() keeps a strong reference
671 # to the type while calling tp_dealloc()
672 expected_type_refcnt += 1
673
674 self.assertEqual(expected_type_refcnt, HeapCTypeSubclassWithFinalizer.refcnt_in_del)
675 self.assertEqual(expected_new_type_refcnt, HeapCTypeSubclass.refcnt_in_del)
676
677 # Test that the original type already has decreased its refcnt
678 self.assertEqual(type_refcnt - 1, sys.getrefcount(HeapCTypeSubclassWithFinalizer))
679
680 # Test that subtype_dealloc decref the newly assigned __class__ only once
681 self.assertEqual(new_type_refcnt, sys.getrefcount(HeapCTypeSubclass))
682
683 def test_heaptype_with_setattro(self):
684 obj = _testcapi.HeapCTypeSetattr()
685 self.assertEqual(obj.pvalue, 10)
686 obj.value = 12
687 self.assertEqual(obj.pvalue, 12)
688 del obj.value
689 self.assertEqual(obj.pvalue, 0)
690
691 def test_heaptype_with_custom_metaclass(self):
692 metaclass = _testcapi.HeapCTypeMetaclass
693 self.assertIsSubclass(metaclass, type)
694
695 # Class creation from C
696 t = _testcapi.pytype_fromspec_meta(metaclass)
697 self.assertIsInstance(t, type)
698 self.assertEqual(t.__name__, "HeapCTypeViaMetaclass")
699 self.assertIs(type(t), metaclass)
700
701 # Class creation from Python
702 t = metaclass("PyClassViaMetaclass", (), {})
703 self.assertIsInstance(t, type)
704 self.assertEqual(t.__name__, "PyClassViaMetaclass")
705
706 def test_heaptype_with_custom_metaclass_null_new(self):
707 metaclass = _testcapi.HeapCTypeMetaclassNullNew
708
709 self.assertIsSubclass(metaclass, type)
710
711 # Class creation from C
712 t = _testcapi.pytype_fromspec_meta(metaclass)
713 self.assertIsInstance(t, type)
714 self.assertEqual(t.__name__, "HeapCTypeViaMetaclass")
715 self.assertIs(type(t), metaclass)
716
717 # Class creation from Python
718 with self.assertRaisesRegex(TypeError, "cannot create .* instances"):
719 metaclass("PyClassViaMetaclass", (), {})
720
721 def test_heaptype_with_custom_metaclass_custom_new(self):
722 metaclass = _testcapi.HeapCTypeMetaclassCustomNew
723
724 self.assertIsSubclass(_testcapi.HeapCTypeMetaclassCustomNew, type)
725
726 msg = "Metaclasses with custom tp_new are not supported."
727 with self.assertRaisesRegex(TypeError, msg):
728 t = _testcapi.pytype_fromspec_meta(metaclass)
729
730 def test_heaptype_base_with_custom_metaclass(self):
731 metaclass = _testcapi.HeapCTypeMetaclassCustomNew
732
733 class Base(metaclass=metaclass):
734 pass
735
736 # Class creation from C
737 msg = "Metaclasses with custom tp_new are not supported."
738 with self.assertRaisesRegex(TypeError, msg):
739 sub = _testcapi.make_type_with_base(Base)
740
741 def test_heaptype_with_tp_vectorcall(self):
742 tp = _testcapi.HeapCTypeVectorcall
743 v0 = tp.__new__(tp)
744 v0.__init__()
745 v1 = tp()
746 self.assertEqual(v0.value, 2)
747 self.assertEqual(v1.value, 1)
748
749 def test_multiple_inheritance_ctypes_with_weakref_or_dict(self):
750 for weakref_cls in (_testcapi.HeapCTypeWithWeakref,
751 _testlimitedcapi.HeapCTypeWithRelativeWeakref):
752 for dict_cls in (_testcapi.HeapCTypeWithDict,
753 _testlimitedcapi.HeapCTypeWithRelativeDict):
754 with self.subTest(weakref_cls=weakref_cls, dict_cls=dict_cls):
755
756 with self.assertRaises(TypeError):
757 class Both1(weakref_cls, dict_cls):
758 pass
759 with self.assertRaises(TypeError):
760 class Both2(dict_cls, weakref_cls):
761 pass
762
763 def test_multiple_inheritance_ctypes_with_weakref_or_dict_and_other_builtin(self):
764 for dict_cls in (_testcapi.HeapCTypeWithDict,
765 _testlimitedcapi.HeapCTypeWithRelativeDict):
766 for weakref_cls in (_testcapi.HeapCTypeWithWeakref,
767 _testlimitedcapi.HeapCTypeWithRelativeWeakref):
768 with self.subTest(dict_cls=dict_cls, weakref_cls=weakref_cls):
769
770 with self.assertRaises(TypeError):
771 class C1(dict_cls, list):
772 pass
773
774 with self.assertRaises(TypeError):
775 class C2(weakref_cls, list):
776 pass
777
778 class C3(_testcapi.HeapCTypeWithManagedDict, list):
779 pass
780 class C4(_testcapi.HeapCTypeWithManagedWeakref, list):
781 pass
782
783 inst = C3()
784 inst.append(0)
785 str(inst.__dict__)
786
787 inst = C4()
788 inst.append(0)
789 str(inst.__weakref__)
790
791 for cls in (_testcapi.HeapCTypeWithManagedDict,
792 _testcapi.HeapCTypeWithManagedWeakref):
793 for cls2 in (dict_cls, weakref_cls):
794 class S(cls, cls2):
795 pass
796 class B1(C3, cls):
797 pass
798 class B2(C4, cls):
799 pass
800
801 def test_pytype_fromspec_with_repeated_slots(self):
802 for variant in range(2):
803 with self.subTest(variant=variant):
804 with self.assertRaises(SystemError):
805 _testcapi.create_type_from_repeated_slots(variant)
806
807 def test_immutable_type_with_mutable_base(self):
808 class MutableBase: ...
809
810 with self.assertRaisesRegex(TypeError, 'Creating immutable type'):
811 _testcapi.make_immutable_type_with_base(MutableBase)
812
813 def test_pynumber_tobase(self):
814 from _testcapi import pynumber_tobase
815 small_number = 123
816 large_number = 2**64
817 class IDX:
818 def __init__(self, val):
819 self.val = val
820 def __index__(self):
821 return self.val
822
823 test_cases = ((2, '0b1111011', '0b10000000000000000000000000000000000000000000000000000000000000000'),
824 (8, '0o173', '0o2000000000000000000000'),
825 (10, '123', '18446744073709551616'),
826 (16, '0x7b', '0x10000000000000000'))
827 for base, small_target, large_target in test_cases:
828 with self.subTest(base=base, st=small_target, lt=large_target):
829 # Test for small number
830 self.assertEqual(pynumber_tobase(small_number, base), small_target)
831 self.assertEqual(pynumber_tobase(-small_number, base), '-' + small_target)
832 self.assertEqual(pynumber_tobase(IDX(small_number), base), small_target)
833 # Test for large number(out of range of a longlong,i.e.[-2**63, 2**63-1])
834 self.assertEqual(pynumber_tobase(large_number, base), large_target)
835 self.assertEqual(pynumber_tobase(-large_number, base), '-' + large_target)
836 self.assertEqual(pynumber_tobase(IDX(large_number), base), large_target)
837 self.assertRaises(TypeError, pynumber_tobase, IDX(123.0), 10)
838 self.assertRaises(TypeError, pynumber_tobase, IDX('123'), 10)
839 self.assertRaises(TypeError, pynumber_tobase, 123.0, 10)
840 self.assertRaises(TypeError, pynumber_tobase, '123', 10)
841 self.assertRaises(SystemError, pynumber_tobase, 123, 0)
842
843 def test_pyobject_repr_from_null(self):
844 s = _testcapi.pyobject_repr_from_null()
845 self.assertEqual(s, '<NULL>')
846
847 def test_pyobject_str_from_null(self):
848 s = _testcapi.pyobject_str_from_null()
849 self.assertEqual(s, '<NULL>')
850
851 def test_pyobject_bytes_from_null(self):
852 s = _testcapi.pyobject_bytes_from_null()
853 self.assertEqual(s, b'<NULL>')
854
855 def test_Py_CompileString(self):
856 # Check that Py_CompileString respects the coding cookie
857 _compile = _testcapi.Py_CompileString
858 code = b"# -*- coding: latin1 -*-\nprint('\xc2\xa4')\n"
859 result = _compile(code)
860 expected = compile(code, "<string>", "exec")
861 self.assertEqual(result.co_consts, expected.co_consts)
862
863 def test_export_symbols(self):
864 # bpo-44133: Ensure that the "Py_FrozenMain" and
865 # "PyThread_get_thread_native_id" symbols are exported by the Python
866 # (directly by the binary, or via by the Python dynamic library).
867 ctypes = import_helper.import_module('ctypes')
868 names = []
869
870 # Test if the PY_HAVE_THREAD_NATIVE_ID macro is defined
871 if hasattr(_thread, 'get_native_id'):
872 names.append('PyThread_get_thread_native_id')
873
874 # Python/frozenmain.c fails to build on Windows when the symbols are
875 # missing:
876 # - PyWinFreeze_ExeInit
877 # - PyWinFreeze_ExeTerm
878 # - PyInitFrozenExtensions
879 if os.name != 'nt':
880 names.append('Py_FrozenMain')
881
882 for name in names:
883 self.assertHasAttr(ctypes.pythonapi, name)
884
885 def test_clear_managed_dict(self):
886
887 class C:
888 def __init__(self):
889 self.a = 1
890
891 c = C()
892 _testcapi.clear_managed_dict(c)
893 self.assertEqual(c.__dict__, {})
894 c = C()
895 self.assertEqual(c.__dict__, {'a':1})
896 _testcapi.clear_managed_dict(c)
897 self.assertEqual(c.__dict__, {})
898
899 def test_unstable_gc_new_with_extra_data(self):
900 class Data(_testcapi.ObjExtraData):
901 __slots__ = ('x', 'y')
902
903 d = Data()
904 d.x = 10
905 d.y = 20
906 d.extra = 30
907 self.assertEqual(d.x, 10)
908 self.assertEqual(d.y, 20)
909 self.assertEqual(d.extra, 30)
910 del d.extra
911 self.assertIsNone(d.extra)
912
913 def test_gen_get_code(self):
914 def genf(): yield
915 gen = genf()
916 self.assertEqual(_testcapi.gen_get_code(gen), gen.gi_code)
917
918 def test_tp_bases_slot(self):
919 cls = _testcapi.HeapCTypeWithBasesSlot
920 self.assertEqual(cls.__bases__, (int,))
921 self.assertEqual(cls.__base__, int)
922
923 def test_tp_bases_slot_none(self):
924 self.assertRaisesRegex(
925 SystemError,
926 "Py_tp_bases is not a tuple",
927 _testcapi.create_heapctype_with_none_bases_slot
928 )
929
930
931 @requires_limited_api
932 class TestHeapTypeRelative(unittest.TestCase):
933 """Test API for extending opaque types (PEP 697)"""
934
935 @requires_limited_api
936 def test_heaptype_relative_sizes(self):
937 # Test subclassing using "relative" basicsize, see PEP 697
938 def check(extra_base_size, extra_size):
939 Base, Sub, instance, data_ptr, data_offset, data_size = (
940 _testlimitedcapi.make_sized_heaptypes(
941 extra_base_size, -extra_size))
942
943 # no alignment shenanigans when inheriting directly
944 if extra_size == 0:
945 self.assertEqual(Base.__basicsize__, Sub.__basicsize__)
946 self.assertEqual(data_size, 0)
947
948 else:
949 # The following offsets should be in increasing order:
950 offsets = [
951 (0, 'start of object'),
952 (Base.__basicsize__, 'end of base data'),
953 (data_offset, 'subclass data'),
954 (data_offset + extra_size, 'end of requested subcls data'),
955 (data_offset + data_size, 'end of reserved subcls data'),
956 (Sub.__basicsize__, 'end of object'),
957 ]
958 ordered_offsets = sorted(offsets, key=operator.itemgetter(0))
959 self.assertEqual(
960 offsets, ordered_offsets,
961 msg=f'Offsets not in expected order, got: {ordered_offsets}')
962
963 # end of reserved subcls data == end of object
964 self.assertEqual(Sub.__basicsize__, data_offset + data_size)
965
966 # we don't reserve (requested + alignment) or more data
967 self.assertLess(data_size - extra_size,
968 _testlimitedcapi.ALIGNOF_MAX_ALIGN_T)
969
970 # The offsets/sizes we calculated should be aligned.
971 self.assertEqual(data_offset % _testlimitedcapi.ALIGNOF_MAX_ALIGN_T, 0)
972 self.assertEqual(data_size % _testlimitedcapi.ALIGNOF_MAX_ALIGN_T, 0)
973
974 sizes = sorted({0, 1, 2, 3, 4, 7, 8, 123,
975 object.__basicsize__,
976 object.__basicsize__-1,
977 object.__basicsize__+1})
978 for extra_base_size in sizes:
979 for extra_size in sizes:
980 args = dict(extra_base_size=extra_base_size,
981 extra_size=extra_size)
982 with self.subTest(**args):
983 check(**args)
984
985 def test_HeapCCollection(self):
986 """Make sure HeapCCollection works properly by itself"""
987 collection = _testcapi.HeapCCollection(1, 2, 3)
988 self.assertEqual(list(collection), [1, 2, 3])
989
990 def test_heaptype_inherit_itemsize(self):
991 """Test HeapCCollection subclasses work properly"""
992 sizes = sorted({0, 1, 2, 3, 4, 7, 8, 123,
993 object.__basicsize__,
994 object.__basicsize__-1,
995 object.__basicsize__+1})
996 for extra_size in sizes:
997 with self.subTest(extra_size=extra_size):
998 Sub = _testlimitedcapi.subclass_var_heaptype(
999 _testcapi.HeapCCollection, -extra_size, 0, 0)
1000 collection = Sub(1, 2, 3)
1001 collection.set_data_to_3s()
1002
1003 self.assertEqual(list(collection), [1, 2, 3])
1004 mem = collection.get_data()
1005 self.assertGreaterEqual(len(mem), extra_size)
1006 self.assertTrue(set(mem) <= {3}, f'got {mem!r}')
1007
1008 def test_heaptype_invalid_inheritance(self):
1009 with self.assertRaises(SystemError,
1010 msg="Cannot extend variable-size class without "
1011 + "Py_TPFLAGS_ITEMS_AT_END"):
1012 _testlimitedcapi.subclass_heaptype(int, -8, 0)
1013
1014 def test_heaptype_relative_members(self):
1015 """Test HeapCCollection subclasses work properly"""
1016 sizes = sorted({0, 1, 2, 3, 4, 7, 8, 123,
1017 object.__basicsize__,
1018 object.__basicsize__-1,
1019 object.__basicsize__+1})
1020 for extra_base_size in sizes:
1021 for extra_size in sizes:
1022 for offset in sizes:
1023 with self.subTest(extra_base_size=extra_base_size, extra_size=extra_size, offset=offset):
1024 if offset < extra_size:
1025 Sub = _testlimitedcapi.make_heaptype_with_member(
1026 extra_base_size, -extra_size, offset, True)
1027 Base = Sub.mro()[1]
1028 instance = Sub()
1029 self.assertEqual(instance.memb, instance.get_memb())
1030 instance.set_memb(13)
1031 self.assertEqual(instance.memb, instance.get_memb())
1032 self.assertEqual(instance.get_memb(), 13)
1033 instance.memb = 14
1034 self.assertEqual(instance.memb, instance.get_memb())
1035 self.assertEqual(instance.get_memb(), 14)
1036 self.assertGreaterEqual(instance.get_memb_offset(), Base.__basicsize__)
1037 self.assertLess(instance.get_memb_offset(), Sub.__basicsize__)
1038 with self.assertRaises(SystemError):
1039 instance.get_memb_relative()
1040 with self.assertRaises(SystemError):
1041 instance.set_memb_relative(0)
1042 else:
1043 with self.assertRaises(SystemError):
1044 Sub = _testlimitedcapi.make_heaptype_with_member(
1045 extra_base_size, -extra_size, offset, True)
1046 with self.assertRaises(SystemError):
1047 Sub = _testlimitedcapi.make_heaptype_with_member(
1048 extra_base_size, extra_size, offset, True)
1049 with self.subTest(extra_base_size=extra_base_size, extra_size=extra_size):
1050 with self.assertRaises(SystemError):
1051 Sub = _testlimitedcapi.make_heaptype_with_member(
1052 extra_base_size, -extra_size, -1, True)
1053
1054 def test_heaptype_relative_members_errors(self):
1055 with self.assertRaisesRegex(
1056 SystemError,
1057 r"With Py_RELATIVE_OFFSET, basicsize must be negative"):
1058 _testlimitedcapi.make_heaptype_with_member(0, 1234, 0, True)
1059 with self.assertRaisesRegex(
1060 SystemError, r"Member offset out of range \(0\.\.-basicsize\)"):
1061 _testlimitedcapi.make_heaptype_with_member(0, -8, 1234, True)
1062 with self.assertRaisesRegex(
1063 SystemError, r"Member offset out of range \(0\.\.-basicsize\)"):
1064 _testlimitedcapi.make_heaptype_with_member(0, -8, -1, True)
1065
1066 Sub = _testlimitedcapi.make_heaptype_with_member(0, -8, 0, True)
1067 instance = Sub()
1068 with self.assertRaisesRegex(
1069 SystemError, r"PyMember_GetOne used with Py_RELATIVE_OFFSET"):
1070 instance.get_memb_relative()
1071 with self.assertRaisesRegex(
1072 SystemError, r"PyMember_SetOne used with Py_RELATIVE_OFFSET"):
1073 instance.set_memb_relative(0)
1074
1075 def test_heaptype_relative_special_members_errors(self):
1076 for member_name in "__vectorcalloffset__", "__dictoffset__", "__weaklistoffset__":
1077 with self.subTest(member_name=member_name):
1078 with self.assertRaisesRegex(
1079 SystemError,
1080 r"With Py_RELATIVE_OFFSET, basicsize must be negative."):
1081 _testlimitedcapi.make_heaptype_with_member(
1082 basicsize=sys.getsizeof(object()) + 100,
1083 add_relative_flag=True,
1084 member_name=member_name,
1085 member_offset=0,
1086 member_type=_testlimitedcapi.Py_T_PYSSIZET,
1087 member_flags=_testlimitedcapi.Py_READONLY,
1088 )
1089 with self.assertRaisesRegex(
1090 SystemError,
1091 r"Member offset out of range \(0\.\.-basicsize\)"):
1092 _testlimitedcapi.make_heaptype_with_member(
1093 basicsize=-8,
1094 add_relative_flag=True,
1095 member_name=member_name,
1096 member_offset=-1,
1097 member_type=_testlimitedcapi.Py_T_PYSSIZET,
1098 member_flags=_testlimitedcapi.Py_READONLY,
1099 )
1100 with self.assertRaisesRegex(
1101 SystemError,
1102 r"type of %s must be Py_T_PYSSIZET" % member_name):
1103 _testlimitedcapi.make_heaptype_with_member(
1104 basicsize=-100,
1105 add_relative_flag=True,
1106 member_name=member_name,
1107 member_offset=0,
1108 member_flags=_testlimitedcapi.Py_READONLY,
1109 )
1110 with self.assertRaisesRegex(
1111 SystemError,
1112 r"flags for %s must be " % member_name):
1113 _testlimitedcapi.make_heaptype_with_member(
1114 basicsize=-100,
1115 add_relative_flag=True,
1116 member_name=member_name,
1117 member_offset=0,
1118 member_type=_testlimitedcapi.Py_T_PYSSIZET,
1119 member_flags=0,
1120 )
1121
1122 def test_pyobject_getitemdata_error(self):
1123 """Test PyObject_GetItemData fails on unsupported types"""
1124 with self.assertRaises(TypeError):
1125 # None is not variable-length
1126 _testcapi.pyobject_getitemdata(None)
1127 with self.assertRaises(TypeError):
1128 # int is variable-length, but doesn't have the
1129 # Py_TPFLAGS_ITEMS_AT_END layout (and flag)
1130 _testcapi.pyobject_getitemdata(0)
1131
1132
1133 class TestPendingCalls(unittest.TestCase):
1134
1135 # See the comment in ceval.c (at the "handle_eval_breaker" label)
1136 # about when pending calls get run. This is especially relevant
1137 # here for creating deterministic tests.
1138
1139 def main_pendingcalls_submit(self, l, n):
1140 def callback():
1141 #this function can be interrupted by thread switching so let's
1142 #use an atomic operation
1143 l.append(None)
1144
1145 for i in range(n):
1146 time.sleep(random.random()*0.02) #0.01 secs on average
1147 #try submitting callback until successful.
1148 #rely on regular interrupt to flush queue if we are
1149 #unsuccessful.
1150 while True:
1151 if _testcapi._pending_threadfunc(callback):
1152 break
1153
1154 def pendingcalls_submit(self, l, n, *, main=True, ensure=False):
1155 def callback():
1156 #this function can be interrupted by thread switching so let's
1157 #use an atomic operation
1158 l.append(None)
1159
1160 if main:
1161 return _testcapi._pending_threadfunc(callback, n,
1162 blocking=False,
1163 ensure_added=ensure)
1164 else:
1165 return _testinternalcapi.pending_threadfunc(callback, n,
1166 blocking=False,
1167 ensure_added=ensure)
1168
1169 def pendingcalls_wait(self, l, numadded, context = None):
1170 #now, stick around until l[0] has grown to 10
1171 count = 0
1172 while len(l) != numadded:
1173 #this busy loop is where we expect to be interrupted to
1174 #run our callbacks. Note that some callbacks are only run on the
1175 #main thread
1176 if False and support.verbose:
1177 print("(%i)"%(len(l),),)
1178 for i in range(1000):
1179 a = i*i
1180 if context and not context.event.is_set():
1181 continue
1182 count += 1
1183 self.assertTrue(count < 10000,
1184 "timeout waiting for %i callbacks, got %i"%(numadded, len(l)))
1185 if False and support.verbose:
1186 print("(%i)"%(len(l),))
1187
1188 @threading_helper.requires_working_threading()
1189 def test_main_pendingcalls_threaded(self):
1190
1191 #do every callback on a separate thread
1192 n = 32 #total callbacks
1193 threads = []
1194 class foo(object):pass
1195 context = foo()
1196 context.l = []
1197 context.n = 2 #submits per thread
1198 context.nThreads = n // context.n
1199 context.nFinished = 0
1200 context.lock = threading.Lock()
1201 context.event = threading.Event()
1202
1203 threads = [threading.Thread(target=self.main_pendingcalls_thread,
1204 args=(context,))
1205 for i in range(context.nThreads)]
1206 with threading_helper.start_threads(threads):
1207 self.pendingcalls_wait(context.l, n, context)
1208
1209 def main_pendingcalls_thread(self, context):
1210 try:
1211 self.main_pendingcalls_submit(context.l, context.n)
1212 finally:
1213 with context.lock:
1214 context.nFinished += 1
1215 nFinished = context.nFinished
1216 if False and support.verbose:
1217 print("finished threads: ", nFinished)
1218 if nFinished == context.nThreads:
1219 context.event.set()
1220
1221 def test_main_pendingcalls_non_threaded(self):
1222 #again, just using the main thread, likely they will all be dispatched at
1223 #once. It is ok to ask for too many, because we loop until we find a slot.
1224 #the loop can be interrupted to dispatch.
1225 #there are only 32 dispatch slots, so we go for twice that!
1226 l = []
1227 n = 64
1228 self.main_pendingcalls_submit(l, n)
1229 self.pendingcalls_wait(l, n)
1230
1231 def test_max_pending(self):
1232 with self.subTest('main-only'):
1233 maxpending = 32
1234
1235 l = []
1236 added = self.pendingcalls_submit(l, 1, main=True)
1237 self.pendingcalls_wait(l, added)
1238 self.assertEqual(added, 1)
1239
1240 l = []
1241 added = self.pendingcalls_submit(l, maxpending, main=True)
1242 self.pendingcalls_wait(l, added)
1243 self.assertEqual(added, maxpending)
1244
1245 l = []
1246 added = self.pendingcalls_submit(l, maxpending+1, main=True)
1247 self.pendingcalls_wait(l, added)
1248 self.assertEqual(added, maxpending)
1249
1250 with self.subTest('not main-only'):
1251 # Per-interpreter pending calls has a much higher limit
1252 # on how many may be pending at a time.
1253 maxpending = 300
1254
1255 l = []
1256 added = self.pendingcalls_submit(l, 1, main=False)
1257 self.pendingcalls_wait(l, added)
1258 self.assertEqual(added, 1)
1259
1260 l = []
1261 added = self.pendingcalls_submit(l, maxpending, main=False)
1262 self.pendingcalls_wait(l, added)
1263 self.assertEqual(added, maxpending)
1264
1265 l = []
1266 added = self.pendingcalls_submit(l, maxpending+1, main=False)
1267 self.pendingcalls_wait(l, added)
1268 self.assertEqual(added, maxpending)
1269
1270 class PendingTask(types.SimpleNamespace):
1271
1272 _add_pending = _testinternalcapi.pending_threadfunc
1273
1274 def __init__(self, req, taskid=None, notify_done=None):
1275 self.id = taskid
1276 self.req = req
1277 self.notify_done = notify_done
1278
1279 self.creator_tid = threading.get_ident()
1280 self.requester_tid = None
1281 self.runner_tid = None
1282 self.result = None
1283
1284 def run(self):
1285 assert self.result is None
1286 self.runner_tid = threading.get_ident()
1287 self._run()
1288 if self.notify_done is not None:
1289 self.notify_done()
1290
1291 def _run(self):
1292 self.result = self.req
1293
1294 def run_in_pending_call(self, worker_tids):
1295 assert self._add_pending is _testinternalcapi.pending_threadfunc
1296 self.requester_tid = threading.get_ident()
1297 def callback():
1298 assert self.result is None
1299 # It can be tricky to control which thread handles
1300 # the eval breaker, so we take a naive approach to
1301 # make sure.
1302 if threading.get_ident() not in worker_tids:
1303 self._add_pending(callback, ensure_added=True)
1304 return
1305 self.run()
1306 self._add_pending(callback, ensure_added=True)
1307
1308 def create_thread(self, worker_tids):
1309 return threading.Thread(
1310 target=self.run_in_pending_call,
1311 args=(worker_tids,),
1312 )
1313
1314 def wait_for_result(self):
1315 while self.result is None:
1316 time.sleep(0.01)
1317
1318 @threading_helper.requires_working_threading()
1319 def test_subthreads_can_handle_pending_calls(self):
1320 payload = 'Spam spam spam spam. Lovely spam! Wonderful spam!'
1321
1322 task = self.PendingTask(payload)
1323 def do_the_work():
1324 tid = threading.get_ident()
1325 t = task.create_thread({tid})
1326 with threading_helper.start_threads([t]):
1327 task.wait_for_result()
1328 t = threading.Thread(target=do_the_work)
1329 with threading_helper.start_threads([t]):
1330 pass
1331
1332 self.assertEqual(task.result, payload)
1333
1334 @threading_helper.requires_working_threading()
1335 def test_many_subthreads_can_handle_pending_calls(self):
1336 main_tid = threading.get_ident()
1337 self.assertEqual(threading.main_thread().ident, main_tid)
1338
1339 # We can't use queue.Queue since it isn't reentrant relative
1340 # to pending calls.
1341 _queue = deque()
1342 _active = deque()
1343 _done_lock = threading.Lock()
1344 def queue_put(task):
1345 _queue.append(task)
1346 _active.append(True)
1347 def queue_get():
1348 try:
1349 task = _queue.popleft()
1350 except IndexError:
1351 raise queue.Empty
1352 return task
1353 def queue_task_done():
1354 _active.pop()
1355 if not _active:
1356 try:
1357 _done_lock.release()
1358 except RuntimeError:
1359 assert not _done_lock.locked()
1360 def queue_empty():
1361 return not _queue
1362 def queue_join():
1363 _done_lock.acquire()
1364 _done_lock.release()
1365
1366 tasks = []
1367 for i in range(20):
1368 task = self.PendingTask(
1369 req=f'request {i}',
1370 taskid=i,
1371 notify_done=queue_task_done,
1372 )
1373 tasks.append(task)
1374 queue_put(task)
1375 # This will be released once all the tasks have finished.
1376 _done_lock.acquire()
1377
1378 def add_tasks(worker_tids):
1379 while True:
1380 if done:
1381 return
1382 try:
1383 task = queue_get()
1384 except queue.Empty:
1385 break
1386 task.run_in_pending_call(worker_tids)
1387
1388 done = False
1389 def run_tasks():
1390 while not queue_empty():
1391 if done:
1392 return
1393 time.sleep(0.01)
1394 # Give the worker a chance to handle any remaining pending calls.
1395 while not done:
1396 time.sleep(0.01)
1397
1398 # Start the workers and wait for them to finish.
1399 worker_threads = [threading.Thread(target=run_tasks)
1400 for _ in range(3)]
1401 with threading_helper.start_threads(worker_threads):
1402 try:
1403 # Add a pending call for each task.
1404 worker_tids = [t.ident for t in worker_threads]
1405 threads = [threading.Thread(target=add_tasks, args=(worker_tids,))
1406 for _ in range(3)]
1407 with threading_helper.start_threads(threads):
1408 try:
1409 pass
1410 except BaseException:
1411 done = True
1412 raise # re-raise
1413 # Wait for the pending calls to finish.
1414 queue_join()
1415 # Notify the workers that they can stop.
1416 done = True
1417 except BaseException:
1418 done = True
1419 raise # re-raise
1420 runner_tids = [t.runner_tid for t in tasks]
1421
1422 self.assertNotIn(main_tid, runner_tids)
1423 for task in tasks:
1424 with self.subTest(f'task {task.id}'):
1425 self.assertNotEqual(task.requester_tid, main_tid)
1426 self.assertNotEqual(task.requester_tid, task.runner_tid)
1427 self.assertNotIn(task.requester_tid, runner_tids)
1428
1429 @requires_subinterpreters
1430 @support.skip_if_sanitizer("gh-129824: race on assign_version_tag", thread=True)
1431 def test_isolated_subinterpreter(self):
1432 # We exercise the most important permutations.
1433
1434 # This test relies on pending calls getting called
1435 # (eval breaker tripped) at each loop iteration
1436 # and at each call.
1437
1438 maxtext = 250
1439 main_interpid = 0
1440 interpid = _interpreters.create()
1441 self.addCleanup(lambda: _interpreters.destroy(interpid))
1442 _interpreters.run_string(interpid, f"""if True:
1443 import json
1444 import os
1445 import threading
1446 import time
1447 import _testinternalcapi
1448 from test.support import threading_helper
1449 """)
1450
1451 def create_pipe():
1452 r, w = os.pipe()
1453 self.addCleanup(lambda: os.close(r))
1454 self.addCleanup(lambda: os.close(w))
1455 return r, w
1456
1457 with self.subTest('add in main, run in subinterpreter'):
1458 r_ready, w_ready = create_pipe()
1459 r_done, w_done= create_pipe()
1460 timeout = time.time() + 30 # seconds
1461
1462 def do_work():
1463 _interpreters.run_string(interpid, f"""if True:
1464 # Wait until this interp has handled the pending call.
1465 waiting = False
1466 done = False
1467 def wait(os_read=os.read):
1468 global done, waiting
1469 waiting = True
1470 os_read({r_done}, 1)
1471 done = True
1472 t = threading.Thread(target=wait)
1473 with threading_helper.start_threads([t]):
1474 while not waiting:
1475 pass
1476 os.write({w_ready}, b'\\0')
1477 # Loop to trigger the eval breaker.
1478 while not done:
1479 time.sleep(0.01)
1480 if time.time() > {timeout}:
1481 raise Exception('timed out!')
1482 """)
1483 t = threading.Thread(target=do_work)
1484 with threading_helper.start_threads([t]):
1485 os.read(r_ready, 1)
1486 # Add the pending call and wait for it to finish.
1487 actual = _testinternalcapi.pending_identify(interpid)
1488 # Signal the subinterpreter to stop.
1489 os.write(w_done, b'\0')
1490
1491 self.assertEqual(actual, int(interpid))
1492
1493 with self.subTest('add in main, run in subinterpreter sub-thread'):
1494 r_ready, w_ready = create_pipe()
1495 r_done, w_done= create_pipe()
1496 timeout = time.time() + 30 # seconds
1497
1498 def do_work():
1499 _interpreters.run_string(interpid, f"""if True:
1500 waiting = False
1501 done = False
1502 def subthread():
1503 while not waiting:
1504 pass
1505 os.write({w_ready}, b'\\0')
1506 # Loop to trigger the eval breaker.
1507 while not done:
1508 time.sleep(0.01)
1509 if time.time() > {timeout}:
1510 raise Exception('timed out!')
1511 t = threading.Thread(target=subthread)
1512 with threading_helper.start_threads([t]):
1513 # Wait until this interp has handled the pending call.
1514 waiting = True
1515 os.read({r_done}, 1)
1516 done = True
1517 """)
1518 t = threading.Thread(target=do_work)
1519 with threading_helper.start_threads([t]):
1520 os.read(r_ready, 1)
1521 # Add the pending call and wait for it to finish.
1522 actual = _testinternalcapi.pending_identify(interpid)
1523 # Signal the subinterpreter to stop.
1524 os.write(w_done, b'\0')
1525
1526 self.assertEqual(actual, int(interpid))
1527
1528 with self.subTest('add in subinterpreter, run in main'):
1529 r_ready, w_ready = create_pipe()
1530 r_done, w_done= create_pipe()
1531 r_data, w_data= create_pipe()
1532 timeout = time.time() + 30 # seconds
1533
1534 def add_job():
1535 os.read(r_ready, 1)
1536 _interpreters.run_string(interpid, f"""if True:
1537 # Add the pending call and wait for it to finish.
1538 actual = _testinternalcapi.pending_identify({main_interpid})
1539 # Signal the subinterpreter to stop.
1540 os.write({w_done}, b'\\0')
1541 os.write({w_data}, actual.to_bytes(1, 'little'))
1542 """)
1543 # Wait until this interp has handled the pending call.
1544 waiting = False
1545 done = False
1546 def wait(os_read=os.read):
1547 nonlocal done, waiting
1548 waiting = True
1549 os_read(r_done, 1)
1550 done = True
1551 t1 = threading.Thread(target=add_job)
1552 t2 = threading.Thread(target=wait)
1553 with threading_helper.start_threads([t1, t2]):
1554 while not waiting:
1555 pass
1556 os.write(w_ready, b'\0')
1557 # Loop to trigger the eval breaker.
1558 while not done:
1559 time.sleep(0.01)
1560 if time.time() > timeout:
1561 raise Exception('timed out!')
1562 text = os.read(r_data, 1)
1563 actual = int.from_bytes(text, 'little')
1564
1565 self.assertEqual(actual, int(main_interpid))
1566
1567 with self.subTest('add in subinterpreter, run in sub-thread'):
1568 r_ready, w_ready = create_pipe()
1569 r_done, w_done= create_pipe()
1570 r_data, w_data= create_pipe()
1571 timeout = time.time() + 30 # seconds
1572
1573 def add_job():
1574 os.read(r_ready, 1)
1575 _interpreters.run_string(interpid, f"""if True:
1576 # Add the pending call and wait for it to finish.
1577 actual = _testinternalcapi.pending_identify({main_interpid})
1578 # Signal the subinterpreter to stop.
1579 os.write({w_done}, b'\\0')
1580 os.write({w_data}, actual.to_bytes(1, 'little'))
1581 """)
1582 # Wait until this interp has handled the pending call.
1583 waiting = False
1584 done = False
1585 def wait(os_read=os.read):
1586 nonlocal done, waiting
1587 waiting = True
1588 os_read(r_done, 1)
1589 done = True
1590 def subthread():
1591 while not waiting:
1592 pass
1593 os.write(w_ready, b'\0')
1594 # Loop to trigger the eval breaker.
1595 while not done:
1596 time.sleep(0.01)
1597 if time.time() > timeout:
1598 raise Exception('timed out!')
1599 t1 = threading.Thread(target=add_job)
1600 t2 = threading.Thread(target=wait)
1601 t3 = threading.Thread(target=subthread)
1602 with threading_helper.start_threads([t1, t2, t3]):
1603 pass
1604 text = os.read(r_data, 1)
1605 actual = int.from_bytes(text, 'little')
1606
1607 self.assertEqual(actual, int(main_interpid))
1608
1609 # XXX We can't use the rest until gh-105716 is fixed.
1610 return
1611
1612 with self.subTest('add in subinterpreter, run in subinterpreter sub-thread'):
1613 r_ready, w_ready = create_pipe()
1614 r_done, w_done= create_pipe()
1615 r_data, w_data= create_pipe()
1616 timeout = time.time() + 30 # seconds
1617
1618 def do_work():
1619 _interpreters.run_string(interpid, f"""if True:
1620 waiting = False
1621 done = False
1622 def subthread():
1623 while not waiting:
1624 pass
1625 os.write({w_ready}, b'\\0')
1626 # Loop to trigger the eval breaker.
1627 while not done:
1628 time.sleep(0.01)
1629 if time.time() > {timeout}:
1630 raise Exception('timed out!')
1631 t = threading.Thread(target=subthread)
1632 with threading_helper.start_threads([t]):
1633 # Wait until this interp has handled the pending call.
1634 waiting = True
1635 os.read({r_done}, 1)
1636 done = True
1637 """)
1638 t = threading.Thread(target=do_work)
1639 #with threading_helper.start_threads([t]):
1640 t.start()
1641 if True:
1642 os.read(r_ready, 1)
1643 _interpreters.run_string(interpid, f"""if True:
1644 # Add the pending call and wait for it to finish.
1645 actual = _testinternalcapi.pending_identify({interpid})
1646 # Signal the subinterpreter to stop.
1647 os.write({w_done}, b'\\0')
1648 os.write({w_data}, actual.to_bytes(1, 'little'))
1649 """)
1650 t.join()
1651 text = os.read(r_data, 1)
1652 actual = int.from_bytes(text, 'little')
1653
1654 self.assertEqual(actual, int(interpid))
1655
1656
1657 class SubinterpreterTest(unittest.TestCase):
1658
1659 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
1660 def test_subinterps(self):
1661 import builtins
1662 r, w = os.pipe()
1663 code = """if 1:
1664 import sys, builtins, pickle
1665 with open({:d}, "wb") as f:
1666 pickle.dump(id(sys.modules), f)
1667 pickle.dump(id(builtins), f)
1668 """.format(w)
1669 with open(r, "rb") as f:
1670 ret = support.run_in_subinterp(code)
1671 self.assertEqual(ret, 0)
1672 self.assertNotEqual(pickle.load(f), id(sys.modules))
1673 self.assertNotEqual(pickle.load(f), id(builtins))
1674
1675 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
1676 def test_subinterps_recent_language_features(self):
1677 r, w = os.pipe()
1678 code = """if 1:
1679 import pickle
1680 with open({:d}, "wb") as f:
1681
1682 @(lambda x:x) # Py 3.9
1683 def noop(x): return x
1684
1685 a = (b := f'1{{2}}3') + noop('x') # Py 3.8 (:=) / 3.6 (f'')
1686
1687 async def foo(arg): return await arg # Py 3.5
1688
1689 pickle.dump(dict(a=a, b=b), f)
1690 """.format(w)
1691
1692 with open(r, "rb") as f:
1693 ret = support.run_in_subinterp(code)
1694 self.assertEqual(ret, 0)
1695 self.assertEqual(pickle.load(f), {'a': '123x', 'b': '123'})
1696
1697 # _testcapi cannot be imported in a subinterpreter on a Free Threaded build
1698 @support.requires_gil_enabled()
1699 def test_py_config_isoloated_per_interpreter(self):
1700 # A config change in one interpreter must not leak to out to others.
1701 #
1702 # This test could verify ANY config value, it just happens to have been
1703 # written around the time of int_max_str_digits. Refactoring is okay.
1704 code = """if 1:
1705 import sys, _testcapi
1706
1707 # Any config value would do, this happens to be the one being
1708 # double checked at the time this test was written.
1709 _testcapi.config_set('int_max_str_digits', 55555)
1710 sub_value = _testcapi.config_get('int_max_str_digits')
1711 assert sub_value == 55555, sub_value
1712 """
1713 before_config = _testcapi.config_get('int_max_str_digits')
1714 assert before_config != 55555
1715 self.assertEqual(support.run_in_subinterp(code), 0,
1716 'subinterp code failure, check stderr.')
1717 after_config = _testcapi.config_get('int_max_str_digits')
1718 self.assertIsNot(
1719 before_config, after_config,
1720 "Expected get_config() to return a new dict on each call")
1721 self.assertEqual(before_config, after_config,
1722 "CAUTION: Tests executed after this may be "
1723 "running under an altered config.")
1724 # try:...finally: calling set_config(before_config) not done
1725 # as that results in sys.argv, sys.path, and sys.warnoptions
1726 # "being modified by test_capi" per test.regrtest. So if this
1727 # test fails, assume that the environment in this process may
1728 # be altered and suspect.
1729
1730 @requires_subinterpreters
1731 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
1732 def test_configured_settings(self):
1733 """
1734 The config with which an interpreter is created corresponds
1735 1-to-1 with the new interpreter's settings. This test verifies
1736 that they match.
1737 """
1738
1739 OBMALLOC = 1<<5
1740 EXTENSIONS = 1<<8
1741 THREADS = 1<<10
1742 DAEMON_THREADS = 1<<11
1743 FORK = 1<<15
1744 EXEC = 1<<16
1745 ALL_FLAGS = (OBMALLOC | FORK | EXEC | THREADS | DAEMON_THREADS
1746 | EXTENSIONS);
1747
1748 features = [
1749 'obmalloc',
1750 'fork',
1751 'exec',
1752 'threads',
1753 'daemon_threads',
1754 'extensions',
1755 'own_gil',
1756 ]
1757 kwlist = [f'allow_{n}' for n in features]
1758 kwlist[0] = 'use_main_obmalloc'
1759 kwlist[-2] = 'check_multi_interp_extensions'
1760 kwlist[-1] = 'own_gil'
1761
1762 expected_to_work = {
1763 (True, True, True, True, True, True, True):
1764 (ALL_FLAGS, True),
1765 (True, False, False, False, False, False, False):
1766 (OBMALLOC, False),
1767 (False, False, False, True, False, True, False):
1768 (THREADS | EXTENSIONS, False),
1769 }
1770
1771 expected_to_fail = {
1772 (False, False, False, False, False, False, False),
1773 }
1774
1775 # gh-117649: The free-threaded build does not currently allow
1776 # setting check_multi_interp_extensions to False.
1777 if Py_GIL_DISABLED:
1778 for config in list(expected_to_work.keys()):
1779 kwargs = dict(zip(kwlist, config))
1780 if not kwargs['check_multi_interp_extensions']:
1781 del expected_to_work[config]
1782 expected_to_fail.add(config)
1783
1784 # expected to work
1785 for config, expected in expected_to_work.items():
1786 kwargs = dict(zip(kwlist, config))
1787 exp_flags, exp_gil = expected
1788 expected = {
1789 'feature_flags': exp_flags,
1790 'own_gil': exp_gil,
1791 }
1792 with self.subTest(config):
1793 r, w = os.pipe()
1794 script = textwrap.dedent(f'''
1795 import _testinternalcapi, json, os
1796 settings = _testinternalcapi.get_interp_settings()
1797 with os.fdopen({w}, "w") as stdin:
1798 json.dump(settings, stdin)
1799 ''')
1800 with os.fdopen(r) as stdout:
1801 ret = support.run_in_subinterp_with_config(script, **kwargs)
1802 self.assertEqual(ret, 0)
1803 out = stdout.read()
1804 settings = json.loads(out)
1805
1806 self.assertEqual(settings, expected)
1807
1808 # expected to fail
1809 for config in expected_to_fail:
1810 kwargs = dict(zip(kwlist, config))
1811 with self.subTest(config):
1812 script = textwrap.dedent(f'''
1813 import _testinternalcapi
1814 _testinternalcapi.get_interp_settings()
1815 raise NotImplementedError('unreachable')
1816 ''')
1817 with self.assertRaises(_interpreters.InterpreterError):
1818 support.run_in_subinterp_with_config(script, **kwargs)
1819
1820 @unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
1821 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
1822 # gh-117649: The free-threaded build does not currently allow overriding
1823 # the check_multi_interp_extensions setting.
1824 @expected_failure_if_gil_disabled()
1825 def test_overridden_setting_extensions_subinterp_check(self):
1826 """
1827 PyInterpreterConfig.check_multi_interp_extensions can be overridden
1828 with PyInterpreterState.override_multi_interp_extensions_check.
1829 This verifies that the override works but does not modify
1830 the underlying setting.
1831 """
1832
1833 OBMALLOC = 1<<5
1834 EXTENSIONS = 1<<8
1835 THREADS = 1<<10
1836 DAEMON_THREADS = 1<<11
1837 FORK = 1<<15
1838 EXEC = 1<<16
1839 BASE_FLAGS = OBMALLOC | FORK | EXEC | THREADS | DAEMON_THREADS
1840 base_kwargs = {
1841 'use_main_obmalloc': True,
1842 'allow_fork': True,
1843 'allow_exec': True,
1844 'allow_threads': True,
1845 'allow_daemon_threads': True,
1846 'own_gil': False,
1847 }
1848
1849 def check(enabled, override):
1850 kwargs = dict(
1851 base_kwargs,
1852 check_multi_interp_extensions=enabled,
1853 )
1854 flags = BASE_FLAGS | EXTENSIONS if enabled else BASE_FLAGS
1855 settings = {
1856 'feature_flags': flags,
1857 'own_gil': False,
1858 }
1859
1860 expected = {
1861 'requested': override,
1862 'override__initial': 0,
1863 'override_after': override,
1864 'override_restored': 0,
1865 # The override should not affect the config or settings.
1866 'settings__initial': settings,
1867 'settings_after': settings,
1868 'settings_restored': settings,
1869 # These are the most likely values to be wrong.
1870 'allowed__initial': not enabled,
1871 'allowed_after': not ((override > 0) if override else enabled),
1872 'allowed_restored': not enabled,
1873 }
1874
1875 r, w = os.pipe()
1876 if Py_GIL_DISABLED:
1877 # gh-117649: The test fails before `w` is closed
1878 self.addCleanup(os.close, w)
1879 script = textwrap.dedent(f'''
1880 from test.test_capi.check_config import run_singlephase_check
1881 run_singlephase_check({override}, {w})
1882 ''')
1883 with os.fdopen(r) as stdout:
1884 ret = support.run_in_subinterp_with_config(script, **kwargs)
1885 self.assertEqual(ret, 0)
1886 out = stdout.read()
1887 results = json.loads(out)
1888
1889 self.assertEqual(results, expected)
1890
1891 self.maxDiff = None
1892
1893 # setting: check disabled
1894 with self.subTest('config: check disabled; override: disabled'):
1895 check(False, -1)
1896 with self.subTest('config: check disabled; override: use config'):
1897 check(False, 0)
1898 with self.subTest('config: check disabled; override: enabled'):
1899 check(False, 1)
1900
1901 # setting: check enabled
1902 with self.subTest('config: check enabled; override: disabled'):
1903 check(True, -1)
1904 with self.subTest('config: check enabled; override: use config'):
1905 check(True, 0)
1906 with self.subTest('config: check enabled; override: enabled'):
1907 check(True, 1)
1908
1909 def test_mutate_exception(self):
1910 """
1911 Exceptions saved in global module state get shared between
1912 individual module instances. This test checks whether or not
1913 a change in one interpreter's module gets reflected into the
1914 other ones.
1915 """
1916 import binascii
1917
1918 support.run_in_subinterp("import binascii; binascii.Error.foobar = 'foobar'")
1919
1920 self.assertNotHasAttr(binascii.Error, "foobar")
1921
1922 @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
1923 # gh-117649: The free-threaded build does not currently support sharing
1924 # extension module state between interpreters.
1925 @expected_failure_if_gil_disabled()
1926 def test_module_state_shared_in_global(self):
1927 """
1928 bpo-44050: Extension module state should be shared between interpreters
1929 when it doesn't support sub-interpreters.
1930 """
1931 r, w = os.pipe()
1932 self.addCleanup(os.close, r)
1933 self.addCleanup(os.close, w)
1934
1935 # Apple extensions must be distributed as frameworks. This requires
1936 # a specialist loader.
1937 if support.is_apple_mobile:
1938 loader = "AppleFrameworkLoader"
1939 else:
1940 loader = "ExtensionFileLoader"
1941
1942 script = textwrap.dedent(f"""
1943 import importlib.machinery
1944 import importlib.util
1945 import os
1946
1947 fullname = '_test_module_state_shared'
1948 origin = importlib.util.find_spec('_testmultiphase').origin
1949 loader = importlib.machinery.{loader}(fullname, origin)
1950 spec = importlib.util.spec_from_loader(fullname, loader)
1951 module = importlib.util.module_from_spec(spec)
1952 attr_id = str(id(module.Error)).encode()
1953
1954 os.write({w}, attr_id)
1955 """)
1956 exec(script)
1957 main_attr_id = os.read(r, 100)
1958
1959 ret = support.run_in_subinterp(script)
1960 self.assertEqual(ret, 0)
1961 subinterp_attr_id = os.read(r, 100)
1962 self.assertEqual(main_attr_id, subinterp_attr_id)
1963
1964
1965 @requires_subinterpreters
1966 class InterpreterConfigTests(unittest.TestCase):
1967
1968 supported = {
1969 'isolated': types.SimpleNamespace(
1970 use_main_obmalloc=False,
1971 allow_fork=False,
1972 allow_exec=False,
1973 allow_threads=True,
1974 allow_daemon_threads=False,
1975 check_multi_interp_extensions=True,
1976 gil='own',
1977 ),
1978 'legacy': types.SimpleNamespace(
1979 use_main_obmalloc=True,
1980 allow_fork=True,
1981 allow_exec=True,
1982 allow_threads=True,
1983 allow_daemon_threads=True,
1984 check_multi_interp_extensions=bool(Py_GIL_DISABLED),
1985 gil='shared',
1986 ),
1987 'empty': types.SimpleNamespace(
1988 use_main_obmalloc=False,
1989 allow_fork=False,
1990 allow_exec=False,
1991 allow_threads=False,
1992 allow_daemon_threads=False,
1993 check_multi_interp_extensions=False,
1994 gil='default',
1995 ),
1996 }
1997 gil_supported = ['default', 'shared', 'own']
1998
1999 def iter_all_configs(self):
2000 for use_main_obmalloc in (True, False):
2001 for allow_fork in (True, False):
2002 for allow_exec in (True, False):
2003 for allow_threads in (True, False):
2004 for allow_daemon in (True, False):
2005 for checkext in (True, False):
2006 for gil in ('shared', 'own', 'default'):
2007 yield types.SimpleNamespace(
2008 use_main_obmalloc=use_main_obmalloc,
2009 allow_fork=allow_fork,
2010 allow_exec=allow_exec,
2011 allow_threads=allow_threads,
2012 allow_daemon_threads=allow_daemon,
2013 check_multi_interp_extensions=checkext,
2014 gil=gil,
2015 )
2016
2017 def assert_ns_equal(self, ns1, ns2, msg=None):
2018 # This is mostly copied from TestCase.assertDictEqual.
2019 self.assertEqual(type(ns1), type(ns2))
2020 if ns1 == ns2:
2021 return
2022
2023 import difflib
2024 import pprint
2025 from unittest.util import _common_shorten_repr
2026 standardMsg = '%s != %s' % _common_shorten_repr(ns1, ns2)
2027 diff = ('\n' + '\n'.join(difflib.ndiff(
2028 pprint.pformat(vars(ns1)).splitlines(),
2029 pprint.pformat(vars(ns2)).splitlines())))
2030 diff = f'namespace({diff})'
2031 standardMsg = self._truncateMessage(standardMsg, diff)
2032 self.fail(self._formatMessage(msg, standardMsg))
2033
2034 def test_predefined_config(self):
2035 def check(name, expected):
2036 expected = self.supported[expected]
2037 args = (name,) if name else ()
2038
2039 config1 = _interpreters.new_config(*args)
2040 self.assert_ns_equal(config1, expected)
2041 self.assertIsNot(config1, expected)
2042
2043 config2 = _interpreters.new_config(*args)
2044 self.assert_ns_equal(config2, expected)
2045 self.assertIsNot(config2, expected)
2046 self.assertIsNot(config2, config1)
2047
2048 with self.subTest('default'):
2049 check(None, 'isolated')
2050
2051 for name in self.supported:
2052 with self.subTest(name):
2053 check(name, name)
2054
2055 def test_update_from_dict(self):
2056 for name, vanilla in self.supported.items():
2057 with self.subTest(f'noop ({name})'):
2058 expected = vanilla
2059 overrides = vars(vanilla)
2060 config = _interpreters.new_config(name, **overrides)
2061 self.assert_ns_equal(config, expected)
2062
2063 with self.subTest(f'change all ({name})'):
2064 overrides = {k: not v for k, v in vars(vanilla).items()}
2065 for gil in self.gil_supported:
2066 if vanilla.gil == gil:
2067 continue
2068 overrides['gil'] = gil
2069 expected = types.SimpleNamespace(**overrides)
2070 config = _interpreters.new_config(
2071 name, **overrides)
2072 self.assert_ns_equal(config, expected)
2073
2074 # Override individual fields.
2075 for field, old in vars(vanilla).items():
2076 if field == 'gil':
2077 values = [v for v in self.gil_supported if v != old]
2078 else:
2079 values = [not old]
2080 for val in values:
2081 with self.subTest(f'{name}.{field} ({old!r} -> {val!r})'):
2082 overrides = {field: val}
2083 expected = types.SimpleNamespace(
2084 **dict(vars(vanilla), **overrides),
2085 )
2086 config = _interpreters.new_config(
2087 name, **overrides)
2088 self.assert_ns_equal(config, expected)
2089
2090 with self.subTest('unsupported field'):
2091 for name in self.supported:
2092 with self.assertRaises(ValueError):
2093 _interpreters.new_config(name, spam=True)
2094
2095 # Bad values for bool fields.
2096 for field, value in vars(self.supported['empty']).items():
2097 if field == 'gil':
2098 continue
2099 assert isinstance(value, bool)
2100 for value in [1, '', 'spam', 1.0, None, object()]:
2101 with self.subTest(f'unsupported value ({field}={value!r})'):
2102 with self.assertRaises(TypeError):
2103 _interpreters.new_config(**{field: value})
2104
2105 # Bad values for .gil.
2106 for value in [True, 1, 1.0, None, object()]:
2107 with self.subTest(f'unsupported value(gil={value!r})'):
2108 with self.assertRaises(TypeError):
2109 _interpreters.new_config(gil=value)
2110 for value in ['', 'spam']:
2111 with self.subTest(f'unsupported value (gil={value!r})'):
2112 with self.assertRaises(ValueError):
2113 _interpreters.new_config(gil=value)
2114
2115 def test_interp_init(self):
2116 questionable = [
2117 # strange
2118 dict(
2119 allow_fork=True,
2120 allow_exec=False,
2121 ),
2122 dict(
2123 gil='shared',
2124 use_main_obmalloc=False,
2125 ),
2126 # risky
2127 dict(
2128 allow_fork=True,
2129 allow_threads=True,
2130 ),
2131 # ought to be invalid?
2132 dict(
2133 allow_threads=False,
2134 allow_daemon_threads=True,
2135 ),
2136 dict(
2137 gil='own',
2138 use_main_obmalloc=True,
2139 ),
2140 ]
2141 invalid = [
2142 dict(
2143 use_main_obmalloc=False,
2144 check_multi_interp_extensions=False
2145 ),
2146 ]
2147 if Py_GIL_DISABLED:
2148 invalid.append(dict(check_multi_interp_extensions=False))
2149 def match(config, override_cases):
2150 ns = vars(config)
2151 for overrides in override_cases:
2152 if dict(ns, **overrides) == ns:
2153 return True
2154 return False
2155
2156 def check(config):
2157 script = 'pass'
2158 rc = _testinternalcapi.run_in_subinterp_with_config(script, config)
2159 self.assertEqual(rc, 0)
2160
2161 for config in self.iter_all_configs():
2162 if config.gil == 'default':
2163 continue
2164 if match(config, invalid):
2165 with self.subTest(f'invalid: {config}'):
2166 with self.assertRaises(_interpreters.InterpreterError):
2167 check(config)
2168 elif match(config, questionable):
2169 with self.subTest(f'questionable: {config}'):
2170 check(config)
2171 else:
2172 with self.subTest(f'valid: {config}'):
2173 check(config)
2174
2175 def test_get_config(self):
2176 @contextlib.contextmanager
2177 def new_interp(config):
2178 interpid = _interpreters.create(config, reqrefs=False)
2179 try:
2180 yield interpid
2181 finally:
2182 try:
2183 _interpreters.destroy(interpid)
2184 except _interpreters.InterpreterNotFoundError:
2185 pass
2186
2187 with self.subTest('main'):
2188 expected = _interpreters.new_config('legacy')
2189 expected.gil = 'own'
2190 if Py_GIL_DISABLED:
2191 expected.check_multi_interp_extensions = False
2192 interpid, *_ = _interpreters.get_main()
2193 config = _interpreters.get_config(interpid)
2194 self.assert_ns_equal(config, expected)
2195
2196 with self.subTest('isolated'):
2197 expected = _interpreters.new_config('isolated')
2198 with new_interp('isolated') as interpid:
2199 config = _interpreters.get_config(interpid)
2200 self.assert_ns_equal(config, expected)
2201
2202 with self.subTest('legacy'):
2203 expected = _interpreters.new_config('legacy')
2204 with new_interp('legacy') as interpid:
2205 config = _interpreters.get_config(interpid)
2206 self.assert_ns_equal(config, expected)
2207
2208 with self.subTest('custom'):
2209 orig = _interpreters.new_config(
2210 'empty',
2211 use_main_obmalloc=True,
2212 gil='shared',
2213 check_multi_interp_extensions=bool(Py_GIL_DISABLED),
2214 )
2215 with new_interp(orig) as interpid:
2216 config = _interpreters.get_config(interpid)
2217 self.assert_ns_equal(config, orig)
2218
2219
2220 @requires_subinterpreters
2221 class InterpreterIDTests(unittest.TestCase):
2222
2223 def add_interp_cleanup(self, interpid):
2224 def ensure_destroyed():
2225 try:
2226 _interpreters.destroy(interpid)
2227 except _interpreters.InterpreterNotFoundError:
2228 pass
2229 self.addCleanup(ensure_destroyed)
2230
2231 def new_interpreter(self):
2232 id = _interpreters.create()
2233 self.add_interp_cleanup(id)
2234 return id
2235
2236 def test_conversion_int(self):
2237 convert = _testinternalcapi.normalize_interp_id
2238 interpid = convert(10)
2239 self.assertEqual(interpid, 10)
2240
2241 def test_conversion_coerced(self):
2242 convert = _testinternalcapi.normalize_interp_id
2243 class MyInt(str):
2244 def __index__(self):
2245 return 10
2246 interpid = convert(MyInt())
2247 self.assertEqual(interpid, 10)
2248
2249 def test_conversion_from_interpreter(self):
2250 convert = _testinternalcapi.normalize_interp_id
2251 interpid = self.new_interpreter()
2252 converted = convert(interpid)
2253 self.assertEqual(converted, interpid)
2254
2255 def test_conversion_bad(self):
2256 convert = _testinternalcapi.normalize_interp_id
2257
2258 for badid in [
2259 object(),
2260 10.0,
2261 '10',
2262 b'10',
2263 ]:
2264 with self.subTest(f'bad: {badid!r}'):
2265 with self.assertRaises(TypeError):
2266 convert(badid)
2267
2268 badid = -1
2269 with self.subTest(f'bad: {badid!r}'):
2270 with self.assertRaises(ValueError):
2271 convert(badid)
2272
2273 badid = 2**64
2274 with self.subTest(f'bad: {badid!r}'):
2275 with self.assertRaises(OverflowError):
2276 convert(badid)
2277
2278 def test_lookup_exists(self):
2279 interpid = self.new_interpreter()
2280 self.assertTrue(
2281 _testinternalcapi.interpreter_exists(interpid))
2282
2283 def test_lookup_does_not_exist(self):
2284 interpid = _testinternalcapi.unused_interpreter_id()
2285 self.assertFalse(
2286 _testinternalcapi.interpreter_exists(interpid))
2287
2288 def test_lookup_destroyed(self):
2289 interpid = _interpreters.create()
2290 _interpreters.destroy(interpid)
2291 self.assertFalse(
2292 _testinternalcapi.interpreter_exists(interpid))
2293
2294 def get_refcount_helpers(self):
2295 return (
2296 _testinternalcapi.get_interpreter_refcount,
2297 (lambda id: _interpreters.incref(id, implieslink=False)),
2298 _interpreters.decref,
2299 )
2300
2301 def test_linked_lifecycle_does_not_exist(self):
2302 exists = _testinternalcapi.interpreter_exists
2303 is_linked = _testinternalcapi.interpreter_refcount_linked
2304 link = _testinternalcapi.link_interpreter_refcount
2305 unlink = _testinternalcapi.unlink_interpreter_refcount
2306 get_refcount, incref, decref = self.get_refcount_helpers()
2307
2308 with self.subTest('never existed'):
2309 interpid = _testinternalcapi.unused_interpreter_id()
2310 self.assertFalse(
2311 exists(interpid))
2312 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2313 is_linked(interpid)
2314 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2315 link(interpid)
2316 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2317 unlink(interpid)
2318 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2319 get_refcount(interpid)
2320 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2321 incref(interpid)
2322 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2323 decref(interpid)
2324
2325 with self.subTest('destroyed'):
2326 interpid = _interpreters.create()
2327 _interpreters.destroy(interpid)
2328 self.assertFalse(
2329 exists(interpid))
2330 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2331 is_linked(interpid)
2332 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2333 link(interpid)
2334 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2335 unlink(interpid)
2336 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2337 get_refcount(interpid)
2338 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2339 incref(interpid)
2340 with self.assertRaises(_interpreters.InterpreterNotFoundError):
2341 decref(interpid)
2342
2343 def test_linked_lifecycle_initial(self):
2344 is_linked = _testinternalcapi.interpreter_refcount_linked
2345 get_refcount, _, _ = self.get_refcount_helpers()
2346
2347 # A new interpreter will start out not linked, with a refcount of 0.
2348 interpid = self.new_interpreter()
2349 linked = is_linked(interpid)
2350 refcount = get_refcount(interpid)
2351
2352 self.assertFalse(linked)
2353 self.assertEqual(refcount, 0)
2354
2355 def test_linked_lifecycle_never_linked(self):
2356 exists = _testinternalcapi.interpreter_exists
2357 is_linked = _testinternalcapi.interpreter_refcount_linked
2358 get_refcount, incref, decref = self.get_refcount_helpers()
2359
2360 interpid = self.new_interpreter()
2361
2362 # Incref will not automatically link it.
2363 incref(interpid)
2364 self.assertFalse(
2365 is_linked(interpid))
2366 self.assertEqual(
2367 1, get_refcount(interpid))
2368
2369 # It isn't linked so it isn't destroyed.
2370 decref(interpid)
2371 self.assertTrue(
2372 exists(interpid))
2373 self.assertFalse(
2374 is_linked(interpid))
2375 self.assertEqual(
2376 0, get_refcount(interpid))
2377
2378 def test_linked_lifecycle_link_unlink(self):
2379 exists = _testinternalcapi.interpreter_exists
2380 is_linked = _testinternalcapi.interpreter_refcount_linked
2381 link = _testinternalcapi.link_interpreter_refcount
2382 unlink = _testinternalcapi.unlink_interpreter_refcount
2383
2384 interpid = self.new_interpreter()
2385
2386 # Linking at refcount 0 does not destroy the interpreter.
2387 link(interpid)
2388 self.assertTrue(
2389 exists(interpid))
2390 self.assertTrue(
2391 is_linked(interpid))
2392
2393 # Unlinking at refcount 0 does not destroy the interpreter.
2394 unlink(interpid)
2395 self.assertTrue(
2396 exists(interpid))
2397 self.assertFalse(
2398 is_linked(interpid))
2399
2400 def test_linked_lifecycle_link_incref_decref(self):
2401 exists = _testinternalcapi.interpreter_exists
2402 is_linked = _testinternalcapi.interpreter_refcount_linked
2403 link = _testinternalcapi.link_interpreter_refcount
2404 get_refcount, incref, decref = self.get_refcount_helpers()
2405
2406 interpid = self.new_interpreter()
2407
2408 # Linking it will not change the refcount.
2409 link(interpid)
2410 self.assertTrue(
2411 is_linked(interpid))
2412 self.assertEqual(
2413 0, get_refcount(interpid))
2414
2415 # Decref with a refcount of 0 is not allowed.
2416 incref(interpid)
2417 self.assertEqual(
2418 1, get_refcount(interpid))
2419
2420 # When linked, decref back to 0 destroys the interpreter.
2421 decref(interpid)
2422 self.assertFalse(
2423 exists(interpid))
2424
2425 def test_linked_lifecycle_incref_link(self):
2426 is_linked = _testinternalcapi.interpreter_refcount_linked
2427 link = _testinternalcapi.link_interpreter_refcount
2428 get_refcount, incref, _ = self.get_refcount_helpers()
2429
2430 interpid = self.new_interpreter()
2431
2432 incref(interpid)
2433 self.assertEqual(
2434 1, get_refcount(interpid))
2435
2436 # Linking it will not reset the refcount.
2437 link(interpid)
2438 self.assertTrue(
2439 is_linked(interpid))
2440 self.assertEqual(
2441 1, get_refcount(interpid))
2442
2443 def test_linked_lifecycle_link_incref_unlink_decref(self):
2444 exists = _testinternalcapi.interpreter_exists
2445 is_linked = _testinternalcapi.interpreter_refcount_linked
2446 link = _testinternalcapi.link_interpreter_refcount
2447 unlink = _testinternalcapi.unlink_interpreter_refcount
2448 get_refcount, incref, decref = self.get_refcount_helpers()
2449
2450 interpid = self.new_interpreter()
2451
2452 link(interpid)
2453 self.assertTrue(
2454 is_linked(interpid))
2455
2456 incref(interpid)
2457 self.assertEqual(
2458 1, get_refcount(interpid))
2459
2460 # Unlinking it will not change the refcount.
2461 unlink(interpid)
2462 self.assertFalse(
2463 is_linked(interpid))
2464 self.assertEqual(
2465 1, get_refcount(interpid))
2466
2467 # Unlinked: decref back to 0 does not destroys the interpreter.
2468 decref(interpid)
2469 self.assertTrue(
2470 exists(interpid))
2471 self.assertEqual(
2472 0, get_refcount(interpid))
2473
2474
2475 class TestStaticTypes(unittest.TestCase):
2476
2477 _has_run = False
2478
2479 @classmethod
2480 def setUpClass(cls):
2481 # The tests here don't play nice with our approach to refleak
2482 # detection, so we bail out in that case.
2483 if cls._has_run:
2484 raise unittest.SkipTest('these tests do not support re-running')
2485 cls._has_run = True
2486
2487 @contextlib.contextmanager
2488 def basic_static_type(self, *args):
2489 cls = _testcapi.get_basic_static_type(*args)
2490 yield cls
2491
2492 def test_pytype_ready_always_sets_tp_type(self):
2493 # The point of this test is to prevent something like
2494 # https://github.com/python/cpython/issues/104614
2495 # from happening again.
2496
2497 # First check when tp_base/tp_bases is *not* set before PyType_Ready().
2498 with self.basic_static_type() as cls:
2499 self.assertIs(cls.__base__, object);
2500 self.assertEqual(cls.__bases__, (object,));
2501 self.assertIs(type(cls), type(object));
2502
2503 # Then check when we *do* set tp_base/tp_bases first.
2504 with self.basic_static_type(object) as cls:
2505 self.assertIs(cls.__base__, object);
2506 self.assertEqual(cls.__bases__, (object,));
2507 self.assertIs(type(cls), type(object));
2508
2509
2510 class TestThreadState(unittest.TestCase):
2511
2512 @threading_helper.reap_threads
2513 @threading_helper.requires_working_threading()
2514 def test_thread_state(self):
2515 # some extra thread-state tests driven via _testcapi
2516 def target():
2517 idents = []
2518
2519 def callback():
2520 idents.append(threading.get_ident())
2521
2522 _testcapi._test_thread_state(callback)
2523 a = b = callback
2524 time.sleep(1)
2525 # Check our main thread is in the list exactly 3 times.
2526 self.assertEqual(idents.count(threading.get_ident()), 3,
2527 "Couldn't find main thread correctly in the list")
2528
2529 target()
2530 t = threading.Thread(target=target)
2531 t.start()
2532 t.join()
2533
2534 @threading_helper.reap_threads
2535 @threading_helper.requires_working_threading()
2536 def test_thread_gilstate_in_clear(self):
2537 # See https://github.com/python/cpython/issues/119585
2538 class C:
2539 def __del__(self):
2540 _testcapi.gilstate_ensure_release()
2541
2542 # Thread-local variables are destroyed in `PyThreadState_Clear()`.
2543 local_var = threading.local()
2544
2545 def callback():
2546 local_var.x = C()
2547
2548 _testcapi._test_thread_state(callback)
2549
2550 @threading_helper.reap_threads
2551 @threading_helper.requires_working_threading()
2552 def test_gilstate_ensure_no_deadlock(self):
2553 # See https://github.com/python/cpython/issues/96071
2554 code = textwrap.dedent("""
2555 import _testcapi
2556
2557 def callback():
2558 print('callback called')
2559
2560 _testcapi._test_thread_state(callback)
2561 """)
2562 ret = assert_python_ok('-X', 'tracemalloc', '-c', code)
2563 self.assertIn(b'callback called', ret.out)
2564
2565 def test_gilstate_matches_current(self):
2566 _testcapi.test_current_tstate_matches()
2567
2568
2569 def get_test_funcs(mod, exclude_prefix=None):
2570 funcs = {}
2571 for name in dir(mod):
2572 if not name.startswith('test_'):
2573 continue
2574 if exclude_prefix is not None and name.startswith(exclude_prefix):
2575 continue
2576 funcs[name] = getattr(mod, name)
2577 return funcs
2578
2579
2580 class Test_testcapi(unittest.TestCase):
2581 locals().update(get_test_funcs(_testcapi))
2582
2583 # Suppress warning from PyUnicode_FromUnicode().
2584 @warnings_helper.ignore_warnings(category=DeprecationWarning)
2585 def test_widechar(self):
2586 _testlimitedcapi.test_widechar()
2587
2588 def test_version_api_data(self):
2589 self.assertEqual(_testcapi.Py_Version, sys.hexversion)
2590
2591
2592 class Test_testlimitedcapi(unittest.TestCase):
2593 locals().update(get_test_funcs(_testlimitedcapi))
2594
2595
2596 class Test_testinternalcapi(unittest.TestCase):
2597 locals().update(get_test_funcs(_testinternalcapi,
2598 exclude_prefix='test_lock_'))
2599
2600
2601 @threading_helper.requires_working_threading()
2602 class Test_PyLock(unittest.TestCase):
2603 locals().update((name, getattr(_testinternalcapi, name))
2604 for name in dir(_testinternalcapi)
2605 if name.startswith('test_lock_'))
2606
2607
2608 @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
2609 class Test_ModuleStateAccess(unittest.TestCase):
2610 """Test access to module start (PEP 573)"""
2611
2612 # The C part of the tests lives in _testmultiphase, in a module called
2613 # _testmultiphase_meth_state_access.
2614 # This module has multi-phase initialization, unlike _testcapi.
2615
2616 def setUp(self):
2617 fullname = '_testmultiphase_meth_state_access' # XXX
2618 origin = importlib.util.find_spec('_testmultiphase').origin
2619 # Apple extensions must be distributed as frameworks. This requires
2620 # a specialist loader.
2621 if support.is_apple_mobile:
2622 loader = importlib.machinery.AppleFrameworkLoader(fullname, origin)
2623 else:
2624 loader = importlib.machinery.ExtensionFileLoader(fullname, origin)
2625 spec = importlib.util.spec_from_loader(fullname, loader)
2626 module = importlib.util.module_from_spec(spec)
2627 loader.exec_module(module)
2628 self.module = module
2629
2630 def test_subclass_get_module(self):
2631 """PyType_GetModule for defining_class"""
2632 class StateAccessType_Subclass(self.module.StateAccessType):
2633 pass
2634
2635 instance = StateAccessType_Subclass()
2636 self.assertIs(instance.get_defining_module(), self.module)
2637
2638 def test_subclass_get_module_with_super(self):
2639 class StateAccessType_Subclass(self.module.StateAccessType):
2640 def get_defining_module(self):
2641 return super().get_defining_module()
2642
2643 instance = StateAccessType_Subclass()
2644 self.assertIs(instance.get_defining_module(), self.module)
2645
2646 def test_state_access(self):
2647 """Checks methods defined with and without argument clinic
2648
2649 This tests a no-arg method (get_count) and a method with
2650 both a positional and keyword argument.
2651 """
2652
2653 a = self.module.StateAccessType()
2654 b = self.module.StateAccessType()
2655
2656 methods = {
2657 'clinic': a.increment_count_clinic,
2658 'noclinic': a.increment_count_noclinic,
2659 }
2660
2661 for name, increment_count in methods.items():
2662 with self.subTest(name):
2663 self.assertEqual(a.get_count(), b.get_count())
2664 self.assertEqual(a.get_count(), 0)
2665
2666 increment_count()
2667 self.assertEqual(a.get_count(), b.get_count())
2668 self.assertEqual(a.get_count(), 1)
2669
2670 increment_count(3)
2671 self.assertEqual(a.get_count(), b.get_count())
2672 self.assertEqual(a.get_count(), 4)
2673
2674 increment_count(-2, twice=True)
2675 self.assertEqual(a.get_count(), b.get_count())
2676 self.assertEqual(a.get_count(), 0)
2677
2678 with self.assertRaises(TypeError):
2679 increment_count(thrice=3)
2680
2681 with self.assertRaises(TypeError):
2682 increment_count(1, 2, 3)
2683
2684 def test_get_module_bad_def(self):
2685 # PyType_GetModuleByDef fails gracefully if it doesn't
2686 # find what it's looking for.
2687 # see bpo-46433
2688 instance = self.module.StateAccessType()
2689 with self.assertRaises(TypeError):
2690 instance.getmodulebydef_bad_def()
2691
2692 def test_get_module_static_in_mro(self):
2693 # Here, the class PyType_GetModuleByDef is looking for
2694 # appears in the MRO after a static type (Exception).
2695 # see bpo-46433
2696 class Subclass(BaseException, self.module.StateAccessType):
2697 pass
2698 self.assertIs(Subclass().get_defining_module(), self.module)
2699
2700
2701 class TestInternalFrameApi(unittest.TestCase):
2702
2703 @staticmethod
2704 def func():
2705 return sys._getframe()
2706
2707 def test_code(self):
2708 frame = self.func()
2709 code = _testinternalcapi.iframe_getcode(frame)
2710 self.assertIs(code, self.func.__code__)
2711
2712 def test_lasti(self):
2713 frame = self.func()
2714 lasti = _testinternalcapi.iframe_getlasti(frame)
2715 self.assertGreater(lasti, 0)
2716 self.assertLess(lasti, len(self.func.__code__.co_code))
2717
2718 def test_line(self):
2719 frame = self.func()
2720 line = _testinternalcapi.iframe_getline(frame)
2721 firstline = self.func.__code__.co_firstlineno
2722 self.assertEqual(line, firstline + 2)
2723
2724
2725 SUFFICIENT_TO_DEOPT_AND_SPECIALIZE = 100
2726
2727 class Test_Pep523API(unittest.TestCase):
2728
2729 def do_test(self, func, names):
2730 actual_calls = []
2731 start = SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
2732 count = start + SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
2733 try:
2734 for i in range(count):
2735 if i == start:
2736 _testinternalcapi.set_eval_frame_record(actual_calls)
2737 func()
2738 finally:
2739 _testinternalcapi.set_eval_frame_default()
2740 expected_calls = names * SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
2741 self.assertEqual(len(expected_calls), len(actual_calls))
2742 for expected, actual in zip(expected_calls, actual_calls, strict=True):
2743 self.assertEqual(expected, actual)
2744
2745 def test_inlined_binary_subscr(self):
2746 class C:
2747 def __getitem__(self, other):
2748 return None
2749 def func():
2750 C()[42]
2751 names = ["func", "__getitem__"]
2752 self.do_test(func, names)
2753
2754 def test_inlined_call(self):
2755 def inner(x=42):
2756 pass
2757 def func():
2758 inner()
2759 inner(42)
2760 names = ["func", "inner", "inner"]
2761 self.do_test(func, names)
2762
2763 def test_inlined_call_function_ex(self):
2764 def inner(x):
2765 pass
2766 def func():
2767 inner(*[42])
2768 names = ["func", "inner"]
2769 self.do_test(func, names)
2770
2771 def test_inlined_for_iter(self):
2772 def gen():
2773 yield 42
2774 def func():
2775 for _ in gen():
2776 pass
2777 names = ["func", "gen", "gen", "gen"]
2778 self.do_test(func, names)
2779
2780 def test_inlined_load_attr(self):
2781 class C:
2782 @property
2783 def a(self):
2784 return 42
2785 class D:
2786 def __getattribute__(self, name):
2787 return 42
2788 def func():
2789 C().a
2790 D().a
2791 names = ["func", "a", "__getattribute__"]
2792 self.do_test(func, names)
2793
2794 def test_inlined_send(self):
2795 def inner():
2796 yield 42
2797 def outer():
2798 yield from inner()
2799 def func():
2800 list(outer())
2801 names = ["func", "outer", "outer", "inner", "inner", "outer", "inner"]
2802 self.do_test(func, names)
2803
2804
2805 @unittest.skipUnless(support.Py_GIL_DISABLED, 'need Py_GIL_DISABLED')
2806 class TestPyThreadId(unittest.TestCase):
2807 def test_py_thread_id(self):
2808 # gh-112535: Test _Py_ThreadId(): make sure that thread identifiers
2809 # in a few threads are unique
2810 py_thread_id = _testinternalcapi.py_thread_id
2811 short_sleep = 0.010
2812
2813 class GetThreadId(threading.Thread):
2814 def __init__(self):
2815 super().__init__()
2816 self.get_lock = threading.Lock()
2817 self.get_lock.acquire()
2818 self.started_lock = threading.Event()
2819 self.py_tid = None
2820
2821 def run(self):
2822 self.started_lock.set()
2823 self.get_lock.acquire()
2824 self.py_tid = py_thread_id()
2825 time.sleep(short_sleep)
2826 self.py_tid2 = py_thread_id()
2827
2828 nthread = 5
2829 threads = [GetThreadId() for _ in range(nthread)]
2830
2831 # first make run sure that all threads are running
2832 for thread in threads:
2833 thread.start()
2834 for thread in threads:
2835 thread.started_lock.wait()
2836
2837 # call _Py_ThreadId() in the main thread
2838 py_thread_ids = [py_thread_id()]
2839
2840 # now call _Py_ThreadId() in each thread
2841 for thread in threads:
2842 thread.get_lock.release()
2843
2844 # call _Py_ThreadId() in each thread and wait until threads complete
2845 for thread in threads:
2846 thread.join()
2847 py_thread_ids.append(thread.py_tid)
2848 # _PyThread_Id() should not change for a given thread.
2849 # For example, it should remain the same after a short sleep.
2850 self.assertEqual(thread.py_tid2, thread.py_tid)
2851
2852 # make sure that all _Py_ThreadId() are unique
2853 for tid in py_thread_ids:
2854 self.assertIsInstance(tid, int)
2855 self.assertGreater(tid, 0)
2856 self.assertEqual(len(set(py_thread_ids)), len(py_thread_ids),
2857 py_thread_ids)
2858
2859 class TestVersions(unittest.TestCase):
2860 full_cases = (
2861 (3, 4, 1, 0xA, 2, 0x030401a2),
2862 (3, 10, 0, 0xF, 0, 0x030a00f0),
2863 (0x103, 0x10B, 0xFF00, -1, 0xF0, 0x030b00f0), # test masking
2864 )
2865 xy_cases = (
2866 (3, 4, 0x03040000),
2867 (3, 10, 0x030a0000),
2868 (0x103, 0x10B, 0x030b0000), # test masking
2869 )
2870
2871 def test_pack_full_version(self):
2872 for *args, expected in self.full_cases:
2873 with self.subTest(hexversion=hex(expected)):
2874 result = _testlimitedcapi.pack_full_version(*args)
2875 self.assertEqual(result, expected)
2876
2877 def test_pack_version(self):
2878 for *args, expected in self.xy_cases:
2879 with self.subTest(hexversion=hex(expected)):
2880 result = _testlimitedcapi.pack_version(*args)
2881 self.assertEqual(result, expected)
2882
2883 def test_pack_full_version_ctypes(self):
2884 ctypes = import_helper.import_module('ctypes')
2885 ctypes_func = ctypes.pythonapi.Py_PACK_FULL_VERSION
2886 ctypes_func.restype = ctypes.c_uint32
2887 ctypes_func.argtypes = [ctypes.c_int] * 5
2888 for *args, expected in self.full_cases:
2889 with self.subTest(hexversion=hex(expected)):
2890 result = ctypes_func(*args)
2891 self.assertEqual(result, expected)
2892
2893 def test_pack_version_ctypes(self):
2894 ctypes = import_helper.import_module('ctypes')
2895 ctypes_func = ctypes.pythonapi.Py_PACK_VERSION
2896 ctypes_func.restype = ctypes.c_uint32
2897 ctypes_func.argtypes = [ctypes.c_int] * 2
2898 for *args, expected in self.xy_cases:
2899 with self.subTest(hexversion=hex(expected)):
2900 result = ctypes_func(*args)
2901 self.assertEqual(result, expected)
2902
2903
2904 class TestCEval(unittest.TestCase):
2905 def test_ceval_decref(self):
2906 code = textwrap.dedent("""
2907 import _testcapi
2908 _testcapi.toggle_reftrace_printer(True)
2909 l1 = []
2910 l2 = []
2911 del l1
2912 del l2
2913 _testcapi.toggle_reftrace_printer(False)
2914 """)
2915 _, out, _ = assert_python_ok("-c", code)
2916 lines = out.decode("utf-8").splitlines()
2917 self.assertEqual(lines.count("CREATE list"), 2)
2918 self.assertEqual(lines.count("DESTROY list"), 2)
2919
2920
2921 if __name__ == "__main__":
2922 unittest.main()