blob: 77e44bec923a2e7fd68ff3958b181a05dbedb194 [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Brett Cannon373d90b2006-10-03 23:23:14 +00002import unittest
3from test import test_support
Neal Norwitz2294c0d2003-02-12 23:02:21 +00004
Neal Norwitz2294c0d2003-02-12 23:02:21 +00005
Brett Cannon373d90b2006-10-03 23:23:14 +00006class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +00007
Brett Cannon373d90b2006-10-03 23:23:14 +00008 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +00009
Brett Cannon373d90b2006-10-03 23:23:14 +000010 def verify_lock_state(self, expected):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000011 self.assertEqual(imp.lock_held(), expected,
Brett Cannon373d90b2006-10-03 23:23:14 +000012 "expected imp.lock_held() to be %r" % expected)
13 def testLock(self):
14 LOOPS = 50
Tim Peters579bed72003-04-26 14:31:24 +000015
Brett Cannon373d90b2006-10-03 23:23:14 +000016 # The import lock may already be held, e.g. if the test suite is run
17 # via "import test.autotest".
18 lock_held_at_start = imp.lock_held()
19 self.verify_lock_state(lock_held_at_start)
Tim Peters579bed72003-04-26 14:31:24 +000020
Brett Cannon373d90b2006-10-03 23:23:14 +000021 for i in range(LOOPS):
22 imp.acquire_lock()
23 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000024
Brett Cannon373d90b2006-10-03 23:23:14 +000025 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000026 imp.release_lock()
Brett Cannon373d90b2006-10-03 23:23:14 +000027
28 # The original state should be restored now.
29 self.verify_lock_state(lock_held_at_start)
30
31 if not lock_held_at_start:
32 try:
33 imp.release_lock()
34 except RuntimeError:
35 pass
36 else:
37 self.fail("release_lock() without lock should raise "
38 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000039
Brett Cannon3aa2a492008-08-06 22:28:09 +000040class ReloadTests(unittest.TestCase):
41
42 """Very basic tests to make sure that imp.reload() operates just like
43 reload()."""
44
45 def test_source(self):
Nick Coghlan9039b832009-10-18 05:38:48 +000046 # XXX (ncoghlan): It would be nice to use test_support.CleanImport
47 # here, but that breaks because the os module registers some
48 # handlers in copy_reg on import. Since CleanImport doesn't
49 # revert that registration, the module is left in a broken
50 # state after reversion. Reinitialising the module contents
51 # and just reverting os.environ to its previous state is an OK
52 # workaround
53 with test_support.EnvironmentVarGuard():
Nick Coghlan788d7662009-10-17 15:57:42 +000054 import os
55 imp.reload(os)
Brett Cannon3aa2a492008-08-06 22:28:09 +000056
57 def test_extension(self):
Nick Coghlan788d7662009-10-17 15:57:42 +000058 with test_support.CleanImport('time'):
59 import time
60 imp.reload(time)
Brett Cannon3aa2a492008-08-06 22:28:09 +000061
62 def test_builtin(self):
Nick Coghlan788d7662009-10-17 15:57:42 +000063 with test_support.CleanImport('marshal'):
64 import marshal
65 imp.reload(marshal)
Brett Cannon3aa2a492008-08-06 22:28:09 +000066
67
Neal Norwitz996acf12003-02-17 14:51:41 +000068def test_main():
Hirokazu Yamamoto631be012008-09-08 23:38:42 +000069 tests = [
70 ReloadTests,
71 ]
72 try:
73 import thread
74 except ImportError:
75 pass
76 else:
77 tests.append(LockTests)
78 test_support.run_unittest(*tests)
Neal Norwitz996acf12003-02-17 14:51:41 +000079
Neal Norwitz2294c0d2003-02-12 23:02:21 +000080if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000081 test_main()