C API PyObject_GetAttrString returns <getset_descriptor> not the object I expected

Barry Scott barry at barrys-emacs.org
Sun Feb 10 10:34:24 EST 2019


On Sunday, 10 February 2019 13:58:57 GMT Stefan Behnel wrote:
> Barry Scott schrieb am 10.02.19 um 13:08:
> > After calling PyObject_GetAttrString() I expected to get a PyObject string
> > back but I found that I had been given a <getset_description> instead.
> > 
> > (gdb) p *args_o
> > $4 = <getset_descriptor at remote 0x7fffea87c7e0>
> > 
> > What is going on and how do I get from the <getset_descriptor> to the
> > object I want?
> 
> Phil is right about the function itself, but my guess is that you called
> GetAttr() on a class instead of an instance. Read up on Python descriptors
> to understand what difference that makes.
> 
> https://docs.python.org/3/howto/descriptor.html
> 
> Basically, you got something like a property object back, but not the value
> that the property maintaines. If you look up the attribute on the instance,
> the property (or descriptor) will hand it to you. The same applies to
> method lookups and other special attributes that may also be implemented as
> descriptors.

Thanks that the clue I needed. I had assumed that PyErr_Occurred() returned 
the instance, but it returns the type and as you explained that gives you the 
get_set_descriptor. I need to use PyErr_Fetch to get the instance.


> Also take a look at Cython, which is designed to keep users from having to
> learn all these things and instead lets you do them in Python.
> 
> https://cython.org/

I'm adding better code to look at exceptions to PyCXX and hit this issue.

Barry
PyCXX maintainer.






More information about the Python-list mailing list