Skip to content

[3.10] bpo-45614: Fix traceback display for exceptions with invalid module n… #29826

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 1 commit into from
Nov 29, 2021
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
11 changes: 11 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,17 @@ def test_message_none(self):
err = self.get_report(Exception(''))
self.assertIn('Exception\n', err)

def test_exception_modulename_not_unicode(self):
class X(Exception):
def __str__(self):
return "I am X"

X.__module__ = 42

err = self.get_report(X())
exp = f'<unknown>.{X.__qualname__}: I am X\n'
self.assertEqual(exp, err)

def test_syntax_error_various_offsets(self):
for offset in range(-5, 10):
for add in [0, 2]:
Expand Down
2 changes: 2 additions & 0 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ def format_exception_only(self):
stype = self.exc_type.__qualname__
smod = self.exc_type.__module__
if smod not in ("__main__", "builtins"):
if not isinstance(smod, str):
smod = "<unknown>"
stype = smod + '.' + stype

if not issubclass(self.exc_type, SyntaxError):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :mod:`traceback` display for exceptions with invalid module name.
2 changes: 1 addition & 1 deletion Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ print_exception(PyObject *f, PyObject *value)
{
Py_XDECREF(modulename);
PyErr_Clear();
err = PyFile_WriteString("<unknown>", f);
err = PyFile_WriteString("<unknown>.", f);
}
else {
if (!_PyUnicode_EqualToASCIIId(modulename, &PyId_builtins))
Expand Down