summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-10-22 18:31:34 (GMT)
committerGitHub <noreply@github.com>2017-10-22 18:31:34 (GMT)
commit04e36af9b88cd5e80fc818e51d46f07252a2ff3f (patch)
tree56b0323213eed8da8ed589d51de4a41f7289d864 /Modules
parentc872d39d324cd6f1a71b73e10406bbaed192d35f (diff)
downloadcpython-04e36af9b88cd5e80fc818e51d46f07252a2ff3f.zip
cpython-04e36af9b88cd5e80fc818e51d46f07252a2ff3f.tar.gz
cpython-04e36af9b88cd5e80fc818e51d46f07252a2ff3f.tar.bz2
bpo-31572: Get rid of using _PyObject_HasAttrId() in pickle. (#3729)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_pickle.c63
1 files changed, 33 insertions, 30 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index d531dee..b71fb93 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -4241,19 +4241,23 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
self->fast = 0;
self->fast_nesting = 0;
self->fast_memo = NULL;
- self->pers_func = NULL;
- if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
- self->pers_func = _PyObject_GetAttrId((PyObject *)self,
- &PyId_persistent_id);
- if (self->pers_func == NULL)
+
+ self->pers_func = _PyObject_GetAttrId((PyObject *)self,
+ &PyId_persistent_id);
+ if (self->pers_func == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
return -1;
+ }
+ PyErr_Clear();
}
- self->dispatch_table = NULL;
- if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
- self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
- &PyId_dispatch_table);
- if (self->dispatch_table == NULL)
+
+ self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
+ &PyId_dispatch_table);
+ if (self->dispatch_table == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
return -1;
+ }
+ PyErr_Clear();
}
return 0;
@@ -5208,22 +5212,24 @@ load_frozenset(UnpicklerObject *self)
static PyObject *
instantiate(PyObject *cls, PyObject *args)
{
- PyObject *result = NULL;
- _Py_IDENTIFIER(__getinitargs__);
/* Caller must assure args are a tuple. Normally, args come from
Pdata_poptuple which packs objects from the top of the stack
into a newly created tuple. */
assert(PyTuple_Check(args));
- if (PyTuple_GET_SIZE(args) > 0 || !PyType_Check(cls) ||
- _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
- result = PyObject_CallObject(cls, args);
- }
- else {
+ if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
+ _Py_IDENTIFIER(__getinitargs__);
_Py_IDENTIFIER(__new__);
-
- result = _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
+ PyObject *func = _PyObject_GetAttrId(cls, &PyId___getinitargs__);
+ if (func == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ return NULL;
+ }
+ PyErr_Clear();
+ return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
+ }
+ Py_DECREF(func);
}
- return result;
+ return PyObject_CallObject(cls, args);
}
static int
@@ -6679,17 +6685,14 @@ _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
return -1;
self->fix_imports = fix_imports;
- if (self->fix_imports == -1)
- return -1;
- if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
- self->pers_func = _PyObject_GetAttrId((PyObject *)self,
- &PyId_persistent_load);
- if (self->pers_func == NULL)
- return 1;
- }
- else {
- self->pers_func = NULL;
+ self->pers_func = _PyObject_GetAttrId((PyObject *)self,
+ &PyId_persistent_load);
+ if (self->pers_func == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ return -1;
+ }
+ PyErr_Clear();
}
self->stack = (Pdata *)Pdata_New();