summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/test
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-04-18 01:41:35 (GMT)
committerBrett Cannon <brett@python.org>2012-04-18 01:41:35 (GMT)
commit7bd329d800b29e195a55b16093ef74c22e20476d (patch)
tree04d1ecb74f0abcb0ee9708f212bf6fd5818ca813 /Lib/importlib/test
parent64befe939c1da377053f6a410f082db02184c5e2 (diff)
downloadcpython-7bd329d800b29e195a55b16093ef74c22e20476d.zip
cpython-7bd329d800b29e195a55b16093ef74c22e20476d.tar.gz
cpython-7bd329d800b29e195a55b16093ef74c22e20476d.tar.bz2
Issue #12599: Be more strict in accepting None vs. a false-like object
in importlib. Thanks to PJE for pointing out the issue and Nick Coghlan for filing the bug.
Diffstat (limited to 'Lib/importlib/test')
-rw-r--r--Lib/importlib/test/import_/test_path.py9
-rw-r--r--Lib/importlib/test/test_util.py16
2 files changed, 24 insertions, 1 deletions
diff --git a/Lib/importlib/test/import_/test_path.py b/Lib/importlib/test/import_/test_path.py
index a211bdf..fe47717 100644
--- a/Lib/importlib/test/import_/test_path.py
+++ b/Lib/importlib/test/import_/test_path.py
@@ -42,6 +42,15 @@ class FinderTests(unittest.TestCase):
loader = machinery.PathFinder.find_module(module, [path])
self.assertTrue(loader is importer)
+ def test_empty_list(self):
+ # An empty list should not count as asking for sys.path.
+ module = 'module'
+ path = '<test path>'
+ importer = util.mock_modules(module)
+ with util.import_state(path_importer_cache={path: importer},
+ path=[path]):
+ self.assertIsNone(machinery.PathFinder.find_module('module', []))
+
def test_path_hooks(self):
# Test that sys.path_hooks is used.
# Test that sys.path_importer_cache is set.
diff --git a/Lib/importlib/test/test_util.py b/Lib/importlib/test/test_util.py
index c7cdad1..b035d65 100644
--- a/Lib/importlib/test/test_util.py
+++ b/Lib/importlib/test/test_util.py
@@ -65,9 +65,23 @@ class ModuleForLoaderTests(unittest.TestCase):
self.assertEqual(wrapped.__name__, fxn.__name__)
self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
-class SetPackageTests(unittest.TestCase):
+ def test_false_module(self):
+ # If for some odd reason a module is considered false, still return it
+ # from sys.modules.
+ class FalseModule(types.ModuleType):
+ def __bool__(self): return False
+
+ name = 'mod'
+ module = FalseModule(name)
+ with test_util.uncache(name):
+ self.assertFalse(module)
+ sys.modules[name] = module
+ given = self.return_module(name)
+ self.assertTrue(given is module)
+class SetPackageTests(unittest.TestCase):
+
"""Tests for importlib.util.set_package."""
def verify(self, module, expect):