Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 1 | from . import util |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 2 | import imp |
| 3 | import importlib |
| 4 | import sys |
| 5 | import unittest |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | class ImportModuleTests(unittest.TestCase): |
| 9 | |
| 10 | """Test importlib.import_module.""" |
| 11 | |
| 12 | def test_module_import(self): |
| 13 | # Test importing a top-level module. |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 14 | with util.mock_modules('top_level') as mock: |
| 15 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 16 | module = importlib.import_module('top_level') |
| 17 | self.assertEqual(module.__name__, 'top_level') |
| 18 | |
| 19 | def test_absolute_package_import(self): |
| 20 | # Test importing a module from a package with an absolute name. |
| 21 | pkg_name = 'pkg' |
| 22 | pkg_long_name = '{0}.__init__'.format(pkg_name) |
| 23 | name = '{0}.mod'.format(pkg_name) |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 24 | with util.mock_modules(pkg_long_name, name) as mock: |
| 25 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 26 | module = importlib.import_module(name) |
| 27 | self.assertEqual(module.__name__, name) |
| 28 | |
Brett Cannon | b5f03c6 | 2009-03-04 01:02:54 +0000 | [diff] [blame] | 29 | def test_shallow_relative_package_import(self): |
Brett Cannon | 2cf1585 | 2010-07-03 22:03:16 +0000 | [diff] [blame] | 30 | # Test importing a module from a package through a relative import. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 31 | pkg_name = 'pkg' |
| 32 | pkg_long_name = '{0}.__init__'.format(pkg_name) |
| 33 | module_name = 'mod' |
| 34 | absolute_name = '{0}.{1}'.format(pkg_name, module_name) |
| 35 | relative_name = '.{0}'.format(module_name) |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 36 | with util.mock_modules(pkg_long_name, absolute_name) as mock: |
| 37 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 38 | importlib.import_module(pkg_name) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 39 | module = importlib.import_module(relative_name, pkg_name) |
| 40 | self.assertEqual(module.__name__, absolute_name) |
| 41 | |
Brett Cannon | b5f03c6 | 2009-03-04 01:02:54 +0000 | [diff] [blame] | 42 | def test_deep_relative_package_import(self): |
| 43 | modules = ['a.__init__', 'a.b.__init__', 'a.c'] |
| 44 | with util.mock_modules(*modules) as mock: |
| 45 | with util.import_state(meta_path=[mock]): |
| 46 | importlib.import_module('a') |
| 47 | importlib.import_module('a.b') |
| 48 | module = importlib.import_module('..c', 'a.b') |
| 49 | self.assertEqual(module.__name__, 'a.c') |
| 50 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 51 | def test_absolute_import_with_package(self): |
| 52 | # Test importing a module from a package with an absolute name with |
| 53 | # the 'package' argument given. |
| 54 | pkg_name = 'pkg' |
| 55 | pkg_long_name = '{0}.__init__'.format(pkg_name) |
| 56 | name = '{0}.mod'.format(pkg_name) |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 57 | with util.mock_modules(pkg_long_name, name) as mock: |
| 58 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 59 | importlib.import_module(pkg_name) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 60 | module = importlib.import_module(name, pkg_name) |
| 61 | self.assertEqual(module.__name__, name) |
| 62 | |
| 63 | def test_relative_import_wo_package(self): |
| 64 | # Relative imports cannot happen without the 'package' argument being |
| 65 | # set. |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 66 | with self.assertRaises(TypeError): |
| 67 | importlib.import_module('.support') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 68 | |
| 69 | |
Meador Inge | 416f12d | 2011-12-14 22:23:46 -0600 | [diff] [blame] | 70 | def test_loaded_once(self): |
| 71 | # Issue #13591: Modules should only be loaded once when |
| 72 | # initializing the parent package attempts to import the |
| 73 | # module currently being imported. |
| 74 | b_load_count = 0 |
| 75 | def load_a(): |
| 76 | importlib.import_module('a.b') |
| 77 | def load_b(): |
| 78 | nonlocal b_load_count |
| 79 | b_load_count += 1 |
| 80 | code = {'a': load_a, 'a.b': load_b} |
| 81 | modules = ['a.__init__', 'a.b'] |
| 82 | with util.mock_modules(*modules, module_code=code) as mock: |
| 83 | with util.import_state(meta_path=[mock]): |
| 84 | importlib.import_module('a.b') |
| 85 | self.assertEqual(b_load_count, 1) |
| 86 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 87 | def test_main(): |
| 88 | from test.support import run_unittest |
| 89 | run_unittest(ImportModuleTests) |
| 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | test_main() |