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