[C++-sig] IndexError when returning pointer

Nicodemus nicodemus at globalite.com.br
Fri Apr 11 22:25:54 CEST 2003


Hail Camelo!

Use return_value_policy<reference_existing_object> for the policy of 
"get_singleton". That should do it.

Pato!
Nicodemus.

Marcelo A. Camelo wrote:

>Oops,
>
>I've made a terrible mess on the sample code 
>I've sent on my last message. Sorry!!!
>
>Here is the correct version:
>
>	struct foo
>	{
>		static foo* get_singleton() { return singleton; }
>		static foo* singleton;
>	};
>
>	foo* foo::singleton = NULL;
>
>	BOOST_PYTHON_MODULE( frog )
>	{
>		using namespace boost::python;
>
>		class_< foo, boost::noncopyable >( "foo", no_init )
>			.def( "get_singleton", (foo*(*)() )
>&foo::get_singleton, return_internal_reference<>() )
>			.staticmethod( "get_singleton" )
>		;
>	}
>
>
>	>>> import frog
>	>>> a = frog.foo.get_singleton()
>	Traceback (most recent call last):
>	  File "<stdin>", line 1, in ?
>	IndexError: tuple index out of range
>	>>>
>
>-----Original Message-----
>From: c++-sig-admin at python.org [mailto:c++-sig-admin at python.org] On
>Behalf Of Marcelo A. Camelo
>Sent: sexta-feira, 11 de abril de 2003 16:11
>To: c++-sig at python.org
>Subject: [C++-sig] IndexError when returning pointer
>
>
>Hi,
>
>I'm exporting a singleton class:
>
>	struct foo
>	{
>		static foo* get_singleton() { return singleton; }
>		foo* singleton;
>	};
>
>	BOOST_PYTHON_MODULE( _frog )
>	{
>		class_< foo, boost::noncopyable >( "foo", no_init )
>			.def( "bar", (foo*(*)() ) &foo::bar,
>return_internal_reference<>() )
>			.staticmethod( "bar" )
>		;
>	}
>
>But, when I try to use it, I get:
>
>	>>> import _foo
>	>>> a = _frog.foo.get_singleton()
>	Traceback (most recent call last):
>	  File "<stdin>", line 1, in ?
>	IndexError: tuple index out of range
>	>>>
>
>I couldn't figure out what I'm doing wrong.
>Can anyone shed some light on this?
>
>[]'s 
> 
>
>Marcelo A. Camelo, M. Eng. - Project Leader
>ESSS - Engineering Simulation and Scientific Software
> 
>E-mail: camelo at esss.com.br
>Phone: +55-48-239-2226
> 
>
>
>_______________________________________________
>C++-sig mailing list
>C++-sig at python.org
>http://mail.python.org/mailman/listinfo/c++-sig
>
>
>_______________________________________________
>C++-sig mailing list
>C++-sig at python.org
>http://mail.python.org/mailman/listinfo/c++-sig
>
>.
>
>  
>






More information about the Cplusplus-sig mailing list