blob: 0813abb9a69722f6e032d7c7679218892b19cfee [file] [log] [blame]
Tim Petersd66595f2001-02-04 03:09:53 +00001# Run the _testcapi module tests (tests for the Python/C API): by defn,
Guido van Rossum361c5352001-04-13 17:03:04 +00002# these are all functions _testcapi exports whose name begins with 'test_'.
Tim Peters9ea17ac2001-02-02 05:57:15 +00003
Nick Coghlan39f0bb52017-11-28 08:11:51 +10004from collections import OrderedDict
Antoine Pitrou8e605772011-04-25 21:21:07 +02005import os
Antoine Pitrou2f828f22012-01-18 00:21:11 +01006import pickle
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00007import random
Victor Stinnerb3adb1a2016-03-14 17:40:09 +01008import re
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +00009import subprocess
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +000010import sys
Victor Stinner34be8072016-03-14 12:04:26 +010011import sysconfig
Victor Stinnerefde1462015-03-21 15:04:43 +010012import textwrap
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020013import threading
Benjamin Petersona54c9092009-01-13 02:11:23 +000014import time
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000015import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +000016from test import support
Larry Hastingsfcafe432013-11-23 17:35:48 -080017from test.support import MISSING_C_DOCSTRINGS
xdegaye85f64302017-07-01 14:14:45 +020018from test.support.script_helper import assert_python_failure, assert_python_ok
Victor Stinner45df8202010-04-28 22:31:17 +000019try:
Stefan Krahfd24f9e2012-08-20 11:04:24 +020020 import _posixsubprocess
21except ImportError:
22 _posixsubprocess = None
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020023
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +020024# Skip this test if the _testcapi module isn't available.
25_testcapi = support.import_module('_testcapi')
Tim Peters9ea17ac2001-02-02 05:57:15 +000026
Victor Stinnerefde1462015-03-21 15:04:43 +010027# Were we compiled --with-pydebug or with #define Py_DEBUG?
28Py_DEBUG = hasattr(sys, 'gettotalrefcount')
29
Jeroen Demeyereb65e242019-05-28 14:42:53 +020030Py_TPFLAGS_METHOD_DESCRIPTOR = 1 << 17
31
Benjamin Petersona54c9092009-01-13 02:11:23 +000032
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000033def testfunction(self):
34 """some doc"""
35 return self
36
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020037def testfunction_kw(self, *, kw):
38 """some doc"""
39 return self
40
41
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000042class InstanceMethod:
43 id = _testcapi.instancemethod(id)
44 testfunction = _testcapi.instancemethod(testfunction)
45
46class CAPITest(unittest.TestCase):
47
48 def test_instancemethod(self):
49 inst = InstanceMethod()
50 self.assertEqual(id(inst), inst.id())
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000051 self.assertTrue(inst.testfunction() is inst)
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000052 self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__)
53 self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__)
54
55 InstanceMethod.testfunction.attribute = "test"
56 self.assertEqual(testfunction.attribute, "test")
57 self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
58
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +000059 def test_no_FatalError_infinite_loop(self):
Antoine Pitrou77e904e2013-10-08 23:04:32 +020060 with support.SuppressCrashReport():
Ezio Melotti25a40452013-03-05 20:26:17 +020061 p = subprocess.Popen([sys.executable, "-c",
Ezio Melottie1857d92013-03-05 20:31:34 +020062 'import _testcapi;'
63 '_testcapi.crash_no_current_thread()'],
64 stdout=subprocess.PIPE,
65 stderr=subprocess.PIPE)
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +000066 (out, err) = p.communicate()
67 self.assertEqual(out, b'')
68 # This used to cause an infinite loop.
Vinay Sajip73954042012-05-06 11:34:50 +010069 self.assertTrue(err.rstrip().startswith(
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +000070 b'Fatal Python error:'
Vinay Sajip73954042012-05-06 11:34:50 +010071 b' PyThreadState_Get: no current thread'))
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +000072
Antoine Pitrou915605c2011-02-24 20:53:48 +000073 def test_memoryview_from_NULL_pointer(self):
74 self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer)
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000075
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +020076 def test_exc_info(self):
77 raised_exception = ValueError("5")
78 new_exc = TypeError("TEST")
79 try:
80 raise raised_exception
81 except ValueError as e:
82 tb = e.__traceback__
83 orig_sys_exc_info = sys.exc_info()
84 orig_exc_info = _testcapi.set_exc_info(new_exc.__class__, new_exc, None)
85 new_sys_exc_info = sys.exc_info()
86 new_exc_info = _testcapi.set_exc_info(*orig_exc_info)
87 reset_sys_exc_info = sys.exc_info()
88
89 self.assertEqual(orig_exc_info[1], e)
90
91 self.assertSequenceEqual(orig_exc_info, (raised_exception.__class__, raised_exception, tb))
92 self.assertSequenceEqual(orig_sys_exc_info, orig_exc_info)
93 self.assertSequenceEqual(reset_sys_exc_info, orig_exc_info)
94 self.assertSequenceEqual(new_exc_info, (new_exc.__class__, new_exc, None))
95 self.assertSequenceEqual(new_sys_exc_info, new_exc_info)
96 else:
97 self.assertTrue(False)
98
Stefan Krahfd24f9e2012-08-20 11:04:24 +020099 @unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.')
100 def test_seq_bytes_to_charp_array(self):
101 # Issue #15732: crash in _PySequence_BytesToCharpArray()
102 class Z(object):
103 def __len__(self):
104 return 1
105 self.assertRaises(TypeError, _posixsubprocess.fork_exec,
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300106 1,Z(),3,(1, 2),5,6,7,8,9,10,11,12,13,14,15,16,17)
Stefan Krah7cacd2e2012-08-21 08:16:09 +0200107 # Issue #15736: overflow in _PySequence_BytesToCharpArray()
108 class Z(object):
109 def __len__(self):
110 return sys.maxsize
111 def __getitem__(self, i):
112 return b'x'
113 self.assertRaises(MemoryError, _posixsubprocess.fork_exec,
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300114 1,Z(),3,(1, 2),5,6,7,8,9,10,11,12,13,14,15,16,17)
Stefan Krahfd24f9e2012-08-20 11:04:24 +0200115
Stefan Krahdb579d72012-08-20 14:36:47 +0200116 @unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.')
117 def test_subprocess_fork_exec(self):
118 class Z(object):
119 def __len__(self):
120 return 1
121
122 # Issue #15738: crash in subprocess_fork_exec()
123 self.assertRaises(TypeError, _posixsubprocess.fork_exec,
Serhiy Storchaka66bffd12017-04-19 21:12:46 +0300124 Z(),[b'1'],3,(1, 2),5,6,7,8,9,10,11,12,13,14,15,16,17)
Stefan Krahdb579d72012-08-20 14:36:47 +0200125
Larry Hastingsfcafe432013-11-23 17:35:48 -0800126 @unittest.skipIf(MISSING_C_DOCSTRINGS,
127 "Signature information for builtins requires docstrings")
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800128 def test_docstring_signature_parsing(self):
129
130 self.assertEqual(_testcapi.no_docstring.__doc__, None)
131 self.assertEqual(_testcapi.no_docstring.__text_signature__, None)
132
Zachary Ware8ef887c2015-04-13 18:22:35 -0500133 self.assertEqual(_testcapi.docstring_empty.__doc__, None)
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800134 self.assertEqual(_testcapi.docstring_empty.__text_signature__, None)
135
136 self.assertEqual(_testcapi.docstring_no_signature.__doc__,
137 "This docstring has no signature.")
138 self.assertEqual(_testcapi.docstring_no_signature.__text_signature__, None)
139
140 self.assertEqual(_testcapi.docstring_with_invalid_signature.__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800141 "docstring_with_invalid_signature($module, /, boo)\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800142 "\n"
143 "This docstring has an invalid signature."
144 )
145 self.assertEqual(_testcapi.docstring_with_invalid_signature.__text_signature__, None)
146
Larry Hastings2623c8c2014-02-08 22:15:29 -0800147 self.assertEqual(_testcapi.docstring_with_invalid_signature2.__doc__,
148 "docstring_with_invalid_signature2($module, /, boo)\n"
149 "\n"
150 "--\n"
151 "\n"
152 "This docstring also has an invalid signature."
153 )
154 self.assertEqual(_testcapi.docstring_with_invalid_signature2.__text_signature__, None)
155
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800156 self.assertEqual(_testcapi.docstring_with_signature.__doc__,
157 "This docstring has a valid signature.")
Larry Hastings2623c8c2014-02-08 22:15:29 -0800158 self.assertEqual(_testcapi.docstring_with_signature.__text_signature__, "($module, /, sig)")
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800159
Zachary Ware8ef887c2015-04-13 18:22:35 -0500160 self.assertEqual(_testcapi.docstring_with_signature_but_no_doc.__doc__, None)
161 self.assertEqual(_testcapi.docstring_with_signature_but_no_doc.__text_signature__,
162 "($module, /, sig)")
163
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800164 self.assertEqual(_testcapi.docstring_with_signature_and_extra_newlines.__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800165 "\nThis docstring has a valid signature and some extra newlines.")
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800166 self.assertEqual(_testcapi.docstring_with_signature_and_extra_newlines.__text_signature__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800167 "($module, /, parameter)")
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800168
Benjamin Petersond51374e2014-04-09 23:55:56 -0400169 def test_c_type_with_matrix_multiplication(self):
170 M = _testcapi.matmulType
171 m1 = M()
172 m2 = M()
173 self.assertEqual(m1 @ m2, ("matmul", m1, m2))
174 self.assertEqual(m1 @ 42, ("matmul", m1, 42))
175 self.assertEqual(42 @ m1, ("matmul", 42, m1))
176 o = m1
177 o @= m2
178 self.assertEqual(o, ("imatmul", m1, m2))
179 o = m1
180 o @= 42
181 self.assertEqual(o, ("imatmul", m1, 42))
182 o = 42
183 o @= m1
184 self.assertEqual(o, ("matmul", 42, m1))
185
Victor Stinnerefde1462015-03-21 15:04:43 +0100186 def test_return_null_without_error(self):
187 # Issue #23571: A function must not return NULL without setting an
188 # error
189 if Py_DEBUG:
190 code = textwrap.dedent("""
191 import _testcapi
192 from test import support
193
194 with support.SuppressCrashReport():
195 _testcapi.return_null_without_error()
196 """)
197 rc, out, err = assert_python_failure('-c', code)
Victor Stinner381a9bc2015-03-24 14:01:32 +0100198 self.assertRegex(err.replace(b'\r', b''),
Victor Stinner944fbcc2015-03-24 16:28:52 +0100199 br'Fatal Python error: a function returned NULL '
200 br'without setting an error\n'
Victor Stinner381a9bc2015-03-24 14:01:32 +0100201 br'SystemError: <built-in function '
202 br'return_null_without_error> returned NULL '
203 br'without setting an error\n'
204 br'\n'
205 br'Current thread.*:\n'
206 br' File .*", line 6 in <module>')
Victor Stinnerefde1462015-03-21 15:04:43 +0100207 else:
208 with self.assertRaises(SystemError) as cm:
209 _testcapi.return_null_without_error()
210 self.assertRegex(str(cm.exception),
211 'return_null_without_error.* '
212 'returned NULL without setting an error')
213
214 def test_return_result_with_error(self):
215 # Issue #23571: A function must not return a result with an error set
216 if Py_DEBUG:
217 code = textwrap.dedent("""
218 import _testcapi
219 from test import support
220
221 with support.SuppressCrashReport():
222 _testcapi.return_result_with_error()
223 """)
224 rc, out, err = assert_python_failure('-c', code)
Victor Stinner381a9bc2015-03-24 14:01:32 +0100225 self.assertRegex(err.replace(b'\r', b''),
Victor Stinner944fbcc2015-03-24 16:28:52 +0100226 br'Fatal Python error: a function returned a '
227 br'result with an error set\n'
Victor Stinner381a9bc2015-03-24 14:01:32 +0100228 br'ValueError\n'
229 br'\n'
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300230 br'The above exception was the direct cause '
231 br'of the following exception:\n'
Victor Stinner381a9bc2015-03-24 14:01:32 +0100232 br'\n'
233 br'SystemError: <built-in '
234 br'function return_result_with_error> '
235 br'returned a result with an error set\n'
236 br'\n'
237 br'Current thread.*:\n'
238 br' File .*, line 6 in <module>')
Victor Stinnerefde1462015-03-21 15:04:43 +0100239 else:
240 with self.assertRaises(SystemError) as cm:
241 _testcapi.return_result_with_error()
242 self.assertRegex(str(cm.exception),
243 'return_result_with_error.* '
244 'returned a result with an error set')
245
Serhiy Storchaka13e602e2016-05-20 22:31:14 +0300246 def test_buildvalue_N(self):
247 _testcapi.test_buildvalue_N()
248
xdegaye85f64302017-07-01 14:14:45 +0200249 def test_set_nomemory(self):
250 code = """if 1:
251 import _testcapi
252
253 class C(): pass
254
255 # The first loop tests both functions and that remove_mem_hooks()
256 # can be called twice in a row. The second loop checks a call to
257 # set_nomemory() after a call to remove_mem_hooks(). The third
258 # loop checks the start and stop arguments of set_nomemory().
259 for outer_cnt in range(1, 4):
260 start = 10 * outer_cnt
261 for j in range(100):
262 if j == 0:
263 if outer_cnt != 3:
264 _testcapi.set_nomemory(start)
265 else:
266 _testcapi.set_nomemory(start, start + 1)
267 try:
268 C()
269 except MemoryError as e:
270 if outer_cnt != 3:
271 _testcapi.remove_mem_hooks()
272 print('MemoryError', outer_cnt, j)
273 _testcapi.remove_mem_hooks()
274 break
275 """
276 rc, out, err = assert_python_ok('-c', code)
277 self.assertIn(b'MemoryError 1 10', out)
278 self.assertIn(b'MemoryError 2 20', out)
279 self.assertIn(b'MemoryError 3 30', out)
280
Oren Milman0ccc0f62017-10-08 11:17:46 +0300281 def test_mapping_keys_values_items(self):
282 class Mapping1(dict):
283 def keys(self):
284 return list(super().keys())
285 def values(self):
286 return list(super().values())
287 def items(self):
288 return list(super().items())
289 class Mapping2(dict):
290 def keys(self):
291 return tuple(super().keys())
292 def values(self):
293 return tuple(super().values())
294 def items(self):
295 return tuple(super().items())
296 dict_obj = {'foo': 1, 'bar': 2, 'spam': 3}
297
298 for mapping in [{}, OrderedDict(), Mapping1(), Mapping2(),
299 dict_obj, OrderedDict(dict_obj),
300 Mapping1(dict_obj), Mapping2(dict_obj)]:
301 self.assertListEqual(_testcapi.get_mapping_keys(mapping),
302 list(mapping.keys()))
303 self.assertListEqual(_testcapi.get_mapping_values(mapping),
304 list(mapping.values()))
305 self.assertListEqual(_testcapi.get_mapping_items(mapping),
306 list(mapping.items()))
307
308 def test_mapping_keys_values_items_bad_arg(self):
309 self.assertRaises(AttributeError, _testcapi.get_mapping_keys, None)
310 self.assertRaises(AttributeError, _testcapi.get_mapping_values, None)
311 self.assertRaises(AttributeError, _testcapi.get_mapping_items, None)
312
313 class BadMapping:
314 def keys(self):
315 return None
316 def values(self):
317 return None
318 def items(self):
319 return None
320 bad_mapping = BadMapping()
321 self.assertRaises(TypeError, _testcapi.get_mapping_keys, bad_mapping)
322 self.assertRaises(TypeError, _testcapi.get_mapping_values, bad_mapping)
323 self.assertRaises(TypeError, _testcapi.get_mapping_items, bad_mapping)
324
Victor Stinner18618e652018-10-25 17:28:11 +0200325 @unittest.skipUnless(hasattr(_testcapi, 'negative_refcount'),
326 'need _testcapi.negative_refcount')
327 def test_negative_refcount(self):
328 # bpo-35059: Check that Py_DECREF() reports the correct filename
329 # when calling _Py_NegativeRefcount() to abort Python.
330 code = textwrap.dedent("""
331 import _testcapi
332 from test import support
333
334 with support.SuppressCrashReport():
335 _testcapi.negative_refcount()
336 """)
337 rc, out, err = assert_python_failure('-c', code)
338 self.assertRegex(err,
Victor Stinner3ec9af72018-10-26 02:12:34 +0200339 br'_testcapimodule\.c:[0-9]+: '
Victor Stinnerf1d002c2018-11-21 23:53:44 +0100340 br'_Py_NegativeRefcount: Assertion failed: '
Victor Stinner3ec9af72018-10-26 02:12:34 +0200341 br'object has negative ref count')
Victor Stinner18618e652018-10-25 17:28:11 +0200342
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200343 def test_trashcan_subclass(self):
344 # bpo-35983: Check that the trashcan mechanism for "list" is NOT
345 # activated when its tp_dealloc is being called by a subclass
346 from _testcapi import MyList
347 L = None
348 for i in range(1000):
349 L = MyList((L,))
350
351 def test_trashcan_python_class1(self):
352 self.do_test_trashcan_python_class(list)
353
354 def test_trashcan_python_class2(self):
355 from _testcapi import MyList
356 self.do_test_trashcan_python_class(MyList)
357
358 def do_test_trashcan_python_class(self, base):
359 # Check that the trashcan mechanism works properly for a Python
360 # subclass of a class using the trashcan (this specific test assumes
361 # that the base class "base" behaves like list)
362 class PyList(base):
363 # Count the number of PyList instances to verify that there is
364 # no memory leak
365 num = 0
366 def __init__(self, *args):
367 __class__.num += 1
368 super().__init__(*args)
369 def __del__(self):
370 __class__.num -= 1
371
372 for parity in (0, 1):
373 L = None
374 # We need in the order of 2**20 iterations here such that a
375 # typical 8MB stack would overflow without the trashcan.
376 for i in range(2**20):
377 L = PyList((L,))
378 L.attr = i
379 if parity:
380 # Add one additional nesting layer
381 L = (L,)
382 self.assertGreater(PyList.num, 0)
383 del L
384 self.assertEqual(PyList.num, 0)
385
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800386
Benjamin Petersona54c9092009-01-13 02:11:23 +0000387class TestPendingCalls(unittest.TestCase):
388
389 def pendingcalls_submit(self, l, n):
390 def callback():
391 #this function can be interrupted by thread switching so let's
392 #use an atomic operation
393 l.append(None)
394
395 for i in range(n):
396 time.sleep(random.random()*0.02) #0.01 secs on average
397 #try submitting callback until successful.
398 #rely on regular interrupt to flush queue if we are
399 #unsuccessful.
400 while True:
401 if _testcapi._pending_threadfunc(callback):
402 break;
403
Benjamin Petersone1cdfd72009-01-18 21:02:37 +0000404 def pendingcalls_wait(self, l, n, context = None):
Benjamin Petersona54c9092009-01-13 02:11:23 +0000405 #now, stick around until l[0] has grown to 10
406 count = 0;
407 while len(l) != n:
408 #this busy loop is where we expect to be interrupted to
409 #run our callbacks. Note that callbacks are only run on the
410 #main thread
Benjamin Petersone1cdfd72009-01-18 21:02:37 +0000411 if False and support.verbose:
Benjamin Petersona54c9092009-01-13 02:11:23 +0000412 print("(%i)"%(len(l),),)
413 for i in range(1000):
414 a = i*i
Benjamin Petersone1cdfd72009-01-18 21:02:37 +0000415 if context and not context.event.is_set():
416 continue
Benjamin Petersona54c9092009-01-13 02:11:23 +0000417 count += 1
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000418 self.assertTrue(count < 10000,
Benjamin Petersona54c9092009-01-13 02:11:23 +0000419 "timeout waiting for %i callbacks, got %i"%(n, len(l)))
Benjamin Petersone1cdfd72009-01-18 21:02:37 +0000420 if False and support.verbose:
Benjamin Petersona54c9092009-01-13 02:11:23 +0000421 print("(%i)"%(len(l),))
422
423 def test_pendingcalls_threaded(self):
Benjamin Petersona54c9092009-01-13 02:11:23 +0000424
425 #do every callback on a separate thread
Eric Snowb75b1a352019-04-12 10:20:10 -0600426 n = 32 #total callbacks
Benjamin Petersona54c9092009-01-13 02:11:23 +0000427 threads = []
Benjamin Petersone1cdfd72009-01-18 21:02:37 +0000428 class foo(object):pass
429 context = foo()
430 context.l = []
431 context.n = 2 #submits per thread
432 context.nThreads = n // context.n
433 context.nFinished = 0
434 context.lock = threading.Lock()
435 context.event = threading.Event()
436
Serhiy Storchaka263dcd22015-04-01 13:01:14 +0300437 threads = [threading.Thread(target=self.pendingcalls_thread,
438 args=(context,))
439 for i in range(context.nThreads)]
440 with support.start_threads(threads):
441 self.pendingcalls_wait(context.l, n, context)
Benjamin Petersona54c9092009-01-13 02:11:23 +0000442
Benjamin Petersone1cdfd72009-01-18 21:02:37 +0000443 def pendingcalls_thread(self, context):
444 try:
445 self.pendingcalls_submit(context.l, context.n)
446 finally:
447 with context.lock:
448 context.nFinished += 1
449 nFinished = context.nFinished
450 if False and support.verbose:
451 print("finished threads: ", nFinished)
452 if nFinished == context.nThreads:
453 context.event.set()
454
Benjamin Petersona54c9092009-01-13 02:11:23 +0000455 def test_pendingcalls_non_threaded(self):
Ezio Melotti13925002011-03-16 11:05:33 +0200456 #again, just using the main thread, likely they will all be dispatched at
Benjamin Petersona54c9092009-01-13 02:11:23 +0000457 #once. It is ok to ask for too many, because we loop until we find a slot.
458 #the loop can be interrupted to dispatch.
459 #there are only 32 dispatch slots, so we go for twice that!
460 l = []
461 n = 64
462 self.pendingcalls_submit(l, n)
463 self.pendingcalls_wait(l, n)
464
Antoine Pitrou7a2572c2013-08-01 20:43:26 +0200465
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200466class TestPEP590(unittest.TestCase):
467
468 def test_method_descriptor_flag(self):
469 import functools
470 cached = functools.lru_cache(1)(testfunction)
471
472 self.assertFalse(type(repr).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR)
473 self.assertTrue(type(list.append).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR)
474 self.assertTrue(type(list.__add__).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR)
475 self.assertTrue(type(testfunction).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR)
476 self.assertTrue(type(cached).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR)
477
478 self.assertTrue(_testcapi.MethodDescriptorBase.__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR)
479 self.assertTrue(_testcapi.MethodDescriptorDerived.__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR)
480 self.assertFalse(_testcapi.MethodDescriptorNopGet.__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR)
481
482 # Heap type should not inherit Py_TPFLAGS_METHOD_DESCRIPTOR
483 class MethodDescriptorHeap(_testcapi.MethodDescriptorBase):
484 pass
485 self.assertFalse(MethodDescriptorHeap.__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR)
486
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200487 def test_vectorcall(self):
488 # Test a bunch of different ways to call objects:
489 # 1. normal call
490 # 2. vectorcall using _PyObject_Vectorcall()
491 # 3. vectorcall using PyVectorcall_Call()
492 # 4. call as bound method
493 # 5. call using functools.partial
494
495 # A list of (function, args, kwargs, result) calls to test
496 calls = [(len, (range(42),), {}, 42),
497 (list.append, ([], 0), {}, None),
498 ([].append, (0,), {}, None),
499 (sum, ([36],), {"start":6}, 42),
500 (testfunction, (42,), {}, 42),
501 (testfunction_kw, (42,), {"kw":None}, 42)]
502
503 from _testcapi import pyobject_vectorcall, pyvectorcall_call
504 from types import MethodType
505 from functools import partial
506
507 def vectorcall(func, args, kwargs):
508 args = *args, *kwargs.values()
509 kwnames = tuple(kwargs)
510 return pyobject_vectorcall(func, args, kwnames)
511
512 for (func, args, kwargs, expected) in calls:
513 with self.subTest(str(func)):
514 args1 = args[1:]
515 meth = MethodType(func, args[0])
516 wrapped = partial(func)
517 if not kwargs:
518 self.assertEqual(expected, func(*args))
519 self.assertEqual(expected, pyobject_vectorcall(func, args, None))
520 self.assertEqual(expected, pyvectorcall_call(func, args))
521 self.assertEqual(expected, meth(*args1))
522 self.assertEqual(expected, wrapped(*args))
523 self.assertEqual(expected, func(*args, **kwargs))
524 self.assertEqual(expected, vectorcall(func, args, kwargs))
525 self.assertEqual(expected, pyvectorcall_call(func, args, kwargs))
526 self.assertEqual(expected, meth(*args1, **kwargs))
527 self.assertEqual(expected, wrapped(*args, **kwargs))
528
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200529
Antoine Pitrou7a2572c2013-08-01 20:43:26 +0200530class SubinterpreterTest(unittest.TestCase):
531
Antoine Pitrou2f828f22012-01-18 00:21:11 +0100532 def test_subinterps(self):
Antoine Pitrou2f828f22012-01-18 00:21:11 +0100533 import builtins
534 r, w = os.pipe()
535 code = """if 1:
536 import sys, builtins, pickle
537 with open({:d}, "wb") as f:
538 pickle.dump(id(sys.modules), f)
539 pickle.dump(id(builtins), f)
540 """.format(w)
541 with open(r, "rb") as f:
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100542 ret = support.run_in_subinterp(code)
Antoine Pitrou2f828f22012-01-18 00:21:11 +0100543 self.assertEqual(ret, 0)
544 self.assertNotEqual(pickle.load(f), id(sys.modules))
545 self.assertNotEqual(pickle.load(f), id(builtins))
546
Marcel Plch33e71e02019-05-22 13:51:26 +0200547 def test_mutate_exception(self):
548 """
549 Exceptions saved in global module state get shared between
550 individual module instances. This test checks whether or not
551 a change in one interpreter's module gets reflected into the
552 other ones.
553 """
554 import binascii
555
556 support.run_in_subinterp("import binascii; binascii.Error.foobar = 'foobar'")
557
558 self.assertFalse(hasattr(binascii.Error, "foobar"))
559
Antoine Pitrou7a2572c2013-08-01 20:43:26 +0200560
Ezio Melotti29267c82013-02-23 05:52:46 +0200561class TestThreadState(unittest.TestCase):
562
563 @support.reap_threads
564 def test_thread_state(self):
565 # some extra thread-state tests driven via _testcapi
566 def target():
567 idents = []
568
569 def callback():
Ezio Melotti35246bd2013-02-23 05:58:38 +0200570 idents.append(threading.get_ident())
Ezio Melotti29267c82013-02-23 05:52:46 +0200571
572 _testcapi._test_thread_state(callback)
573 a = b = callback
574 time.sleep(1)
575 # Check our main thread is in the list exactly 3 times.
Ezio Melotti35246bd2013-02-23 05:58:38 +0200576 self.assertEqual(idents.count(threading.get_ident()), 3,
Ezio Melotti29267c82013-02-23 05:52:46 +0200577 "Couldn't find main thread correctly in the list")
578
579 target()
580 t = threading.Thread(target=target)
581 t.start()
582 t.join()
583
Victor Stinner34be8072016-03-14 12:04:26 +0100584
Zachary Warec12f09e2013-11-11 22:47:04 -0600585class Test_testcapi(unittest.TestCase):
Serhiy Storchaka8f7bb102018-08-06 16:50:19 +0300586 locals().update((name, getattr(_testcapi, name))
587 for name in dir(_testcapi)
588 if name.startswith('test_') and not name.endswith('_code'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589
Victor Stinner34be8072016-03-14 12:04:26 +0100590
Victor Stinnerc4aec362016-03-14 22:26:53 +0100591class PyMemDebugTests(unittest.TestCase):
592 PYTHONMALLOC = 'debug'
Victor Stinnera1bc28a2016-03-14 17:10:36 +0100593 # '0x04c06e0' or '04C06E0'
Victor Stinner08572f62016-03-14 21:55:43 +0100594 PTR_REGEX = r'(?:0x)?[0-9a-fA-F]+'
Victor Stinner34be8072016-03-14 12:04:26 +0100595
596 def check(self, code):
597 with support.SuppressCrashReport():
Victor Stinnerc4aec362016-03-14 22:26:53 +0100598 out = assert_python_failure('-c', code,
599 PYTHONMALLOC=self.PYTHONMALLOC)
Victor Stinner34be8072016-03-14 12:04:26 +0100600 stderr = out.err
601 return stderr.decode('ascii', 'replace')
602
603 def test_buffer_overflow(self):
604 out = self.check('import _testcapi; _testcapi.pymem_buffer_overflow()')
Victor Stinnera1bc28a2016-03-14 17:10:36 +0100605 regex = (r"Debug memory block at address p={ptr}: API 'm'\n"
Victor Stinner34be8072016-03-14 12:04:26 +0100606 r" 16 bytes originally requested\n"
Victor Stinnerb3adb1a2016-03-14 17:40:09 +0100607 r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n"
608 r" The [0-9] pad bytes at tail={ptr} are not all FORBIDDENBYTE \(0x[0-9a-f]{{2}}\):\n"
Victor Stinner34be8072016-03-14 12:04:26 +0100609 r" at tail\+0: 0x78 \*\*\* OUCH\n"
Victor Stinner4c409be2019-04-11 13:01:15 +0200610 r" at tail\+1: 0xfd\n"
611 r" at tail\+2: 0xfd\n"
Victor Stinnerb3adb1a2016-03-14 17:40:09 +0100612 r" .*\n"
Victor Stinnere8f9acf2019-04-12 21:54:06 +0200613 r"( The block was made by call #[0-9]+ to debug malloc/realloc.\n)?"
Victor Stinner4c409be2019-04-11 13:01:15 +0200614 r" Data at p: cd cd cd .*\n"
Victor Stinner6453e9e2016-03-15 23:36:28 +0100615 r"\n"
Victor Stinnerf966e532018-11-13 15:14:58 +0100616 r"Enable tracemalloc to get the memory block allocation traceback\n"
617 r"\n"
Victor Stinner34be8072016-03-14 12:04:26 +0100618 r"Fatal Python error: bad trailing pad byte")
Victor Stinnera1bc28a2016-03-14 17:10:36 +0100619 regex = regex.format(ptr=self.PTR_REGEX)
Victor Stinnerb3adb1a2016-03-14 17:40:09 +0100620 regex = re.compile(regex, flags=re.DOTALL)
Victor Stinner34be8072016-03-14 12:04:26 +0100621 self.assertRegex(out, regex)
622
623 def test_api_misuse(self):
624 out = self.check('import _testcapi; _testcapi.pymem_api_misuse()')
Victor Stinnera1bc28a2016-03-14 17:10:36 +0100625 regex = (r"Debug memory block at address p={ptr}: API 'm'\n"
Victor Stinner34be8072016-03-14 12:04:26 +0100626 r" 16 bytes originally requested\n"
Victor Stinnerb3adb1a2016-03-14 17:40:09 +0100627 r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n"
628 r" The [0-9] pad bytes at tail={ptr} are FORBIDDENBYTE, as expected.\n"
Victor Stinnere8f9acf2019-04-12 21:54:06 +0200629 r"( The block was made by call #[0-9]+ to debug malloc/realloc.\n)?"
Victor Stinner4c409be2019-04-11 13:01:15 +0200630 r" Data at p: cd cd cd .*\n"
Victor Stinner6453e9e2016-03-15 23:36:28 +0100631 r"\n"
Victor Stinnerf966e532018-11-13 15:14:58 +0100632 r"Enable tracemalloc to get the memory block allocation traceback\n"
633 r"\n"
Victor Stinner34be8072016-03-14 12:04:26 +0100634 r"Fatal Python error: bad ID: Allocated using API 'm', verified using API 'r'\n")
Victor Stinnera1bc28a2016-03-14 17:10:36 +0100635 regex = regex.format(ptr=self.PTR_REGEX)
Victor Stinner34be8072016-03-14 12:04:26 +0100636 self.assertRegex(out, regex)
637
Victor Stinnerad524372016-03-16 12:12:53 +0100638 def check_malloc_without_gil(self, code):
Victor Stinnerc4aec362016-03-14 22:26:53 +0100639 out = self.check(code)
640 expected = ('Fatal Python error: Python memory allocator called '
641 'without holding the GIL')
642 self.assertIn(expected, out)
Victor Stinner34be8072016-03-14 12:04:26 +0100643
Victor Stinnerad524372016-03-16 12:12:53 +0100644 def test_pymem_malloc_without_gil(self):
645 # Debug hooks must raise an error if PyMem_Malloc() is called
646 # without holding the GIL
647 code = 'import _testcapi; _testcapi.pymem_malloc_without_gil()'
648 self.check_malloc_without_gil(code)
649
650 def test_pyobject_malloc_without_gil(self):
651 # Debug hooks must raise an error if PyObject_Malloc() is called
652 # without holding the GIL
653 code = 'import _testcapi; _testcapi.pyobject_malloc_without_gil()'
654 self.check_malloc_without_gil(code)
655
Victor Stinner2b00db62019-04-11 11:33:27 +0200656 def check_pyobject_is_freed(self, func):
657 code = textwrap.dedent('''
658 import gc, os, sys, _testcapi
659 # Disable the GC to avoid crash on GC collection
660 gc.disable()
661 obj = _testcapi.{func}()
662 error = (_testcapi.pyobject_is_freed(obj) == False)
663 # Exit immediately to avoid a crash while deallocating
664 # the invalid object
665 os._exit(int(error))
666 ''')
667 code = code.format(func=func)
668 assert_python_ok('-c', code, PYTHONMALLOC=self.PYTHONMALLOC)
669
670 def test_pyobject_is_freed_uninitialized(self):
671 self.check_pyobject_is_freed('pyobject_uninitialized')
672
673 def test_pyobject_is_freed_forbidden_bytes(self):
674 self.check_pyobject_is_freed('pyobject_forbidden_bytes')
675
676 def test_pyobject_is_freed_free(self):
677 self.check_pyobject_is_freed('pyobject_freed')
678
Victor Stinnerc4aec362016-03-14 22:26:53 +0100679
680class PyMemMallocDebugTests(PyMemDebugTests):
681 PYTHONMALLOC = 'malloc_debug'
Victor Stinner34be8072016-03-14 12:04:26 +0100682
683
Victor Stinner5d39e042017-11-29 17:20:38 +0100684@unittest.skipUnless(support.with_pymalloc(), 'need pymalloc')
Victor Stinnerc4aec362016-03-14 22:26:53 +0100685class PyMemPymallocDebugTests(PyMemDebugTests):
686 PYTHONMALLOC = 'pymalloc_debug'
Victor Stinner34be8072016-03-14 12:04:26 +0100687
688
689@unittest.skipUnless(Py_DEBUG, 'need Py_DEBUG')
Victor Stinnerc4aec362016-03-14 22:26:53 +0100690class PyMemDefaultTests(PyMemDebugTests):
691 # test default allocator of Python compiled in debug mode
692 PYTHONMALLOC = ''
Victor Stinner34be8072016-03-14 12:04:26 +0100693
694
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000695if __name__ == "__main__":
Zachary Warec12f09e2013-11-11 22:47:04 -0600696 unittest.main()