/Lib/test/test_capi.py

http://unladen-swallow.googlecode.com/ · Python · 54 lines · 40 code · 10 blank · 4 comment · 11 complexity · 4d5889c67e2bd3dcabf2a08f7375d5a6 MD5 · raw file

  1. # Run the _testcapi module tests (tests for the Python/C API): by defn,
  2. # these are all functions _testcapi exports whose name begins with 'test_'.
  3. import sys
  4. from test import test_support
  5. import _testcapi
  6. def test_main():
  7. for name in dir(_testcapi):
  8. if name.startswith('test_'):
  9. test = getattr(_testcapi, name)
  10. if test_support.verbose:
  11. print "internal", name
  12. try:
  13. test()
  14. except _testcapi.error:
  15. raise test_support.TestFailed, sys.exc_info()[1]
  16. # some extra thread-state tests driven via _testcapi
  17. def TestThreadState():
  18. if test_support.verbose:
  19. print "auto-thread-state"
  20. idents = []
  21. def callback():
  22. idents.append(thread.get_ident())
  23. _testcapi._test_thread_state(callback)
  24. a = b = callback
  25. time.sleep(1)
  26. # Check our main thread is in the list exactly 3 times.
  27. if idents.count(thread.get_ident()) != 3:
  28. raise test_support.TestFailed, \
  29. "Couldn't find main thread correctly in the list"
  30. try:
  31. _testcapi._test_thread_state
  32. have_thread_state = True
  33. except AttributeError:
  34. have_thread_state = False
  35. if have_thread_state:
  36. import thread
  37. import time
  38. TestThreadState()
  39. import threading
  40. t=threading.Thread(target=TestThreadState)
  41. t.start()
  42. t.join()
  43. if __name__ == "__main__":
  44. test_main()