Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 1 | from .. import util |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 2 | from . import util as import_util |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 3 | from types import MethodType |
| 4 | import unittest |
| 5 | |
| 6 | |
| 7 | class 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 Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 18 | first = util.mock_modules(mod) |
| 19 | second = util.mock_modules(mod) |
Raymond Hettinger | 554290d | 2009-06-12 11:25:59 +0000 | [diff] [blame] | 20 | with util.mock_modules(mod) as first, util.mock_modules(mod) as second: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 21 | first.modules[mod] = 42 |
| 22 | second.modules[mod] = -13 |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 23 | with util.import_state(meta_path=[first, second]): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 24 | self.assertEqual(import_util.import_(mod), 42) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 25 | |
| 26 | def test_continuing(self): |
| 27 | # [continuing] |
| 28 | mod_name = 'for_real' |
Raymond Hettinger | 554290d | 2009-06-12 11:25:59 +0000 | [diff] [blame] | 29 | with util.mock_modules('nonexistent') as first, \ |
| 30 | util.mock_modules(mod_name) as second: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 31 | first.find_module = lambda self, fullname, path=None: None |
| 32 | second.modules[mod_name] = 42 |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 33 | with util.import_state(meta_path=[first, second]): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 34 | self.assertEqual(import_util.import_(mod_name), 42) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | class 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 Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 55 | with util.mock_modules(mod_name) as importer: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 56 | log, wrapped_call = self.log(importer.find_module) |
| 57 | importer.find_module = MethodType(wrapped_call, importer) |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 58 | with util.import_state(meta_path=[importer]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 59 | import_util.import_(mod_name) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 60 | assert len(log) == 1 |
| 61 | args = log[0][0] |
| 62 | kwargs = log[0][1] |
| 63 | # Assuming all arguments are positional. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 64 | self.assertEqual(len(args), 2) |
| 65 | self.assertEqual(len(kwargs), 0) |
| 66 | self.assertEqual(args[0], mod_name) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 67 | self.assertTrue(args[1] is None) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 68 | |
| 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 Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 75 | with util.mock_modules(pkg_name+'.__init__', mod_name) as importer: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 76 | importer.modules[pkg_name].__path__ = path |
| 77 | log, wrapped_call = self.log(importer.find_module) |
| 78 | importer.find_module = MethodType(wrapped_call, importer) |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 79 | with util.import_state(meta_path=[importer]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 80 | import_util.import_(mod_name) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 81 | assert len(log) == 2 |
| 82 | args = log[1][0] |
| 83 | kwargs = log[1][1] |
| 84 | # Assuming all arguments are positional. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 85 | self.assertTrue(not kwargs) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 86 | self.assertEqual(args[0], mod_name) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 87 | self.assertTrue(args[1] is path) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | |
| 91 | def test_main(): |
| 92 | from test.support import run_unittest |
| 93 | run_unittest(CallingOrder, CallSignature) |
| 94 | |
| 95 | |
| 96 | if __name__ == '__main__': |
| 97 | test_main() |