Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 32 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a helper for doing this: test.support.assert_python_ok. Would you mind switching to that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I've changed the to use it, although the version assert_python_failure as we looking for an exception


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):
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.builtin_module_names:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature should use stdlib_module_names, not builtin module names.

Suggested change
getattr(exc_value, "name", None) not in sys.builtin_module_names:
getattr(exc_value, "name", None) not in sys.stdlib_module_names:

(It could be tested by importing a non-existent stdlib module, like msvcrt on *nix or grp on Windows. The note should not be added for those -- you can't solve a "wrong platform" issue by adding the site-packages directory.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented the feedback, and it case stdlib the message it's not thrown :)

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 exception when the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, missed this the first time:

Suggested change
Improve :exc:`ModuleNotFoundError` by adding flavour text to exception when the
Improve :exc:`ModuleNotFoundError` by adding flavour text to the exception when the

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

:option:`-S` option is passed. Patch by Andrea Mattei.
Loading