blob: 87efc33415edcc7acf28d2f5e59e468c7cca3747 [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Thomas Wouters89f507f2006-12-13 04:49:30 +00002import thread
3import unittest
4from test import test_support
Neal Norwitz2294c0d2003-02-12 23:02:21 +00005
Neal Norwitz2294c0d2003-02-12 23:02:21 +00006
Thomas Wouters89f507f2006-12-13 04:49:30 +00007class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +00008
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +000010
Thomas Wouters89f507f2006-12-13 04:49:30 +000011 def verify_lock_state(self, expected):
12 self.failUnlessEqual(imp.lock_held(), expected,
13 "expected imp.lock_held() to be %r" % expected)
14 def testLock(self):
15 LOOPS = 50
Tim Peters579bed72003-04-26 14:31:24 +000016
Thomas Wouters89f507f2006-12-13 04:49:30 +000017 # The import lock may already be held, e.g. if the test suite is run
18 # via "import test.autotest".
19 lock_held_at_start = imp.lock_held()
20 self.verify_lock_state(lock_held_at_start)
Tim Peters579bed72003-04-26 14:31:24 +000021
Thomas Wouters89f507f2006-12-13 04:49:30 +000022 for i in range(LOOPS):
23 imp.acquire_lock()
24 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000025
Thomas Wouters89f507f2006-12-13 04:49:30 +000026 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000027 imp.release_lock()
Thomas Wouters89f507f2006-12-13 04:49:30 +000028
29 # The original state should be restored now.
30 self.verify_lock_state(lock_held_at_start)
31
32 if not lock_held_at_start:
33 try:
34 imp.release_lock()
35 except RuntimeError:
36 pass
37 else:
38 self.fail("release_lock() without lock should raise "
39 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000040
Guido van Rossumce3a72a2007-10-19 23:16:50 +000041class ImportTests(unittest.TestCase):
42
43 def test_find_module_encoding(self):
44 fd = imp.find_module("heapq")[0]
45 self.assertEqual(fd.encoding, "iso-8859-1")
46
Neal Norwitz996acf12003-02-17 14:51:41 +000047def test_main():
Thomas Wouters89f507f2006-12-13 04:49:30 +000048 test_support.run_unittest(
49 LockTests,
Guido van Rossumce3a72a2007-10-19 23:16:50 +000050 ImportTests,
Thomas Wouters89f507f2006-12-13 04:49:30 +000051 )
Neal Norwitz996acf12003-02-17 14:51:41 +000052
Neal Norwitz2294c0d2003-02-12 23:02:21 +000053if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000054 test_main()