Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 1 | import imp |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 2 | from test.test_support import TestFailed |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 3 | |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 4 | def verify_lock_state(expected): |
| 5 | if imp.lock_held() != expected: |
| 6 | raise TestFailed("expected imp.lock_held() to be %r" % expected) |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 7 | |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 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: |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 28 | imp.release_lock() |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 29 | except RuntimeError: |
| 30 | pass |
| 31 | else: |
| 32 | raise TestFailed("release_lock() without lock should raise " |
| 33 | "RuntimeError") |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 34 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 35 | def test_main(): |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 36 | testLock() |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 37 | |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 38 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 39 | test_main() |