Skip to content
Closed
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
Prev Previous commit
Next Next commit
Update test_exceptions
  • Loading branch information
vstinner committed Jun 7, 2024
commit 6a879156a924df2dbe078cc95c6a3b549daf6f2b
14 changes: 10 additions & 4 deletions Lib/test/test_capi/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,22 @@ def test_format(self):
PyErr_Format = getattr(pythonapi, name)
PyErr_Format.argtypes = (py_object, c_char_p,)
PyErr_Format.restype = py_object

with self.assertRaises(ZeroDivisionError) as e:
PyErr_Format(ZeroDivisionError, b'%s %d', b'error', c_int(42))
self.assertEqual(e.exception.args, ('error 42',))

with self.assertRaises(ZeroDivisionError) as e:
PyErr_Format(ZeroDivisionError, b'invalid \xff')
self.assertEqual(e.exception.args, ('invalid \ufffd',))

with self.assertRaises(ZeroDivisionError) as e:
PyErr_Format(ZeroDivisionError, b'%s', 'помилка'.encode())
self.assertEqual(e.exception.args, ('помилка',))

with self.assertRaisesRegex(OverflowError, 'not in range'):
PyErr_Format(ZeroDivisionError, b'%c', c_int(-1))
with self.assertRaisesRegex(ValueError, 'format string'):
PyErr_Format(ZeroDivisionError, b'\xff')

self.assertRaises(SystemError, PyErr_Format, list, b'error')
# CRASHES PyErr_Format(ZeroDivisionError, NULL)
# CRASHES PyErr_Format(py_object(), b'error')
Expand Down Expand Up @@ -377,7 +382,7 @@ def test_err_formatunraisable(self):
self.assertEqual(str(cm.unraisable.exc_value), 'oops!')
self.assertEqual(cm.unraisable.exc_traceback.tb_lineno,
firstline + 15)
self.assertIsNone(cm.unraisable.err_msg)
self.assertEqual(cm.unraisable.err_msg, 'undecodable \ufffd')
self.assertIsNone(cm.unraisable.object)

with support.catch_unraisable_exception() as cm:
Expand All @@ -401,7 +406,8 @@ def test_err_formatunraisable(self):
support.captured_stderr() as stderr):
formatunraisable(CustomError('oops!'), b'undecodable \xff')
lines = stderr.getvalue().splitlines()
self.assertEqual(lines[0], 'Traceback (most recent call last):')
self.assertEqual(lines[0], 'undecodable \ufffd:')
self.assertEqual(lines[1], 'Traceback (most recent call last):')
self.assertEqual(lines[-1], f'{__name__}.CustomError: oops!')

with (support.swap_attr(sys, 'unraisablehook', None),
Expand Down