mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #14605: Stop having implicit entries for sys.meta_path.
ImportWarning is raised if sys.meta_path is found to be empty.
This commit is contained in:
parent
3c6ea1c14d
commit
ce418b448f
8 changed files with 3006 additions and 2983 deletions
|
@ -956,8 +956,9 @@ def _resolve_name(name, package, level):
|
||||||
|
|
||||||
def _find_module(name, path):
|
def _find_module(name, path):
|
||||||
"""Find a module's loader."""
|
"""Find a module's loader."""
|
||||||
meta_path = sys.meta_path + _IMPLICIT_META_PATH
|
if not sys.meta_path:
|
||||||
for finder in meta_path:
|
_warnings.warn('sys.meta_path is empty', ImportWarning)
|
||||||
|
for finder in sys.meta_path:
|
||||||
loader = finder.find_module(name, path)
|
loader = finder.find_module(name, path)
|
||||||
if loader is not None:
|
if loader is not None:
|
||||||
# The parent import may have already imported this module.
|
# The parent import may have already imported this module.
|
||||||
|
@ -986,8 +987,6 @@ def _sanity_check(name, package, level):
|
||||||
raise ValueError("Empty module name")
|
raise ValueError("Empty module name")
|
||||||
|
|
||||||
|
|
||||||
_IMPLICIT_META_PATH = [BuiltinImporter, FrozenImporter, PathFinder]
|
|
||||||
|
|
||||||
_ERR_MSG = 'No module named {!r}'
|
_ERR_MSG = 'No module named {!r}'
|
||||||
|
|
||||||
def _find_and_load(name, import_):
|
def _find_and_load(name, import_):
|
||||||
|
@ -1195,3 +1194,4 @@ def _install(sys_module, _imp_module):
|
||||||
(SourcelessFileLoader, _suffix_list(2), True)]
|
(SourcelessFileLoader, _suffix_list(2), True)]
|
||||||
sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders),
|
sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders),
|
||||||
_imp.NullImporter])
|
_imp.NullImporter])
|
||||||
|
sys.meta_path.extend([BuiltinImporter, FrozenImporter, PathFinder])
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
from .. import util
|
from .. import util
|
||||||
from . import util as import_util
|
from . import util as import_util
|
||||||
|
import importlib._bootstrap
|
||||||
|
import sys
|
||||||
from types import MethodType
|
from types import MethodType
|
||||||
import unittest
|
import unittest
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
class CallingOrder(unittest.TestCase):
|
class CallingOrder(unittest.TestCase):
|
||||||
|
@ -33,6 +36,21 @@ class CallingOrder(unittest.TestCase):
|
||||||
with util.import_state(meta_path=[first, second]):
|
with util.import_state(meta_path=[first, second]):
|
||||||
self.assertEqual(import_util.import_(mod_name), 42)
|
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):
|
class CallSignature(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,9 @@ What's New in Python 3.3.0 Alpha 3?
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #14605: No longer have implicit entries in sys.meta_path. If
|
||||||
|
sys.meta_path is found to be empty, raise ImportWarning.
|
||||||
|
|
||||||
- Issue #14605: No longer have implicit entries in sys.path_hooks. If
|
- Issue #14605: No longer have implicit entries in sys.path_hooks. If
|
||||||
sys.path_hooks is found to be empty, a warning will be raised. If None is
|
sys.path_hooks is found to be empty, a warning will be raised. If None is
|
||||||
found in sys.path_importer_cache, a warning is raised and a search on
|
found in sys.path_importer_cache, a warning is raised and a search on
|
||||||
|
|
|
@ -45,7 +45,6 @@ struct _inittab _PyImport_Inittab[] = {
|
||||||
{"_ast", PyInit__ast},
|
{"_ast", PyInit__ast},
|
||||||
|
|
||||||
/* These entries are here for sys.builtin_module_names */
|
/* These entries are here for sys.builtin_module_names */
|
||||||
{"__main__", NULL},
|
|
||||||
{"builtins", NULL},
|
{"builtins", NULL},
|
||||||
{"sys", NULL},
|
{"sys", NULL},
|
||||||
|
|
||||||
|
|
|
@ -146,7 +146,6 @@ struct _inittab _PyImport_Inittab[] = {
|
||||||
{"_imp", PyInit_imp},
|
{"_imp", PyInit_imp},
|
||||||
|
|
||||||
/* These entries are here for sys.builtin_module_names */
|
/* These entries are here for sys.builtin_module_names */
|
||||||
{"__main__", NULL},
|
|
||||||
{"builtins", NULL},
|
{"builtins", NULL},
|
||||||
{"sys", NULL},
|
{"sys", NULL},
|
||||||
{"_warnings", _PyWarnings_Init},
|
{"_warnings", _PyWarnings_Init},
|
||||||
|
|
|
@ -153,7 +153,6 @@ struct _inittab _PyImport_Inittab[] = {
|
||||||
{"_imp", initimp},
|
{"_imp", initimp},
|
||||||
|
|
||||||
/* These entries are here for sys.builtin_module_names */
|
/* These entries are here for sys.builtin_module_names */
|
||||||
{"__main__", NULL},
|
|
||||||
{"builtins", NULL},
|
{"builtins", NULL},
|
||||||
{"sys", NULL},
|
{"sys", NULL},
|
||||||
|
|
||||||
|
|
|
@ -91,7 +91,6 @@ struct _inittab _PyImport_Inittab[] = {
|
||||||
{"_imp", initimp},
|
{"_imp", initimp},
|
||||||
|
|
||||||
/* These entries are here for sys.builtin_module_names */
|
/* These entries are here for sys.builtin_module_names */
|
||||||
{"__main__", NULL},
|
|
||||||
{"builtins", NULL},
|
{"builtins", NULL},
|
||||||
{"sys", NULL},
|
{"sys", NULL},
|
||||||
|
|
||||||
|
|
5956
Python/importlib.h
5956
Python/importlib.h
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue