Skip to content

Commit ccf70c1

Browse files
refactor(traceback): consolidate ModuleNotFoundError handling and fix suggestion formatting
Move ModuleNotFoundError stdlib module detection into the unified suggestion handling block, removing duplicate exception handling code. Also fix the site initialization disabled message to include virtual environment hint and add proper handling for non-ASCII suggestion names in the _suggestion_message function.
1 parent 77e6a19 commit ccf70c1

1 file changed

Lines changed: 13 additions & 52 deletions

File tree

Lib/traceback.py

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,13 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
11031103
self.msg = exc_value.msg
11041104
self._is_syntax_error = True
11051105
self._exc_metadata = getattr(exc_value, "_metadata", None)
1106+
elif (issubclass(exc_type, ModuleNotFoundError)
1107+
and getattr(exc_value, "name", None) in sys.stdlib_module_names):
1108+
module_name = exc_value.name
1109+
self._str = _MISSING_STDLIB_MODULE_MESSAGES.get(
1110+
module_name,
1111+
f"Standard library module {module_name!r} was not found"
1112+
)
11061113
elif suggestion := _suggestion_message(exc_type, exc_value, exc_traceback):
11071114
if self._str.endswith(('.', '?', '!')):
11081115
punctuation = ''
@@ -1113,56 +1120,6 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
11131120
self.exc_type_qualname = None
11141121
self.exc_type_module = None
11151122

1116-
if exc_type and issubclass(exc_type, SyntaxError):
1117-
# Handle SyntaxError's specially
1118-
self.filename = exc_value.filename
1119-
lno = exc_value.lineno
1120-
self.lineno = str(lno) if lno is not None else None
1121-
end_lno = exc_value.end_lineno
1122-
self.end_lineno = str(end_lno) if end_lno is not None else None
1123-
self.text = exc_value.text
1124-
self.offset = exc_value.offset
1125-
self.end_offset = exc_value.end_offset
1126-
self.msg = exc_value.msg
1127-
self._is_syntax_error = True
1128-
self._exc_metadata = getattr(exc_value, "_metadata", None)
1129-
elif exc_type and issubclass(exc_type, ImportError) and \
1130-
getattr(exc_value, "name_from", None) is not None:
1131-
wrong_name = getattr(exc_value, "name_from", None)
1132-
suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
1133-
if suggestion:
1134-
if suggestion.isascii():
1135-
self._str += f". Did you mean: '{suggestion}'?"
1136-
else:
1137-
self._str += f". Did you mean: '{suggestion}' ({suggestion!a})?"
1138-
elif exc_type and issubclass(exc_type, ModuleNotFoundError):
1139-
module_name = getattr(exc_value, "name", None)
1140-
if module_name in sys.stdlib_module_names:
1141-
message = _MISSING_STDLIB_MODULE_MESSAGES.get(
1142-
module_name,
1143-
f"Standard library module {module_name!r} was not found"
1144-
)
1145-
self._str = message
1146-
elif sys.flags.no_site:
1147-
self._str += (". Site initialization is disabled, did you forget to "
1148-
+ "add the site-packages directory to sys.path "
1149-
+ "or to enable your virtual environment?")
1150-
elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \
1151-
getattr(exc_value, "name", None) is not None:
1152-
wrong_name = getattr(exc_value, "name", None)
1153-
suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
1154-
if suggestion:
1155-
if suggestion.isascii():
1156-
self._str += f". Did you mean: '{suggestion}'?"
1157-
else:
1158-
self._str += f". Did you mean: '{suggestion}' ({suggestion!a})?"
1159-
if issubclass(exc_type, NameError):
1160-
wrong_name = getattr(exc_value, "name", None)
1161-
if wrong_name is not None and wrong_name in sys.stdlib_module_names:
1162-
if suggestion:
1163-
self._str += f" Or did you forget to import '{wrong_name}'?"
1164-
else:
1165-
self._str += f". Did you forget to import '{wrong_name}'?"
11661123
if lookup_lines:
11671124
self._load_lines()
11681125
self.__suppress_context__ = \
@@ -1798,7 +1755,8 @@ def _suggestion_message(exc_type, exc_value, exc_traceback):
17981755
and getattr(exc_value, "name", None) not in sys.stdlib_module_names
17991756
):
18001757
return ("Site initialization is disabled, did you forget to "
1801-
"add the site-packages directory to sys.path?")
1758+
"add the site-packages directory to sys.path "
1759+
"or to enable your virtual environment?")
18021760
if issubclass(exc_type, (ImportError, NameError, AttributeError)):
18031761
if issubclass(exc_type, ImportError):
18041762
wrong_name = getattr(exc_value, "name_from", None)
@@ -1816,7 +1774,10 @@ def _suggestion_message(exc_type, exc_value, exc_traceback):
18161774
if maybe_stdlib_import:
18171775
return f"Did you forget to import '{wrong_name}'?"
18181776
return None
1819-
text = f"Did you mean: '{other_name}'?"
1777+
if other_name.isascii():
1778+
text = f"Did you mean: '{other_name}'?"
1779+
else:
1780+
text = f"Did you mean: '{other_name}' ({other_name!a})?"
18201781
if maybe_stdlib_import:
18211782
return f"{text} Or did you forget to import '{wrong_name}'?"
18221783
return text

0 commit comments

Comments
 (0)