blob: 4ff5f5e825423e19e1141639d1eb6ad0fe735b3d [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 Cannone0d88a12012-04-25 20:54:04 -04004import imp
Brett Cannon23cbd8a2009-01-18 00:24:28 +00005import unittest
6
7class 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 Cannonbcb26c52009-02-01 04:00:05 +000020 with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
21 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000022 module = import_util.import_('pkg.module')
Ezio Melottib3aedd42010-11-20 19:04:17 +000023 self.assertEqual(module.__name__, 'pkg')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000024
25 def test_return_from_from_import(self):
26 # [from return]
Brett Cannonbcb26c52009-02-01 04:00:05 +000027 with util.mock_modules('pkg.__init__', 'pkg.module')as importer:
28 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000029 module = import_util.import_('pkg.module', fromlist=['attr'])
Ezio Melottib3aedd42010-11-20 19:04:17 +000030 self.assertEqual(module.__name__, 'pkg.module')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000031
32
33class 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 Cannonbcb26c52009-02-01 04:00:05 +000052 with util.mock_modules('module') as importer:
53 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000054 module = import_util.import_('module', fromlist=['attr'])
Ezio Melottib3aedd42010-11-20 19:04:17 +000055 self.assertEqual(module.__name__, 'module')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000056
57 def test_unexistent_object(self):
58 # [bad object]
Brett Cannonbcb26c52009-02-01 04:00:05 +000059 with util.mock_modules('module') as importer:
60 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000061 module = import_util.import_('module', fromlist=['non_existent'])
Ezio Melottib3aedd42010-11-20 19:04:17 +000062 self.assertEqual(module.__name__, 'module')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000063 self.assertTrue(not hasattr(module, 'non_existent'))
Brett Cannon23cbd8a2009-01-18 00:24:28 +000064
65 def test_module_from_package(self):
66 # [module]
Brett Cannonbcb26c52009-02-01 04:00:05 +000067 with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
68 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000069 module = import_util.import_('pkg', fromlist=['module'])
Ezio Melottib3aedd42010-11-20 19:04:17 +000070 self.assertEqual(module.__name__, 'pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000071 self.assertTrue(hasattr(module, 'module'))
Ezio Melottib3aedd42010-11-20 19:04:17 +000072 self.assertEqual(module.module.__name__, 'pkg.module')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000073
74 def test_no_module_from_package(self):
75 # [no module]
Brett Cannonbcb26c52009-02-01 04:00:05 +000076 with util.mock_modules('pkg.__init__') as importer:
Brett Cannone0d88a12012-04-25 20:54:04 -040077 with util.import_state(meta_path=[importer],
78 path_hooks=[imp.NullImporter]):
Brett Cannond720b362009-02-01 04:28:04 +000079 module = import_util.import_('pkg', fromlist='non_existent')
Ezio Melottib3aedd42010-11-20 19:04:17 +000080 self.assertEqual(module.__name__, 'pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000081 self.assertTrue(not hasattr(module, 'non_existent'))
Brett Cannon23cbd8a2009-01-18 00:24:28 +000082
83 def test_empty_string(self):
Brett Cannonbcb26c52009-02-01 04:00:05 +000084 with util.mock_modules('pkg.__init__', 'pkg.mod') as importer:
85 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000086 module = import_util.import_('pkg.mod', fromlist=[''])
Ezio Melottib3aedd42010-11-20 19:04:17 +000087 self.assertEqual(module.__name__, 'pkg.mod')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000088
Brett Cannon9e0e1a62009-08-30 18:28:46 +000089 def basic_star_test(self, fromlist=['*']):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000090 # [using *]
Brett Cannonbcb26c52009-02-01 04:00:05 +000091 with util.mock_modules('pkg.__init__', 'pkg.module') as mock:
92 with util.import_state(meta_path=[mock]):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000093 mock['pkg'].__all__ = ['module']
Brett Cannon9e0e1a62009-08-30 18:28:46 +000094 module = import_util.import_('pkg', fromlist=fromlist)
Ezio Melottib3aedd42010-11-20 19:04:17 +000095 self.assertEqual(module.__name__, 'pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000096 self.assertTrue(hasattr(module, 'module'))
Brett Cannon23cbd8a2009-01-18 00:24:28 +000097 self.assertEqual(module.module.__name__, 'pkg.module')
98
Brett Cannon9e0e1a62009-08-30 18:28:46 +000099 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 Cannon23cbd8a2009-01-18 00:24:28 +0000106 def test_star_with_others(self):
107 # [using * with others]
Brett Cannonbcb26c52009-02-01 04:00:05 +0000108 context = util.mock_modules('pkg.__init__', 'pkg.module1', 'pkg.module2')
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000109 with context as mock:
Brett Cannonbcb26c52009-02-01 04:00:05 +0000110 with util.import_state(meta_path=[mock]):
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000111 mock['pkg'].__all__ = ['module1']
Brett Cannond720b362009-02-01 04:28:04 +0000112 module = import_util.import_('pkg', fromlist=['module2', '*'])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000113 self.assertEqual(module.__name__, 'pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000114 self.assertTrue(hasattr(module, 'module1'))
115 self.assertTrue(hasattr(module, 'module2'))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000116 self.assertEqual(module.module1.__name__, 'pkg.module1')
117 self.assertEqual(module.module2.__name__, 'pkg.module2')
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000118
119
120def test_main():
121 from test.support import run_unittest
122 run_unittest(ReturnValue, HandlingFromlist)
123
124if __name__ == '__main__':
125 test_main()