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

Side by Side Diff: Lib/test/test_importlib/test_api.py

Issue 18864: Implementation for PEP 451 (importlib.machinery.ModuleSpec)
Patch Set: Created 6 years, 1 month 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:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_importlib/import_/test_meta_path.py ('k') | Lib/test/test_importlib/test_spec.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 from . import util 1 from . import util
2 2
3 import importlib 3 import importlib
4 from importlib import _bootstrap 4 from importlib import _bootstrap
5 from importlib import machinery 5 from importlib import machinery
6 import sys 6 import sys
7 from test import support 7 from test import support
8 import types 8 import types
9 import unittest 9 import unittest
10 10
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 @staticmethod 95 @staticmethod
96 def find_module(name, path=None): return name, path 96 def find_module(name, path=None): return name, path
97 97
98 def test_sys_modules(self): 98 def test_sys_modules(self):
99 # If a module with __loader__ is in sys.modules, then return it. 99 # If a module with __loader__ is in sys.modules, then return it.
100 name = 'some_mod' 100 name = 'some_mod'
101 with util.uncache(name): 101 with util.uncache(name):
102 module = types.ModuleType(name) 102 module = types.ModuleType(name)
103 loader = 'a loader!' 103 loader = 'a loader!'
104 module.__loader__ = loader 104 module.__loader__ = loader
105 module.__spec__ = machinery.ModuleSpec(name, loader)
105 sys.modules[name] = module 106 sys.modules[name] = module
106 found = importlib.find_loader(name) 107 found = importlib.find_loader(name)
107 self.assertEqual(loader, found) 108 self.assertEqual(loader, found)
109
110 def test_sys_modules_without___loader__(self):
111 # If a module with __spec__ is in sys.modules, then return it.
112 name = 'some_mod'
113 with util.uncache(name):
114 module = types.ModuleType(name)
115 loader = 'a loader!'
116 module.__spec__ = machinery.ModuleSpec(name, loader)
117 sys.modules[name] = module
118 found = importlib.find_loader(name)
119 self.assertEqual(loader, found)
120
121 def test_sys_modules_spec_is_None(self):
122 # If sys.modules[name].__loader__ is None, raise ValueError.
123 name = 'some_mod'
124 with util.uncache(name):
125 module = types.ModuleType(name)
126 module.__spec__ = None
127 sys.modules[name] = module
128 with self.assertRaises(ValueError):
129 importlib.find_loader(name)
108 130
109 def test_sys_modules_loader_is_None(self): 131 def test_sys_modules_loader_is_None(self):
110 # If sys.modules[name].__loader__ is None, raise ValueError. 132 # If sys.modules[name].__loader__ is None, raise ValueError.
111 name = 'some_mod' 133 name = 'some_mod'
112 with util.uncache(name): 134 with util.uncache(name):
113 module = types.ModuleType(name) 135 module = types.ModuleType(name)
114 module.__loader__ = None 136 module.__spec__ = machinery.ModuleSpec(name, None)
115 sys.modules[name] = module 137 sys.modules[name] = module
116 with self.assertRaises(ValueError): 138 with self.assertRaises(ValueError):
117 importlib.find_loader(name) 139 importlib.find_loader(name)
140
141 def test_sys_modules_spec_is_not_set(self):
142 # Should raise ValueError
143 # Issue #17099
144 name = 'some_mod'
145 with util.uncache(name):
146 module = types.ModuleType(name)
147 try:
148 del module.__spec__
149 except AttributeError:
150 pass
151 sys.modules[name] = module
152 with self.assertRaises(ValueError):
153 importlib.find_loader(name)
118 154
119 def test_sys_modules_loader_is_not_set(self): 155 def test_sys_modules_loader_is_not_set(self):
120 # Should raise ValueError 156 # Should raise ValueError
121 # Issue #17099 157 # Issue #17099
122 name = 'some_mod' 158 name = 'some_mod'
123 with util.uncache(name): 159 with util.uncache(name):
124 module = types.ModuleType(name) 160 module = types.ModuleType(name)
161 module.__spec__ = machinery.ModuleSpec(name, None)
125 try: 162 try:
126 del module.__loader__ 163 del module.__spec__.loader
127 except AttributeError: 164 except AttributeError:
128 pass 165 pass
129 sys.modules[name] = module 166 sys.modules[name] = module
130 with self.assertRaises(ValueError): 167 with self.assertRaises(ValueError):
131 importlib.find_loader(name) 168 importlib.find_loader(name)
132 169
133 def test_success(self): 170 def test_success(self):
134 # Return the loader found on sys.meta_path. 171 # Return the loader found on sys.meta_path.
135 name = 'some_mod' 172 name = 'some_mod'
136 with util.uncache(name): 173 with util.uncache(name):
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 self.assertTrue(hasattr(module, '__loader__'), 265 self.assertTrue(hasattr(module, '__loader__'),
229 '{!r} lacks a __loader__ attribute'.format(name) ) 266 '{!r} lacks a __loader__ attribute'.format(name) )
230 if importlib.machinery.BuiltinImporter.find_module(name): 267 if importlib.machinery.BuiltinImporter.find_module(name):
231 self.assertIsNot(module.__loader__, None) 268 self.assertIsNot(module.__loader__, None)
232 elif importlib.machinery.FrozenImporter.find_module(name): 269 elif importlib.machinery.FrozenImporter.find_module(name):
233 self.assertIsNot(module.__loader__, None) 270 self.assertIsNot(module.__loader__, None)
234 271
235 272
236 if __name__ == '__main__': 273 if __name__ == '__main__':
237 unittest.main() 274 unittest.main()
OLDNEW
« no previous file with comments | « Lib/test/test_importlib/import_/test_meta_path.py ('k') | Lib/test/test_importlib/test_spec.py » ('j') | no next file with comments »

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