Skip to content

Commit fcafc37

Browse files
authored
[3.11] gh-102493: backport unit test for PyErr_SetObject (#102602)
gh-102493: backport unit test for PyErr_SetObject
1 parent e467cb3 commit fcafc37

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Lib/test/test_capi/test_misc.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,34 @@ def test_exc_info(self):
133133
else:
134134
self.assertTrue(False)
135135

136+
def test_set_object(self):
137+
# new exception as obj is not an exception
138+
with self.assertRaises(ValueError) as e:
139+
_testcapi.exc_set_object(ValueError, 42)
140+
self.assertEqual(e.exception.args, (42,))
141+
142+
# wraps the exception because unrelated types
143+
with self.assertRaises(ValueError) as e:
144+
_testcapi.exc_set_object(ValueError, TypeError(1,2,3))
145+
wrapped = e.exception.args[0]
146+
self.assertIsInstance(wrapped, TypeError)
147+
self.assertEqual(wrapped.args, (1, 2, 3))
148+
149+
# is superclass, so does not wrap
150+
with self.assertRaises(PermissionError) as e:
151+
_testcapi.exc_set_object(OSError, PermissionError(24))
152+
self.assertEqual(e.exception.args, (24,))
153+
154+
class Meta(type):
155+
def __subclasscheck__(cls, sub):
156+
1/0
157+
158+
class Broken(Exception, metaclass=Meta):
159+
pass
160+
161+
with self.assertRaises(ZeroDivisionError) as e:
162+
_testcapi.exc_set_object(Broken, Broken())
163+
136164
@unittest.skipUnless(_posixsubprocess, '_posixsubprocess required for this test.')
137165
def test_seq_bytes_to_charp_array(self):
138166
# Issue #15732: crash in _PySequence_BytesToCharpArray()

Modules/_testcapimodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,20 @@ pyobject_bytes_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
25232523
return PyObject_Bytes(NULL);
25242524
}
25252525

2526+
static PyObject *
2527+
exc_set_object(PyObject *self, PyObject *args)
2528+
{
2529+
PyObject *exc;
2530+
PyObject *obj;
2531+
2532+
if (!PyArg_ParseTuple(args, "OO:exc_set_object", &exc, &obj)) {
2533+
return NULL;
2534+
}
2535+
2536+
PyErr_SetObject(exc, obj);
2537+
return NULL;
2538+
}
2539+
25262540
static PyObject *
25272541
raise_exception(PyObject *self, PyObject *args)
25282542
{
@@ -6379,6 +6393,7 @@ static PyObject *getargs_s_hash_int2(PyObject *, PyObject *, PyObject*);
63796393
static PyObject *gh_99240_clear_args(PyObject *, PyObject *);
63806394

63816395
static PyMethodDef TestMethods[] = {
6396+
{"exc_set_object", exc_set_object, METH_VARARGS},
63826397
{"raise_exception", raise_exception, METH_VARARGS},
63836398
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
63846399
{"set_errno", set_errno, METH_VARARGS},

0 commit comments

Comments
 (0)