[Python-Dev] Make the stable API-ABI usable

Hrvoje Niksic hrvoje.niksic at avl.com
Mon Nov 20 08:31:24 EST 2017


On 11/19/2017 12:50 PM, Serhiy Storchaka wrote:
> But if PyTuple_GET_ITEM() is used for getting a reference to a C array
> of items it can't be replaced with PyTuple_GetItem(). And actually there
> is no replacement for this case in the limited API.
> 
>       PyObject **items = &PyTuple_GET_ITEM(tuple, 0);

That use case might be better covered with a new function, e.g. 
PyTuple_GetStorage, which the PyObject ** pointing to the first element 
of the internal array.

This function would serve two purposes:

* provide the performance benefits of PyTuple_GET_ITEM in tight loops, 
but without the drawback of exposing the PyTuple layout to the code that 
invokes the macro;

* allow invocation of APIs that expect a pointer to contiguous storage, 
such as STL algorithms that expect random access iterators.

Something similar is already available as PySequence_Fast_ITEMS, except 
that one is again a macro, and is tied to PySequence_FAST API, which may 
not be appropriate for the kind of performance-critical code where 
PyTuple_GET_ITEM tends to be used. (That kind of code is designed to 
deal specifically with lists or tuples and doesn't benefit from implicit 
conversion of arbitrary sequences to a temporary list; that conversion 
would only serve to mask bugs.)


More information about the Python-Dev mailing list