[issue32571] Speed up and clean up getting optional attributes in C code

Serhiy Storchaka report at bugs.python.org
Tue Jan 16 13:18:05 EST 2018


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

In issue32544 there was introduced a new private C API function _PyObject_GetAttrWithoutError() which never raises an AttributeError, but returns NULL without error set if an attribute is absent. This allowed to speed up Python builtins hasattr() and getattr() with the default argument.

But C code also could gain a benefit from this. It is common to look up an attribute and silence an AttributeError. Actually it is more common than the opposite case.

The proposed patch adds yet one function, _PyObject_GetAttrIdWithoutError(), and uses these two functions if it is possible to avoid checking an AttributeError after PyObject_GetAttr() and _PyObject_GetAttrId(). This could speed up some code, and also makes it cleaner.

----------
components: Extension Modules, Interpreter Core
messages: 310101
nosy: inada.naoki, serhiy.storchaka, vstinner
priority: normal
severity: normal
status: open
title: Speed up and clean up getting optional attributes in C code
type: performance
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32571>
_______________________________________


More information about the Python-bugs-list mailing list