home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / pythonwin / python.exe / TEST_IMP.PY < prev    next >
Encoding:
Python Source  |  2003-04-26  |  1022 b   |  40 lines

  1. import imp
  2. from test.test_support import TestFailed
  3.  
  4. def verify_lock_state(expected):
  5.     if imp.lock_held() != expected:
  6.         raise TestFailed("expected imp.lock_held() to be %r" % expected)
  7.  
  8. def testLock():
  9.     LOOPS = 50
  10.  
  11.     # The import lock may already be held, e.g. if the test suite is run
  12.     # via "import test.autotest".
  13.     lock_held_at_start = imp.lock_held()
  14.     verify_lock_state(lock_held_at_start)
  15.  
  16.     for i in range(LOOPS):
  17.         imp.acquire_lock()
  18.         verify_lock_state(True)
  19.  
  20.     for i in range(LOOPS):
  21.         imp.release_lock()
  22.  
  23.     # The original state should be restored now.
  24.     verify_lock_state(lock_held_at_start)
  25.  
  26.     if not lock_held_at_start:
  27.         try:
  28.             imp.release_lock()
  29.         except RuntimeError:
  30.             pass
  31.         else:
  32.             raise TestFailed("release_lock() without lock should raise "
  33.                              "RuntimeError")
  34.  
  35. def test_main():
  36.     testLock()
  37.  
  38. if __name__ == "__main__":
  39.     test_main()
  40.