Re: C API PyObject_GetAttrString returns not the object I expected

2019-02-10 Thread Barry Scott
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  instead.
> > 
> > (gdb) p *args_o
> > $4 = 
> > 
> > What is going on and how do I get from the  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.



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_GetAttrString returns not the object I expected

2019-02-10 Thread Stefan Behnel
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  instead.
> 
> (gdb) p *args_o 
> $4 = 
> 
> What is going on and how do I get from the  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.

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/

Stefan

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C API PyObject_GetAttrString returns not the object I expected

2019-02-10 Thread Phil Thompson
On 10 Feb 2019, at 12:08 pm, Barry Scott  wrote:
> 
> After calling PyObject_GetAttrString() I expected to get a PyObject string 
> back but I found that I had been given a  instead.
> 
> (gdb) p *args_o 
> $4 = 
> 
> What is going on and how do I get from the  to the object 
> I 
> want?

The "String" part of "PyObject_GetAttrString()" refers to the way you provide 
the attribute name and not the type of the object returned.

Phil
-- 
https://mail.python.org/mailman/listinfo/python-list


C API PyObject_GetAttrString returns not the object I expected

2019-02-10 Thread Barry Scott
After calling PyObject_GetAttrString() I expected to get a PyObject string 
back but I found that I had been given a  instead.

(gdb) p *args_o 
$4 = 

What is going on and how do I get from the  to the object I 
want?

Barry



-- 
https://mail.python.org/mailman/listinfo/python-list