blob: b903e8e61126076c94934adf397a99f0ecfcfb9c [file] [log] [blame]
Brett Cannon23cbd8a2009-01-18 00:24:28 +00001"""Test that the semantics relating to the 'fromlist' argument are correct."""
Brett Cannonbcb26c52009-02-01 04:00:05 +00002from .. import util
Brett Cannond720b362009-02-01 04:28:04 +00003from . import util as import_util
Brett Cannon23cbd8a2009-01-18 00:24:28 +00004import unittest
5
6class 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 Cannonbcb26c52009-02-01 04:00:05 +000019 with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
20 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000021 module = import_util.import_('pkg.module')
Ezio Melottib3aedd42010-11-20 19:04:17 +000022 self.assertEqual(module.__name__, 'pkg')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000023
24 def test_return_from_from_import(self):
25 # [from return]
Brett Cannonbcb26c52009-02-01 04:00:05 +000026 with util.mock_modules('pkg.__init__', 'pkg.module')as importer:
27 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000028 module = import_util.import_('pkg.module', fromlist=['attr'])
Ezio Melottib3aedd42010-11-20 19:04:17 +000029 self.assertEqual(module.__name__, 'pkg.module')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000030
31
32class 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 Cannonbcb26c52009-02-01 04:00:05 +000051 with util.mock_modules('module') as importer:
52 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000053 module = import_util.import_('module', fromlist=['attr'])
Ezio Melottib3aedd42010-11-20 19:04:17 +000054 self.assertEqual(module.__name__, 'module')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000055
56 def test_unexistent_object(self):
57 # [bad object]
Brett Cannonbcb26c52009-02-01 04:00:05 +000058 with util.mock_modules('module') as importer:
59 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000060 module = import_util.import_('module', fromlist=['non_existent'])
Ezio Melottib3aedd42010-11-20 19:04:17 +000061 self.assertEqual(module.__name__, 'module')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000062 self.assertTrue(not hasattr(module, 'non_existent'))
Brett Cannon23cbd8a2009-01-18 00:24:28 +000063
64 def test_module_from_package(self):
65 # [module]
Brett Cannonbcb26c52009-02-01 04:00:05 +000066 with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
67 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000068 module = import_util.import_('pkg', fromlist=['module'])
Ezio Melottib3aedd42010-11-20 19:04:17 +000069 self.assertEqual(module.__name__, 'pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000070 self.assertTrue(hasattr(module, 'module'))
Ezio Melottib3aedd42010-11-20 19:04:17 +000071 self.assertEqual(module.module.__name__, 'pkg.module')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000072
73 def test_no_module_from_package(self):
74 # [no module]
Brett Cannonbcb26c52009-02-01 04:00:05 +000075 with util.mock_modules('pkg.__init__') as importer:
76 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000077 module = import_util.import_('pkg', fromlist='non_existent')
Ezio Melottib3aedd42010-11-20 19:04:17 +000078 self.assertEqual(module.__name__, 'pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000079 self.assertTrue(not hasattr(module, 'non_existent'))
Brett Cannon23cbd8a2009-01-18 00:24:28 +000080
81 def test_empty_string(self):
Brett Cannonbcb26c52009-02-01 04:00:05 +000082 with util.mock_modules('pkg.__init__', 'pkg.mod') as importer:
83 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000084 module = import_util.import_('pkg.mod', fromlist=[''])
Ezio Melottib3aedd42010-11-20 19:04:17 +000085 self.assertEqual(module.__name__, 'pkg.mod')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000086
Brett Cannon9e0e1a62009-08-30 18:28:46 +000087 def basic_star_test(self, fromlist=['*']):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000088 # [using *]
Brett Cannonbcb26c52009-02-01 04:00:05 +000089 with util.mock_modules('pkg.__init__', 'pkg.module') as mock:
90 with util.import_state(meta_path=[mock]):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000091 mock['pkg'].__all__ = ['module']
Brett Cannon9e0e1a62009-08-30 18:28:46 +000092 module = import_util.import_('pkg', fromlist=fromlist)
Ezio Melottib3aedd42010-11-20 19:04:17 +000093 self.assertEqual(module.__name__, 'pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000094 self.assertTrue(hasattr(module, 'module'))
Brett Cannon23cbd8a2009-01-18 00:24:28 +000095 self.assertEqual(module.module.__name__, 'pkg.module')
96
Brett Cannon9e0e1a62009-08-30 18:28:46 +000097 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 Cannon23cbd8a2009-01-18 00:24:28 +0000104 def test_star_with_others(self):
105 # [using * with others]
Brett Cannonbcb26c52009-02-01 04:00:05 +0000106 context = util.mock_modules('pkg.__init__', 'pkg.module1', 'pkg.module2')
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000107 with context as mock:
Brett Cannonbcb26c52009-02-01 04:00:05 +0000108 with util.import_state(meta_path=[mock]):
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000109 mock['pkg'].__all__ = ['module1']
Brett Cannond720b362009-02-01 04:28:04 +0000110 module = import_util.import_('pkg', fromlist=['module2', '*'])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000111 self.assertEqual(module.__name__, 'pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000112 self.assertTrue(hasattr(module, 'module1'))
113 self.assertTrue(hasattr(module, 'module2'))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000114 self.assertEqual(module.module1.__name__, 'pkg.module1')
115 self.assertEqual(module.module2.__name__, 'pkg.module2')
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000116
117
118def test_main():
119 from test.support import run_unittest
120 run_unittest(ReturnValue, HandlingFromlist)
121
122if __name__ == '__main__':
123 test_main()