summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-10-26 07:41:59 (GMT)
committerGitHub <noreply@github.com>2017-10-26 07:41:59 (GMT)
commit41c56940c6edf3ea169332a6b039b6c8796f0475 (patch)
tree749eafdafa42c822e8251d045e7853daa148bf13 /Lib/test/test_importlib
parent4eaf7f949069882e385f2297c9e70031caf9144c (diff)
downloadcpython-41c56940c6edf3ea169332a6b039b6c8796f0475.zip
cpython-41c56940c6edf3ea169332a6b039b6c8796f0475.tar.gz
cpython-41c56940c6edf3ea169332a6b039b6c8796f0475.tar.bz2
bpo-21720: Restore the Python 2.7 logic in handling a fromlist. (#4118)
BytesWarning no longer emitted when the fromlist argument of __import__() or the __all__ attribute of the module contain bytes instances.
Diffstat (limited to 'Lib/test/test_importlib')
-rw-r--r--Lib/test/test_importlib/import_/test_fromlist.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/test/test_importlib/import_/test_fromlist.py b/Lib/test/test_importlib/import_/test_fromlist.py
index 1464003..018c172 100644
--- a/Lib/test/test_importlib/import_/test_fromlist.py
+++ b/Lib/test/test_importlib/import_/test_fromlist.py
@@ -1,5 +1,6 @@
"""Test that the semantics relating to the 'fromlist' argument are correct."""
from .. import util
+import warnings
import unittest
@@ -73,6 +74,13 @@ class HandlingFromlist:
self.assertTrue(hasattr(module, 'module'))
self.assertEqual(module.module.__name__, 'pkg.module')
+ def test_nonexistent_from_package(self):
+ with util.mock_modules('pkg.__init__') as importer:
+ with util.import_state(meta_path=[importer]):
+ module = self.__import__('pkg', fromlist=['non_existent'])
+ self.assertEqual(module.__name__, 'pkg')
+ self.assertFalse(hasattr(module, 'non_existent'))
+
def test_module_from_package_triggers_ModuleNotFoundError(self):
# If a submodule causes an ModuleNotFoundError because it tries
# to import a module which doesn't exist, that should let the
@@ -122,6 +130,41 @@ class HandlingFromlist:
self.assertEqual(module.module1.__name__, 'pkg.module1')
self.assertEqual(module.module2.__name__, 'pkg.module2')
+ def test_nonexistent_in_all(self):
+ with util.mock_modules('pkg.__init__') as importer:
+ with util.import_state(meta_path=[importer]):
+ importer['pkg'].__all__ = ['non_existent']
+ module = self.__import__('pkg', fromlist=['*'])
+ self.assertEqual(module.__name__, 'pkg')
+ self.assertFalse(hasattr(module, 'non_existent'))
+
+ def test_star_in_all(self):
+ with util.mock_modules('pkg.__init__') as importer:
+ with util.import_state(meta_path=[importer]):
+ importer['pkg'].__all__ = ['*']
+ module = self.__import__('pkg', fromlist=['*'])
+ self.assertEqual(module.__name__, 'pkg')
+ self.assertFalse(hasattr(module, '*'))
+
+ def test_invalid_type(self):
+ with util.mock_modules('pkg.__init__') as importer:
+ with util.import_state(meta_path=[importer]), \
+ warnings.catch_warnings():
+ warnings.simplefilter('error', BytesWarning)
+ with self.assertRaisesRegex(TypeError, r'\bfrom\b'):
+ self.__import__('pkg', fromlist=[b'attr'])
+ with self.assertRaisesRegex(TypeError, r'\bfrom\b'):
+ self.__import__('pkg', fromlist=iter([b'attr']))
+
+ def test_invalid_type_in_all(self):
+ with util.mock_modules('pkg.__init__') as importer:
+ with util.import_state(meta_path=[importer]), \
+ warnings.catch_warnings():
+ warnings.simplefilter('error', BytesWarning)
+ importer['pkg'].__all__ = [b'attr']
+ with self.assertRaisesRegex(TypeError, r'\bpkg\.__all__\b'):
+ self.__import__('pkg', fromlist=['*'])
+
(Frozen_FromList,
Source_FromList