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