Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 1 | """Test that the semantics relating to the 'fromlist' argument are correct.""" |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 2 | from .. import util |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 3 | from . import util as import_util |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 4 | import imp |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | |
| 7 | class ReturnValue(unittest.TestCase): |
| 8 | |
| 9 | """The use of fromlist influences what import returns. |
| 10 | |
| 11 | If direct ``import ...`` statement is used, the root module or package is |
| 12 | returned [import return]. But if fromlist is set, then the specified module |
| 13 | is actually returned (whether it is a relative import or not) |
| 14 | [from return]. |
| 15 | |
| 16 | """ |
| 17 | |
| 18 | def test_return_from_import(self): |
| 19 | # [import return] |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 20 | with util.mock_modules('pkg.__init__', 'pkg.module') as importer: |
| 21 | with util.import_state(meta_path=[importer]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 22 | module = import_util.import_('pkg.module') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 23 | self.assertEqual(module.__name__, 'pkg') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 24 | |
| 25 | def test_return_from_from_import(self): |
| 26 | # [from return] |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 27 | with util.mock_modules('pkg.__init__', 'pkg.module')as importer: |
| 28 | with util.import_state(meta_path=[importer]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 29 | module = import_util.import_('pkg.module', fromlist=['attr']) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 30 | self.assertEqual(module.__name__, 'pkg.module') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | class HandlingFromlist(unittest.TestCase): |
| 34 | |
| 35 | """Using fromlist triggers different actions based on what is being asked |
| 36 | of it. |
| 37 | |
| 38 | If fromlist specifies an object on a module, nothing special happens |
| 39 | [object case]. This is even true if the object does not exist [bad object]. |
| 40 | |
| 41 | If a package is being imported, then what is listed in fromlist may be |
| 42 | treated as a module to be imported [module]. But once again, even if |
| 43 | something in fromlist does not exist as a module, no error is thrown |
| 44 | [no module]. And this extends to what is contained in __all__ when '*' is |
| 45 | imported [using *]. And '*' does not need to be the only name in the |
| 46 | fromlist [using * with others]. |
| 47 | |
| 48 | """ |
| 49 | |
| 50 | def test_object(self): |
| 51 | # [object case] |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 52 | with util.mock_modules('module') as importer: |
| 53 | with util.import_state(meta_path=[importer]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 54 | module = import_util.import_('module', fromlist=['attr']) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 55 | self.assertEqual(module.__name__, 'module') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 56 | |
| 57 | def test_unexistent_object(self): |
| 58 | # [bad object] |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 59 | with util.mock_modules('module') as importer: |
| 60 | with util.import_state(meta_path=[importer]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 61 | module = import_util.import_('module', fromlist=['non_existent']) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 62 | self.assertEqual(module.__name__, 'module') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 63 | self.assertTrue(not hasattr(module, 'non_existent')) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 64 | |
| 65 | def test_module_from_package(self): |
| 66 | # [module] |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 67 | with util.mock_modules('pkg.__init__', 'pkg.module') as importer: |
| 68 | with util.import_state(meta_path=[importer]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 69 | module = import_util.import_('pkg', fromlist=['module']) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 70 | self.assertEqual(module.__name__, 'pkg') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 71 | self.assertTrue(hasattr(module, 'module')) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 72 | self.assertEqual(module.module.__name__, 'pkg.module') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 73 | |
| 74 | def test_no_module_from_package(self): |
| 75 | # [no module] |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 76 | with util.mock_modules('pkg.__init__') as importer: |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 77 | with util.import_state(meta_path=[importer], |
| 78 | path_hooks=[imp.NullImporter]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 79 | module = import_util.import_('pkg', fromlist='non_existent') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 80 | self.assertEqual(module.__name__, 'pkg') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 81 | self.assertTrue(not hasattr(module, 'non_existent')) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 82 | |
| 83 | def test_empty_string(self): |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 84 | with util.mock_modules('pkg.__init__', 'pkg.mod') as importer: |
| 85 | with util.import_state(meta_path=[importer]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 86 | module = import_util.import_('pkg.mod', fromlist=['']) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 87 | self.assertEqual(module.__name__, 'pkg.mod') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 88 | |
Brett Cannon | 9e0e1a6 | 2009-08-30 18:28:46 +0000 | [diff] [blame] | 89 | def basic_star_test(self, fromlist=['*']): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 90 | # [using *] |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 91 | with util.mock_modules('pkg.__init__', 'pkg.module') as mock: |
| 92 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 93 | mock['pkg'].__all__ = ['module'] |
Brett Cannon | 9e0e1a6 | 2009-08-30 18:28:46 +0000 | [diff] [blame] | 94 | module = import_util.import_('pkg', fromlist=fromlist) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 95 | self.assertEqual(module.__name__, 'pkg') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 96 | self.assertTrue(hasattr(module, 'module')) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 97 | self.assertEqual(module.module.__name__, 'pkg.module') |
| 98 | |
Brett Cannon | 9e0e1a6 | 2009-08-30 18:28:46 +0000 | [diff] [blame] | 99 | def test_using_star(self): |
| 100 | # [using *] |
| 101 | self.basic_star_test() |
| 102 | |
| 103 | def test_fromlist_as_tuple(self): |
| 104 | self.basic_star_test(('*',)) |
| 105 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 106 | def test_star_with_others(self): |
| 107 | # [using * with others] |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 108 | context = util.mock_modules('pkg.__init__', 'pkg.module1', 'pkg.module2') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 109 | with context as mock: |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 110 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 111 | mock['pkg'].__all__ = ['module1'] |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 112 | module = import_util.import_('pkg', fromlist=['module2', '*']) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 113 | self.assertEqual(module.__name__, 'pkg') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 114 | self.assertTrue(hasattr(module, 'module1')) |
| 115 | self.assertTrue(hasattr(module, 'module2')) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 116 | self.assertEqual(module.module1.__name__, 'pkg.module1') |
| 117 | self.assertEqual(module.module2.__name__, 'pkg.module2') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | def test_main(): |
| 121 | from test.support import run_unittest |
| 122 | run_unittest(ReturnValue, HandlingFromlist) |
| 123 | |
| 124 | if __name__ == '__main__': |
| 125 | test_main() |