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 | import sys |
| 4 | import unittest |
| 5 | import importlib |
Brett Cannon | d62cd56 | 2012-04-06 13:13:08 -0400 | [diff] [blame] | 6 | from test import support |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 7 | |
| 8 | |
| 9 | class ParentModuleTests(unittest.TestCase): |
| 10 | |
| 11 | """Importing a submodule should import the parent modules.""" |
| 12 | |
| 13 | def test_import_parent(self): |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 14 | with util.mock_modules('pkg.__init__', 'pkg.module') as mock: |
| 15 | with util.import_state(meta_path=[mock]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 16 | module = import_util.import_('pkg.module') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 17 | self.assertIn('pkg', sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 18 | |
| 19 | def test_bad_parent(self): |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 20 | with util.mock_modules('pkg.module') as mock: |
| 21 | with util.import_state(meta_path=[mock]): |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 22 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 23 | import_util.import_('pkg.module') |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 24 | self.assertEqual(cm.exception.name, 'pkg') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 25 | |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 26 | def test_raising_parent_after_importing_child(self): |
| 27 | def __init__(): |
| 28 | import pkg.module |
| 29 | 1/0 |
| 30 | mock = util.mock_modules('pkg.__init__', 'pkg.module', |
| 31 | module_code={'pkg': __init__}) |
| 32 | with mock: |
| 33 | with util.import_state(meta_path=[mock]): |
| 34 | with self.assertRaises(ZeroDivisionError): |
| 35 | import_util.import_('pkg') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 36 | self.assertNotIn('pkg', sys.modules) |
| 37 | self.assertIn('pkg.module', sys.modules) |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 38 | with self.assertRaises(ZeroDivisionError): |
| 39 | import_util.import_('pkg.module') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 40 | self.assertNotIn('pkg', sys.modules) |
| 41 | self.assertIn('pkg.module', sys.modules) |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 42 | |
| 43 | def test_raising_parent_after_relative_importing_child(self): |
| 44 | def __init__(): |
| 45 | from . import module |
| 46 | 1/0 |
| 47 | mock = util.mock_modules('pkg.__init__', 'pkg.module', |
| 48 | module_code={'pkg': __init__}) |
| 49 | with mock: |
| 50 | with util.import_state(meta_path=[mock]): |
| 51 | with self.assertRaises((ZeroDivisionError, ImportError)): |
| 52 | # This raises ImportError on the "from . import module" |
| 53 | # line, not sure why. |
| 54 | import_util.import_('pkg') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 55 | self.assertNotIn('pkg', sys.modules) |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 56 | with self.assertRaises((ZeroDivisionError, ImportError)): |
| 57 | import_util.import_('pkg.module') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 58 | self.assertNotIn('pkg', sys.modules) |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 59 | # XXX False |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 60 | #self.assertIn('pkg.module', sys.modules) |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 61 | |
| 62 | def test_raising_parent_after_double_relative_importing_child(self): |
| 63 | def __init__(): |
| 64 | from ..subpkg import module |
| 65 | 1/0 |
| 66 | mock = util.mock_modules('pkg.__init__', 'pkg.subpkg.__init__', |
| 67 | 'pkg.subpkg.module', |
| 68 | module_code={'pkg.subpkg': __init__}) |
| 69 | with mock: |
| 70 | with util.import_state(meta_path=[mock]): |
| 71 | with self.assertRaises((ZeroDivisionError, ImportError)): |
| 72 | # This raises ImportError on the "from ..subpkg import module" |
| 73 | # line, not sure why. |
| 74 | import_util.import_('pkg.subpkg') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 75 | self.assertNotIn('pkg.subpkg', sys.modules) |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 76 | with self.assertRaises((ZeroDivisionError, ImportError)): |
| 77 | import_util.import_('pkg.subpkg.module') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 78 | self.assertNotIn('pkg.subpkg', sys.modules) |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 79 | # XXX False |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 80 | #self.assertIn('pkg.subpkg.module', sys.modules) |
Antoine Pitrou | 6efa50a | 2012-05-07 21:41:59 +0200 | [diff] [blame] | 81 | |
Brett Cannon | 1c1dcbf | 2009-08-30 20:22:21 +0000 | [diff] [blame] | 82 | def test_module_not_package(self): |
| 83 | # Try to import a submodule from a non-package should raise ImportError. |
| 84 | assert not hasattr(sys, '__path__') |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 85 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 1c1dcbf | 2009-08-30 20:22:21 +0000 | [diff] [blame] | 86 | import_util.import_('sys.no_submodules_here') |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 87 | self.assertEqual(cm.exception.name, 'sys.no_submodules_here') |
Brett Cannon | 1c1dcbf | 2009-08-30 20:22:21 +0000 | [diff] [blame] | 88 | |
Brett Cannon | 927d874 | 2012-04-02 20:33:56 -0400 | [diff] [blame] | 89 | def test_module_not_package_but_side_effects(self): |
| 90 | # If a module injects something into sys.modules as a side-effect, then |
| 91 | # pick up on that fact. |
| 92 | name = 'mod' |
| 93 | subname = name + '.b' |
| 94 | def module_injection(): |
| 95 | sys.modules[subname] = 'total bunk' |
| 96 | mock_modules = util.mock_modules('mod', |
| 97 | module_code={'mod': module_injection}) |
| 98 | with mock_modules as mock: |
| 99 | with util.import_state(meta_path=[mock]): |
Brett Cannon | d62cd56 | 2012-04-06 13:13:08 -0400 | [diff] [blame] | 100 | try: |
| 101 | submodule = import_util.import_(subname) |
| 102 | finally: |
| 103 | support.unload(subname) |
Brett Cannon | 927d874 | 2012-04-02 20:33:56 -0400 | [diff] [blame] | 104 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 105 | |
| 106 | def test_main(): |
| 107 | from test.support import run_unittest |
| 108 | run_unittest(ParentModuleTests) |
| 109 | |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | test_main() |