Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce418b4

Browse files
committed
Issue #14605: Stop having implicit entries for sys.meta_path.
ImportWarning is raised if sys.meta_path is found to be empty.
1 parent 3c6ea1c commit ce418b4

8 files changed

Lines changed: 2992 additions & 2969 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,9 @@ def _resolve_name(name, package, level):
956956

957957
def _find_module(name, path):
958958
"""Find a module's loader."""
959-
meta_path = sys.meta_path + _IMPLICIT_META_PATH
960-
for finder in meta_path:
959+
if not sys.meta_path:
960+
_warnings.warn('sys.meta_path is empty', ImportWarning)
961+
for finder in sys.meta_path:
961962
loader = finder.find_module(name, path)
962963
if loader is not None:
963964
# The parent import may have already imported this module.
@@ -986,8 +987,6 @@ def _sanity_check(name, package, level):
986987
raise ValueError("Empty module name")
987988

988989

989-
_IMPLICIT_META_PATH = [BuiltinImporter, FrozenImporter, PathFinder]
990-
991990
_ERR_MSG = 'No module named {!r}'
992991

993992
def _find_and_load(name, import_):
@@ -1195,3 +1194,4 @@ def _install(sys_module, _imp_module):
11951194
(SourcelessFileLoader, _suffix_list(2), True)]
11961195
sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders),
11971196
_imp.NullImporter])
1197+
sys.meta_path.extend([BuiltinImporter, FrozenImporter, PathFinder])

Lib/importlib/test/import_/test_meta_path.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from .. import util
22
from . import util as import_util
3+
import importlib._bootstrap
4+
import sys
35
from types import MethodType
46
import unittest
7+
import warnings
58

69

710
class CallingOrder(unittest.TestCase):
@@ -33,6 +36,21 @@ def test_continuing(self):
3336
with util.import_state(meta_path=[first, second]):
3437
self.assertEqual(import_util.import_(mod_name), 42)
3538

39+
def test_empty(self):
40+
# Raise an ImportWarning if sys.meta_path is empty.
41+
module_name = 'nothing'
42+
try:
43+
del sys.modules[module_name]
44+
except KeyError:
45+
pass
46+
with util.import_state(meta_path=[]):
47+
with warnings.catch_warnings(record=True) as w:
48+
warnings.simplefilter('always')
49+
self.assertIsNone(importlib._bootstrap._find_module('nothing',
50+
None))
51+
self.assertEqual(len(w), 1)
52+
self.assertTrue(issubclass(w[-1].category, ImportWarning))
53+
3654

3755
class CallSignature(unittest.TestCase):
3856

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Alpha 3?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #14605: No longer have implicit entries in sys.meta_path. If
14+
sys.meta_path is found to be empty, raise ImportWarning.
15+
1316
- Issue #14605: No longer have implicit entries in sys.path_hooks. If
1417
sys.path_hooks is found to be empty, a warning will be raised. If None is
1518
found in sys.path_importer_cache, a warning is raised and a search on

Modules/config.c.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ struct _inittab _PyImport_Inittab[] = {
4545
{"_ast", PyInit__ast},
4646

4747
/* These entries are here for sys.builtin_module_names */
48-
{"__main__", NULL},
4948
{"builtins", NULL},
5049
{"sys", NULL},
5150

PC/config.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ struct _inittab _PyImport_Inittab[] = {
146146
{"_imp", PyInit_imp},
147147

148148
/* These entries are here for sys.builtin_module_names */
149-
{"__main__", NULL},
150149
{"builtins", NULL},
151150
{"sys", NULL},
152151
{"_warnings", _PyWarnings_Init},

PC/os2emx/config.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ struct _inittab _PyImport_Inittab[] = {
153153
{"_imp", initimp},
154154

155155
/* These entries are here for sys.builtin_module_names */
156-
{"__main__", NULL},
157156
{"builtins", NULL},
158157
{"sys", NULL},
159158

PC/os2vacpp/config.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ struct _inittab _PyImport_Inittab[] = {
9191
{"_imp", initimp},
9292

9393
/* These entries are here for sys.builtin_module_names */
94-
{"__main__", NULL},
9594
{"builtins", NULL},
9695
{"sys", NULL},
9796

Python/importlib.h

Lines changed: 2967 additions & 2961 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)