-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PyList_GetItemRef()
(a variant of PyList_GetItem
that returns a strong reference)
#9
Comments
Sounds good to me, given the precedent. |
Why int PyList_GetItemRef(PyObject *list, Py_ssize_t index, PyObject **result) and not simply PyObject *PyList_GetItemRef(PyObject *list, Py_ssize_t index) ? For |
I agree with Serhyi. If there are only two return codes, the error should be indicated by The precedent should be for cases where one kind of “failure” is very common, and we want to avoid allocating an exception in this common case. For list, users can easily check whether they have a valid index (e.g. they're getting items in a |
Okay, I can live with that, too. It means |
Yes, I felt that it would be better to use a different verb for such kind of API and asked about this before adding But |
Okay, I lost track of what we've decided here. Or is there still no conclusion? |
AFAIK, we're agreeing on everything except a general naming scheme (which would be incompatible with current API). One thing the OP doesn't cover is type checking. in issues#29 the consensus was that arguments should be checked at runtime. (Here, this is fast thanks to I hope it's time for a formal vote:
returning:
@colesbury, I hope that's fine implementation-wise. |
@colesbury: Can you please run a microbenchmark to measure the performance of |
+1: I'm fine with
Maybe there is a way to remove a few conditions in |
Presumably it's the memory indirections that hurt. It bypasses the I hope that such a minor perf difference doesn't encourage developers to prevent custom types being used with their functions, but it does seem difficult to justify not having a slightly faster path for those who already know they have a list [subclass]. Perhaps a function to get the Otherwise, yes fine with the same API but better semantics. |
Follow-up question: should this be part of the stable ABI? (I think so -- the similar functions |
@iritkatriel, what's your opinion on the proposed API? @gvanrossum, it sounded like you were in favor of this API. If so, would you please mark as such on Petr's vote checkboxes: #9 (comment) |
LGTM |
FYI, here's the PR that adds |
I'm fine with adding PyList_GetItemRef() to the stable ABI 3.13. |
It was perhaps unnoticed, but there is yet one difference of the new C API from |
Sam's comment:
|
The transitive error behaviour (C extension that just passes a user value into the call) is going to be more consistent. If we're going to do the type check, we shouldn't force the user to also do it and generate a normal looking exception message themselves. |
I can see the arguments for both exception types. But The vote is over; I'm closing this issue. Follow-ups welcome. |
Issue: python/cpython#114329
PR: python/cpython#114504
EDIT: Updated signature based on feedback below.
The free-threaded builds need a variant of
PyList_GetItem
that returns a strong reference instead of a borrowed reference for thread-safety reasons. PEP 703 proposedPyList_FetchItem
, but since thenPyDict_GetItemRef
and functions with similar signatures have been added.This proposes
PyList_GetItemRef
with the following signature:Return a strong reference to the object at position index in the list pointed to by list. If
index
is out of bounds (<0 or >=len(list)), return NULL and set an IndexError. Iflist
is not a list instance, return NULL and set a TypeError.The text was updated successfully, but these errors were encountered: