blob: eea41c1f7c4a9259ae14233185ec5c12cd46b414 [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
Barry Warsaw04f357c2002-07-23 19:04:11 +00005from test import test_support
Tim Petersd66595f2001-02-04 03:09:53 +00006import _testcapi
Tim Peters9ea17ac2001-02-02 05:57:15 +00007
Tim Peters59b96c12006-03-21 03:58:41 +00008def test_main():
9
10 for name in dir(_testcapi):
11 if name.startswith('test_'):
12 test = getattr(_testcapi, name)
13 if test_support.verbose:
14 print "internal", name
15 try:
16 test()
17 except _testcapi.error:
18 raise test_support.TestFailed, sys.exc_info()[1]
19
20 # some extra thread-state tests driven via _testcapi
21 def TestThreadState():
Tim Peters9ea17ac2001-02-02 05:57:15 +000022 if test_support.verbose:
Tim Peters59b96c12006-03-21 03:58:41 +000023 print "auto-thread-state"
Mark Hammond8d98d2c2003-04-19 15:41:53 +000024
Tim Peters59b96c12006-03-21 03:58:41 +000025 idents = []
Mark Hammond8d98d2c2003-04-19 15:41:53 +000026
Tim Peters59b96c12006-03-21 03:58:41 +000027 def callback():
28 idents.append(thread.get_ident())
Mark Hammond8d98d2c2003-04-19 15:41:53 +000029
Tim Peters59b96c12006-03-21 03:58:41 +000030 _testcapi._test_thread_state(callback)
31 a = b = callback
32 time.sleep(1)
33 # Check our main thread is in the list exactly 3 times.
34 if idents.count(thread.get_ident()) != 3:
35 raise test_support.TestFailed, \
36 "Couldn't find main thread correctly in the list"
Mark Hammond8d98d2c2003-04-19 15:41:53 +000037
Tim Peters59b96c12006-03-21 03:58:41 +000038 try:
39 _testcapi._test_thread_state
40 have_thread_state = True
41 except AttributeError:
42 have_thread_state = False
Tim Peters0eadaac2003-04-24 16:02:54 +000043
Tim Peters59b96c12006-03-21 03:58:41 +000044 if have_thread_state:
Amaury Forgeot d'Arc4b798bd2008-04-08 21:27:42 +000045 import thread
46 import time
Tim Peters59b96c12006-03-21 03:58:41 +000047 TestThreadState()
48 import threading
49 t=threading.Thread(target=TestThreadState)
50 t.start()
51 t.join()
Mark Hammond8d98d2c2003-04-19 15:41:53 +000052
Tim Peters59b96c12006-03-21 03:58:41 +000053if __name__ == "__main__":
54 test_main()