|
1 | 1 | """Test that the semantics relating to the 'fromlist' argument are correct."""
|
2 | 2 | from .. import util
|
| 3 | +import warnings |
3 | 4 | import unittest
|
4 | 5 |
|
5 | 6 |
|
@@ -73,6 +74,13 @@ def test_module_from_package(self):
|
73 | 74 | self.assertTrue(hasattr(module, 'module'))
|
74 | 75 | self.assertEqual(module.module.__name__, 'pkg.module')
|
75 | 76 |
|
| 77 | + def test_nonexistent_from_package(self): |
| 78 | + with util.mock_modules('pkg.__init__') as importer: |
| 79 | + with util.import_state(meta_path=[importer]): |
| 80 | + module = self.__import__('pkg', fromlist=['non_existent']) |
| 81 | + self.assertEqual(module.__name__, 'pkg') |
| 82 | + self.assertFalse(hasattr(module, 'non_existent')) |
| 83 | + |
76 | 84 | def test_module_from_package_triggers_ModuleNotFoundError(self):
|
77 | 85 | # If a submodule causes an ModuleNotFoundError because it tries
|
78 | 86 | # to import a module which doesn't exist, that should let the
|
@@ -122,6 +130,41 @@ def test_star_with_others(self):
|
122 | 130 | self.assertEqual(module.module1.__name__, 'pkg.module1')
|
123 | 131 | self.assertEqual(module.module2.__name__, 'pkg.module2')
|
124 | 132 |
|
| 133 | + def test_nonexistent_in_all(self): |
| 134 | + with util.mock_modules('pkg.__init__') as importer: |
| 135 | + with util.import_state(meta_path=[importer]): |
| 136 | + importer['pkg'].__all__ = ['non_existent'] |
| 137 | + module = self.__import__('pkg', fromlist=['*']) |
| 138 | + self.assertEqual(module.__name__, 'pkg') |
| 139 | + self.assertFalse(hasattr(module, 'non_existent')) |
| 140 | + |
| 141 | + def test_star_in_all(self): |
| 142 | + with util.mock_modules('pkg.__init__') as importer: |
| 143 | + with util.import_state(meta_path=[importer]): |
| 144 | + importer['pkg'].__all__ = ['*'] |
| 145 | + module = self.__import__('pkg', fromlist=['*']) |
| 146 | + self.assertEqual(module.__name__, 'pkg') |
| 147 | + self.assertFalse(hasattr(module, '*')) |
| 148 | + |
| 149 | + def test_invalid_type(self): |
| 150 | + with util.mock_modules('pkg.__init__') as importer: |
| 151 | + with util.import_state(meta_path=[importer]), \ |
| 152 | + warnings.catch_warnings(): |
| 153 | + warnings.simplefilter('error', BytesWarning) |
| 154 | + with self.assertRaisesRegex(TypeError, r'\bfrom\b'): |
| 155 | + self.__import__('pkg', fromlist=[b'attr']) |
| 156 | + with self.assertRaisesRegex(TypeError, r'\bfrom\b'): |
| 157 | + self.__import__('pkg', fromlist=iter([b'attr'])) |
| 158 | + |
| 159 | + def test_invalid_type_in_all(self): |
| 160 | + with util.mock_modules('pkg.__init__') as importer: |
| 161 | + with util.import_state(meta_path=[importer]), \ |
| 162 | + warnings.catch_warnings(): |
| 163 | + warnings.simplefilter('error', BytesWarning) |
| 164 | + importer['pkg'].__all__ = [b'attr'] |
| 165 | + with self.assertRaisesRegex(TypeError, r'\bpkg\.__all__\b'): |
| 166 | + self.__import__('pkg', fromlist=['*']) |
| 167 | + |
125 | 168 |
|
126 | 169 | (Frozen_FromList,
|
127 | 170 | Source_FromList
|
|
0 commit comments