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 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 4 | from __future__ import with_statement |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 5 | import os |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 6 | import pickle |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 7 | import random |
| 8 | import subprocess |
Martin v. Löwis | 6ce7ed2 | 2005-03-03 12:26:35 +0000 | [diff] [blame] | 9 | import sys |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 10 | import time |
Ezio Melotti | 29267c8 | 2013-02-23 05:52:46 +0200 | [diff] [blame^] | 11 | import _thread |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 12 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 13 | from test import support |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 14 | try: |
Stefan Krah | fd24f9e | 2012-08-20 11:04:24 +0200 | [diff] [blame] | 15 | import _posixsubprocess |
| 16 | except ImportError: |
| 17 | _posixsubprocess = None |
| 18 | try: |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 19 | import threading |
| 20 | except ImportError: |
| 21 | threading = None |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 22 | import _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): |
| 48 | p = subprocess.Popen([sys.executable, "-c", |
| 49 | 'import _testcapi;' |
| 50 | '_testcapi.crash_no_current_thread()'], |
| 51 | stdout=subprocess.PIPE, |
| 52 | stderr=subprocess.PIPE) |
| 53 | (out, err) = p.communicate() |
| 54 | self.assertEqual(out, b'') |
| 55 | # This used to cause an infinite loop. |
Victor Stinner | 4000ffa | 2010-05-15 01:40:41 +0000 | [diff] [blame] | 56 | self.assertEqual(err.rstrip(), |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 57 | b'Fatal Python error:' |
Victor Stinner | 4000ffa | 2010-05-15 01:40:41 +0000 | [diff] [blame] | 58 | b' PyThreadState_Get: no current thread') |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 59 | |
Antoine Pitrou | 915605c | 2011-02-24 20:53:48 +0000 | [diff] [blame] | 60 | def test_memoryview_from_NULL_pointer(self): |
| 61 | self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer) |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 62 | |
Stefan Krah | fd24f9e | 2012-08-20 11:04:24 +0200 | [diff] [blame] | 63 | @unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.') |
| 64 | def test_seq_bytes_to_charp_array(self): |
| 65 | # Issue #15732: crash in _PySequence_BytesToCharpArray() |
| 66 | class Z(object): |
| 67 | def __len__(self): |
| 68 | return 1 |
| 69 | self.assertRaises(TypeError, _posixsubprocess.fork_exec, |
| 70 | 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] | 71 | # Issue #15736: overflow in _PySequence_BytesToCharpArray() |
| 72 | class Z(object): |
| 73 | def __len__(self): |
| 74 | return sys.maxsize |
| 75 | def __getitem__(self, i): |
| 76 | return b'x' |
| 77 | self.assertRaises(MemoryError, _posixsubprocess.fork_exec, |
| 78 | 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] | 79 | |
Stefan Krah | db579d7 | 2012-08-20 14:36:47 +0200 | [diff] [blame] | 80 | @unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.') |
| 81 | def test_subprocess_fork_exec(self): |
| 82 | class Z(object): |
| 83 | def __len__(self): |
| 84 | return 1 |
| 85 | |
| 86 | # Issue #15738: crash in subprocess_fork_exec() |
| 87 | self.assertRaises(TypeError, _posixsubprocess.fork_exec, |
| 88 | Z(),[b'1'],3,[1, 2],5,6,7,8,9,10,11,12,13,14,15,16,17) |
| 89 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 90 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 91 | class TestPendingCalls(unittest.TestCase): |
| 92 | |
| 93 | def pendingcalls_submit(self, l, n): |
| 94 | def callback(): |
| 95 | #this function can be interrupted by thread switching so let's |
| 96 | #use an atomic operation |
| 97 | l.append(None) |
| 98 | |
| 99 | for i in range(n): |
| 100 | time.sleep(random.random()*0.02) #0.01 secs on average |
| 101 | #try submitting callback until successful. |
| 102 | #rely on regular interrupt to flush queue if we are |
| 103 | #unsuccessful. |
| 104 | while True: |
| 105 | if _testcapi._pending_threadfunc(callback): |
| 106 | break; |
| 107 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 108 | def pendingcalls_wait(self, l, n, context = None): |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 109 | #now, stick around until l[0] has grown to 10 |
| 110 | count = 0; |
| 111 | while len(l) != n: |
| 112 | #this busy loop is where we expect to be interrupted to |
| 113 | #run our callbacks. Note that callbacks are only run on the |
| 114 | #main thread |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 115 | if False and support.verbose: |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 116 | print("(%i)"%(len(l),),) |
| 117 | for i in range(1000): |
| 118 | a = i*i |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 119 | if context and not context.event.is_set(): |
| 120 | continue |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 121 | count += 1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 122 | self.assertTrue(count < 10000, |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 123 | "timeout waiting for %i callbacks, got %i"%(n, len(l))) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 124 | if False and support.verbose: |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 125 | print("(%i)"%(len(l),)) |
| 126 | |
| 127 | def test_pendingcalls_threaded(self): |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 128 | |
| 129 | #do every callback on a separate thread |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 130 | n = 32 #total callbacks |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 131 | threads = [] |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 132 | class foo(object):pass |
| 133 | context = foo() |
| 134 | context.l = [] |
| 135 | context.n = 2 #submits per thread |
| 136 | context.nThreads = n // context.n |
| 137 | context.nFinished = 0 |
| 138 | context.lock = threading.Lock() |
| 139 | context.event = threading.Event() |
| 140 | |
| 141 | for i in range(context.nThreads): |
| 142 | t = threading.Thread(target=self.pendingcalls_thread, args = (context,)) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 143 | t.start() |
| 144 | threads.append(t) |
| 145 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 146 | self.pendingcalls_wait(context.l, n, context) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 147 | |
| 148 | for t in threads: |
| 149 | t.join() |
| 150 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 151 | def pendingcalls_thread(self, context): |
| 152 | try: |
| 153 | self.pendingcalls_submit(context.l, context.n) |
| 154 | finally: |
| 155 | with context.lock: |
| 156 | context.nFinished += 1 |
| 157 | nFinished = context.nFinished |
| 158 | if False and support.verbose: |
| 159 | print("finished threads: ", nFinished) |
| 160 | if nFinished == context.nThreads: |
| 161 | context.event.set() |
| 162 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 163 | def test_pendingcalls_non_threaded(self): |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 164 | #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] | 165 | #once. It is ok to ask for too many, because we loop until we find a slot. |
| 166 | #the loop can be interrupted to dispatch. |
| 167 | #there are only 32 dispatch slots, so we go for twice that! |
| 168 | l = [] |
| 169 | n = 64 |
| 170 | self.pendingcalls_submit(l, n) |
| 171 | self.pendingcalls_wait(l, n) |
| 172 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 173 | def test_subinterps(self): |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 174 | import builtins |
| 175 | r, w = os.pipe() |
| 176 | code = """if 1: |
| 177 | import sys, builtins, pickle |
| 178 | with open({:d}, "wb") as f: |
| 179 | pickle.dump(id(sys.modules), f) |
| 180 | pickle.dump(id(builtins), f) |
| 181 | """.format(w) |
| 182 | with open(r, "rb") as f: |
| 183 | ret = _testcapi.run_in_subinterp(code) |
| 184 | self.assertEqual(ret, 0) |
| 185 | self.assertNotEqual(pickle.load(f), id(sys.modules)) |
| 186 | self.assertNotEqual(pickle.load(f), id(builtins)) |
| 187 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 188 | # Bug #6012 |
| 189 | class Test6012(unittest.TestCase): |
| 190 | def test(self): |
| 191 | self.assertEqual(_testcapi.argparsing("Hello", "World"), 1) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 193 | |
| 194 | class EmbeddingTest(unittest.TestCase): |
| 195 | |
Antoine Pitrou | 71cbafb | 2011-06-30 20:02:54 +0200 | [diff] [blame] | 196 | @unittest.skipIf( |
| 197 | sys.platform.startswith('win'), |
| 198 | "test doesn't work under Windows") |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 199 | def test_subinterps(self): |
| 200 | # XXX only tested under Unix checkouts |
| 201 | basepath = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
| 202 | oldcwd = os.getcwd() |
| 203 | # This is needed otherwise we get a fatal error: |
| 204 | # "Py_Initialize: Unable to get the locale encoding |
| 205 | # LookupError: no codec search functions registered: can't find encoding" |
| 206 | os.chdir(basepath) |
| 207 | try: |
| 208 | exe = os.path.join(basepath, "Modules", "_testembed") |
| 209 | if not os.path.exists(exe): |
| 210 | self.skipTest("%r doesn't exist" % exe) |
| 211 | p = subprocess.Popen([exe], |
| 212 | stdout=subprocess.PIPE, |
| 213 | stderr=subprocess.PIPE) |
| 214 | (out, err) = p.communicate() |
| 215 | self.assertEqual(p.returncode, 0, |
| 216 | "bad returncode %d, stderr is %r" % |
| 217 | (p.returncode, err)) |
| 218 | if support.verbose: |
| 219 | print() |
| 220 | print(out.decode('latin1')) |
| 221 | print(err.decode('latin1')) |
| 222 | finally: |
| 223 | os.chdir(oldcwd) |
| 224 | |
| 225 | |
Ezio Melotti | 29267c8 | 2013-02-23 05:52:46 +0200 | [diff] [blame^] | 226 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 227 | class TestThreadState(unittest.TestCase): |
| 228 | |
| 229 | @support.reap_threads |
| 230 | def test_thread_state(self): |
| 231 | # some extra thread-state tests driven via _testcapi |
| 232 | def target(): |
| 233 | idents = [] |
| 234 | |
| 235 | def callback(): |
| 236 | idents.append(_thread.get_ident()) |
| 237 | |
| 238 | _testcapi._test_thread_state(callback) |
| 239 | a = b = callback |
| 240 | time.sleep(1) |
| 241 | # Check our main thread is in the list exactly 3 times. |
| 242 | self.assertEqual(idents.count(_thread.get_ident()), 3, |
| 243 | "Couldn't find main thread correctly in the list") |
| 244 | |
| 245 | target() |
| 246 | t = threading.Thread(target=target) |
| 247 | t.start() |
| 248 | t.join() |
| 249 | |
| 250 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 251 | def test_main(): |
Ezio Melotti | 29267c8 | 2013-02-23 05:52:46 +0200 | [diff] [blame^] | 252 | support.run_unittest(CAPITest, TestPendingCalls, Test6012, |
| 253 | EmbeddingTest, TestThreadState) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 254 | |
| 255 | for name in dir(_testcapi): |
| 256 | if name.startswith('test_'): |
| 257 | test = getattr(_testcapi, name) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 258 | if support.verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 259 | print("internal", name) |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 260 | test() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 261 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 262 | if __name__ == "__main__": |
| 263 | test_main() |