[New-bugs-announce] [issue35444] Unify and optimize the helper for getting a builtin object

Serhiy Storchaka report at bugs.python.org
Sun Dec 9 06:01:30 EST 2018

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

_PyIter_GetBuiltin() was introduced in issue14288 (31668b8f7a3efc7b17511bb08525b28e8ff5f23a). This was used for getting references to the builtin "iter" and "reverse". It was renamed to _PyObject_GetBuiltin() in a701388de1135241b5a8e4c970e06c0e83a66dc0.

There is other code that gets references to the builtin "getattr" using PyEval_GetBuiltins(). It is more efficient, but contains bugs.

The proposed PR unifies getting references to builtins:

* The prefix _PyObject_ is changed to _PyEval_, since this function has relation not to the object type but to the evaluation environment.

* It uses now the private _Py_Identifier API instead of a raw C string. This saves time by omitting the creation of a Unicode object on every call.

* It uses now fast PyEval_GetBuiltins() instead of slower PyImport_Import().

* Fixed errors handling in the code that used PyEval_GetBuiltins() before. It no longer swallows unexpected exceptions, no longer returns an error without setting an exception, and no longer causes raising a SystemError.

An example of an error in current code:

>>> import builtins
>>> del builtins.getattr
>>> int.bit_length.__reduce__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: NULL object passed to Py_BuildValue

components: Interpreter Core
messages: 331424
nosy: kristjan.jonsson, pitrou, serhiy.storchaka, vstinner
priority: normal
severity: normal
status: open
title: Unify and optimize the helper for getting a builtin object
versions: Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list