Is it possible to cast "PyObject*" to "object" without incrementing refcount?
See original GitHub issueLet’s suppose I have a C function that returns a NEW reference to PyObject
. And then I want to call it and return it’s result from a function that returns object
. Currently if I simply do something like this:
def pyfunc():
return <object>PyDict_New()
There will be refcount increment in the generated pyfunc()
code for the returned dict which is undesirable.
My question is - is there a canonical solution to this problem?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Cython cast PyObject* to object without increasing reference ...
I understand that when I'm make casting like: <object> p , where p is PyObject* reference counter is increased. Is it possible to...
Read more >Reference Counting — Python 3.11.1 documentation
Increment the reference count for object o. This function is usually used to convert a borrowed reference to a strong reference in-place.
Read more >2. PyObjects and Reference Counting
Creation is via Python's C API and destruction is done by decrementing the reference count. If this count hits zero then CPython will...
Read more >Reference Counting in Python
It is not necessary to increment an object's reference count for every local variable that contains a pointer to an object. But don't...
Read more >Finding Reference-Counting Errors in Python/C Programs with ...
In more detail, references to Python objects have type “PyObject ... However, it is safe not to increment the refcount of the object...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You should really use
return {}
instead.Apart from that, the whole point of
object
is to be refcounted. That is the difference toPyObject*
, which is a pointer.My guess is that you declared
PyDict_New()
as returning aPyObject*
. That is incorrect. It returns an owned reference, i.e.object
.Have you tried
@boundscheck(False)
? That’s more likely to make a difference here than the reference counting.