summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/importlib/_bootstrap.py8
-rw-r--r--Lib/importlib/test/import_/test_meta_path.py18
-rw-r--r--Lib/test/script_helper.py2
-rw-r--r--Lib/test/test_threaded_import.py2
4 files changed, 25 insertions, 5 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 2f429060d4..b88acc57f9 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -956,8 +956,9 @@ def _resolve_name(name, package, level):
def _find_module(name, path):
"""Find a module's loader."""
- meta_path = sys.meta_path + _IMPLICIT_META_PATH
- for finder in meta_path:
+ if not sys.meta_path:
+ _warnings.warn('sys.meta_path is empty', ImportWarning)
+ for finder in sys.meta_path:
loader = finder.find_module(name, path)
if loader is not None:
# The parent import may have already imported this module.
@@ -986,8 +987,6 @@ def _sanity_check(name, package, level):
raise ValueError("Empty module name")
-_IMPLICIT_META_PATH = [BuiltinImporter, FrozenImporter, PathFinder]
-
_ERR_MSG = 'No module named {!r}'
def _find_and_load(name, import_):
@@ -1195,3 +1194,4 @@ def _install(sys_module, _imp_module):
(SourcelessFileLoader, _suffix_list(2), True)]
sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders),
_imp.NullImporter])
+ sys.meta_path.extend([BuiltinImporter, FrozenImporter, PathFinder])
diff --git a/Lib/importlib/test/import_/test_meta_path.py b/Lib/importlib/test/import_/test_meta_path.py
index 3b130c9a13..2f65af99a9 100644
--- a/Lib/importlib/test/import_/test_meta_path.py
+++ b/Lib/importlib/test/import_/test_meta_path.py
@@ -1,7 +1,10 @@
from .. import util
from . import util as import_util
+import importlib._bootstrap
+import sys
from types import MethodType
import unittest
+import warnings
class CallingOrder(unittest.TestCase):
@@ -33,6 +36,21 @@ class CallingOrder(unittest.TestCase):
with util.import_state(meta_path=[first, second]):
self.assertEqual(import_util.import_(mod_name), 42)
+ def test_empty(self):
+ # Raise an ImportWarning if sys.meta_path is empty.
+ module_name = 'nothing'
+ try:
+ del sys.modules[module_name]
+ except KeyError:
+ pass
+ with util.import_state(meta_path=[]):
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ self.assertIsNone(importlib._bootstrap._find_module('nothing',
+ None))
+ self.assertEqual(len(w), 1)
+ self.assertTrue(issubclass(w[-1].category, ImportWarning))
+
class CallSignature(unittest.TestCase):
diff --git a/Lib/test/script_helper.py b/Lib/test/script_helper.py
index 10ada6d0d1..b09f4bf49e 100644
--- a/Lib/test/script_helper.py
+++ b/Lib/test/script_helper.py
@@ -1,6 +1,7 @@
# Common utility functions used by various script execution tests
# e.g. test_cmd_line, test_cmd_line_script and test_runpy
+import importlib
import sys
import os
import os.path
@@ -93,6 +94,7 @@ def make_script(script_dir, script_basename, source):
script_file = open(script_name, 'w', encoding='utf-8')
script_file.write(source)
script_file.close()
+ importlib.invalidate_caches()
return script_name
def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
diff --git a/Lib/test/test_threaded_import.py b/Lib/test/test_threaded_import.py
index 3faa1845e5..bb72ad4054 100644
--- a/Lib/test/test_threaded_import.py
+++ b/Lib/test/test_threaded_import.py
@@ -126,7 +126,7 @@ class ThreadedImportTests(unittest.TestCase):
def test_parallel_meta_path(self):
finder = Finder()
- sys.meta_path.append(finder)
+ sys.meta_path.insert(0, finder)
try:
self.check_parallel_module_init()
self.assertGreater(finder.numcalls, 0)