Skip to content

Commit a8d21f6

Browse files
committed
Enhance tests
1 parent 402967d commit a8d21f6

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

Lib/test/test_capi/test_object.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,20 @@ def test_object_setattr_null_exc(self):
251251
class Obj:
252252
pass
253253
obj = Obj()
254-
255254
obj.attr = 123
256-
with self.assertRaises(SystemError):
257-
_testcapi.object_setattr_null_exc(obj, 'attr')
258-
self.assertTrue(hasattr(obj, 'attr'))
259255

260-
with self.assertRaises(SystemError):
261-
_testcapi.object_setattrstring_null_exc(obj, 'attr')
262-
self.assertTrue(hasattr(obj, 'attr'))
256+
exc = ValueError("error")
257+
with self.assertRaises(SystemError) as cm:
258+
_testcapi.object_setattr_null_exc(obj, 'attr', exc)
259+
self.assertIs(cm.exception.__context__, exc)
260+
self.assertIsNone(cm.exception.__cause__)
261+
self.assertHasAttr(obj, 'attr')
262+
263+
with self.assertRaises(SystemError) as cm:
264+
_testcapi.object_setattrstring_null_exc(obj, 'attr', exc)
265+
self.assertIs(cm.exception.__context__, exc)
266+
self.assertIsNone(cm.exception.__cause__)
267+
self.assertHasAttr(obj, 'attr')
263268

264269

265270
if __name__ == "__main__":

Modules/_testcapi/abstract.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ sequence_fast_get_item(PyObject *self, PyObject *args)
181181
static PyObject *
182182
object_setattr_null_exc(PyObject *self, PyObject *args)
183183
{
184-
PyObject *obj, *name;
185-
if (!PyArg_ParseTuple(args, "OO", &obj, &name)) {
184+
PyObject *obj, *name, *exc;
185+
if (!PyArg_ParseTuple(args, "OOO", &obj, &name, &exc)) {
186186
return NULL;
187187
}
188188

189-
PyErr_SetString(PyExc_ValueError, "error");
189+
PyErr_SetObject((PyObject*)Py_TYPE(exc), exc);
190190
if (PyObject_SetAttr(obj, name, NULL) < 0) {
191191
return NULL;
192192
}
@@ -198,13 +198,13 @@ object_setattr_null_exc(PyObject *self, PyObject *args)
198198
static PyObject *
199199
object_setattrstring_null_exc(PyObject *self, PyObject *args)
200200
{
201-
PyObject *obj;
201+
PyObject *obj, *exc;
202202
const char *name;
203-
if (!PyArg_ParseTuple(args, "Os", &obj, &name)) {
203+
if (!PyArg_ParseTuple(args, "OsO", &obj, &name, &exc)) {
204204
return NULL;
205205
}
206206

207-
PyErr_SetString(PyExc_ValueError, "error");
207+
PyErr_SetObject((PyObject*)Py_TYPE(exc), exc);
208208
if (PyObject_SetAttrString(obj, name, NULL) < 0) {
209209
return NULL;
210210
}

0 commit comments

Comments
 (0)