Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_imp.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 5e7c5c94c572 |
| children |
| rev | line source |
|---|---|
|
28857
68c6186dc1a1
Rewrote. As reported on c.l.py, when the test suite is run via
Tim Peters <tim.peters@gmail.com>
parents:
27923
diff
changeset
|
1 import imp |
|
39979
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
2 import unittest |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
3 from test import test_support |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
4 |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
5 |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
6 class LockTests(unittest.TestCase): |
| 27853 | 7 |
|
39979
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
8 """Very basic test of import lock functions.""" |
| 27853 | 9 |
|
39979
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
10 def verify_lock_state(self, expected): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49097
diff
changeset
|
11 self.assertEqual(imp.lock_held(), expected, |
|
39979
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
12 "expected imp.lock_held() to be %r" % expected) |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
13 def testLock(self): |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
14 LOOPS = 50 |
| 27853 | 15 |
|
39979
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
16 # The import lock may already be held, e.g. if the test suite is run |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
17 # via "import test.autotest". |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
18 lock_held_at_start = imp.lock_held() |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
19 self.verify_lock_state(lock_held_at_start) |
|
28857
68c6186dc1a1
Rewrote. As reported on c.l.py, when the test suite is run via
Tim Peters <tim.peters@gmail.com>
parents:
27923
diff
changeset
|
20 |
|
39979
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
21 for i in range(LOOPS): |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
22 imp.acquire_lock() |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
23 self.verify_lock_state(True) |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
24 |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
25 for i in range(LOOPS): |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
26 imp.release_lock() |
| 27853 | 27 |
|
39979
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
28 # The original state should be restored now. |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
29 self.verify_lock_state(lock_held_at_start) |
|
28857
68c6186dc1a1
Rewrote. As reported on c.l.py, when the test suite is run via
Tim Peters <tim.peters@gmail.com>
parents:
27923
diff
changeset
|
30 |
|
39979
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
31 if not lock_held_at_start: |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
32 try: |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
33 imp.release_lock() |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
34 except RuntimeError: |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
35 pass |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
36 else: |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
37 self.fail("release_lock() without lock should raise " |
|
9e35b62ed4d2
Convert test_imp over to unittest.
Brett Cannon <bcannon@gmail.com>
parents:
34188
diff
changeset
|
38 "RuntimeError") |
| 27853 | 39 |
|
48189
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
40 class ReloadTests(unittest.TestCase): |
|
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
41 |
|
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
42 """Very basic tests to make sure that imp.reload() operates just like |
|
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
43 reload().""" |
|
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
44 |
|
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
45 def test_source(self): |
|
56580
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
46 # XXX (ncoghlan): It would be nice to use test_support.CleanImport |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
47 # here, but that breaks because the os module registers some |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
48 # handlers in copy_reg on import. Since CleanImport doesn't |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
49 # revert that registration, the module is left in a broken |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
50 # state after reversion. Reinitialising the module contents |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
51 # and just reverting os.environ to its previous state is an OK |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
52 # workaround |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
53 with test_support.EnvironmentVarGuard(): |
|
56567
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
54 import os |
|
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
55 imp.reload(os) |
|
48189
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
56 |
|
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
57 def test_extension(self): |
|
56567
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
58 with test_support.CleanImport('time'): |
|
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
59 import time |
|
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
60 imp.reload(time) |
|
48189
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
61 |
|
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
62 def test_builtin(self): |
|
56567
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
63 with test_support.CleanImport('marshal'): |
|
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
64 import marshal |
|
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
65 imp.reload(marshal) |
|
48189
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
66 |
|
53ade9e68b56
Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents:
45468
diff
changeset
|
67 |
|
27912
c5ebacbba0be
Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents:
27853
diff
changeset
|
68 def test_main(): |
|
49097
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
69 tests = [ |
|
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
70 ReloadTests, |
|
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
71 ] |
|
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
72 try: |
|
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
73 import thread |
|
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
74 except ImportError: |
|
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
75 pass |
|
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
76 else: |
|
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
77 tests.append(LockTests) |
|
23b452ac1a50
Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48189
diff
changeset
|
78 test_support.run_unittest(*tests) |
|
27912
c5ebacbba0be
Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents:
27853
diff
changeset
|
79 |
| 27853 | 80 if __name__ == "__main__": |
|
27912
c5ebacbba0be
Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents:
27853
diff
changeset
|
81 test_main() |
