Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 48 additions & 1 deletion Lib/test/test_crossinterp.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import contextlib
import itertools
import sys
import traceback
import types
import unittest
import warnings

from test.support import import_helper
from test import support
from test.support import import_helper, swap_attr
Comment thread
picnixz marked this conversation as resolved.
Outdated

_testinternalcapi = import_helper.import_module('_testinternalcapi')
_interpreters = import_helper.import_module('_interpreters')
Expand Down Expand Up @@ -1491,5 +1493,50 @@ def test_builtin_objects(self):
])


class CaptureExceptionTests(unittest.TestCase):

# Prevent crashes with incompatible TracebackException.format().
# Regression test for https://github.com/python/cpython/issues/143377.

def capture_with_formatter(self, exc, formatter):
with swap_attr(traceback.TracebackException, "format", formatter):
return _interpreters.capture_exception(exc)

def test_capture_exception(self):
captured = _interpreters.capture_exception(ValueError("hello"))

self.assertEqual(captured.type.__name__, "ValueError")
self.assertEqual(captured.type.__qualname__, "ValueError")
self.assertEqual(captured.type.__module__, "builtins")

self.assertEqual(captured.msg, "hello")
self.assertEqual(captured.formatted, "ValueError: hello")

def test_capture_exception_custom_format(self):
exc = ValueError("good bye!")
formatter = lambda self: ["hello\n", "world\n"]
captured = self.capture_with_formatter(exc, formatter)
self.assertEqual(captured.msg, "good bye!")
self.assertEqual(captured.formatted, "ValueError: good bye!")
self.assertEqual(captured.errdisplay, "hello\nworld")

@support.subTests("exc_lines", ([], ["x-no-nl"], ["x-no-nl", "y-no-nl"]))
def test_capture_exception_invalid_format(self, exc_lines):
formatter = lambda self: exc_lines
captured = self.capture_with_formatter(ValueError(), formatter)
self.assertEqual(captured.msg, "")
self.assertEqual(captured.formatted, "ValueError: ")
self.assertEqual(captured.errdisplay, "".join(exc_lines))

def test_capture_exception_unraisable_exception(self):
formatter = lambda self: 1
with support.catch_unraisable_exception() as cm:
captured = self.capture_with_formatter(ValueError(), formatter)
self.assertNotHasAttr(captured, "errdisplay")
self.assertEqual(cm.unraisable.exc_type, TypeError)
self.assertEqual(str(cm.unraisable.exc_value),
"can only join an iterable")


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :func:`!_interpreters.capture_exception` when
the exception is incorrectly formatted. Patch by Bénédikt Tran.
11 changes: 8 additions & 3 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,9 +1141,14 @@ _format_TracebackException(PyObject *tbexc)
Py_ssize_t size = -1;
const char *formatted = _copy_string_obj_raw(formatted_obj, &size);
Py_DECREF(formatted_obj);
// We remove trailing the newline added by TracebackException.format().
assert(formatted[size-1] == '\n');
((char *)formatted)[size-1] = '\0';
if (formatted == NULL || size == 0) {
return formatted;
}
assert(formatted[size] == '\0');
// Remove a trailing newline if needed.
if (formatted[size-1] == '\n') {
((char *)formatted)[size-1] = '\0';
Comment thread
picnixz marked this conversation as resolved.
Outdated
}
return formatted;
}

Expand Down
Loading