]> 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>
Sat, 27 Nov 2021 22:00:10 +0000 (22:00 +0000)
committerGitHub <noreply@github.com>
Sat, 27 Nov 2021 22:00:10 +0000 (22:00 +0000)
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 d88851ddda43137b2b6647c127fa3da72f104007..cde35f5dacb2d3e813cda78fd699a7f68edd01a7 100644 (file)
@@ -1254,6 +1254,17 @@ class BaseExceptionReportingTests:
                 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):
index 97caa1372f47882523550dfde6e1b7887c4ccda8..77f8590719eb82b5879d92d6d2a8ee548e802172 100644 (file)
@@ -808,6 +808,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 2c0950ee17e8a358c8fc8a715aee93fc5c60c3cb..2f68b214603e162409b08edb6454dc6acc481967 100644 (file)
@@ -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) &&