Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(24145)

Delta Between Two Patch Sets: Lib/test/test_importlib/builtin/test_loader.py

Issue 15836: unittest assertRaises should verify excClass is actually a BaseException class
Left Patch Set: Created 7 years ago
Right Patch Set: Created 4 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Lib/unittest/case.py » ('j') | Lib/unittest/case.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import importlib
2 from importlib import machinery
3 from .. import abc 1 from .. import abc
4 from .. import util 2 from .. import util
5 from . import util as builtin_util 3
4 machinery = util.import_importlib('importlib.machinery')
6 5
7 import sys 6 import sys
8 import types 7 import types
9 import unittest 8 import unittest
10 9
11 10 @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module' )
12 class LoaderTests(abc.LoaderTests): 11 class LoaderTests(abc.LoaderTests):
13 12
14 """Test load_module() for built-in modules.""" 13 """Test load_module() for built-in modules."""
15 14
16 verification = {'__name__': 'errno', '__package__': '', 15 def setUp(self):
17 '__loader__': machinery.BuiltinImporter} 16 self.verification = {'__name__': 'errno', '__package__': '',
17 '__loader__': self.machinery.BuiltinImporter}
18 18
19 def verify(self, module): 19 def verify(self, module):
20 """Verify that the module matches against what it should have.""" 20 """Verify that the module matches against what it should have."""
21 self.assertIsInstance(module, types.ModuleType) 21 self.assertIsInstance(module, types.ModuleType)
22 for attr, value in self.verification.items(): 22 for attr, value in self.verification.items():
23 self.assertEqual(getattr(module, attr), value) 23 self.assertEqual(getattr(module, attr), value)
24 self.assertIn(module.__name__, sys.modules) 24 self.assertIn(module.__name__, sys.modules)
25 25
26 load_module = staticmethod(lambda name: 26 def load_module(self, name):
27 machinery.BuiltinImporter.load_module(name)) 27 return self.machinery.BuiltinImporter.load_module(name)
28 28
29 def test_module(self): 29 def test_module(self):
30 # Common case. 30 # Common case.
31 with util.uncache(builtin_util.NAME): 31 with util.uncache(util.BUILTINS.good_name):
32 module = self.load_module(builtin_util.NAME) 32 module = self.load_module(util.BUILTINS.good_name)
33 self.verify(module) 33 self.verify(module)
34 34
35 def test_package(self): 35 # Built-in modules cannot be a package.
36 # Built-in modules cannot be a package. 36 test_package = test_lacking_parent = None
37 pass
38 37
39 def test_lacking_parent(self): 38 # No way to force an import failure.
40 # Built-in modules cannot be a package. 39 test_state_after_failure = None
41 pass
42
43 def test_state_after_failure(self):
44 # Not way to force an imoprt failure.
45 pass
46 40
47 def test_module_reuse(self): 41 def test_module_reuse(self):
48 # Test that the same module is used in a reload. 42 # Test that the same module is used in a reload.
49 with util.uncache(builtin_util.NAME): 43 with util.uncache(util.BUILTINS.good_name):
50 module1 = self.load_module(builtin_util.NAME) 44 module1 = self.load_module(util.BUILTINS.good_name)
51 module2 = self.load_module(builtin_util.NAME) 45 module2 = self.load_module(util.BUILTINS.good_name)
52 self.assertIs(module1, module2) 46 self.assertIs(module1, module2)
53 47
54 def test_unloadable(self): 48 def test_unloadable(self):
55 name = 'dssdsdfff' 49 name = 'dssdsdfff'
56 assert name not in sys.builtin_module_names 50 assert name not in sys.builtin_module_names
57 with self.assertRaises(ImportError) as cm: 51 with self.assertRaises(ImportError) as cm:
58 self.load_module(name) 52 self.load_module(name)
59 self.assertEqual(cm.exception.name, name) 53 self.assertEqual(cm.exception.name, name)
60 54
61 def test_already_imported(self): 55 def test_already_imported(self):
62 # Using the name of a module already imported but not a built-in should 56 # Using the name of a module already imported but not a built-in should
63 # still fail. 57 # still fail.
64 assert hasattr(importlib, '__file__') # Not a built-in. 58 module_name = 'builtin_reload_test'
59 assert module_name not in sys.builtin_module_names
60 with util.uncache(module_name):
61 module = types.ModuleType(module_name)
62 sys.modules[module_name] = module
65 with self.assertRaises(ImportError) as cm: 63 with self.assertRaises(ImportError) as cm:
66 self.load_module('importlib') 64 self.load_module(module_name)
67 self.assertEqual(cm.exception.name, 'importlib') 65 self.assertEqual(cm.exception.name, module_name)
68 66
69 67
70 class InspectLoaderTests(unittest.TestCase): 68 (Frozen_LoaderTests,
69 Source_LoaderTests
70 ) = util.test_both(LoaderTests, machinery=machinery)
71
72
73 @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module' )
74 class InspectLoaderTests:
71 75
72 """Tests for InspectLoader methods for BuiltinImporter.""" 76 """Tests for InspectLoader methods for BuiltinImporter."""
73 77
74 def test_get_code(self): 78 def test_get_code(self):
75 # There is no code object. 79 # There is no code object.
76 result = machinery.BuiltinImporter.get_code(builtin_util.NAME) 80 result = self.machinery.BuiltinImporter.get_code(util.BUILTINS.good_name )
77 self.assertIsNone(result) 81 self.assertIsNone(result)
78 82
79 def test_get_source(self): 83 def test_get_source(self):
80 # There is no source. 84 # There is no source.
81 result = machinery.BuiltinImporter.get_source(builtin_util.NAME) 85 result = self.machinery.BuiltinImporter.get_source(util.BUILTINS.good_na me)
82 self.assertIsNone(result) 86 self.assertIsNone(result)
83 87
84 def test_is_package(self): 88 def test_is_package(self):
85 # Cannot be a package. 89 # Cannot be a package.
86 result = machinery.BuiltinImporter.is_package(builtin_util.NAME) 90 result = self.machinery.BuiltinImporter.is_package(util.BUILTINS.good_na me)
87 self.assertTrue(not result) 91 self.assertFalse(result)
88 92
93 @unittest.skipIf(util.BUILTINS.bad_name is None, 'all modules are built in')
89 def test_not_builtin(self): 94 def test_not_builtin(self):
90 # Modules not built-in should raise ImportError. 95 # Modules not built-in should raise ImportError.
91 for meth_name in ('get_code', 'get_source', 'is_package'): 96 for meth_name in ('get_code', 'get_source', 'is_package'):
92 method = getattr(machinery.BuiltinImporter, meth_name) 97 method = getattr(self.machinery.BuiltinImporter, meth_name)
93 with self.assertRaises(ImportError) as cm: 98 with self.assertRaises(ImportError) as cm:
94 method(builtin_util.BAD_NAME) 99 method(util.BUILTINS.bad_name)
95 100
96 101
97 102 (Frozen_InspectLoaderTests,
98 def test_main(): 103 Source_InspectLoaderTests
99 from test.support import run_unittest 104 ) = util.test_both(InspectLoaderTests, machinery=machinery)
100 run_unittest(LoaderTests, InspectLoaderTests)
101 105
102 106
103 if __name__ == '__main__': 107 if __name__ == '__main__':
104 test_main() 108 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+