[C++-sig] Re: how to convert c++ objects to Python?

David Abrahams dave at boost-consulting.com
Tue Aug 12 06:38:58 CEST 2003


cavok at filibusta.crema.unimi.it writes:

> hi,
>
> i have a c++ function that returns a reference to a c++ object.  i want
> to expose this function to Python making it return the Python version
> of the object. i'm using Boost.Python.
>
> i understood that i have to use to_python_converter 

Why do you think that?  Your code should work find without it as
written.

> but the body of the
> A_to_Python::convert function is mystery for me. Boost documentation's
> example solves the problem in a manner i'm not able to apply to my case.
> my A object is complex and i'd like to somewhat (re)use the class_ object.
>
> ---------------- CUT HERE ----------------
> #include <boost/python.hpp>
>
> using namespace boost::python;
>
> struct A
> {
> 	void f() {}
> };
>
> A a;
>
> struct A_to_Python
> {
> 	static PyObject* convert(const A& a)
> 	{
> 		// documentation says:
> 		// return PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
> 	}
> };
>
> A& get_A() { return a; }
>
> BOOST_PYTHON_MODULE(hello)
> {
> 	class_<A>("A")
> 		.def("f", &A::f)
> 	;
>
> 	to_python_converter<A, A_to_Python>();
>
> 	def("get_A", get_A, return_value_policy<reference_existing_object>());
> }
> ---------------- CUT HERE ----------------
>
> please, somebody enlighten me.
>
> many thanks
> domenico
>
> -----[ Domenico Andreoli, aka cavok
>  --[ http://filibusta.crema.unimi.it/~cavok/gpgkey.asc
>    ---[ 3A0F 2F80 F79C 678A 8936  4FEE 0677 9033 A20E BC50
>
> ------------------------ Yahoo! Groups Sponsor ---------------------~-->
> Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
> Printer at Myinks.com. Free s/h on orders $50 or more to the US & Canada. http://www.c1tracking.com/l.asp?cid=5511
> http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/EbFolB/TM
> ---------------------------------------------------------------------~->
>
> Info: <http://www.boost.org>
> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl>
> Unsubscribe: <mailto:boost-users-unsubscribe at yahoogroups.com>
>  
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
>
>
>

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list