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