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