Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 1 | from importlib import util |
| 2 | from . import util as test_util |
| 3 | import imp |
| 4 | import sys |
| 5 | import types |
| 6 | import unittest |
| 7 | |
| 8 | |
| 9 | class ModuleForLoaderTests(unittest.TestCase): |
| 10 | |
| 11 | """Tests for importlib.util.module_for_loader.""" |
| 12 | |
| 13 | def return_module(self, name): |
| 14 | fxn = util.module_for_loader(lambda self, module: module) |
| 15 | return fxn(self, name) |
| 16 | |
| 17 | def raise_exception(self, name): |
| 18 | def to_wrap(self, module): |
| 19 | raise ImportError |
| 20 | fxn = util.module_for_loader(to_wrap) |
| 21 | try: |
| 22 | fxn(self, name) |
| 23 | except ImportError: |
| 24 | pass |
| 25 | |
| 26 | def test_new_module(self): |
| 27 | # Test that when no module exists in sys.modules a new module is |
| 28 | # created. |
| 29 | module_name = 'a.b.c' |
| 30 | with test_util.uncache(module_name): |
| 31 | module = self.return_module(module_name) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 32 | self.assertTrue(module_name in sys.modules) |
| 33 | self.assertTrue(isinstance(module, types.ModuleType)) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 34 | self.assertEqual(module.__name__, module_name) |
| 35 | |
| 36 | def test_reload(self): |
| 37 | # Test that a module is reused if already in sys.modules. |
| 38 | name = 'a.b.c' |
| 39 | module = imp.new_module('a.b.c') |
| 40 | with test_util.uncache(name): |
| 41 | sys.modules[name] = module |
| 42 | returned_module = self.return_module(name) |
Brett Cannon | c56b094 | 2010-06-21 02:49:35 +0000 | [diff] [blame] | 43 | self.assertIs(returned_module, sys.modules[name]) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 44 | |
| 45 | def test_new_module_failure(self): |
| 46 | # Test that a module is removed from sys.modules if added but an |
| 47 | # exception is raised. |
| 48 | name = 'a.b.c' |
| 49 | with test_util.uncache(name): |
| 50 | self.raise_exception(name) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 51 | self.assertTrue(name not in sys.modules) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 52 | |
| 53 | def test_reload_failure(self): |
| 54 | # Test that a failure on reload leaves the module in-place. |
| 55 | name = 'a.b.c' |
| 56 | module = imp.new_module(name) |
| 57 | with test_util.uncache(name): |
| 58 | sys.modules[name] = module |
| 59 | self.raise_exception(name) |
Brett Cannon | c56b094 | 2010-06-21 02:49:35 +0000 | [diff] [blame] | 60 | self.assertIs(module, sys.modules[name]) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 61 | |
Meador Inge | 96ff084 | 2011-12-14 22:53:13 -0600 | [diff] [blame] | 62 | def test_decorator_attrs(self): |
| 63 | def fxn(self, module): pass |
| 64 | wrapped = util.module_for_loader(fxn) |
| 65 | self.assertEqual(wrapped.__name__, fxn.__name__) |
| 66 | self.assertEqual(wrapped.__qualname__, fxn.__qualname__) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 67 | |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame^] | 68 | def test_false_module(self): |
| 69 | # If for some odd reason a module is considered false, still return it |
| 70 | # from sys.modules. |
| 71 | class FalseModule(types.ModuleType): |
| 72 | def __bool__(self): return False |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 73 | |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame^] | 74 | name = 'mod' |
| 75 | module = FalseModule(name) |
| 76 | with test_util.uncache(name): |
| 77 | self.assertFalse(module) |
| 78 | sys.modules[name] = module |
| 79 | given = self.return_module(name) |
| 80 | self.assertTrue(given is module) |
| 81 | |
| 82 | |
| 83 | class SetPackageTests(unittest.TestCase): |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 84 | |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 85 | """Tests for importlib.util.set_package.""" |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 86 | |
| 87 | def verify(self, module, expect): |
| 88 | """Verify the module has the expected value for __package__ after |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 89 | passing through set_package.""" |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 90 | fxn = lambda: module |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 91 | wrapped = util.set_package(fxn) |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 92 | wrapped() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 93 | self.assertTrue(hasattr(module, '__package__')) |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 94 | self.assertEqual(expect, module.__package__) |
| 95 | |
| 96 | def test_top_level(self): |
| 97 | # __package__ should be set to the empty string if a top-level module. |
| 98 | # Implicitly tests when package is set to None. |
| 99 | module = imp.new_module('module') |
| 100 | module.__package__ = None |
| 101 | self.verify(module, '') |
| 102 | |
| 103 | def test_package(self): |
| 104 | # Test setting __package__ for a package. |
| 105 | module = imp.new_module('pkg') |
| 106 | module.__path__ = ['<path>'] |
| 107 | module.__package__ = None |
| 108 | self.verify(module, 'pkg') |
| 109 | |
| 110 | def test_submodule(self): |
| 111 | # Test __package__ for a module in a package. |
| 112 | module = imp.new_module('pkg.mod') |
| 113 | module.__package__ = None |
| 114 | self.verify(module, 'pkg') |
| 115 | |
| 116 | def test_setting_if_missing(self): |
| 117 | # __package__ should be set if it is missing. |
| 118 | module = imp.new_module('mod') |
| 119 | if hasattr(module, '__package__'): |
| 120 | delattr(module, '__package__') |
| 121 | self.verify(module, '') |
| 122 | |
| 123 | def test_leaving_alone(self): |
| 124 | # If __package__ is set and not None then leave it alone. |
| 125 | for value in (True, False): |
| 126 | module = imp.new_module('mod') |
| 127 | module.__package__ = value |
| 128 | self.verify(module, value) |
| 129 | |
Meador Inge | 96ff084 | 2011-12-14 22:53:13 -0600 | [diff] [blame] | 130 | def test_decorator_attrs(self): |
| 131 | def fxn(module): pass |
| 132 | wrapped = util.set_package(fxn) |
| 133 | self.assertEqual(wrapped.__name__, fxn.__name__) |
| 134 | self.assertEqual(wrapped.__qualname__, fxn.__qualname__) |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 135 | |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 136 | def test_main(): |
| 137 | from test import support |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 138 | support.run_unittest(ModuleForLoaderTests, SetPackageTests) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 139 | |
| 140 | |
| 141 | if __name__ == '__main__': |
| 142 | test_main() |