bpo-31572: Get rid of using _PyObject_HasAttrId() in pickle. (#3729) · python/cpython@04e36af (original) (raw)

`@@ -4241,19 +4241,23 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,

`

4241

4241

`self->fast = 0;

`

4242

4242

`self->fast_nesting = 0;

`

4243

4243

`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)) {

`

4249

4249

`return -1;

`

``

4250

`+

}

`

``

4251

`+

PyErr_Clear();

`

4250

4252

` }

`

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)) {

`

4256

4258

`return -1;

`

``

4259

`+

}

`

``

4260

`+

PyErr_Clear();

`

4257

4261

` }

`

4258

4262

``

4259

4263

`return 0;

`

`@@ -5208,22 +5212,24 @@ load_frozenset(UnpicklerObject *self)

`

5208

5212

`static PyObject *

`

5209

5213

`instantiate(PyObject *cls, PyObject *args)

`

5210

5214

`{

`

5211

``

`-

PyObject *result = NULL;

`

5212

``

`-

_Py_IDENTIFIER(getinitargs);

`

5213

5215

`/* Caller must assure args are a tuple. Normally, args come from

`

5214

5216

` Pdata_poptuple which packs objects from the top of the stack

`

5215

5217

` into a newly created tuple. */

`

5216

5218

`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);

`

5222

5221

`_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);

`

5225

5231

` }

`

5226

``

`-

return result;

`

``

5232

`+

return PyObject_CallObject(cls, args);

`

5227

5233

`}

`

5228

5234

``

5229

5235

`static int

`

`@@ -6679,17 +6685,14 @@ _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,

`

6679

6685

`return -1;

`

6680

6686

``

6681

6687

`self->fix_imports = fix_imports;

`

6682

``

`-

if (self->fix_imports == -1)

`

6683

``

`-

return -1;

`

6684

6688

``

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();

`

6693

6696

` }

`

6694

6697

``

6695

6698

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

`