Skip to content

Commit c155923

Browse files
Commit
1 parent 947517b commit c155923

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Lib/test/test_traceback.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5049,6 +5049,16 @@ def test_no_site_package_flavour(self):
50495049
b"add the site-packages directory to sys.path?"), stderr
50505050
)
50515051

5052+
def test_missing_stdlib_package(self):
5053+
code = """
5054+
import sys
5055+
sys.stdlib_module_names |= {'spam'}
5056+
import spam
5057+
"""
5058+
_, _, stderr = assert_python_failure('-S', '-c', code)
5059+
5060+
self.assertIn(b"Standard library module 'spam' was not found", stderr)
5061+
50525062

50535063
class TestColorizedTraceback(unittest.TestCase):
50545064
maxDiff = None

Lib/traceback.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,11 +1109,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
11091109
self._str += f". Did you mean: '{suggestion}'?"
11101110
elif exc_type and issubclass(exc_type, ModuleNotFoundError):
11111111
module_name = getattr(exc_value, "name", None)
1112-
if sys.flags.no_site and module_name not in sys.stdlib_module_names:
1112+
if module_name in sys.stdlib_module_names:
1113+
self._str = f"Standard library module '{module_name}' was not found"
1114+
elif sys.flags.no_site:
11131115
self._str += (". Site initialization is disabled, did you forget to "
11141116
+ "add the site-packages directory to sys.path?")
1115-
elif module_name in sys.stdlib_module_names:
1116-
self._str = f"Standard library module '{module_name}' was not found"
11171117
elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \
11181118
getattr(exc_value, "name", None) is not None:
11191119
wrong_name = getattr(exc_value, "name", None)

0 commit comments

Comments
 (0)