[Python-checkins] cpython: Issue #14605: Stop having implicit entries for sys.meta_path.
brett.cannon
python-checkins at python.org
Fri Apr 27 20:02:45 CEST 2012
http://hg.python.org/cpython/rev/3bd60cc27664
changeset: 76579:3bd60cc27664
user: Brett Cannon <brett at python.org>
date: Fri Apr 27 14:01:58 2012 -0400
summary:
Issue #14605: Stop having implicit entries for sys.meta_path.
ImportWarning is raised if sys.meta_path is found to be empty.
files:
Lib/importlib/_bootstrap.py | 8 ++--
Lib/importlib/test/import_/test_meta_path.py | 18 ++++++++++
Misc/NEWS | 3 +
Modules/config.c.in | 1 -
PC/config.c | 1 -
PC/os2emx/config.c | 1 -
PC/os2vacpp/config.c | 1 -
Python/importlib.h | Bin
8 files changed, 25 insertions(+), 8 deletions(-)
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -956,8 +956,9 @@
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 @@
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 @@
(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
--- 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 @@
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/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
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
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
diff --git a/Modules/config.c.in b/Modules/config.c.in
--- a/Modules/config.c.in
+++ b/Modules/config.c.in
@@ -45,7 +45,6 @@
{"_ast", PyInit__ast},
/* These entries are here for sys.builtin_module_names */
- {"__main__", NULL},
{"builtins", NULL},
{"sys", NULL},
diff --git a/PC/config.c b/PC/config.c
--- a/PC/config.c
+++ b/PC/config.c
@@ -146,7 +146,6 @@
{"_imp", PyInit_imp},
/* These entries are here for sys.builtin_module_names */
- {"__main__", NULL},
{"builtins", NULL},
{"sys", NULL},
{"_warnings", _PyWarnings_Init},
diff --git a/PC/os2emx/config.c b/PC/os2emx/config.c
--- a/PC/os2emx/config.c
+++ b/PC/os2emx/config.c
@@ -153,7 +153,6 @@
{"_imp", initimp},
/* These entries are here for sys.builtin_module_names */
- {"__main__", NULL},
{"builtins", NULL},
{"sys", NULL},
diff --git a/PC/os2vacpp/config.c b/PC/os2vacpp/config.c
--- a/PC/os2vacpp/config.c
+++ b/PC/os2vacpp/config.c
@@ -91,7 +91,6 @@
{"_imp", initimp},
/* These entries are here for sys.builtin_module_names */
- {"__main__", NULL},
{"builtins", NULL},
{"sys", NULL},
diff --git a/Python/importlib.h b/Python/importlib.h
index 7a83c0239cb1e9ca030daf74a03bfe3c6859b220..c3a60ef62dd7e8c77ab350d680a441c60903d845
GIT binary patch
[stripped]
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list