Skip to content

Commit 77af0a3

Browse files
[3.6] bpo-31572: Get rid of using _PyObject_HasAttrId() in pickle. (GH-3729). (#4081)
(cherry picked from commit 04e36af)
1 parent 251de30 commit 77af0a3

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

Modules/_pickle.c

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,19 +4198,23 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
41984198
self->fast = 0;
41994199
self->fast_nesting = 0;
42004200
self->fast_memo = NULL;
4201-
self->pers_func = NULL;
4202-
if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4203-
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4204-
&PyId_persistent_id);
4205-
if (self->pers_func == NULL)
4201+
4202+
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4203+
&PyId_persistent_id);
4204+
if (self->pers_func == NULL) {
4205+
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
42064206
return -1;
4207+
}
4208+
PyErr_Clear();
42074209
}
4208-
self->dispatch_table = NULL;
4209-
if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4210-
self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4211-
&PyId_dispatch_table);
4212-
if (self->dispatch_table == NULL)
4210+
4211+
self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4212+
&PyId_dispatch_table);
4213+
if (self->dispatch_table == NULL) {
4214+
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
42134215
return -1;
4216+
}
4217+
PyErr_Clear();
42144218
}
42154219

42164220
return 0;
@@ -5165,22 +5169,24 @@ load_frozenset(UnpicklerObject *self)
51655169
static PyObject *
51665170
instantiate(PyObject *cls, PyObject *args)
51675171
{
5168-
PyObject *result = NULL;
5169-
_Py_IDENTIFIER(__getinitargs__);
51705172
/* Caller must assure args are a tuple. Normally, args come from
51715173
Pdata_poptuple which packs objects from the top of the stack
51725174
into a newly created tuple. */
51735175
assert(PyTuple_Check(args));
5174-
if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
5175-
_PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
5176-
result = PyObject_CallObject(cls, args);
5177-
}
5178-
else {
5176+
if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
5177+
_Py_IDENTIFIER(__getinitargs__);
51795178
_Py_IDENTIFIER(__new__);
5180-
5181-
result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
5179+
PyObject *func = _PyObject_GetAttrId(cls, &PyId___getinitargs__);
5180+
if (func == NULL) {
5181+
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
5182+
return NULL;
5183+
}
5184+
PyErr_Clear();
5185+
return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
5186+
}
5187+
Py_DECREF(func);
51825188
}
5183-
return result;
5189+
return PyObject_CallObject(cls, args);
51845190
}
51855191

51865192
static int
@@ -6626,17 +6632,14 @@ _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
66266632
return -1;
66276633

66286634
self->fix_imports = fix_imports;
6629-
if (self->fix_imports == -1)
6630-
return -1;
66316635

6632-
if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
6633-
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6634-
&PyId_persistent_load);
6635-
if (self->pers_func == NULL)
6636-
return 1;
6637-
}
6638-
else {
6639-
self->pers_func = NULL;
6636+
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6637+
&PyId_persistent_load);
6638+
if (self->pers_func == NULL) {
6639+
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
6640+
return -1;
6641+
}
6642+
PyErr_Clear();
66406643
}
66416644

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

0 commit comments

Comments
 (0)