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

Side by Side Diff: Lib/test/test_importlib/test_util.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/test_spec.py ('k') | Lib/test/test_import.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 importlib import util 1 from importlib import util
2 from . import util as test_util 2 from . import util as test_util
3 3
4 import os 4 import os
5 import sys 5 import sys
6 from test import support 6 from test import support
7 import types 7 import types
8 import unittest 8 import unittest
9 import warnings 9 import warnings
10 10
(...skipping 12 matching lines...) Expand all
23 assert source_bytes != source.encode('utf-8') 23 assert source_bytes != source.encode('utf-8')
24 self.assertEqual(util.decode_source(source_bytes), source) 24 self.assertEqual(util.decode_source(source_bytes), source)
25 25
26 def test_universal_newlines(self): 26 def test_universal_newlines(self):
27 source = '\r\n'.join([self.source, self.source]) 27 source = '\r\n'.join([self.source, self.source])
28 source_bytes = source.encode('utf-8') 28 source_bytes = source.encode('utf-8')
29 self.assertEqual(util.decode_source(source_bytes), 29 self.assertEqual(util.decode_source(source_bytes),
30 '\n'.join([self.source, self.source])) 30 '\n'.join([self.source, self.source]))
31 31
32 32
33 class ModuleToLoadTests(unittest.TestCase): 33 #class ModuleToLoadTests(unittest.TestCase):
34 34 #
35 module_name = 'ModuleManagerTest_module' 35 # module_name = 'ModuleManagerTest_module'
36 36 #
37 def setUp(self): 37 # def setUp(self):
38 support.unload(self.module_name) 38 # support.unload(self.module_name)
39 self.addCleanup(support.unload, self.module_name) 39 # self.addCleanup(support.unload, self.module_name)
40 40 #
41 def test_new_module(self): 41 # def test_new_module(self):
42 # Test a new module is created, inserted into sys.modules, has 42 # # Test a new module is created, inserted into sys.modules, has
43 # __initializing__ set to True after entering the context manager, 43 # # __initializing__ set to True after entering the context manager,
44 # and __initializing__ set to False after exiting. 44 # # and __initializing__ set to False after exiting.
45 with util.module_to_load(self.module_name) as module: 45 # with util.module_to_load(self.module_name) as module:
46 self.assertIn(self.module_name, sys.modules) 46 # self.assertIn(self.module_name, sys.modules)
47 self.assertIs(sys.modules[self.module_name], module) 47 # self.assertIs(sys.modules[self.module_name], module)
48 self.assertTrue(module.__initializing__) 48 # self.assertTrue(module.__initializing__)
49 self.assertFalse(module.__initializing__) 49 # self.assertFalse(module.__initializing__)
50 50 #
51 def test_new_module_failed(self): 51 # def test_new_module_failed(self):
52 # Test the module is removed from sys.modules. 52 # # Test the module is removed from sys.modules.
53 try: 53 # try:
54 with util.module_to_load(self.module_name) as module: 54 # with util.module_to_load(self.module_name) as module:
55 self.assertIn(self.module_name, sys.modules) 55 # self.assertIn(self.module_name, sys.modules)
56 raise exception 56 # raise exception
57 except Exception: 57 # except Exception:
58 self.assertNotIn(self.module_name, sys.modules) 58 # self.assertNotIn(self.module_name, sys.modules)
59 else: 59 # else:
60 self.fail('importlib.util.module_to_load swallowed an exception') 60 # self.fail('importlib.util.module_to_load swallowed an exception')
61 61 #
62 def test_reload(self): 62 # def test_reload(self):
63 # Test that the same module is in sys.modules. 63 # # Test that the same module is in sys.modules.
64 created_module = types.ModuleType(self.module_name) 64 # created_module = types.ModuleType(self.module_name)
65 sys.modules[self.module_name] = created_module 65 # sys.modules[self.module_name] = created_module
66 with util.module_to_load(self.module_name) as module: 66 # with util.module_to_load(self.module_name) as module:
67 self.assertIs(module, created_module) 67 # self.assertIs(module, created_module)
68 68 #
69 def test_reload_failed(self): 69 # def test_reload_failed(self):
70 # Test that the module was left in sys.modules. 70 # # Test that the module was left in sys.modules.
71 created_module = types.ModuleType(self.module_name) 71 # created_module = types.ModuleType(self.module_name)
72 sys.modules[self.module_name] = created_module 72 # sys.modules[self.module_name] = created_module
73 try: 73 # try:
74 with util.module_to_load(self.module_name) as module: 74 # with util.module_to_load(self.module_name) as module:
75 raise Exception 75 # raise Exception
76 except Exception: 76 # except Exception:
77 self.assertIn(self.module_name, sys.modules) 77 # self.assertIn(self.module_name, sys.modules)
78 else: 78 # else:
79 self.fail('importlib.util.module_to_load swallowed an exception') 79 # self.fail('importlib.util.module_to_load swallowed an exception')
80 80 #
81 def test_reset_name(self): 81 # def test_reset_name(self):
82 # If reset_name is true then module.__name__ = name, else leave it be. 82 # # If reset_name is true then module.__name__ = name, else leave it be.
83 odd_name = 'not your typical name' 83 # odd_name = 'not your typical name'
84 created_module = types.ModuleType(self.module_name) 84 # created_module = types.ModuleType(self.module_name)
85 created_module.__name__ = odd_name 85 # created_module.__name__ = odd_name
86 sys.modules[self.module_name] = created_module 86 # sys.modules[self.module_name] = created_module
87 with util.module_to_load(self.module_name) as module: 87 # with util.module_to_load(self.module_name) as module:
88 self.assertEqual(module.__name__, self.module_name) 88 # self.assertEqual(module.__name__, self.module_name)
89 created_module.__name__ = odd_name 89 # created_module.__name__ = odd_name
90 with util.module_to_load(self.module_name, reset_name=False) as module: 90 # with util.module_to_load(self.module_name, reset_name=False) as module:
91 self.assertEqual(module.__name__, odd_name) 91 # self.assertEqual(module.__name__, odd_name)
92 92
93 93
94 class ModuleForLoaderTests(unittest.TestCase): 94 class ModuleForLoaderTests(unittest.TestCase):
95 95
96 """Tests for importlib.util.module_for_loader.""" 96 """Tests for importlib.util.module_for_loader."""
97 97
98 @staticmethod 98 @staticmethod
99 def module_for_loader(func): 99 def module_for_loader(func):
100 with warnings.catch_warnings(): 100 with warnings.catch_warnings():
101 warnings.simplefilter('ignore', PendingDeprecationWarning) 101 warnings.simplefilter('ignore', PendingDeprecationWarning)
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 456
457 def test_source_from_cache_no__pycache__(self): 457 def test_source_from_cache_no__pycache__(self):
458 # Another problem with the path -> ValueError 458 # Another problem with the path -> ValueError
459 self.assertRaises( 459 self.assertRaises(
460 ValueError, util.source_from_cache, 460 ValueError, util.source_from_cache,
461 '/foo/bar/foo.cpython-32.foo.pyc') 461 '/foo/bar/foo.cpython-32.foo.pyc')
462 462
463 463
464 if __name__ == '__main__': 464 if __name__ == '__main__':
465 unittest.main() 465 unittest.main()
OLDNEW
« no previous file with comments | « Lib/test/test_importlib/test_spec.py ('k') | Lib/test/test_import.py » ('j') | no next file with comments »

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