]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | import imp\r |
2 | import unittest\r | |
3 | from test import test_support\r | |
4 | \r | |
5 | \r | |
6 | class LockTests(unittest.TestCase):\r | |
7 | \r | |
8 | """Very basic test of import lock functions."""\r | |
9 | \r | |
10 | def verify_lock_state(self, expected):\r | |
11 | self.assertEqual(imp.lock_held(), expected,\r | |
12 | "expected imp.lock_held() to be %r" % expected)\r | |
13 | def testLock(self):\r | |
14 | LOOPS = 50\r | |
15 | \r | |
16 | # The import lock may already be held, e.g. if the test suite is run\r | |
17 | # via "import test.autotest".\r | |
18 | lock_held_at_start = imp.lock_held()\r | |
19 | self.verify_lock_state(lock_held_at_start)\r | |
20 | \r | |
21 | for i in range(LOOPS):\r | |
22 | imp.acquire_lock()\r | |
23 | self.verify_lock_state(True)\r | |
24 | \r | |
25 | for i in range(LOOPS):\r | |
26 | imp.release_lock()\r | |
27 | \r | |
28 | # The original state should be restored now.\r | |
29 | self.verify_lock_state(lock_held_at_start)\r | |
30 | \r | |
31 | if not lock_held_at_start:\r | |
32 | try:\r | |
33 | imp.release_lock()\r | |
34 | except RuntimeError:\r | |
35 | pass\r | |
36 | else:\r | |
37 | self.fail("release_lock() without lock should raise "\r | |
38 | "RuntimeError")\r | |
39 | \r | |
40 | class ReloadTests(unittest.TestCase):\r | |
41 | \r | |
42 | """Very basic tests to make sure that imp.reload() operates just like\r | |
43 | reload()."""\r | |
44 | \r | |
45 | def test_source(self):\r | |
46 | # XXX (ncoghlan): It would be nice to use test_support.CleanImport\r | |
47 | # here, but that breaks because the os module registers some\r | |
48 | # handlers in copy_reg on import. Since CleanImport doesn't\r | |
49 | # revert that registration, the module is left in a broken\r | |
50 | # state after reversion. Reinitialising the module contents\r | |
51 | # and just reverting os.environ to its previous state is an OK\r | |
52 | # workaround\r | |
53 | with test_support.EnvironmentVarGuard():\r | |
54 | import os\r | |
55 | imp.reload(os)\r | |
56 | \r | |
57 | def test_extension(self):\r | |
58 | with test_support.CleanImport('time'):\r | |
59 | import time\r | |
60 | imp.reload(time)\r | |
61 | \r | |
62 | def test_builtin(self):\r | |
63 | with test_support.CleanImport('marshal'):\r | |
64 | import marshal\r | |
65 | imp.reload(marshal)\r | |
66 | \r | |
67 | \r | |
68 | def test_main():\r | |
69 | tests = [\r | |
70 | ReloadTests,\r | |
71 | ]\r | |
72 | try:\r | |
73 | import thread\r | |
74 | except ImportError:\r | |
75 | pass\r | |
76 | else:\r | |
77 | tests.append(LockTests)\r | |
78 | test_support.run_unittest(*tests)\r | |
79 | \r | |
80 | if __name__ == "__main__":\r | |
81 | test_main()\r |