blob: 6c87645b0635bcd5c3b4dcf4c04d2ea8c445956a [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 Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Tim Petersd66595f2001-02-04 03:09:53 +00006import _testcapi
Tim Peters9ea17ac2001-02-02 05:57:15 +00007
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008def test_main():
9
10 for name in dir(_testcapi):
11 if name.startswith('test_'):
12 test = getattr(_testcapi, name)
Benjamin Petersonee8712c2008-05-20 21:35:26 +000013 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000014 print("internal", name)
Collin Winter3add4d72007-08-29 23:37:32 +000015 test()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000016
17 # some extra thread-state tests driven via _testcapi
18 def TestThreadState():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000019 if support.verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000020 print("auto-thread-state")
Mark Hammond8d98d2c2003-04-19 15:41:53 +000021
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000022 idents = []
Mark Hammond8d98d2c2003-04-19 15:41:53 +000023
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000024 def callback():
Georg Brandl2067bfd2008-05-25 13:05:15 +000025 idents.append(_thread.get_ident())
Mark Hammond8d98d2c2003-04-19 15:41:53 +000026
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027 _testcapi._test_thread_state(callback)
28 a = b = callback
29 time.sleep(1)
30 # Check our main thread is in the list exactly 3 times.
Georg Brandl2067bfd2008-05-25 13:05:15 +000031 if idents.count(_thread.get_ident()) != 3:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000032 raise support.TestFailed(
Collin Winter3add4d72007-08-29 23:37:32 +000033 "Couldn't find main thread correctly in the list")
Mark Hammond8d98d2c2003-04-19 15:41:53 +000034
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035 try:
36 _testcapi._test_thread_state
37 have_thread_state = True
38 except AttributeError:
39 have_thread_state = False
Tim Peters0eadaac2003-04-24 16:02:54 +000040
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041 if have_thread_state:
Georg Brandl2067bfd2008-05-25 13:05:15 +000042 import _thread
Christian Heimes5e696852008-04-09 08:37:03 +000043 import time
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044 TestThreadState()
45 import threading
Georg Brandl2067bfd2008-05-25 13:05:15 +000046 t = threading.Thread(target=TestThreadState)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047 t.start()
48 t.join()
Mark Hammond8d98d2c2003-04-19 15:41:53 +000049
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050if __name__ == "__main__":
51 test_main()