Skip to content

gh-127598: Improve ModuleNotFoundError when -S is passed #136821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4748,7 +4748,26 @@ class MyList(list):
with self.assertRaises(TypeError):
_suggestions._generate_suggestions(MyList(), "")

def test_no_site_package_flavour(self):
code = """import boo"""
_, _, stderr = assert_python_failure('-S', '-c', code)

self.assertIn(
(b"Site initialization is disabled, did you forget to "
b"add the site-packages directory to sys.path?"), stderr
)

code = """
import sys
sys.stdlib_module_names = sys.stdlib_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
)


class TestColorizedTraceback(unittest.TestCase):
Expand Down
5 changes: 5 additions & 0 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,11 @@ 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 and \
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 \
getattr(exc_value, "name", None) is not None:
wrong_name = getattr(exc_value, "name", None)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve :exc:`ModuleNotFoundError` by adding flavour text to the exception when the
:option:`-S` option is passed. Patch by Andrea Mattei.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy