Skip to content

Commit c142ef2

Browse files
committed
Replacing PyObject_GetAttrString with PyObject_GetOptionalAttrString.
1 parent f58b88a commit c142ef2

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

mypyc/lib-rt/misc_ops.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp,
353353
PyObject *dict, PyObject *annotations) {
354354
PyTypeObject *ttp = (PyTypeObject *)tp;
355355
Py_ssize_t pos;
356-
PyObject *res;
356+
PyObject *res = NULL;
357357

358358
/* Make a copy of the original class __dict__ */
359359
PyObject *orig_dict = PyDict_Copy(ttp->tp_dict);
@@ -384,13 +384,15 @@ CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp,
384384
// These attributes are added or modified by @attr.s(slots=True).
385385
const char * const keys[] = {"__attrs_attrs__", "__attrs_own_setattr__", "__init__", ""};
386386
for (const char * const *key_iter = keys; **key_iter != '\0'; key_iter++) {
387-
PyObject *value = PyObject_GetAttrString(res, *key_iter);
388-
if (value) {
387+
PyObject *value = NULL;
388+
int rv = PyObject_GetOptionalAttrString(res, *key_iter, &value);
389+
if (rv == 1) {
389390
PyObject_SetAttrString(tp, *key_iter, value);
390391
Py_DECREF(value);
392+
} else if (rv == -1) {
393+
goto fail;
391394
}
392395
}
393-
Py_DECREF(res);
394396

395397
/* Copy back the original contents of the dict */
396398
if (_CPy_UpdateObjFromDict(tp, orig_dict) != 0) {
@@ -401,6 +403,7 @@ CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp,
401403
return 1;
402404

403405
fail:
406+
Py_XDECREF(res);
404407
Py_XDECREF(orig_dict);
405408
return 0;
406409
}

0 commit comments

Comments
 (0)