mirror of
https://github.com/python/cpython.git
synced 2025-12-22 08:29:12 +00:00
gh-127598: Improve ModuleNotFoundError when -S is passed (GH-136821)
This commit is contained in:
parent
4a151cae33
commit
18a7f5dad8
3 changed files with 26 additions and 0 deletions
|
|
@ -4748,7 +4748,26 @@ class MiscTest(unittest.TestCase):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
_suggestions._generate_suggestions(MyList(), "")
|
_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):
|
class TestColorizedTraceback(unittest.TestCase):
|
||||||
|
|
|
||||||
|
|
@ -1106,6 +1106,11 @@ class TracebackException:
|
||||||
suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
|
suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
|
||||||
if suggestion:
|
if suggestion:
|
||||||
self._str += f". Did you mean: '{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 \
|
elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \
|
||||||
getattr(exc_value, "name", None) is not None:
|
getattr(exc_value, "name", None) is not None:
|
||||||
wrong_name = getattr(exc_value, "name", None)
|
wrong_name = getattr(exc_value, "name", None)
|
||||||
|
|
|
||||||
|
|
@ -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…
Add table
Add a link
Reference in a new issue