Extending Python: rewriting a single method in C
jmg at ecs.soton.ac.uk
Tue Mar 13 20:36:35 CET 2001
"Alex Martelli" <aleaxit at yahoo.com> writes:
> > How do I get at the members of a class in C if that class was defined
> > in python ?
> By passing the reference to the instance to the function, and
> receiving it on the C side as a PyObject* -- you can then call
> methods and access attributes on it from C, just as you could
> from Python. See the "O" format code in the documentation of
> PyArg_ParseTuple (1.7 in the "Extending and Embedding" manual).
This is exactly what I have been trying to do, using exactly that
reference as a guide, but I haven't managed to get a working piece of
> > JE> In fast_a and fast_b, you can get arguments by using the ParseTuple
> > JE> "(Oii)" and then use PyObject_SetAttrString and PyObject_GetAttrString
> > JE> to store or retrieve attributes (including, if necessary, instance
> > JE> methods) from the instance by their name.
> > Is there an organized reference where I can find the specifications of
> > all these functions ? All I have come across shows example code, but
> > no full descriptions of what can/must be done.
> It's somewhat disorganized. I think Beazley's "Python Essential
> Reference" (New Riders) is your best bet among current books (but
> it only covers Python 1.5.2).
Even now that the local copy of the above has retured from walkabout,
I am not all that much wiser.
I've not seriously used gettattr before, so maybe I'm missing something.
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.)
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
that o should be a pointer to the object whose attribute I want to get
(which I will have obtained by something like
if (! PyArg_ParseTuple( args, "O", o ) )
). 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 ?
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 ?
(Would it be fair to say that the documentaion is, on the whole, less
that satisfactory ?)
> > Maybe some kind soul could show me what the correct code for a minimal
> > extension type is ?
> > For example, I tried to write an extension type equivalent to
> > class foo:
> > def __init__ ( self, a ):
> > self.a = a
> > def show ( self ):
> > print a
> > My various attempts, almost work, but there is always something not
> > quite right.
> 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 --).
More information about the Python-list