Skip to content

Commit b41aec8

Browse files
committed
[3.13] gh-143377: fix crashes in _interpreters.capture_exception (GH-143418)
(cherry picked from commit ce6bae9) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 7c99a42 commit b41aec8

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

Lib/test/test_interpreters/test_api.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,55 @@ def test_set___main___attrs(self):
16671667
self.assertEqual(rc, 0)
16681668

16691669

1670+
class CaptureExceptionTests(unittest.TestCase):
1671+
1672+
# Prevent crashes with incompatible TracebackException.format().
1673+
# Regression test for https://github.com/python/cpython/issues/143377.
1674+
1675+
def capture_with_formatter(self, exc, formatter):
1676+
with support.swap_attr(traceback.TracebackException, "format", formatter):
1677+
return _interpreters.capture_exception(exc)
1678+
1679+
def test_capture_exception(self):
1680+
captured = _interpreters.capture_exception(ValueError("hello"))
1681+
1682+
self.assertEqual(captured.type.__name__, "ValueError")
1683+
self.assertEqual(captured.type.__qualname__, "ValueError")
1684+
self.assertEqual(captured.type.__module__, "builtins")
1685+
1686+
self.assertEqual(captured.msg, "hello")
1687+
self.assertEqual(captured.formatted, "ValueError: hello")
1688+
1689+
def test_capture_exception_custom_format(self):
1690+
exc = ValueError("good bye!")
1691+
formatter = lambda self: ["hello\n", "world\n"]
1692+
captured = self.capture_with_formatter(exc, formatter)
1693+
self.assertEqual(captured.msg, "good bye!")
1694+
self.assertEqual(captured.formatted, "ValueError: good bye!")
1695+
self.assertEqual(captured.errdisplay, "hello\nworld")
1696+
1697+
@support.subTests("exc_lines", ([], ["x-no-nl"], ["x-no-nl", "y-no-nl"]))
1698+
def test_capture_exception_invalid_format(self, exc_lines):
1699+
formatter = lambda self: exc_lines
1700+
captured = self.capture_with_formatter(ValueError(), formatter)
1701+
self.assertEqual(captured.msg, "")
1702+
self.assertEqual(captured.formatted, "ValueError: ")
1703+
self.assertEqual(captured.errdisplay, "".join(exc_lines))
1704+
1705+
@unittest.skipUnless(
1706+
support.Py_DEBUG,
1707+
"printing subinterpreter unraisable exceptions requires DEBUG build",
1708+
)
1709+
def test_capture_exception_unraisable_exception(self):
1710+
formatter = lambda self: 1
1711+
with support.catch_unraisable_exception() as cm:
1712+
captured = self.capture_with_formatter(ValueError(), formatter)
1713+
self.assertNotHasAttr(captured, "errdisplay")
1714+
self.assertEqual(cm.unraisable.exc_type, TypeError)
1715+
self.assertEqual(str(cm.unraisable.exc_value),
1716+
"can only join an iterable")
1717+
1718+
16701719
if __name__ == '__main__':
16711720
# Test needs to be a package, so we can do relative imports.
16721721
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash in :func:`!_interpreters.capture_exception` when
2+
the exception is incorrectly formatted. Patch by Bénédikt Tran.

Python/crossinterp.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ _PyCrossInterpreterData_ReleaseAndRawFree(_PyCrossInterpreterData *data)
337337
/* convenience utilities */
338338
/*************************/
339339

340-
static const char *
340+
static char *
341341
_copy_string_obj_raw(PyObject *strobj, Py_ssize_t *p_size)
342342
{
343343
Py_ssize_t size = -1;
@@ -441,11 +441,16 @@ _format_TracebackException(PyObject *tbexc)
441441
}
442442

443443
Py_ssize_t size = -1;
444-
const char *formatted = _copy_string_obj_raw(formatted_obj, &size);
444+
char *formatted = _copy_string_obj_raw(formatted_obj, &size);
445445
Py_DECREF(formatted_obj);
446-
// We remove trailing the newline added by TracebackException.format().
447-
assert(formatted[size-1] == '\n');
448-
((char *)formatted)[size-1] = '\0';
446+
if (formatted == NULL || size == 0) {
447+
return formatted;
448+
}
449+
assert(formatted[size] == '\0');
450+
// Remove a trailing newline if needed.
451+
if (formatted[size-1] == '\n') {
452+
formatted[size-1] = '\0';
453+
}
449454
return formatted;
450455
}
451456

0 commit comments

Comments
 (0)