bpo-35444: Unify and optimize the helper for getting a builtin object. (GH-11047)

This speeds up pickling of some iterators.

This fixes also error handling in pickling methods when fail to
look up builtin "getattr".
This commit is contained in:
Serhiy Storchaka 2018-12-11 08:28:18 +02:00 committed by GitHub
parent 7cf3d8e251
commit bb86bf4c4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 68 additions and 72 deletions

View file

@ -1080,23 +1080,6 @@ PyObject_SelfIter(PyObject *obj)
return obj;
}
/* Convenience function to get a builtin from its name */
PyObject *
_PyObject_GetBuiltin(const char *name)
{
PyObject *mod_name, *mod, *attr;
mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
if (mod_name == NULL)
return NULL;
mod = PyImport_Import(mod_name);
if (mod == NULL)
return NULL;
attr = PyObject_GetAttrString(mod, name);
Py_DECREF(mod);
return attr;
}
/* Helper used when the __next__ method is removed from a type:
tp_iternext is never NULL and can be safely called without checking
on every iteration.