-
Notifications
You must be signed in to change notification settings - Fork 6
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
Unclear lifetimes of borrowed references #5
Comments
To me reading, this implies that borrowed references can be safely returned, and that we just need some additional mechanism/documentation to make them easier to use safely. I think it would be simpler to say that borrowed references returned from functions cannot be safe in general, so should be treated as always unsafe. |
I agree (with caveats), but that's a solution rather than the problem. This repo is for problem statements. |
Returning borrowed references is, in general, unsafe. So it is a problem that we return them. What I'm saying is that the problem of unclear lifetimes is more simply stated as the more general problem that returning borrowed references is unsafe. |
Could you file a separate issue (or more)? |
I'm with Mark here. While initially it appears convenient to return a borrowed value, the bugs it can cause are too subtle, and we've fixed many bugs in CPython core related to this over the years. |
Let's correct this to "since borrowing is believed to be faster, people want it quite strongly." Performance is a subtle thing. Consistency makes for easier reasoning about behavior, which makes for easier optimization, which makes for better performance. |
We could maybe¹⁾ also correct it to “since borrowing is currently faster, people want it quite strongly.” In the long run, you're right. But people want speed now, and will complain if you take things away without an immediate/certain replacement. ¹⁾ I haven't run the benchmarks so I might be wrong. |
IMO we should design a migration plan to move away from borrowed references. The goal would be to get ride of them by default in the long term. A C extension should be able to define a macro which hides any function returning borrowed references. The number of affected functions is big, and some of them are widely used like Py_TYPE() or PyDict_GetItem(). A first item would be to ease formatting an object type name in an error message without having to use the common Also I failed to find a generic replacement for "get n-th item" API where the index is a C integer: PyTuple_GetItem() and PyList_GetItem() are only for tuple or list, and return a borrowed reference. The HPy API has a "GetItemi" API for that if i recall correctly. |
This might also deserve a separate issue:
See #30 (comment), where I call such APIs convenient. We might want a generic |
What would the difference be between |
Nothing. Sorry, I thought the latter would take a |
Oh right, there is already PySequence_GetItem(). I forgot it. |
See also Function must not return a pointer to content without an explicit resource management: issue #57. |
Lots of functions return borrowed references, but in many cases it's not clear what's lending the reference, and how to ensure that lender keeps the reference alive as long as necessary.
For example,
PyList_GetItem
is very tricky to use 100% correctly -- it's relatively obvious that the list is the lender (so you need to keep it around), but it's not obvious that arbitrary Python code (like a finalizer) could clear the list and drop the reference.The text was updated successfully, but these errors were encountered: