[Python-checkins] bpo-45614: Fix traceback display for exceptions with invalid module name (GH-29726)

iritkatriel webhook-mailer at python.org
Sat Nov 27 17:00:21 EST 2021


https://github.com/python/cpython/commit/4dfae6f38e1720ddafcdd68043e476ecb41cb4d5
commit: 4dfae6f38e1720ddafcdd68043e476ecb41cb4d5
branch: main
author: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2021-11-27T22:00:10Z
summary:

bpo-45614: Fix traceback display for exceptions with invalid module name (GH-29726)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-11-23-12-06-41.bpo-45614.fIekgI.rst
M Lib/test/test_traceback.py
M Lib/traceback.py
M Python/pythonrun.c

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index d88851ddda431..cde35f5dacb2d 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -1254,6 +1254,17 @@ def __str__(self):
                 exp = "%s: %s\n" % (str_name, str_value)
                 self.assertEqual(exp, 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_exception_bad__str__(self):
         class X(Exception):
             def __str__(self):
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 97caa1372f478..77f8590719eb8 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -808,6 +808,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):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-11-23-12-06-41.bpo-45614.fIekgI.rst b/Misc/NEWS.d/next/Core and Builtins/2021-11-23-12-06-41.bpo-45614.fIekgI.rst
new file mode 100644
index 0000000000000..4255e1885ad67
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-11-23-12-06-41.bpo-45614.fIekgI.rst	
@@ -0,0 +1 @@
+Fix :mod:`traceback` display for exceptions with invalid module name.
\ No newline at end of file
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 2c0950ee17e8a..2f68b214603e1 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1022,7 +1022,7 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
             {
                 Py_XDECREF(modulename);
                 PyErr_Clear();
-                err = PyFile_WriteString("<unknown>", f);
+                err = PyFile_WriteString("<unknown>.", f);
             }
             else {
                 if (!_PyUnicode_EqualToASCIIId(modulename, &PyId_builtins) &&



More information about the Python-checkins mailing list