[C++-SIG] Trouble getting a method called

Steve Harris sharris at primus.com
Tue May 23 01:08:21 CEST 2000


"Barry Scott" <barry at scottb.demon.co.uk> writes:

> 	Here is the problem. The object p does not have any attributes.
> 
> 	I suggest that you add cout << debug statements to confirm that
> 	the init_type() and getattr() are really called.

I put some print statements in init_type() and verified that it is
getting called. Here's what I learned:

* You must have called supportGetattr() and implement getattr()
  for a method to be called, even if you don't care about getattr.

* You must call moduleDictionary() in your extension module's
  constructor.

Once I did those _two_ things together, my method appeared. Is this
how it's supposed to work?

If method calls are implemented in terms of getattr(), then calling
add_varargs_method() should implicitly assert that you're "supporting
getattr." Furthermore, it seems that implementing getattr shouldn't be
necessary just to support method calls. After all, all my getattr()
method does is forward a call to getattr_methods(). Can we invert this
such that if there is no method defined for a particular getattr name,
_then_ the call is (optionally) forwarded to the derived class getattr
method? That is, in pseudo-code:

Py::Object handle_getattr_call(const char* pcsz)
{
  const foo::const_iterator it = method_map.find( pcsz );
  if ( it != method_map.end() )
    return *it;  // deref it and return something callable
  else if ( bSupportsGetattr )
    return getattr();
  else
    throw something;
}


This arrangement would make more sense to me, as it better separates
the method dispatching from the getattr implementation. At present,
the add_varargs_method() function is confusing in that it doesn't do
enough to hook up all the machinery to expose the method to Python.


[...]

> 	Make sure you have the latest sources from SourceForge.

Do the latest changes address these problems?


> 	Make sure that the Demo example module works.

Yes, it does. That's where I started pulling random bits of code from
(including the "moduleDictionary()" call). Why is it necessary to call
this function in order for all of this to work?

-- 
Steven E. Harris
Primus Knowledge Solutions, Inc.
http://www.primus.com




More information about the Cplusplus-sig mailing list