question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to cast "PyObject*" to "object" without incrementing refcount?

See original GitHub issue

Let’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:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
scodercommented, Mar 27, 2020

You should really use return {} instead.

Apart from that, the whole point of object is to be refcounted. That is the difference to PyObject*, which is a pointer.

My guess is that you declared PyDict_New() as returning a PyObject*. That is incorrect. It returns an owned reference, i.e. object.

0reactions
scodercommented, Mar 29, 2020

What would you say about functions like PyTuple_GET_ITEM()? Is it normal to use them? I’ve found out that writing them by hand in Cython files gives slight speed advantage.

Have you tried @boundscheck(False)? That’s more likely to make a difference here than the reference counting.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found