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