Skip to content

Commit 04e36af

Browse files
bpo-31572: Get rid of using _PyObject_HasAttrId() in pickle. (#3729)
1 parent c872d39 commit 04e36af

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

Modules/_pickle.c

+33-30
Original file line numberDiff line numberDiff line change
@@ -4241,19 +4241,23 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
42414241
self->fast = 0;
42424242
self->fast_nesting = 0;
42434243
self->fast_memo = NULL;
4244-
self->pers_func = NULL;
4245-
if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4246-
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4247-
&PyId_persistent_id);
4248-
if (self->pers_func == NULL)
4244+
4245+
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4246+
&PyId_persistent_id);
4247+
if (self->pers_func == NULL) {
4248+
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
42494249
return -1;
4250+
}
4251+
PyErr_Clear();
42504252
}
4251-
self->dispatch_table = NULL;
4252-
if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4253-
self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4254-
&PyId_dispatch_table);
4255-
if (self->dispatch_table == NULL)
4253+
4254+
self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4255+
&PyId_dispatch_table);
4256+
if (self->dispatch_table == NULL) {
4257+
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
42564258
return -1;
4259+
}
4260+
PyErr_Clear();
42574261
}
42584262

42594263
return 0;
@@ -5208,22 +5212,24 @@ load_frozenset(UnpicklerObject *self)
52085212
static PyObject *
52095213
instantiate(PyObject *cls, PyObject *args)
52105214
{
5211-
PyObject *result = NULL;
5212-
_Py_IDENTIFIER(__getinitargs__);
52135215
/* Caller must assure args are a tuple. Normally, args come from
52145216
Pdata_poptuple which packs objects from the top of the stack
52155217
into a newly created tuple. */
52165218
assert(PyTuple_Check(args));
5217-
if (PyTuple_GET_SIZE(args) > 0 || !PyType_Check(cls) ||
5218-
_PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
5219-
result = PyObject_CallObject(cls, args);
5220-
}
5221-
else {
5219+
if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5220+
_Py_IDENTIFIER(__getinitargs__);
52225221
_Py_IDENTIFIER(__new__);
5223-
5224-
result = _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5222+
PyObject *func = _PyObject_GetAttrId(cls, &PyId___getinitargs__);
5223+
if (func == NULL) {
5224+
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
5225+
return NULL;
5226+
}
5227+
PyErr_Clear();
5228+
return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5229+
}
5230+
Py_DECREF(func);
52255231
}
5226-
return result;
5232+
return PyObject_CallObject(cls, args);
52275233
}
52285234

52295235
static int
@@ -6679,17 +6685,14 @@ _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
66796685
return -1;
66806686

66816687
self->fix_imports = fix_imports;
6682-
if (self->fix_imports == -1)
6683-
return -1;
66846688

6685-
if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
6686-
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6687-
&PyId_persistent_load);
6688-
if (self->pers_func == NULL)
6689-
return 1;
6690-
}
6691-
else {
6692-
self->pers_func = NULL;
6689+
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6690+
&PyId_persistent_load);
6691+
if (self->pers_func == NULL) {
6692+
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
6693+
return -1;
6694+
}
6695+
PyErr_Clear();
66936696
}
66946697

66956698
self->stack = (Pdata *)Pdata_New();

0 commit comments

Comments
 (0)