From 8df33f8ff081010b1e5166602dc675bd885076b3 Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Mon, 30 Mar 2026 17:13:23 +0800 Subject: [PATCH 1/3] fix bug in suggest module --- Lib/traceback.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Lib/traceback.py b/Lib/traceback.py index 1f9f151ebf5d39..01b3677b521fdf 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -13,6 +13,7 @@ import tokenize import io import importlib.util +from importlib.machinery import ModuleSpec import pathlib import _colorize @@ -1125,16 +1126,14 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None, self._str += f". Did you mean: '{suggestion}' ({suggestion!a})?" elif exc_type and issubclass(exc_type, ModuleNotFoundError): module_name = getattr(exc_value, "name", None) + analyse_sys_no_site = True if module_name in sys.stdlib_module_names: message = _MISSING_STDLIB_MODULE_MESSAGES.get( module_name, f"Standard library module {module_name!r} was not found" ) self._str = message - elif sys.flags.no_site: - self._str += (". Site initialization is disabled, did you forget to " - + "add the site-packages directory to sys.path " - + "or to enable your virtual environment?") + analyse_sys_no_site = False elif abi_tag := _find_incompatible_extension_module(module_name): self._str += ( ". Although a module with this name was found for a " @@ -1144,6 +1143,12 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None, suggestion = _compute_suggestion_error(exc_value, exc_traceback, module_name) if suggestion: self._str += f". Did you mean: '{suggestion}'?" + if analyse_sys_no_site and sys.flags.no_site: + if not self._str.endswith((".", "?")): + self._str += "." + self._str += (" Site initialization is disabled, did you forget to " + "add the site-packages directory to sys.path " + "or to enable your virtual environment?") elif exc_type and issubclass(exc_type, AttributeError) and \ getattr(exc_value, "name", None) is not None: wrong_name = getattr(exc_value, "name", None) @@ -1758,7 +1763,10 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): d = [] for finder in sys.meta_path: if discover := getattr(finder, 'discover', None): - d += [spec.name for spec in discover(parent)] + try: + d += [spec.name for spec in discover(parent) if isinstance(spec, ModuleSpec)] + except Exception: + continue except Exception: return None elif isinstance(exc_value, ImportError): From 0acc59ea25e5a5316937039128529251c8156709 Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Mon, 30 Mar 2026 17:57:57 +0800 Subject: [PATCH 2/3] rollback some change and add test --- Lib/test/test_traceback.py | 9 +++++++++ Lib/traceback.py | 6 +----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 5dc11253e0d5c8..9c6e81ae7e4fe7 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -5189,6 +5189,15 @@ def test_no_site_package_flavour(self): b"or to enable your virtual environment?"), stderr ) + code = """import abs""" + _, _, stderr = assert_python_failure('-S', '-c', code) + self.assertIn(b"Did you mean: 'abc'?", stderr) + self.assertIn( + (b"Site initialization is disabled, did you forget to " + b"add the site-packages directory to sys.path " + b"or to enable your virtual environment?"), stderr + ) + def test_missing_stdlib_module(self): code = """ import sys diff --git a/Lib/traceback.py b/Lib/traceback.py index 01b3677b521fdf..5c13540551c8e7 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -13,7 +13,6 @@ import tokenize import io import importlib.util -from importlib.machinery import ModuleSpec import pathlib import _colorize @@ -1763,10 +1762,7 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): d = [] for finder in sys.meta_path: if discover := getattr(finder, 'discover', None): - try: - d += [spec.name for spec in discover(parent) if isinstance(spec, ModuleSpec)] - except Exception: - continue + d += [spec.name for spec in discover(parent)] except Exception: return None elif isinstance(exc_value, ImportError): From 0d789f56d2517aa532d660c88e7128055cb9f753 Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Mon, 30 Mar 2026 20:25:39 +0800 Subject: [PATCH 3/3] add the test with '-S' and mismatched abi --- Lib/test/test_traceback.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 9c6e81ae7e4fe7..af7a75bd3cda8d 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -5275,9 +5275,17 @@ def test_incompatible_extension_modules_hint(self): sys.path.insert(0, {tmp!r}) import foo ''' - _, _, stderr = assert_python_failure('-c', code, __cwd=tmp) + _, _, stderr1 = assert_python_failure('-c', code, __cwd=tmp) + # if used "-S", the message should contain two suggestions + _, _, stderr2 = assert_python_failure('-S', '-c', code, __cwd=tmp) hint = f'Although a module with this name was found for a different Python version ({incompatible_module}).' - self.assertIn(hint, stderr.decode()) + self.assertIn(hint, stderr1.decode()) + self.assertIn(hint, stderr2.decode()) + self.assertIn( + (b"Site initialization is disabled, did you forget to " + b"add the site-packages directory to sys.path " + b"or to enable your virtual environment?"), stderr2 + ) class TestColorizedTraceback(unittest.TestCase):