mirror of https://github.com/python/cpython.git
bpo-45614: Fix traceback display for exceptions with invalid module name (GH-29726) (GH-29827)
(cherry picked from commit 4dfae6f38e
)
This commit is contained in:
parent
209cec8a2a
commit
5b6aa6ce20
|
@ -740,6 +740,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]:
|
||||
|
|
|
@ -574,6 +574,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):
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Fix :mod:`traceback` display for exceptions with invalid module name.
|
|
@ -903,7 +903,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))
|
||||
|
|
Loading…
Reference in New Issue