standard funcs in ext. module..

Will Ware wware at alum.mit.edu
Mon Aug 27 23:08:23 EDT 2001


Gorny wrote:

> static void Xxo_dealloc(XxoObject *self)
> static PyObject *
> Xxo_getattr(XxoObject *self, char *name)
> static int
> Xxo_setattr(XxoObject *self, char *name, PyObject *v)
> 
> these are not mentiond in the methods passed to Py_InitModule().
> In what circumstances calls the python interpreter these functions?

These become methods of an Xxo object. If you look at Modules/xxmodule.c
you'll see the same things (I'm assuming you were looking previously at
Objects/xxobject.c). You'll see that Xxo_getattr is called out in the
Xxo_Type struct, as the "getattr" method. So the time these would get
invoked would be when you did this:

import xx
myXXobject = xx.new()
myXXobject.foo = 3      # this will call Xxo_setattr
print myXXobject.foo    # this will call Xxo_getattr

> import Xxo
> getattr(Xxo, 'foobar')   <---- calling Xxo_getattr()???

I got curious enough to try this:

% gcc -I.. -I../Include -c xxmodule.c
% gcc -shared -o xx.so xxmodule.o
% python
Python 1.5.2 (#1, Mar  3 2001, 01:35:43)  [GCC 2.96 20000731 (Red Hat 
Linux 7.1 2 on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
 >>> import xx
 >>> z = xx.new()
 >>> dir(z)
['demo']
 >>> z.demo()
 >>> getattr(z, "demo")
<built-in method demo of Xxo object at 80b66a0>
 >>> getattr(Xxo, "demo")
Traceback (innermost last):
   File "<stdin>", line 1, in ?
NameError: Xxo
 >>> type(z)
<type 'Xxo'>
 >>> getattr(xx, "demo")
Traceback (innermost last):
   File "<stdin>", line 1, in ?
AttributeError: demo

If you built and linked xxobject.c, it probably wouldn't make
sense to say "import Xxo", any more than you say "import intobject"
before using integers. So there'd be no "Xxo" in the namespace, and
you'd expect a NameError (as I got above) when you tried
getattr(Xxo, "demo").




More information about the Python-list mailing list