blob: 3b130c9a13c5f00c0f436c99ea424ca180bd8608 [file] [log] [blame]
Brett Cannonbcb26c52009-02-01 04:00:05 +00001from .. import util
Brett Cannond720b362009-02-01 04:28:04 +00002from . import util as import_util
Brett Cannon23cbd8a2009-01-18 00:24:28 +00003from types import MethodType
4import unittest
5
6
7class CallingOrder(unittest.TestCase):
8
9 """Calls to the importers on sys.meta_path happen in order that they are
10 specified in the sequence, starting with the first importer
11 [first called], and then continuing on down until one is found that doesn't
12 return None [continuing]."""
13
14
15 def test_first_called(self):
16 # [first called]
17 mod = 'top_level'
Brett Cannonbcb26c52009-02-01 04:00:05 +000018 first = util.mock_modules(mod)
19 second = util.mock_modules(mod)
Raymond Hettinger554290d2009-06-12 11:25:59 +000020 with util.mock_modules(mod) as first, util.mock_modules(mod) as second:
Brett Cannon23cbd8a2009-01-18 00:24:28 +000021 first.modules[mod] = 42
22 second.modules[mod] = -13
Brett Cannonbcb26c52009-02-01 04:00:05 +000023 with util.import_state(meta_path=[first, second]):
Ezio Melottib3aedd42010-11-20 19:04:17 +000024 self.assertEqual(import_util.import_(mod), 42)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000025
26 def test_continuing(self):
27 # [continuing]
28 mod_name = 'for_real'
Raymond Hettinger554290d2009-06-12 11:25:59 +000029 with util.mock_modules('nonexistent') as first, \
30 util.mock_modules(mod_name) as second:
Brett Cannon23cbd8a2009-01-18 00:24:28 +000031 first.find_module = lambda self, fullname, path=None: None
32 second.modules[mod_name] = 42
Brett Cannonbcb26c52009-02-01 04:00:05 +000033 with util.import_state(meta_path=[first, second]):
Ezio Melottib3aedd42010-11-20 19:04:17 +000034 self.assertEqual(import_util.import_(mod_name), 42)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000035
36
37class CallSignature(unittest.TestCase):
38
39 """If there is no __path__ entry on the parent module, then 'path' is None
40 [no path]. Otherwise, the value for __path__ is passed in for the 'path'
41 argument [path set]."""
42
43 def log(self, fxn):
44 log = []
45 def wrapper(self, *args, **kwargs):
46 log.append([args, kwargs])
47 return fxn(*args, **kwargs)
48 return log, wrapper
49
50
51 def test_no_path(self):
52 # [no path]
53 mod_name = 'top_level'
54 assert '.' not in mod_name
Brett Cannonbcb26c52009-02-01 04:00:05 +000055 with util.mock_modules(mod_name) as importer:
Brett Cannon23cbd8a2009-01-18 00:24:28 +000056 log, wrapped_call = self.log(importer.find_module)
57 importer.find_module = MethodType(wrapped_call, importer)
Brett Cannonbcb26c52009-02-01 04:00:05 +000058 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000059 import_util.import_(mod_name)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000060 assert len(log) == 1
61 args = log[0][0]
62 kwargs = log[0][1]
63 # Assuming all arguments are positional.
Ezio Melottib3aedd42010-11-20 19:04:17 +000064 self.assertEqual(len(args), 2)
65 self.assertEqual(len(kwargs), 0)
66 self.assertEqual(args[0], mod_name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000067 self.assertTrue(args[1] is None)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000068
69 def test_with_path(self):
70 # [path set]
71 pkg_name = 'pkg'
72 mod_name = pkg_name + '.module'
73 path = [42]
74 assert '.' in mod_name
Brett Cannonbcb26c52009-02-01 04:00:05 +000075 with util.mock_modules(pkg_name+'.__init__', mod_name) as importer:
Brett Cannon23cbd8a2009-01-18 00:24:28 +000076 importer.modules[pkg_name].__path__ = path
77 log, wrapped_call = self.log(importer.find_module)
78 importer.find_module = MethodType(wrapped_call, importer)
Brett Cannonbcb26c52009-02-01 04:00:05 +000079 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000080 import_util.import_(mod_name)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000081 assert len(log) == 2
82 args = log[1][0]
83 kwargs = log[1][1]
84 # Assuming all arguments are positional.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000085 self.assertTrue(not kwargs)
Ezio Melottib3aedd42010-11-20 19:04:17 +000086 self.assertEqual(args[0], mod_name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000087 self.assertTrue(args[1] is path)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000088
89
90
91def test_main():
92 from test.support import run_unittest
93 run_unittest(CallingOrder, CallSignature)
94
95
96if __name__ == '__main__':
97 test_main()