Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1 | # Run the _testcapi module tests (tests for the Python/C API): by defn, |
Guido van Rossum | 361c535 | 2001-04-13 17:03:04 +0000 | [diff] [blame] | 2 | # these are all functions _testcapi exports whose name begins with 'test_'. |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 3 | |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 4 | import os |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 5 | import pickle |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 6 | import random |
Victor Stinner | b3adb1a | 2016-03-14 17:40:09 +0100 | [diff] [blame] | 7 | import re |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 8 | import subprocess |
Martin v. Löwis | 6ce7ed2 | 2005-03-03 12:26:35 +0000 | [diff] [blame] | 9 | import sys |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 10 | import sysconfig |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 11 | import textwrap |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 12 | import time |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 13 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 14 | from test import support |
Larry Hastings | fcafe43 | 2013-11-23 17:35:48 -0800 | [diff] [blame] | 15 | from test.support import MISSING_C_DOCSTRINGS |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 16 | from test.support.script_helper import assert_python_failure |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 17 | try: |
Stefan Krah | fd24f9e | 2012-08-20 11:04:24 +0200 | [diff] [blame] | 18 | import _posixsubprocess |
| 19 | except ImportError: |
| 20 | _posixsubprocess = None |
| 21 | try: |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 22 | import threading |
| 23 | except ImportError: |
| 24 | threading = None |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 25 | # Skip this test if the _testcapi module isn't available. |
| 26 | _testcapi = support.import_module('_testcapi') |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 27 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 28 | # Were we compiled --with-pydebug or with #define Py_DEBUG? |
| 29 | Py_DEBUG = hasattr(sys, 'gettotalrefcount') |
| 30 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 31 | |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 32 | def testfunction(self): |
| 33 | """some doc""" |
| 34 | return self |
| 35 | |
| 36 | class InstanceMethod: |
| 37 | id = _testcapi.instancemethod(id) |
| 38 | testfunction = _testcapi.instancemethod(testfunction) |
| 39 | |
| 40 | class CAPITest(unittest.TestCase): |
| 41 | |
| 42 | def test_instancemethod(self): |
| 43 | inst = InstanceMethod() |
| 44 | self.assertEqual(id(inst), inst.id()) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 45 | self.assertTrue(inst.testfunction() is inst) |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 46 | self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__) |
| 47 | self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__) |
| 48 | |
| 49 | InstanceMethod.testfunction.attribute = "test" |
| 50 | self.assertEqual(testfunction.attribute, "test") |
| 51 | self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test") |
| 52 | |
Stefan Krah | 0ca4624 | 2010-06-09 08:56:28 +0000 | [diff] [blame] | 53 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 54 | def test_no_FatalError_infinite_loop(self): |
Antoine Pitrou | 77e904e | 2013-10-08 23:04:32 +0200 | [diff] [blame] | 55 | with support.SuppressCrashReport(): |
Ezio Melotti | 25a4045 | 2013-03-05 20:26:17 +0200 | [diff] [blame] | 56 | p = subprocess.Popen([sys.executable, "-c", |
Ezio Melotti | e1857d9 | 2013-03-05 20:31:34 +0200 | [diff] [blame] | 57 | 'import _testcapi;' |
| 58 | '_testcapi.crash_no_current_thread()'], |
| 59 | stdout=subprocess.PIPE, |
| 60 | stderr=subprocess.PIPE) |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 61 | (out, err) = p.communicate() |
| 62 | self.assertEqual(out, b'') |
| 63 | # This used to cause an infinite loop. |
Vinay Sajip | 7395404 | 2012-05-06 11:34:50 +0100 | [diff] [blame] | 64 | self.assertTrue(err.rstrip().startswith( |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 65 | b'Fatal Python error:' |
Vinay Sajip | 7395404 | 2012-05-06 11:34:50 +0100 | [diff] [blame] | 66 | b' PyThreadState_Get: no current thread')) |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 67 | |
Antoine Pitrou | 915605c | 2011-02-24 20:53:48 +0000 | [diff] [blame] | 68 | def test_memoryview_from_NULL_pointer(self): |
| 69 | self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer) |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 70 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 71 | def test_exc_info(self): |
| 72 | raised_exception = ValueError("5") |
| 73 | new_exc = TypeError("TEST") |
| 74 | try: |
| 75 | raise raised_exception |
| 76 | except ValueError as e: |
| 77 | tb = e.__traceback__ |
| 78 | orig_sys_exc_info = sys.exc_info() |
| 79 | orig_exc_info = _testcapi.set_exc_info(new_exc.__class__, new_exc, None) |
| 80 | new_sys_exc_info = sys.exc_info() |
| 81 | new_exc_info = _testcapi.set_exc_info(*orig_exc_info) |
| 82 | reset_sys_exc_info = sys.exc_info() |
| 83 | |
| 84 | self.assertEqual(orig_exc_info[1], e) |
| 85 | |
| 86 | self.assertSequenceEqual(orig_exc_info, (raised_exception.__class__, raised_exception, tb)) |
| 87 | self.assertSequenceEqual(orig_sys_exc_info, orig_exc_info) |
| 88 | self.assertSequenceEqual(reset_sys_exc_info, orig_exc_info) |
| 89 | self.assertSequenceEqual(new_exc_info, (new_exc.__class__, new_exc, None)) |
| 90 | self.assertSequenceEqual(new_sys_exc_info, new_exc_info) |
| 91 | else: |
| 92 | self.assertTrue(False) |
| 93 | |
Stefan Krah | fd24f9e | 2012-08-20 11:04:24 +0200 | [diff] [blame] | 94 | @unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.') |
| 95 | def test_seq_bytes_to_charp_array(self): |
| 96 | # Issue #15732: crash in _PySequence_BytesToCharpArray() |
| 97 | class Z(object): |
| 98 | def __len__(self): |
| 99 | return 1 |
| 100 | self.assertRaises(TypeError, _posixsubprocess.fork_exec, |
| 101 | 1,Z(),3,[1, 2],5,6,7,8,9,10,11,12,13,14,15,16,17) |
Stefan Krah | 7cacd2e | 2012-08-21 08:16:09 +0200 | [diff] [blame] | 102 | # Issue #15736: overflow in _PySequence_BytesToCharpArray() |
| 103 | class Z(object): |
| 104 | def __len__(self): |
| 105 | return sys.maxsize |
| 106 | def __getitem__(self, i): |
| 107 | return b'x' |
| 108 | self.assertRaises(MemoryError, _posixsubprocess.fork_exec, |
| 109 | 1,Z(),3,[1, 2],5,6,7,8,9,10,11,12,13,14,15,16,17) |
Stefan Krah | fd24f9e | 2012-08-20 11:04:24 +0200 | [diff] [blame] | 110 | |
Stefan Krah | db579d7 | 2012-08-20 14:36:47 +0200 | [diff] [blame] | 111 | @unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.') |
| 112 | def test_subprocess_fork_exec(self): |
| 113 | class Z(object): |
| 114 | def __len__(self): |
| 115 | return 1 |
| 116 | |
| 117 | # Issue #15738: crash in subprocess_fork_exec() |
| 118 | self.assertRaises(TypeError, _posixsubprocess.fork_exec, |
| 119 | Z(),[b'1'],3,[1, 2],5,6,7,8,9,10,11,12,13,14,15,16,17) |
| 120 | |
Larry Hastings | fcafe43 | 2013-11-23 17:35:48 -0800 | [diff] [blame] | 121 | @unittest.skipIf(MISSING_C_DOCSTRINGS, |
| 122 | "Signature information for builtins requires docstrings") |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 123 | def test_docstring_signature_parsing(self): |
| 124 | |
| 125 | self.assertEqual(_testcapi.no_docstring.__doc__, None) |
| 126 | self.assertEqual(_testcapi.no_docstring.__text_signature__, None) |
| 127 | |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 128 | self.assertEqual(_testcapi.docstring_empty.__doc__, None) |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 129 | self.assertEqual(_testcapi.docstring_empty.__text_signature__, None) |
| 130 | |
| 131 | self.assertEqual(_testcapi.docstring_no_signature.__doc__, |
| 132 | "This docstring has no signature.") |
| 133 | self.assertEqual(_testcapi.docstring_no_signature.__text_signature__, None) |
| 134 | |
| 135 | self.assertEqual(_testcapi.docstring_with_invalid_signature.__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 136 | "docstring_with_invalid_signature($module, /, boo)\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 137 | "\n" |
| 138 | "This docstring has an invalid signature." |
| 139 | ) |
| 140 | self.assertEqual(_testcapi.docstring_with_invalid_signature.__text_signature__, None) |
| 141 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 142 | self.assertEqual(_testcapi.docstring_with_invalid_signature2.__doc__, |
| 143 | "docstring_with_invalid_signature2($module, /, boo)\n" |
| 144 | "\n" |
| 145 | "--\n" |
| 146 | "\n" |
| 147 | "This docstring also has an invalid signature." |
| 148 | ) |
| 149 | self.assertEqual(_testcapi.docstring_with_invalid_signature2.__text_signature__, None) |
| 150 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 151 | self.assertEqual(_testcapi.docstring_with_signature.__doc__, |
| 152 | "This docstring has a valid signature.") |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 153 | self.assertEqual(_testcapi.docstring_with_signature.__text_signature__, "($module, /, sig)") |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 154 | |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 155 | self.assertEqual(_testcapi.docstring_with_signature_but_no_doc.__doc__, None) |
| 156 | self.assertEqual(_testcapi.docstring_with_signature_but_no_doc.__text_signature__, |
| 157 | "($module, /, sig)") |
| 158 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 159 | self.assertEqual(_testcapi.docstring_with_signature_and_extra_newlines.__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 160 | "\nThis docstring has a valid signature and some extra newlines.") |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 161 | self.assertEqual(_testcapi.docstring_with_signature_and_extra_newlines.__text_signature__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 162 | "($module, /, parameter)") |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 163 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 164 | def test_c_type_with_matrix_multiplication(self): |
| 165 | M = _testcapi.matmulType |
| 166 | m1 = M() |
| 167 | m2 = M() |
| 168 | self.assertEqual(m1 @ m2, ("matmul", m1, m2)) |
| 169 | self.assertEqual(m1 @ 42, ("matmul", m1, 42)) |
| 170 | self.assertEqual(42 @ m1, ("matmul", 42, m1)) |
| 171 | o = m1 |
| 172 | o @= m2 |
| 173 | self.assertEqual(o, ("imatmul", m1, m2)) |
| 174 | o = m1 |
| 175 | o @= 42 |
| 176 | self.assertEqual(o, ("imatmul", m1, 42)) |
| 177 | o = 42 |
| 178 | o @= m1 |
| 179 | self.assertEqual(o, ("matmul", 42, m1)) |
| 180 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 181 | def test_return_null_without_error(self): |
| 182 | # Issue #23571: A function must not return NULL without setting an |
| 183 | # error |
| 184 | if Py_DEBUG: |
| 185 | code = textwrap.dedent(""" |
| 186 | import _testcapi |
| 187 | from test import support |
| 188 | |
| 189 | with support.SuppressCrashReport(): |
| 190 | _testcapi.return_null_without_error() |
| 191 | """) |
| 192 | rc, out, err = assert_python_failure('-c', code) |
Victor Stinner | 381a9bc | 2015-03-24 14:01:32 +0100 | [diff] [blame] | 193 | self.assertRegex(err.replace(b'\r', b''), |
Victor Stinner | 944fbcc | 2015-03-24 16:28:52 +0100 | [diff] [blame] | 194 | br'Fatal Python error: a function returned NULL ' |
| 195 | br'without setting an error\n' |
Victor Stinner | 381a9bc | 2015-03-24 14:01:32 +0100 | [diff] [blame] | 196 | br'SystemError: <built-in function ' |
| 197 | br'return_null_without_error> returned NULL ' |
| 198 | br'without setting an error\n' |
| 199 | br'\n' |
| 200 | br'Current thread.*:\n' |
| 201 | br' File .*", line 6 in <module>') |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 202 | else: |
| 203 | with self.assertRaises(SystemError) as cm: |
| 204 | _testcapi.return_null_without_error() |
| 205 | self.assertRegex(str(cm.exception), |
| 206 | 'return_null_without_error.* ' |
| 207 | 'returned NULL without setting an error') |
| 208 | |
| 209 | def test_return_result_with_error(self): |
| 210 | # Issue #23571: A function must not return a result with an error set |
| 211 | if Py_DEBUG: |
| 212 | code = textwrap.dedent(""" |
| 213 | import _testcapi |
| 214 | from test import support |
| 215 | |
| 216 | with support.SuppressCrashReport(): |
| 217 | _testcapi.return_result_with_error() |
| 218 | """) |
| 219 | rc, out, err = assert_python_failure('-c', code) |
Victor Stinner | 381a9bc | 2015-03-24 14:01:32 +0100 | [diff] [blame] | 220 | self.assertRegex(err.replace(b'\r', b''), |
Victor Stinner | 944fbcc | 2015-03-24 16:28:52 +0100 | [diff] [blame] | 221 | br'Fatal Python error: a function returned a ' |
| 222 | br'result with an error set\n' |
Victor Stinner | 381a9bc | 2015-03-24 14:01:32 +0100 | [diff] [blame] | 223 | br'ValueError\n' |
| 224 | br'\n' |
| 225 | br'During handling of the above exception, ' |
| 226 | br'another exception occurred:\n' |
| 227 | br'\n' |
| 228 | br'SystemError: <built-in ' |
| 229 | br'function return_result_with_error> ' |
| 230 | br'returned a result with an error set\n' |
| 231 | br'\n' |
| 232 | br'Current thread.*:\n' |
| 233 | br' File .*, line 6 in <module>') |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 234 | else: |
| 235 | with self.assertRaises(SystemError) as cm: |
| 236 | _testcapi.return_result_with_error() |
| 237 | self.assertRegex(str(cm.exception), |
| 238 | 'return_result_with_error.* ' |
| 239 | 'returned a result with an error set') |
| 240 | |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 241 | def test_buildvalue_N(self): |
| 242 | _testcapi.test_buildvalue_N() |
| 243 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 244 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 245 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 246 | class TestPendingCalls(unittest.TestCase): |
| 247 | |
| 248 | def pendingcalls_submit(self, l, n): |
| 249 | def callback(): |
| 250 | #this function can be interrupted by thread switching so let's |
| 251 | #use an atomic operation |
| 252 | l.append(None) |
| 253 | |
| 254 | for i in range(n): |
| 255 | time.sleep(random.random()*0.02) #0.01 secs on average |
| 256 | #try submitting callback until successful. |
| 257 | #rely on regular interrupt to flush queue if we are |
| 258 | #unsuccessful. |
| 259 | while True: |
| 260 | if _testcapi._pending_threadfunc(callback): |
| 261 | break; |
| 262 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 263 | def pendingcalls_wait(self, l, n, context = None): |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 264 | #now, stick around until l[0] has grown to 10 |
| 265 | count = 0; |
| 266 | while len(l) != n: |
| 267 | #this busy loop is where we expect to be interrupted to |
| 268 | #run our callbacks. Note that callbacks are only run on the |
| 269 | #main thread |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 270 | if False and support.verbose: |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 271 | print("(%i)"%(len(l),),) |
| 272 | for i in range(1000): |
| 273 | a = i*i |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 274 | if context and not context.event.is_set(): |
| 275 | continue |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 276 | count += 1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 277 | self.assertTrue(count < 10000, |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 278 | "timeout waiting for %i callbacks, got %i"%(n, len(l))) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 279 | if False and support.verbose: |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 280 | print("(%i)"%(len(l),)) |
| 281 | |
| 282 | def test_pendingcalls_threaded(self): |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 283 | |
| 284 | #do every callback on a separate thread |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 285 | n = 32 #total callbacks |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 286 | threads = [] |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 287 | class foo(object):pass |
| 288 | context = foo() |
| 289 | context.l = [] |
| 290 | context.n = 2 #submits per thread |
| 291 | context.nThreads = n // context.n |
| 292 | context.nFinished = 0 |
| 293 | context.lock = threading.Lock() |
| 294 | context.event = threading.Event() |
| 295 | |
Serhiy Storchaka | 263dcd2 | 2015-04-01 13:01:14 +0300 | [diff] [blame] | 296 | threads = [threading.Thread(target=self.pendingcalls_thread, |
| 297 | args=(context,)) |
| 298 | for i in range(context.nThreads)] |
| 299 | with support.start_threads(threads): |
| 300 | self.pendingcalls_wait(context.l, n, context) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 301 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 302 | def pendingcalls_thread(self, context): |
| 303 | try: |
| 304 | self.pendingcalls_submit(context.l, context.n) |
| 305 | finally: |
| 306 | with context.lock: |
| 307 | context.nFinished += 1 |
| 308 | nFinished = context.nFinished |
| 309 | if False and support.verbose: |
| 310 | print("finished threads: ", nFinished) |
| 311 | if nFinished == context.nThreads: |
| 312 | context.event.set() |
| 313 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 314 | def test_pendingcalls_non_threaded(self): |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 315 | #again, just using the main thread, likely they will all be dispatched at |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 316 | #once. It is ok to ask for too many, because we loop until we find a slot. |
| 317 | #the loop can be interrupted to dispatch. |
| 318 | #there are only 32 dispatch slots, so we go for twice that! |
| 319 | l = [] |
| 320 | n = 64 |
| 321 | self.pendingcalls_submit(l, n) |
| 322 | self.pendingcalls_wait(l, n) |
| 323 | |
Antoine Pitrou | 7a2572c | 2013-08-01 20:43:26 +0200 | [diff] [blame] | 324 | |
| 325 | class SubinterpreterTest(unittest.TestCase): |
| 326 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 327 | def test_subinterps(self): |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 328 | import builtins |
| 329 | r, w = os.pipe() |
| 330 | code = """if 1: |
| 331 | import sys, builtins, pickle |
| 332 | with open({:d}, "wb") as f: |
| 333 | pickle.dump(id(sys.modules), f) |
| 334 | pickle.dump(id(builtins), f) |
| 335 | """.format(w) |
| 336 | with open(r, "rb") as f: |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 337 | ret = support.run_in_subinterp(code) |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 338 | self.assertEqual(ret, 0) |
| 339 | self.assertNotEqual(pickle.load(f), id(sys.modules)) |
| 340 | self.assertNotEqual(pickle.load(f), id(builtins)) |
| 341 | |
Antoine Pitrou | 7a2572c | 2013-08-01 20:43:26 +0200 | [diff] [blame] | 342 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 343 | # Bug #6012 |
| 344 | class Test6012(unittest.TestCase): |
| 345 | def test(self): |
| 346 | self.assertEqual(_testcapi.argparsing("Hello", "World"), 1) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 347 | |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 348 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 349 | class EmbeddingTests(unittest.TestCase): |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 350 | def setUp(self): |
Zachary Ware | 6d8a260 | 2015-12-05 00:16:55 -0600 | [diff] [blame] | 351 | here = os.path.abspath(__file__) |
| 352 | basepath = os.path.dirname(os.path.dirname(os.path.dirname(here))) |
Nick Coghlan | 4e641df | 2013-11-03 16:54:46 +1000 | [diff] [blame] | 353 | exename = "_testembed" |
| 354 | if sys.platform.startswith("win"): |
| 355 | ext = ("_d" if "_d" in sys.executable else "") + ".exe" |
| 356 | exename += ext |
| 357 | exepath = os.path.dirname(sys.executable) |
| 358 | else: |
Nick Coghlan | bca9acf | 2014-09-25 19:48:15 +1000 | [diff] [blame] | 359 | exepath = os.path.join(basepath, "Programs") |
Nick Coghlan | 4e641df | 2013-11-03 16:54:46 +1000 | [diff] [blame] | 360 | self.test_exe = exe = os.path.join(exepath, exename) |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 361 | if not os.path.exists(exe): |
| 362 | self.skipTest("%r doesn't exist" % exe) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 363 | # This is needed otherwise we get a fatal error: |
| 364 | # "Py_Initialize: Unable to get the locale encoding |
| 365 | # LookupError: no codec search functions registered: can't find encoding" |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 366 | self.oldcwd = os.getcwd() |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 367 | os.chdir(basepath) |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 368 | |
| 369 | def tearDown(self): |
| 370 | os.chdir(self.oldcwd) |
| 371 | |
| 372 | def run_embedded_interpreter(self, *args): |
| 373 | """Runs a test in the embedded interpreter""" |
| 374 | cmd = [self.test_exe] |
| 375 | cmd.extend(args) |
| 376 | p = subprocess.Popen(cmd, |
| 377 | stdout=subprocess.PIPE, |
Steve Dower | 86e9deb | 2014-11-01 15:11:05 -0700 | [diff] [blame] | 378 | stderr=subprocess.PIPE, |
| 379 | universal_newlines=True) |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 380 | (out, err) = p.communicate() |
| 381 | self.assertEqual(p.returncode, 0, |
| 382 | "bad returncode %d, stderr is %r" % |
| 383 | (p.returncode, err)) |
Steve Dower | 86e9deb | 2014-11-01 15:11:05 -0700 | [diff] [blame] | 384 | return out, err |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 385 | |
| 386 | def test_subinterps(self): |
| 387 | # This is just a "don't crash" test |
| 388 | out, err = self.run_embedded_interpreter() |
| 389 | if support.verbose: |
| 390 | print() |
| 391 | print(out) |
| 392 | print(err) |
| 393 | |
Nick Coghlan | 4e641df | 2013-11-03 16:54:46 +1000 | [diff] [blame] | 394 | @staticmethod |
| 395 | def _get_default_pipe_encoding(): |
| 396 | rp, wp = os.pipe() |
| 397 | try: |
| 398 | with os.fdopen(wp, 'w') as w: |
| 399 | default_pipe_encoding = w.encoding |
| 400 | finally: |
| 401 | os.close(rp) |
| 402 | return default_pipe_encoding |
| 403 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 404 | def test_forced_io_encoding(self): |
| 405 | # Checks forced configuration of embedded interpreter IO streams |
| 406 | out, err = self.run_embedded_interpreter("forced_io_encoding") |
| 407 | if support.verbose: |
| 408 | print() |
| 409 | print(out) |
| 410 | print(err) |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 411 | expected_errors = sys.__stdout__.errors |
Nick Coghlan | 4e641df | 2013-11-03 16:54:46 +1000 | [diff] [blame] | 412 | expected_stdin_encoding = sys.__stdin__.encoding |
| 413 | expected_pipe_encoding = self._get_default_pipe_encoding() |
Steve Dower | 86e9deb | 2014-11-01 15:11:05 -0700 | [diff] [blame] | 414 | expected_output = '\n'.join([ |
Nick Coghlan | 4e641df | 2013-11-03 16:54:46 +1000 | [diff] [blame] | 415 | "--- Use defaults ---", |
| 416 | "Expected encoding: default", |
| 417 | "Expected errors: default", |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 418 | "stdin: {in_encoding}:{errors}", |
| 419 | "stdout: {out_encoding}:{errors}", |
| 420 | "stderr: {out_encoding}:backslashreplace", |
Nick Coghlan | 4e641df | 2013-11-03 16:54:46 +1000 | [diff] [blame] | 421 | "--- Set errors only ---", |
| 422 | "Expected encoding: default", |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 423 | "Expected errors: ignore", |
| 424 | "stdin: {in_encoding}:ignore", |
| 425 | "stdout: {out_encoding}:ignore", |
| 426 | "stderr: {out_encoding}:backslashreplace", |
Nick Coghlan | 4e641df | 2013-11-03 16:54:46 +1000 | [diff] [blame] | 427 | "--- Set encoding only ---", |
| 428 | "Expected encoding: latin-1", |
| 429 | "Expected errors: default", |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 430 | "stdin: latin-1:{errors}", |
| 431 | "stdout: latin-1:{errors}", |
Nick Coghlan | 4e641df | 2013-11-03 16:54:46 +1000 | [diff] [blame] | 432 | "stderr: latin-1:backslashreplace", |
| 433 | "--- Set encoding and errors ---", |
| 434 | "Expected encoding: latin-1", |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 435 | "Expected errors: replace", |
| 436 | "stdin: latin-1:replace", |
| 437 | "stdout: latin-1:replace", |
| 438 | "stderr: latin-1:backslashreplace"]) |
| 439 | expected_output = expected_output.format( |
| 440 | in_encoding=expected_stdin_encoding, |
| 441 | out_encoding=expected_pipe_encoding, |
| 442 | errors=expected_errors) |
Nick Coghlan | 3321fb8 | 2013-10-18 23:59:58 +1000 | [diff] [blame] | 443 | # This is useful if we ever trip over odd platform behaviour |
Nick Coghlan | 6508dc5 | 2013-10-18 01:44:22 +1000 | [diff] [blame] | 444 | self.maxDiff = None |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 445 | self.assertEqual(out.strip(), expected_output) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 446 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 447 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 448 | class SkipitemTest(unittest.TestCase): |
| 449 | |
| 450 | def test_skipitem(self): |
| 451 | """ |
| 452 | If this test failed, you probably added a new "format unit" |
| 453 | in Python/getargs.c, but neglected to update our poor friend |
| 454 | skipitem() in the same file. (If so, shame on you!) |
| 455 | |
Larry Hastings | 48ed360 | 2012-06-22 12:58:36 -0700 | [diff] [blame] | 456 | With a few exceptions**, this function brute-force tests all |
| 457 | printable ASCII*** characters (32 to 126 inclusive) as format units, |
| 458 | checking to see that PyArg_ParseTupleAndKeywords() return consistent |
| 459 | errors both when the unit is attempted to be used and when it is |
| 460 | skipped. If the format unit doesn't exist, we'll get one of two |
| 461 | specific error messages (one for used, one for skipped); if it does |
| 462 | exist we *won't* get that error--we'll get either no error or some |
| 463 | other error. If we get the specific "does not exist" error for one |
| 464 | test and not for the other, there's a mismatch, and the test fails. |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 465 | |
Larry Hastings | 48ed360 | 2012-06-22 12:58:36 -0700 | [diff] [blame] | 466 | ** Some format units have special funny semantics and it would |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 467 | be difficult to accommodate them here. Since these are all |
Larry Hastings | 48ed360 | 2012-06-22 12:58:36 -0700 | [diff] [blame] | 468 | well-established and properly skipped in skipitem() we can |
| 469 | get away with not testing them--this test is really intended |
| 470 | to catch *new* format units. |
| 471 | |
| 472 | *** Python C source files must be ASCII. Therefore it's impossible |
| 473 | to have non-ASCII format units. |
| 474 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 475 | """ |
| 476 | empty_tuple = () |
| 477 | tuple_1 = (0,) |
| 478 | dict_b = {'b':1} |
| 479 | keywords = ["a", "b"] |
| 480 | |
Larry Hastings | 48ed360 | 2012-06-22 12:58:36 -0700 | [diff] [blame] | 481 | for i in range(32, 127): |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 482 | c = chr(i) |
| 483 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 484 | # skip parentheses, the error reporting is inconsistent about them |
| 485 | # skip 'e', it's always a two-character code |
| 486 | # skip '|' and '$', they don't represent arguments anyway |
Larry Hastings | 48ed360 | 2012-06-22 12:58:36 -0700 | [diff] [blame] | 487 | if c in '()e|$': |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 488 | continue |
| 489 | |
| 490 | # test the format unit when not skipped |
| 491 | format = c + "i" |
| 492 | try: |
| 493 | # (note: the format string must be bytes!) |
| 494 | _testcapi.parse_tuple_and_keywords(tuple_1, dict_b, |
| 495 | format.encode("ascii"), keywords) |
| 496 | when_not_skipped = False |
Serhiy Storchaka | 4cd63ef | 2016-02-08 01:22:47 +0200 | [diff] [blame] | 497 | except SystemError as e: |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 498 | s = "argument 1 (impossible<bad format char>)" |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 499 | when_not_skipped = (str(e) == s) |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 500 | except TypeError: |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 501 | when_not_skipped = False |
| 502 | |
| 503 | # test the format unit when skipped |
| 504 | optional_format = "|" + format |
| 505 | try: |
| 506 | _testcapi.parse_tuple_and_keywords(empty_tuple, dict_b, |
| 507 | optional_format.encode("ascii"), keywords) |
| 508 | when_skipped = False |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 509 | except SystemError as e: |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 510 | s = "impossible<bad format char>: '{}'".format(format) |
| 511 | when_skipped = (str(e) == s) |
| 512 | |
| 513 | message = ("test_skipitem_parity: " |
| 514 | "detected mismatch between convertsimple and skipitem " |
| 515 | "for format unit '{}' ({}), not skipped {}, skipped {}".format( |
| 516 | c, i, when_skipped, when_not_skipped)) |
| 517 | self.assertIs(when_skipped, when_not_skipped, message) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 518 | |
Jesus Cea | 6e1d2b6 | 2012-10-04 16:06:30 +0200 | [diff] [blame] | 519 | def test_parse_tuple_and_keywords(self): |
| 520 | # parse_tuple_and_keywords error handling tests |
| 521 | self.assertRaises(TypeError, _testcapi.parse_tuple_and_keywords, |
| 522 | (), {}, 42, []) |
| 523 | self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords, |
| 524 | (), {}, b'', 42) |
| 525 | self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords, |
| 526 | (), {}, b'', [''] * 42) |
| 527 | self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords, |
| 528 | (), {}, b'', [42]) |
| 529 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 530 | def test_positional_only(self): |
| 531 | parse = _testcapi.parse_tuple_and_keywords |
| 532 | |
| 533 | parse((1, 2, 3), {}, b'OOO', ['', '', 'a']) |
| 534 | parse((1, 2), {'a': 3}, b'OOO', ['', '', 'a']) |
| 535 | with self.assertRaisesRegex(TypeError, |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 536 | r'Function takes at least 2 positional arguments \(1 given\)'): |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 537 | parse((1,), {'a': 3}, b'OOO', ['', '', 'a']) |
| 538 | parse((1,), {}, b'O|OO', ['', '', 'a']) |
| 539 | with self.assertRaisesRegex(TypeError, |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 540 | r'Function takes at least 1 positional arguments \(0 given\)'): |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 541 | parse((), {}, b'O|OO', ['', '', 'a']) |
| 542 | parse((1, 2), {'a': 3}, b'OO$O', ['', '', 'a']) |
| 543 | with self.assertRaisesRegex(TypeError, |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 544 | r'Function takes exactly 2 positional arguments \(1 given\)'): |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 545 | parse((1,), {'a': 3}, b'OO$O', ['', '', 'a']) |
| 546 | parse((1,), {}, b'O|O$O', ['', '', 'a']) |
| 547 | with self.assertRaisesRegex(TypeError, |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 548 | r'Function takes at least 1 positional arguments \(0 given\)'): |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 549 | parse((), {}, b'O|O$O', ['', '', 'a']) |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 550 | with self.assertRaisesRegex(SystemError, r'Empty parameter name after \$'): |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 551 | parse((1,), {}, b'O|$OO', ['', '', 'a']) |
| 552 | with self.assertRaisesRegex(SystemError, 'Empty keyword'): |
| 553 | parse((1,), {}, b'O|OO', ['', 'a', '']) |
| 554 | |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 555 | |
Ezio Melotti | 29267c8 | 2013-02-23 05:52:46 +0200 | [diff] [blame] | 556 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 557 | class TestThreadState(unittest.TestCase): |
| 558 | |
| 559 | @support.reap_threads |
| 560 | def test_thread_state(self): |
| 561 | # some extra thread-state tests driven via _testcapi |
| 562 | def target(): |
| 563 | idents = [] |
| 564 | |
| 565 | def callback(): |
Ezio Melotti | 35246bd | 2013-02-23 05:58:38 +0200 | [diff] [blame] | 566 | idents.append(threading.get_ident()) |
Ezio Melotti | 29267c8 | 2013-02-23 05:52:46 +0200 | [diff] [blame] | 567 | |
| 568 | _testcapi._test_thread_state(callback) |
| 569 | a = b = callback |
| 570 | time.sleep(1) |
| 571 | # Check our main thread is in the list exactly 3 times. |
Ezio Melotti | 35246bd | 2013-02-23 05:58:38 +0200 | [diff] [blame] | 572 | self.assertEqual(idents.count(threading.get_ident()), 3, |
Ezio Melotti | 29267c8 | 2013-02-23 05:52:46 +0200 | [diff] [blame] | 573 | "Couldn't find main thread correctly in the list") |
| 574 | |
| 575 | target() |
| 576 | t = threading.Thread(target=target) |
| 577 | t.start() |
| 578 | t.join() |
| 579 | |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 580 | |
Zachary Ware | c12f09e | 2013-11-11 22:47:04 -0600 | [diff] [blame] | 581 | class Test_testcapi(unittest.TestCase): |
| 582 | def test__testcapi(self): |
| 583 | for name in dir(_testcapi): |
| 584 | if name.startswith('test_'): |
Zachary Ware | dfcd694 | 2013-11-11 22:59:23 -0600 | [diff] [blame] | 585 | with self.subTest("internal", name=name): |
| 586 | test = getattr(_testcapi, name) |
| 587 | test() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 588 | |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 589 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 590 | class PyMemDebugTests(unittest.TestCase): |
| 591 | PYTHONMALLOC = 'debug' |
Victor Stinner | a1bc28a | 2016-03-14 17:10:36 +0100 | [diff] [blame] | 592 | # '0x04c06e0' or '04C06E0' |
Victor Stinner | 08572f6 | 2016-03-14 21:55:43 +0100 | [diff] [blame] | 593 | PTR_REGEX = r'(?:0x)?[0-9a-fA-F]+' |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 594 | |
| 595 | def check(self, code): |
| 596 | with support.SuppressCrashReport(): |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 597 | out = assert_python_failure('-c', code, |
| 598 | PYTHONMALLOC=self.PYTHONMALLOC) |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 599 | stderr = out.err |
| 600 | return stderr.decode('ascii', 'replace') |
| 601 | |
| 602 | def test_buffer_overflow(self): |
| 603 | out = self.check('import _testcapi; _testcapi.pymem_buffer_overflow()') |
Victor Stinner | a1bc28a | 2016-03-14 17:10:36 +0100 | [diff] [blame] | 604 | regex = (r"Debug memory block at address p={ptr}: API 'm'\n" |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 605 | r" 16 bytes originally requested\n" |
Victor Stinner | b3adb1a | 2016-03-14 17:40:09 +0100 | [diff] [blame] | 606 | r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n" |
| 607 | r" The [0-9] pad bytes at tail={ptr} are not all FORBIDDENBYTE \(0x[0-9a-f]{{2}}\):\n" |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 608 | r" at tail\+0: 0x78 \*\*\* OUCH\n" |
| 609 | r" at tail\+1: 0xfb\n" |
| 610 | r" at tail\+2: 0xfb\n" |
Victor Stinner | b3adb1a | 2016-03-14 17:40:09 +0100 | [diff] [blame] | 611 | r" .*\n" |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 612 | r" The block was made by call #[0-9]+ to debug malloc/realloc.\n" |
Victor Stinner | b3adb1a | 2016-03-14 17:40:09 +0100 | [diff] [blame] | 613 | r" Data at p: cb cb cb .*\n" |
Victor Stinner | 6453e9e | 2016-03-15 23:36:28 +0100 | [diff] [blame] | 614 | r"\n" |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 615 | r"Fatal Python error: bad trailing pad byte") |
Victor Stinner | a1bc28a | 2016-03-14 17:10:36 +0100 | [diff] [blame] | 616 | regex = regex.format(ptr=self.PTR_REGEX) |
Victor Stinner | b3adb1a | 2016-03-14 17:40:09 +0100 | [diff] [blame] | 617 | regex = re.compile(regex, flags=re.DOTALL) |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 618 | self.assertRegex(out, regex) |
| 619 | |
| 620 | def test_api_misuse(self): |
| 621 | out = self.check('import _testcapi; _testcapi.pymem_api_misuse()') |
Victor Stinner | a1bc28a | 2016-03-14 17:10:36 +0100 | [diff] [blame] | 622 | regex = (r"Debug memory block at address p={ptr}: API 'm'\n" |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 623 | r" 16 bytes originally requested\n" |
Victor Stinner | b3adb1a | 2016-03-14 17:40:09 +0100 | [diff] [blame] | 624 | r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n" |
| 625 | r" The [0-9] pad bytes at tail={ptr} are FORBIDDENBYTE, as expected.\n" |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 626 | r" The block was made by call #[0-9]+ to debug malloc/realloc.\n" |
Victor Stinner | b3adb1a | 2016-03-14 17:40:09 +0100 | [diff] [blame] | 627 | r" Data at p: cb cb cb .*\n" |
Victor Stinner | 6453e9e | 2016-03-15 23:36:28 +0100 | [diff] [blame] | 628 | r"\n" |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 629 | r"Fatal Python error: bad ID: Allocated using API 'm', verified using API 'r'\n") |
Victor Stinner | a1bc28a | 2016-03-14 17:10:36 +0100 | [diff] [blame] | 630 | regex = regex.format(ptr=self.PTR_REGEX) |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 631 | self.assertRegex(out, regex) |
| 632 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 633 | def check_malloc_without_gil(self, code): |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 634 | out = self.check(code) |
| 635 | expected = ('Fatal Python error: Python memory allocator called ' |
| 636 | 'without holding the GIL') |
| 637 | self.assertIn(expected, out) |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 638 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 639 | def test_pymem_malloc_without_gil(self): |
| 640 | # Debug hooks must raise an error if PyMem_Malloc() is called |
| 641 | # without holding the GIL |
| 642 | code = 'import _testcapi; _testcapi.pymem_malloc_without_gil()' |
| 643 | self.check_malloc_without_gil(code) |
| 644 | |
| 645 | def test_pyobject_malloc_without_gil(self): |
| 646 | # Debug hooks must raise an error if PyObject_Malloc() is called |
| 647 | # without holding the GIL |
| 648 | code = 'import _testcapi; _testcapi.pyobject_malloc_without_gil()' |
| 649 | self.check_malloc_without_gil(code) |
| 650 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 651 | |
| 652 | class PyMemMallocDebugTests(PyMemDebugTests): |
| 653 | PYTHONMALLOC = 'malloc_debug' |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 654 | |
| 655 | |
| 656 | @unittest.skipUnless(sysconfig.get_config_var('WITH_PYMALLOC') == 1, |
| 657 | 'need pymalloc') |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 658 | class PyMemPymallocDebugTests(PyMemDebugTests): |
| 659 | PYTHONMALLOC = 'pymalloc_debug' |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 660 | |
| 661 | |
| 662 | @unittest.skipUnless(Py_DEBUG, 'need Py_DEBUG') |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 663 | class PyMemDefaultTests(PyMemDebugTests): |
| 664 | # test default allocator of Python compiled in debug mode |
| 665 | PYTHONMALLOC = '' |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 666 | |
| 667 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 668 | if __name__ == "__main__": |
Zachary Ware | c12f09e | 2013-11-11 22:47:04 -0600 | [diff] [blame] | 669 | unittest.main() |