From 34c29cf777db834d8ffe075084a1c9e513b90deb Mon Sep 17 00:00:00 2001 From: monadchains Date: Sat, 19 Jul 2025 16:10:31 +0200 Subject: [PATCH 1/8] Improve ModuleNotFoundError when -S is passed --- Lib/traceback.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/traceback.py b/Lib/traceback.py index 31aa8695735f2b..4680e0062029f6 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1106,6 +1106,10 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None, suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name) if suggestion: self._str += f". Did you mean: '{suggestion}'?" + elif exc_type and issubclass(exc_type, ModuleNotFoundError) and \ + sys.flags.no_site: + self._str += ". Site initialization is disabled, did you forget to add the \ + site-package directory to sys.path?" elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \ getattr(exc_value, "name", None) is not None: wrong_name = getattr(exc_value, "name", None) From 05ad9cec47d10f9e7cb058c21d6e32fb9110a54e Mon Sep 17 00:00:00 2001 From: monadchains Date: Sat, 19 Jul 2025 17:21:05 +0200 Subject: [PATCH 2/8] add blurb --- .../2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst new file mode 100644 index 00000000000000..7cde175a030770 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst @@ -0,0 +1,2 @@ +Improve ModuleNotFoundError by adding flavour text to exception when the +argument '-S' is passed. Contributed by Andrea Mattei From c31694ab98f361a8e34918a66e1b61f83b67aaf8 Mon Sep 17 00:00:00 2001 From: monadchains Date: Sat, 19 Jul 2025 17:44:58 +0200 Subject: [PATCH 3/8] improve code --- Lib/traceback.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/traceback.py b/Lib/traceback.py index 4680e0062029f6..2447909bc7e53c 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1107,9 +1107,10 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None, if suggestion: self._str += f". Did you mean: '{suggestion}'?" elif exc_type and issubclass(exc_type, ModuleNotFoundError) and \ - sys.flags.no_site: - self._str += ". Site initialization is disabled, did you forget to add the \ - site-package directory to sys.path?" + sys.flags.no_site and \ + getattr(exc_value, "name", None) not in sys.builtin_module_names: + self._str += (". Site initialization is disabled, did you forget to " + + "add the site-package directory to sys.path?") elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \ getattr(exc_value, "name", None) is not None: wrong_name = getattr(exc_value, "name", None) From c14ce270647d60a5e026cb22f43b09c773850f8d Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 20 Jul 2025 00:48:55 +0200 Subject: [PATCH 4/8] Test + feedbacks --- Lib/test/test_traceback.py | 32 +++++++++++++++++++ Lib/traceback.py | 2 +- ...-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst | 4 +-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 74b979d009664d..c5494c4362bdfa 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4748,7 +4748,39 @@ class MyList(list): with self.assertRaises(TypeError): _suggestions._generate_suggestions(MyList(), "") + @support.requires_subprocess() + def test_no_site_package_flavour(self): + import subprocess + + cmd = [sys.executable, '-S', '-c', 'import boo'] + result = subprocess.run( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True + ) + + self.assertNotEqual(result.returncode, 0) + self.assertTrue( + ("Site initialization is disabled, did you forget to " + + "add the site-packages directory to sys.path?") in result.stderr + ) + + cmd = [sys.executable, '-S', '-c', + 'import sys; sys.builtin_module_names = sys.builtin_module_names + ("boo",); import boo'] + result = subprocess.run( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True + ) + + self.assertNotEqual(result.returncode, 0) + self.assertTrue( + ("Site initialization is disabled, did you forget to " + + "add the site-packages directory to sys.path?") not in result.stderr + ) class TestColorizedTraceback(unittest.TestCase): diff --git a/Lib/traceback.py b/Lib/traceback.py index 2447909bc7e53c..d55602e9f173ac 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1110,7 +1110,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None, sys.flags.no_site and \ getattr(exc_value, "name", None) not in sys.builtin_module_names: self._str += (". Site initialization is disabled, did you forget to " - + "add the site-package directory to sys.path?") + + "add the site-packages directory to sys.path?") elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \ getattr(exc_value, "name", None) is not None: wrong_name = getattr(exc_value, "name", None) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst index 7cde175a030770..bf07b0b0e03960 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst @@ -1,2 +1,2 @@ -Improve ModuleNotFoundError by adding flavour text to exception when the -argument '-S' is passed. Contributed by Andrea Mattei +Improve :exc:`ModuleNotFoundError` by adding flavour text to exception when the +:option:`-S` option is passed. Patch by Andrea Mattei. From df7df73b30ac92640d1e8ae91ea4a6e851ce9828 Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 20 Jul 2025 10:03:25 +0200 Subject: [PATCH 5/8] feedbacks --- Lib/test/test_traceback.py | 43 +++++++------------ ...-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst | 2 +- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index c5494c4362bdfa..2f31790c81290a 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4748,38 +4748,25 @@ class MyList(list): with self.assertRaises(TypeError): _suggestions._generate_suggestions(MyList(), "") - @support.requires_subprocess() def test_no_site_package_flavour(self): - import subprocess - - cmd = [sys.executable, '-S', '-c', 'import boo'] - result = subprocess.run( - cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True - ) - - self.assertNotEqual(result.returncode, 0) - self.assertTrue( - ("Site initialization is disabled, did you forget to " - + "add the site-packages directory to sys.path?") in result.stderr - ) + code = """import boo""" + _, _, stderr = assert_python_failure('-S', '-c', code) - cmd = [sys.executable, '-S', '-c', - 'import sys; sys.builtin_module_names = sys.builtin_module_names + ("boo",); import boo'] - - result = subprocess.run( - cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True + self.assertIn( + (b"Site initialization is disabled, did you forget to " + b"add the site-packages directory to sys.path?"), stderr ) - self.assertNotEqual(result.returncode, 0) - self.assertTrue( - ("Site initialization is disabled, did you forget to " - + "add the site-packages directory to sys.path?") not in result.stderr + code = """ + import sys + sys.builtin_module_names = sys.builtin_module_names + ("boo",) + import boo + """ + _, _, stderr = assert_python_failure('-S', '-c', code) + + self.assertNotIn( + (b"Site initialization is disabled, did you forget to " + b"add the site-packages directory to sys.path?"), stderr ) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst index bf07b0b0e03960..aff047bbef0f07 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-17-08-09.gh-issue-127598.Mx8S-y.rst @@ -1,2 +1,2 @@ -Improve :exc:`ModuleNotFoundError` by adding flavour text to exception when the +Improve :exc:`ModuleNotFoundError` by adding flavour text to the exception when the :option:`-S` option is passed. Patch by Andrea Mattei. From ca64adae21d8dfd00521aa8d144d7249196893c5 Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 20 Jul 2025 12:43:55 +0200 Subject: [PATCH 6/8] change library list --- Lib/traceback.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/traceback.py b/Lib/traceback.py index d55602e9f173ac..f0dbb6352f7760 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1108,7 +1108,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None, self._str += f". Did you mean: '{suggestion}'?" elif exc_type and issubclass(exc_type, ModuleNotFoundError) and \ sys.flags.no_site and \ - getattr(exc_value, "name", None) not in sys.builtin_module_names: + getattr(exc_value, "name", None) not in sys.stdlib_module_names: self._str += (". Site initialization is disabled, did you forget to " + "add the site-packages directory to sys.path?") elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \ From 2ba6351bc2877e6c95b3efffc6925845dad57a5a Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 20 Jul 2025 14:06:40 +0200 Subject: [PATCH 7/8] fix test --- Lib/test/test_traceback.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 2f31790c81290a..0fea7e8dccb4b6 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4758,9 +4758,7 @@ def test_no_site_package_flavour(self): ) code = """ - import sys - sys.builtin_module_names = sys.builtin_module_names + ("boo",) - import boo + import msvcrt """ _, _, stderr = assert_python_failure('-S', '-c', code) From 29aad1dbff7a7267e95ad384bdbdd7b0aa431f22 Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 20 Jul 2025 14:16:47 +0200 Subject: [PATCH 8/8] fix test --- Lib/test/test_traceback.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 0fea7e8dccb4b6..11b7f419bddbe4 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4758,7 +4758,9 @@ def test_no_site_package_flavour(self): ) code = """ - import msvcrt + import sys + sys.stdlib_module_names = sys.stdlib_module_names + ("boo",) + import boo """ _, _, stderr = assert_python_failure('-S', '-c', code)