blob: efa7c345a4200753fa5a5f94fdf4990671413784 [file] [log] [blame]
Tim Petersd66595f2001-02-04 03:09:53 +00001# Run the _testcapi module tests (tests for the Python/C API): by defn,
Guido van Rossum361c5352001-04-13 17:03:04 +00002# these are all functions _testcapi exports whose name begins with 'test_'.
Tim Peters9ea17ac2001-02-02 05:57:15 +00003
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +00004import sys
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +00005import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Tim Petersd66595f2001-02-04 03:09:53 +00007import _testcapi
Tim Peters9ea17ac2001-02-02 05:57:15 +00008
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +00009def testfunction(self):
10 """some doc"""
11 return self
12
13class InstanceMethod:
14 id = _testcapi.instancemethod(id)
15 testfunction = _testcapi.instancemethod(testfunction)
16
17class CAPITest(unittest.TestCase):
18
19 def test_instancemethod(self):
20 inst = InstanceMethod()
21 self.assertEqual(id(inst), inst.id())
22 self.assert_(inst.testfunction() is inst)
23 self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__)
24 self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__)
25
26 InstanceMethod.testfunction.attribute = "test"
27 self.assertEqual(testfunction.attribute, "test")
28 self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
29
30
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000031def test_main():
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000032 support.run_unittest(CAPITest)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033
34 for name in dir(_testcapi):
35 if name.startswith('test_'):
36 test = getattr(_testcapi, name)
Benjamin Petersonee8712c2008-05-20 21:35:26 +000037 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000038 print("internal", name)
Collin Winter3add4d72007-08-29 23:37:32 +000039 test()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040
41 # some extra thread-state tests driven via _testcapi
42 def TestThreadState():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000043 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000044 print("auto-thread-state")
Mark Hammond8d98d2c2003-04-19 15:41:53 +000045
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000046 idents = []
Mark Hammond8d98d2c2003-04-19 15:41:53 +000047
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048 def callback():
Georg Brandl2067bfd2008-05-25 13:05:15 +000049 idents.append(_thread.get_ident())
Mark Hammond8d98d2c2003-04-19 15:41:53 +000050
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000051 _testcapi._test_thread_state(callback)
52 a = b = callback
53 time.sleep(1)
54 # Check our main thread is in the list exactly 3 times.
Georg Brandl2067bfd2008-05-25 13:05:15 +000055 if idents.count(_thread.get_ident()) != 3:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000056 raise support.TestFailed(
Collin Winter3add4d72007-08-29 23:37:32 +000057 "Couldn't find main thread correctly in the list")
Mark Hammond8d98d2c2003-04-19 15:41:53 +000058
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059 try:
60 _testcapi._test_thread_state
61 have_thread_state = True
62 except AttributeError:
63 have_thread_state = False
Tim Peters0eadaac2003-04-24 16:02:54 +000064
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000065 if have_thread_state:
Georg Brandl2067bfd2008-05-25 13:05:15 +000066 import _thread
Christian Heimes5e696852008-04-09 08:37:03 +000067 import time
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068 TestThreadState()
69 import threading
Georg Brandl2067bfd2008-05-25 13:05:15 +000070 t = threading.Thread(target=TestThreadState)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071 t.start()
72 t.join()
Mark Hammond8d98d2c2003-04-19 15:41:53 +000073
Benjamin Peterson9b6df6a2008-10-16 23:56:29 +000074
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000075if __name__ == "__main__":
76 test_main()