Extending Python: rewriting a single method in C

Alex Martelli aleaxit at yahoo.com
Tue Mar 13 17:13:43 EST 2001


"Jacek Generowicz" <jmg at ecs.soton.ac.uk> wrote in message
news:g0lmq9xxz0.fsf at scumbag.ecs.soton.ac.uk...
    [snip]
> As I understand it, I have to provide a __getattr__ method in my
> python class which will get called when I use PyObject_GetAttr. (If
> this is so then I don't see why the function isn't automatically
> provided; any creative definition---ie differing from the blatantly
> obvious (and hence tedious)---surely only opens the door to
> horrors. Still, mine is not to reason why.)

You need only provide __getattr__ if you have to do something
special -- and when you do need it, it's nice to have the ability.
But attribute-getting works just fine for simple cases without
you writing anything special!


> In Beazley I see that the signature of the PyObject_GetAttr is
>
> PyObject * PyObject_GetAttr( PyObject *o, PyObject *attr_name )
>
> Nowhere can I find an explanation of the variables, so I'm guessing

The online Python/C API reference says of this function: '''Retrieve an
attribute named attr_name from object o. [...] equivalent of the Python
expression "o.attr_name". '''.  Seems clear enough to me -- although,
actually, a closer equivalent is "getattr(o,attr_name)" [a level of
indirectness issue wrt attr_name!-)].  This is in 6.1, "Object Protocol",
of said manual.


> ). But what on earth is attr_name ?  Is it a PyObject* pointing to a
> python string containing the name of the attribute I want ?
>
> How about PyObject_GetAttrString ?  Is it the same as PyObject_GetAttr
> except that I can use a C string to give the name of the attribute ?

Yes, and yes.  Again, the quoted subchapter documents that.


> Assuming that is so, and that these funcitons return a pointer to the
> attribute in question, how do I do anything with it ?  For example,
> assume that I know that PyObject * O points to a python integer, how
> do I convert it to a C integer? or is this The Wrong Thing To Do ?

No it isn't -- see e.g. 7.4.1, same manual: PyInt_Check lets you
check if the object is indeed an integer, PyInt_AS_LONG returns
the C int value for a PyObject* that passes PyInt_Check (or,
PyInt_AsLong coerces-to-integer if needed).


> (Would it be fair to say that the documentaion is, on the whole, less
> that satisfactory ?)

I find it pretty decent, myself.


> > > For example, I tried to write an extension type equivalent to
> > >
> > > class foo:
    [snip]
> > Getting equivalence to this is not going to be easy -- the
> > type/class separation hits hard,
>
> Could you elabore a bit on this, please ? (the bit after --).

Normally, the types you define in C are *not* equivalent to
Python-coded classes -- they can't be inherited from, etc.  To
get closer to equivalence, you need to use a "deep and dark
mystery" in the Python internals known as "the Beaudry hook" --
or use a framework that will do it on your behalf, such as the
Boost Python Library (www.boost.org -- but, be warned, it
needs C++, not just C).


Alex






More information about the Python-list mailing list