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