Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5670764

Browse files
committed
Use _PyObject_CallMethodIdObjArgs() in _ctypes
Issue #28915: Replace _PyObject_CallMethodId() with _PyObject_CallMethodIdObjArgs() in unpickle(). _PyObject_CallMethodIdObjArgs() avoids the creation of a temporary tuple and doesn't have to parse a format string. Replace _PyObject_CallMethodId() with _PyObject_GetAttrId()+PyObject_Call() for the second call since it requires to "unpack" a tuple. Add also a check in the type of the second parameter (state): it must be a tuple.
1 parent ddc120f commit 5670764

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

Modules/_ctypes/callproc.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,25 +1631,33 @@ resize(PyObject *self, PyObject *args)
16311631
static PyObject *
16321632
unpickle(PyObject *self, PyObject *args)
16331633
{
1634-
PyObject *typ;
1635-
PyObject *state;
1636-
PyObject *result;
1637-
PyObject *tmp;
1634+
PyObject *typ, *state, *meth, *obj, *result;
16381635
_Py_IDENTIFIER(__new__);
16391636
_Py_IDENTIFIER(__setstate__);
16401637

1641-
if (!PyArg_ParseTuple(args, "OO", &typ, &state))
1638+
if (!PyArg_ParseTuple(args, "OO!", &typ, &PyTuple_Type, &state))
16421639
return NULL;
1643-
result = _PyObject_CallMethodId(typ, &PyId___new__, "O", typ);
1644-
if (result == NULL)
1645-
return NULL;
1646-
tmp = _PyObject_CallMethodId(result, &PyId___setstate__, "O", state);
1647-
if (tmp == NULL) {
1648-
Py_DECREF(result);
1640+
obj = _PyObject_CallMethodIdObjArgs(typ, &PyId___new__, typ, NULL);
1641+
if (obj == NULL)
16491642
return NULL;
1643+
1644+
meth = _PyObject_GetAttrId(obj, &PyId___setstate__);
1645+
if (meth == NULL) {
1646+
goto error;
16501647
}
1651-
Py_DECREF(tmp);
1652-
return result;
1648+
1649+
result = PyObject_Call(meth, state, NULL);
1650+
Py_DECREF(meth);
1651+
if (result == NULL) {
1652+
goto error;
1653+
}
1654+
Py_DECREF(result);
1655+
1656+
return obj;
1657+
1658+
error:
1659+
Py_DECREF(obj);
1660+
return NULL;
16531661
}
16541662

16551663
static PyObject *

0 commit comments

Comments
 (0)