Issue #14605: Stop having implicit entries for sys.meta_path. · pythoncapi/cpython@ce418b4 · GitHub
Skip to content

Commit ce418b4

Browse files
committed
Issue python#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

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

Python/importlib.h

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

0 commit comments

Comments
 (0)