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

Steve Harris sharris at primus.com
Mon May 22 20:53:24 CEST 2000

I'm confused about the interaction between add_varargs_method() and
getattr() in PythonExtension<>-derived types.

First, I tried defining a method, registering it with
add_varargs_method() in my init_type() function, then calling the
method from Python. My aim is to be as minimal as possible. Apparently
my type isn't exposing the method properly:

> python
Python 1.5.2 (#3, May 22 2000, 11:01:12)  [GCC 2.95.2 19991024 (release)] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from seh_test import *  
>>> p = some_pod( "a string" )
Constructing some_pod( "a string" ): 0xd8c60
>>> p.do_it()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
AttributeError: 'some_pod' object has no attribute 'do_it'
>>> ^D
Destructing some_pod: 0xd8c6

At that point, I realized that perhaps methods are just special
functors returned from getattr(), so I tried to complete that
implementation as follows:

Py::Object py_bound_pod::do_it(const Py::Tuple& t)
	// ...
	t.verify_length( 0 );


	return Py::Nothing();

// ....

Py::Object py_bound_pod::getattr(const char* pcsz)
{ return getattr_methods( pcsz ); }

void py_bound_pod::init_type()
	Py::PythonType& pytype = behaviors();
	pytype.name( "some_pod" );
	pytype.doc( "A test POD type constructed from a string" );


	add_varargs_method( "do_it", &py_bound_pod::do_it,
						"dump out the nested string" );

With this setup, I get a different error:

>>> p.do_it()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
RuntimeError: Extension object missing a required method.

Can you see what I'm doing wrong, or do I need to post more of the
source code?

Steven E. Harris
Primus Knowledge Solutions, Inc.

More information about the Cplusplus-sig mailing list