]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45614: Fix traceback display for exceptions with invalid module name (GH-29726...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Mon, 29 Nov 2021 10:07:24 +0000 (10:07 +0000)
committerGitHub <noreply@github.com>
Mon, 29 Nov 2021 10:07:24 +0000 (10:07 +0000)
(cherry picked from commit 4dfae6f38e1720ddafcdd68043e476ecb41cb4d5)

Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS.d/next/Core and Builtins/2021-11-23-12-06-41.bpo-45614.fIekgI.rst [new file with mode: 0644]
Python/pythonrun.c

index ba03ce46294c578ec9f095c67b1f1a2115286794..18cd4aba24af260fc6a10398de4fabb388e9e084 100644 (file)
@@ -785,6 +785,17 @@ class BaseExceptionReportingTests:
         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]:
index 901b99476aaf8536fcc9631ed7b7512727775df2..d6a010f4157582b6f583b30e868fc60e016bd76a 100644 (file)
@@ -604,6 +604,8 @@ class TracebackException:
         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 (file)
index 0000000..4255e18
--- /dev/null
@@ -0,0 +1 @@
+Fix :mod:`traceback` display for exceptions with invalid module name.
\ No newline at end of file
index 5704bcc7589f320b9d9b4d859da815ab270532e8..0f1794acec73ae980096b748afbd61528ec4ffaa 100644 (file)
@@ -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))