blob: 1bdc47affbeabf1b0776fa286cb0b76de8e5de22 [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
Zachary Ware1f702212013-12-10 14:09:20 -06005try:
6 import thread
7except ImportError:
8 thread = None
Neal Norwitz2294c0d2003-02-12 23:02:21 +00009
Zachary Ware1f702212013-12-10 14:09:20 -060010@unittest.skipUnless(thread, 'threading not available')
Brett Cannon373d90b2006-10-03 23:23:14 +000011class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +000012
Brett Cannon373d90b2006-10-03 23:23:14 +000013 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +000014
Brett Cannon373d90b2006-10-03 23:23:14 +000015 def verify_lock_state(self, expected):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000016 self.assertEqual(imp.lock_held(), expected,
Brett Cannon373d90b2006-10-03 23:23:14 +000017 "expected imp.lock_held() to be %r" % expected)
18 def testLock(self):
19 LOOPS = 50
Tim Peters579bed72003-04-26 14:31:24 +000020
Brett Cannon373d90b2006-10-03 23:23:14 +000021 # The import lock may already be held, e.g. if the test suite is run
22 # via "import test.autotest".
23 lock_held_at_start = imp.lock_held()
24 self.verify_lock_state(lock_held_at_start)
Tim Peters579bed72003-04-26 14:31:24 +000025
Brett Cannon373d90b2006-10-03 23:23:14 +000026 for i in range(LOOPS):
27 imp.acquire_lock()
28 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000029
Brett Cannon373d90b2006-10-03 23:23:14 +000030 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000031 imp.release_lock()
Brett Cannon373d90b2006-10-03 23:23:14 +000032
33 # The original state should be restored now.
34 self.verify_lock_state(lock_held_at_start)
35
36 if not lock_held_at_start:
37 try:
38 imp.release_lock()
39 except RuntimeError:
40 pass
41 else:
42 self.fail("release_lock() without lock should raise "
43 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000044
Brett Cannon3aa2a492008-08-06 22:28:09 +000045class ReloadTests(unittest.TestCase):
46
47 """Very basic tests to make sure that imp.reload() operates just like
48 reload()."""
49
50 def test_source(self):
Nick Coghlan9039b832009-10-18 05:38:48 +000051 # XXX (ncoghlan): It would be nice to use test_support.CleanImport
52 # here, but that breaks because the os module registers some
53 # handlers in copy_reg on import. Since CleanImport doesn't
54 # revert that registration, the module is left in a broken
55 # state after reversion. Reinitialising the module contents
56 # and just reverting os.environ to its previous state is an OK
57 # workaround
58 with test_support.EnvironmentVarGuard():
Nick Coghlan788d7662009-10-17 15:57:42 +000059 import os
60 imp.reload(os)
Brett Cannon3aa2a492008-08-06 22:28:09 +000061
62 def test_extension(self):
Nick Coghlan788d7662009-10-17 15:57:42 +000063 with test_support.CleanImport('time'):
64 import time
65 imp.reload(time)
Brett Cannon3aa2a492008-08-06 22:28:09 +000066
67 def test_builtin(self):
Nick Coghlan788d7662009-10-17 15:57:42 +000068 with test_support.CleanImport('marshal'):
69 import marshal
70 imp.reload(marshal)
Brett Cannon3aa2a492008-08-06 22:28:09 +000071
72
Neal Norwitz996acf12003-02-17 14:51:41 +000073def test_main():
Hirokazu Yamamoto631be012008-09-08 23:38:42 +000074 tests = [
75 ReloadTests,
Zachary Ware1f702212013-12-10 14:09:20 -060076 LockTests,
Hirokazu Yamamoto631be012008-09-08 23:38:42 +000077 ]
Hirokazu Yamamoto631be012008-09-08 23:38:42 +000078 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()