From roman.yakovenko at gmail.com Sun Apr 1 08:07:11 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 1 Apr 2007 09:07:11 +0300 Subject: [C++-sig] Two Boost.Python talks at BoostCon'07 In-Reply-To: <87slbm2ypf.fsf@valverde.peloton> References: <87slbm2ypf.fsf@valverde.peloton> Message-ID: <7465b6170703312307j2f659223re0363455b2b8c8db@mail.gmail.com> On 3/31/07, David Abrahams wrote: > > > I'm giving a tutorial called "Hybrid Development with Boost.Python > (and More!)" > [http://www.boostcon.com/program/sessions#abrahams-hybrid] and Timothy > Shead will be speaking on "Implementing out-of-the-ordinary object > models using Boost.Python" > [http://www.boostcon.com/program/sessions#shead-python-object-models] > > Will this talk include few words about the future of the library? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at boost-consulting.com Mon Apr 2 03:06:39 2007 From: dave at boost-consulting.com (David Abrahams) Date: Sun, 01 Apr 2007 21:06:39 -0400 Subject: [C++-sig] debug-python support [was: 1.34 - ONLY ONE FAILURE!] References: <87k5wwrwla.fsf@valverde.peloton> Message-ID: <877isvpotc.fsf@valverde.peloton> on Sun Apr 01 2007, David Abrahams wrote: > As of this morning, we're down to a single failure for 1.34, and > that's just a tester configuration issue. Once Roland Schwartz > configures his cygwin python, that test (and all the Boost.Python > tests that have been marked expected failure on cygwin) will pass. > > Just thought y'all would like to know. And I just checked in what I hope are the final adjustments to BBv2 Python support: the ability to build against a --with-pydebug version of Python. Although our tests don't depend on it, that feature was available in BBv1 and the Python build documentation describes it, so it couldn't disappear. I tested it on a wide variety of platforms before checking it in, so hopefully nothing will break. If something goes wrong, it should be an easy fix (or rollback, if absolutely necessary). -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From dave at boost-consulting.com Mon Apr 2 14:09:42 2007 From: dave at boost-consulting.com (David Abrahams) Date: Mon, 02 Apr 2007 08:09:42 -0400 Subject: [C++-sig] Two Boost.Python talks at BoostCon'07 References: <87slbm2ypf.fsf@valverde.peloton> <7465b6170703312307j2f659223re0363455b2b8c8db@mail.gmail.com> Message-ID: <87odm7t1tl.fsf@valverde.peloton> on Sun Apr 01 2007, "Roman Yakovenko" wrote: > On 3/31/07, David Abrahams wrote: > > I'm giving a tutorial called "Hybrid Development with Boost.Python > (and More!)" > [http://www.boostcon.com/program/sessions#abrahams-hybrid ] and Timothy > Shead will be speaking on "Implementing out-of-the-ordinary object > models using Boost.Python" > [http://www.boostcon.com/program/sessions#shead-python-object-models ] > > Will this talk include few words about the future of the library? Mine will. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From koen_van_herck at yahoo.com Mon Apr 2 17:30:15 2007 From: koen_van_herck at yahoo.com (Koen Van Herck) Date: Mon, 2 Apr 2007 17:30:15 +0200 Subject: [C++-sig] How to return embedded Python object Message-ID: <000001c7753b$d37292d0$9428000a@ctc110207002> Hello, I've got the following class Foo, which is noncopyable, exposed to Python: class Foo: ::boost::noncopyable { public: ::boost::python::object myself() { return ::boost::python::object(::boost::python::ptr(this)); } }; ::boost::python::class_< Foo, ::boost::noncopyable > ("Foo") .def("myself", &Foo::myself) ; I create a Foo() instance from Python: >>> f = Foo() and now I want the myself() method to return a Python object pointing to the same C++ object. With the code above, I can do: >>> m = f.myself() and use m as if it were f (though f==m returns False, but that's not an issue for me). However, if I delete f, or f goes out of scope, the C++ instance is deleted and m points to a deleted C++ object. If I replace the return statement with return ::boost::python::object(this); I get a runtime error: "No to_python (by-value) converter found for C++ type: class Foo", which makes sense because the class is noncopyable. What is the correct way to return the embedded Python object in the myself() method, such that reference counting works, i.e., the C++ instance is deleted when both f and m objects are deleted. My actual use case does not simply return 'this', but returns a list of class instance which hold a pointer to the Foo instance as a data member. Thanks for any help, Koen Van Herck. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chad.hughes at pnl.gov Mon Apr 2 17:38:43 2007 From: chad.hughes at pnl.gov (Hughes, Chad O) Date: Mon, 2 Apr 2007 08:38:43 -0700 Subject: [C++-sig] C coded extinsion question Message-ID: I am writing a c type extension for Python deriving from the list class (using PyList_Type.tp_init). I have the need to overload the ?+? operator (To add two numbers stored as binary lists ? instead of concatenation). I have tried the following: static PyMethodDef BinaryList_methods[] = { ... {"__add__", (PyCFunction)BinaryList___add__, METH_VARARGS, PyDoc_STR("__add__(self,other) -> BinaryList")}, ... {NULL, NULL}, }; This the BinaryList___add__ function is called if I use the __add__ explicitly but the list base class __add__ is called for the ?+? operator, for example: a = BinaryList([1,0,0,0]) b = BinaryList([0,0,0,1]) print a.__add__(b) #prints [1,0,0,1] print a + b #prints [1,0,0,0,0,0,0,1] I read this: object.h -- Added tp_call member to the typeobject struct This along with a minor change to the ceval.c allows overloading of the function call operator for any class. Can anyone either tell me what I am doing wrong and or give me an example of how to properly overload an operator in a c coded extension? From francis_moreau at hotmail.com Mon Apr 2 22:10:15 2007 From: francis_moreau at hotmail.com (Francis Moreau) Date: Mon, 2 Apr 2007 16:10:15 -0400 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr Message-ID: Hi, I am passing Python objects to C++ through a shared_ptr smart pointer and it's work well. However I need to have the same behaviour as shared_ptr but with my own version of smart pointer. I try to register and expose my custom smart pointer but I have this compilation error: boost/python/pointee.hpp(28) : error C2039: 'element_type': is not a member of 'CountedObjPtr' with [ T=PyCallEngineState ] boost/python/pointee.hpp(38) : see reference to class template instantiation 'boost::python::detail::pointee_impl::apply' being compiled with [ T=CountedObjPtr ] boost/python/register_ptr_to_python.hpp(17) : see reference to class template instantiation 'boost::python::pointee' being compiled with [ T=CountedObjPtr ] PythonModules.cpp(113) : see reference to function template instantiation 'void boost::python::register_ptr_to_python>(void)'being compiled with [ T=PyCallEngineState ] Here is the content of the from_python file for my smart pointer: template struct CountedObjPtr_from_python { CountedObjPtr_from_python() { converter::registry::insert(&convertible, &construct, type_id >()); } static void* convertible(PyObject* p) { if (p == Py_None) { return p; } return converter::get_lvalue_from_python( p, python::converter::registered::converters); } static void construct(PyObject* source, python::converter::rvalue_from_python_stage1_data* data) { void* const storage = ((python::converter::rvalue_from_python_storage >*)data)->storage.bytes; // Deal with the "None" case. if (data->convertible == source) { new (storage) CountedObjPtr(); } else { new (storage) CountedObjPtr(static_cast(data->convertible)); } data->convertible = storage; } }; And here is the code where I register the smart pointer: namespace boost{ namespace python{ template struct pointee< CountedObjPtr > { typedef T type; }; template inline T* get_pointer(CountedObjPtr const& p) { return const_cast(p.get()); } }} template void register_CountedObjPtr_conversions() { boost::python::converter::registry::insert( &CountedObjPtr_from_python::convertible, &CountedObjPtr_from_python::construct, boost::python::type_id >()); } BOOST_PYTHON_MODULE(CallEngineExtending) { python::class_("PyCallEngineState") ; //register_ptr_to_python >(); python::register_ptr_to_python >(); register_CountedObjPtr_conversions(); } Is anyone knows what is missing or wrong with this code? Also, I am not sure this code manages the ref counting properly since there is no call to borrowed or something like this? Any help would be appreciated, Francis From rbrown at gamry.com Mon Apr 2 23:59:00 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Mon, 02 Apr 2007 17:59:00 -0400 Subject: [C++-sig] Embedding Python with Boost Message-ID: Greetings, I've been having a bit of trouble in making a simple embedded python application using boost, and am in need of some guidance. Hopefully I'm in the right place. All I'm trying to do is just fit the basic pieces together to see if embedding python is a viable solution for my next project. I'm on Windows using mingw tools, specifically: g++ (GCC) 3.4.2 (mingw-special) Boost 1.33.1 Python 2.5 I was able to build Boost after minimal problems, mostly from setting up my environment for Boost.Jam. All of the examples, including the embedding.cpp example, seem to all compile/link just fine, and I'm able to import the example Python extensions and use them. My problem lies in getting the most basic embedded Python application (see below) to link. I'm trying to link my simple app here with boost_python.lib and python25.lib. When I try to link, I'm getting: C:/Boost/boost_1_33_1/boost/python/cast.hpp: undefined reference to `_imp___Py_NoneStruct' Am I barking up the wrong tree by trying to statically link those libs? Any advice that anyone could give would be greatly appreciated. Thanks in advance, -Ron ------------------------------ #include int main() { Py_Initialize(); Py_Finalize(); return 0; } ------------------------------ From francis_moreau at hotmail.com Tue Apr 3 16:17:12 2007 From: francis_moreau at hotmail.com (Francis Moreau) Date: Tue, 3 Apr 2007 10:17:12 -0400 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr References: Message-ID: Ok I found my compilation problem. "Francis Moreau" wrote in message news:eurnv7$5cg$1 at sea.gmane.org... > Hi, > > I am passing Python objects to C++ through a shared_ptr smart pointer and > it's work well. However I need to have the same behaviour as shared_ptr > but with my own version of smart pointer. I try to register and expose my > custom smart pointer but I have this compilation error: > > boost/python/pointee.hpp(28) : error C2039: 'element_type': is not a > member of 'CountedObjPtr' > with > [ > T=PyCallEngineState > ] > boost/python/pointee.hpp(38) : see reference to class template > instantiation 'boost::python::detail::pointee_impl::apply' being > compiled > with > [ > T=CountedObjPtr > ] > boost/python/register_ptr_to_python.hpp(17) : see reference to > class template instantiation 'boost::python::pointee' being compiled > with > [ > T=CountedObjPtr > ] > PythonModules.cpp(113) : see reference to function template > instantiation 'void > boost::python::register_ptr_to_python>(void)'being > compiled > with > [ > T=PyCallEngineState > ] > > > Here is the content of the from_python file for my smart pointer: > > template > struct CountedObjPtr_from_python > { > CountedObjPtr_from_python() > { > converter::registry::insert(&convertible, > &construct, > type_id >()); > } > > static void* convertible(PyObject* p) > { > if (p == Py_None) { > return p; > } > > return converter::get_lvalue_from_python( > p, python::converter::registered::converters); > } > > static void construct(PyObject* source, > > python::converter::rvalue_from_python_stage1_data* data) > { > void* const storage = > ((python::converter::rvalue_from_python_storage > >*)data)->storage.bytes; > // Deal with the "None" case. > if (data->convertible == source) { > new (storage) CountedObjPtr(); > } else { > new (storage) CountedObjPtr(static_cast(data->convertible)); > } > > data->convertible = storage; > } > }; > > > And here is the code where I register the smart pointer: > > namespace boost{ namespace python{ > > template > struct pointee< CountedObjPtr > > { > typedef T type; > }; > > template > inline T* get_pointer(CountedObjPtr const& p) > { > return const_cast(p.get()); > } > > }} > > template > void register_CountedObjPtr_conversions() > { > boost::python::converter::registry::insert( > &CountedObjPtr_from_python::convertible, > &CountedObjPtr_from_python::construct, > boost::python::type_id >()); > } > > BOOST_PYTHON_MODULE(CallEngineExtending) > { > python::class_ boost::noncopyable>("PyCallEngineState") > ; > > //register_ptr_to_python >(); > > python::register_ptr_to_python >(); > register_CountedObjPtr_conversions(); > } > > Is anyone knows what is missing or wrong with this code? Also, I am not > sure this code manages the ref counting properly since there is no call to > borrowed or something like this? > > Any help would be appreciated, > Francis From dave at boost-consulting.com Tue Apr 3 19:15:55 2007 From: dave at boost-consulting.com (David Abrahams) Date: Tue, 03 Apr 2007 13:15:55 -0400 Subject: [C++-sig] [Attention] BBv2 Python Build Support References: <87ird36sdf.fsf@valverde.peloton> Message-ID: <873b3he5v8.fsf@valverde.peloton> on Thu Mar 15 2007, Neal Becker wrote: > David Abrahams wrote: > >> >> I've just checked into the RC_1_34_0 branch most of the support for >> building and testing Python extensions and Boost.Python in BBv2. > > Will this appear in the cvs head? It's in the HEAD now. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From roman.yakovenko at gmail.com Tue Apr 3 19:48:22 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 3 Apr 2007 20:48:22 +0300 Subject: [C++-sig] How to return embedded Python object In-Reply-To: <000001c7753b$d37292d0$9428000a@ctc110207002> References: <000001c7753b$d37292d0$9428000a@ctc110207002> Message-ID: <7465b6170704031048g7c716c33j336fd9ecdace4fc1@mail.gmail.com> On 4/2/07, Koen Van Herck wrote: > > Hello, > > I've got the following class Foo, which is noncopyable, exposed to Python: > > class Foo: ::boost::noncopyable > { > public: > ::boost::python::object myself() > { > return ::boost::python::object(::boost::python::ptr(this)); > } > }; > > ::boost::python::class_< Foo, ::boost::noncopyable > ("Foo") > .def("myself", &Foo::myself) > ; > > I create a Foo() instance from Python: > >>> f = Foo() > > and now I want the myself() method to return a Python object pointing to > the same C++ object. With the code above, I can do: > >>> m = f.myself() > and use m as if it were f (though f==m returns False, but that's not an > issue for me). > However, if I delete f, or f goes out of scope, the C++ instance is > deleted and m points to a deleted C++ object. > > If I replace the return statement with > return ::boost::python::object(this); > I get a runtime error: "No to_python (by-value) converter found for C++ > type: class Foo", > which makes sense because the class is noncopyable. > > What is the correct way to return the embedded Python object in the > myself() method, such that reference counting works, i.e., the C++ > instance is deleted when both f and m objects are deleted. > My actual use case does not simply return 'this', but returns a list of > class instance which hold a pointer to the Foo instance as a data member. > I think you should use "return_self" call policy( http://boost.org/libs/python/doc/v2/return_arg.html#return_self-spec ) -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Tue Apr 3 19:50:29 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 3 Apr 2007 20:50:29 +0300 Subject: [C++-sig] C coded extinsion question In-Reply-To: References: Message-ID: <7465b6170704031050k7b6b2558i70ef7f46f4c6814b@mail.gmail.com> On 4/2/07, Hughes, Chad O wrote: > > > I am writing a c type extension for Python deriving from the list class > (using PyList_Type.tp_init). I have the need to overload the "+" operator > (To add two numbers stored as binary lists ? instead of concatenation). I > have tried the following: > > static PyMethodDef BinaryList_methods[] = { > ... > {"__add__", (PyCFunction)BinaryList___add__, METH_VARARGS, > PyDoc_STR("__add__(self,other) -> BinaryList")}, > ... > {NULL, NULL}, > }; > > This the BinaryList___add__ function is called if I use the __add__ > explicitly but the list base class __add__ is called for the "+" operator, > for example: > > a = BinaryList([1,0,0,0]) > b = BinaryList([0,0,0,1]) > print a.__add__(b) #prints [1,0,0,1] > print a + b #prints [1,0,0,0,0,0,0,1] > > > I read this: > object.h -- Added tp_call member to the typeobject struct > This along with a minor change to the ceval.c allows overloading > of the function call operator for any class. > > Can anyone either tell me what I am doing wrong and or give me an example > of how to properly overload an operator in a c coded extension? You are on the wrong mailing list. You'd better ask the question on other list -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Tue Apr 3 19:56:18 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 3 Apr 2007 20:56:18 +0300 Subject: [C++-sig] Embedding Python with Boost In-Reply-To: References: Message-ID: <7465b6170704031056v55d1ff36ofb5311ee0da24a84@mail.gmail.com> On 4/3/07, Ron Brown, Jr. wrote: > > Greetings, > > I've been having a bit of trouble in making a simple embedded python > application using boost, and am in need of some guidance. Hopefully I'm > in the right place. All I'm trying to do is just fit the basic pieces > together to see if embedding python is a viable solution for my next > project. > > I'm on Windows using mingw tools, specifically: > g++ (GCC) 3.4.2 (mingw-special) > Boost 1.33.1 > Python 2.5 I suggest you to try other Boost version. Recently there were some post, where David Abrahams said build system was broken on this platform. I was able to build Boost after minimal problems, mostly from setting up > my environment for Boost.Jam. All of the examples, including the > embedding.cpp example, seem to all compile/link just fine, and I'm able > to import the example Python extensions and use them. > > My problem lies in getting the most basic embedded Python application > (see below) to link. > > I'm trying to link my simple app here with boost_python.lib and > python25.lib. When I try to link, I'm getting: > C:/Boost/boost_1_33_1/boost/python/cast.hpp: undefined reference to > `_imp___Py_NoneStruct' Am I barking up the wrong tree by trying to statically link those libs? Are you sure you are linking with the right version of Python? Any advice that anyone could give would be greatly appreciated. Thanks > in advance, I guess the problem with your "make" files. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From rbrown at gamry.com Tue Apr 3 20:26:58 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Tue, 03 Apr 2007 14:26:58 -0400 Subject: [C++-sig] Embedding Python with Boost In-Reply-To: <7465b6170704031056v55d1ff36ofb5311ee0da24a84@mail.gmail.com> References: <7465b6170704031056v55d1ff36ofb5311ee0da24a84@mail.gmail.com> Message-ID: Roman Yakovenko wrote: > I suggest you to try other Boost version. Recently there were some > post, where David Abrahams said build system was broken on this platform. Really? I'll certainly have to look into that. > Are you sure you are linking with the right version of Python? Fairly sure, I only have one version of Python installed on this machine. > I guess the problem with your "make" files. That's the most likely thing. I've had some progress, and am able to get that example I posted to compile, link, and run, but that's essentially just using the Python C API. Now that I'm trying to actually use Boost (the examples in this tutorial: http://members.gamedev.net/sicrane/articles/EmbeddingPythonPart1.html), I'm running into all kinds of problems during linking. Here is my Jamfile: -------------------- project-root ; import python ; exe embedtest # name of the executable : #sources embedtest.cpp : # requirements c:/boost/boost_1_33_1/libs/python/build/bin-stage/boost_python.lib $(PYTHON_PROPERTIES) BOOST_PYTHON_STATIC_LIB BOOST_PYTHON_STATIC_MODULE $(PYTHON_LIB_PATH) $(PYTHON_EMBEDDED_LIBRARY) ; -------------------- Thanks for the reply! -Ron From nick.lee at volition-inc.com Tue Apr 3 23:53:05 2007 From: nick.lee at volition-inc.com (Nick Lee) Date: Tue, 3 Apr 2007 16:53:05 -0500 Subject: [C++-sig] Embedding Python with Boost References: Message-ID: I'm pretty sure boost_python.lib is a dynamic-link library. The static-link library is named libboost_python.lib (or libboost_python_debug.lib, which is the debug version). Unfortunately, the default installation of Boost does not create the static-link library. You have to manually build it yourself from the source code, which is located in the \libs\python\build directory of your Boost installation. Run Boost.Jam in that directory and it will create the static-link libraries. (The relevent documentation can be found at http://www.boost.org/libs/python/doc/building.html -- see the section called "Building Boost.Python") I found the following page helpful for embedding Boost.Python too: http://members.gamedev.net/sicrane/articles/EmbeddingPythonPart1.html I found it much clearer than the documentation that comes packaged with Boost. Hope this helps. "Ron Brown, Jr." wrote in message news:euru7s$sih$1 at sea.gmane.org... > > Am I barking up the wrong tree by trying to statically link those libs? From rbrown at gamry.com Wed Apr 4 15:25:21 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Wed, 04 Apr 2007 09:25:21 -0400 Subject: [C++-sig] Embedding Python with Boost In-Reply-To: References: Message-ID: Thanks Nick, that was extremely helpful! Nick Lee wrote: > I'm pretty sure boost_python.lib is a dynamic-link library. The static-link > library is named libboost_python.lib (or libboost_python_debug.lib, which is > the debug version). > > Unfortunately, the default installation of Boost does not create the > static-link library. You have to manually build it yourself from the source > code, which is located in the \libs\python\build directory of your Boost > installation. Run Boost.Jam in that directory and it will create the > static-link libraries. (The relevent documentation can be found at > http://www.boost.org/libs/python/doc/building.html -- see the section called > "Building Boost.Python") > > I found the following page helpful for embedding Boost.Python too: > > http://members.gamedev.net/sicrane/articles/EmbeddingPythonPart1.html > > I found it much clearer than the documentation that comes packaged with > Boost. > > Hope this helps. > > "Ron Brown, Jr." wrote in message > news:euru7s$sih$1 at sea.gmane.org... >> Am I barking up the wrong tree by trying to statically link those libs? From nick.lee at volition-inc.com Wed Apr 4 17:31:05 2007 From: nick.lee at volition-inc.com (Nick Lee) Date: Wed, 4 Apr 2007 10:31:05 -0500 Subject: [C++-sig] Boost.Python with other Python implementations? Message-ID: Is it possible to get Boost.Python to work with other Python implementations like IronPython? Or is Boost.Python strictly just for CPython 2.2+? From seefeld at sympatico.ca Wed Apr 4 17:59:01 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 04 Apr 2007 11:59:01 -0400 Subject: [C++-sig] Boost.Python with other Python implementations? In-Reply-To: References: Message-ID: <4613CB45.6080100@sympatico.ca> Nick Lee wrote: > Is it possible to get Boost.Python to work with other Python implementations > like IronPython? Or is Boost.Python strictly just for CPython 2.2+? Boost.Python was originally written specifically around the Python C API. However, some efford went into separating the C++ language reflection bits into its own library ('langbinding'). That efford has mostly stalled, though (lack of resources, I believe). It should be possible to retarget to other python implementations with such a framework (originally there were plans to support CPython and Lua). Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From p.jaroszynski at gmail.com Wed Apr 4 20:46:18 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Wed, 4 Apr 2007 20:46:18 +0200 Subject: [C++-sig] Exposing virtual functions with default implementations Message-ID: <200704042046.18735.p.jaroszynski@gmail.com> Hello, I am a little confused by Boost.Python tutorial about this subject - [1]. It is saying that I need to both override and make a default_ for a function I want to expose, but why is it really needed? Just the override w/o using the special def function seems enough - [2], [3]. What am I missing here? [1] - http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#python.virtual_functions_with_default_implementations [2] - http://dev.gentooexperimental.org/~peper/boost/test_virtual.cc [3] - http://dev.gentooexperimental.org/~peper/boost/test_virtual.py -- Best Regards, Piotr Jaroszynski From roman.yakovenko at gmail.com Wed Apr 4 21:03:47 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 4 Apr 2007 22:03:47 +0300 Subject: [C++-sig] Exposing virtual functions with default implementations In-Reply-To: <200704042046.18735.p.jaroszynski@gmail.com> References: <200704042046.18735.p.jaroszynski@gmail.com> Message-ID: <7465b6170704041203x2ceadfdbn8a88c523dac1d216@mail.gmail.com> On 4/4/07, Piotr Jaroszynski wrote: > Hello, > > I am a little confused by Boost.Python tutorial about this subject - [1]. It > is saying that I need to both override and make a default_ for a function I > want to expose, but why is it really needed? Just the override w/o using the > special def function seems enough - [2], [3]. What am I missing here? It allows you to call it, even if you override the function from Python: class X( Other ): #Other defined in extension module: ... def do_smth( self ): super( X, self ).do_smth( self ) #will call Other::do_smth member function -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From ndbecker2 at gmail.com Thu Apr 5 14:49:03 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 05 Apr 2007 08:49:03 -0400 Subject: [C++-sig] Boost.Python with other Python implementations? References: <4613CB45.6080100@sympatico.ca> Message-ID: Stefan Seefeld wrote: > Nick Lee wrote: >> Is it possible to get Boost.Python to work with other Python >> implementations like IronPython? Or is Boost.Python strictly just for >> CPython 2.2+? > > Boost.Python was originally written specifically around the Python C API. > However, some efford went into separating the C++ language reflection bits > into its own library ('langbinding'). That efford has mostly stalled, > though (lack of resources, I believe). > > It should be possible to retarget to other python implementations with > such a framework (originally there were plans to support CPython and Lua). > > Regards, > Stefan > I seems like PyPy might become interesting. From Mark.English at rbccm.com Thu Apr 5 15:06:01 2007 From: Mark.English at rbccm.com (English, Mark) Date: Thu, 5 Apr 2007 14:06:01 +0100 Subject: [C++-sig] Handling exception thrown by Boost.Python c-extension in Python code Message-ID: X-Replace-Address: mark.ignorethisbit.english at xxxrbccmxxx.ignorethisbittoo_and_removethosepreceding_xxxs_.com This is a cross-posting of a reply-less message I posted in comp.lang.python a week ago, so apologies if I'm breaking rules. I suspect that was the wrong place. I'm just getting started on Boost Python and may have missed this obvious looking problem somewhere. Given a c-extension "testext" written using Boost Python containing a base class "Base", a derived class "Derived", and a function "doSomething" which expects a "Derived" parameter, if I pass it a "Base" parameter an exception is thrown. This is a Boost.Python.ArgumentError. My question is how do I catch this error ? I tried the following bit of investigation: #Start code import testext b = testext.Base() try: testext.doSomething(b) except Exception, e: pass help(e.__class__) #End code which produces #Start output Help on class ArgumentError: class ArgumentError(exceptions.TypeError) | Method resolution order: | ArgumentError | exceptions.TypeError | exceptions.StandardError | exceptions.Exception | | Methods inherited from exceptions.Exception: | | __getitem__(...) | | __init__(...) | | __str__(...) #End output "print e" produces "" So I could handle this by writing an except clause for TypeError. Boost.Python doesn't exist as a module i.e. it's not in sys.modules, and I don't know how to import it - should there be a Boost.Python module somewhere on my PythonPath that I've forgotten to setup ? Is there a standard way of catching these errors by their actual type ? Is there an easy way to export the exception classes from my c- extension (testext) so that I can use that ? Thus "except testext.ArgumentError" would catch the "Boost.Python.ArgumentError". Thanks for any help, Mark mark.ignorethisbit.english at xxxrbccmxxx.ignorethisbittoo_and_removethosepreceding_xxxs_.com ________________________________________ This E-Mail (including any attachments) may contain privileged or confidential information. It is intended only for the addressee(s) indicated above. The sender does not waive any of its rights, privileges or other protections respecting this information. Any distribution, copying or other use of this E-Mail or the information it contains, by other than an intended recipient, is not sanctioned and is prohibited. If you received this E-Mail in error, please delete it and advise the sender (by return E-Mail or otherwise) immediately. This E-Mail (including any attachments) has been scanned for viruses. It is believed to be free of any virus or other defect that might affect any computer system into which it is received and opened. However, it is the responsibility of the recipient to ensure that it is virus free. The sender accepts no responsibility for any loss or damage arising in any way from its use. E-Mail received by or sent from RBC Capital Markets is subject to review by Supervisory personnel. Such communications are retained and may be produced to regulatory authorities or others with legal rights to the information. IRS CIRCULAR 230 NOTICE: TO COMPLY WITH U.S. TREASURY REGULATIONS, WE ADVISE YOU THAT ANY U.S. FEDERAL TAX ADVISE INCLUDED IN THIS COMMUNICATION IS NOT INTENDED OR WRITTEN TO BE USED, AND CANNOT BE USED, TO AVOID ANY U.S. FEDERAL TAX PENALTIES OR TO PROMOTE, MARKET, OR RECOMMEND TO ANOTHER PARTY ANY TRANSACTION OR MATTER. From rbrown at gamry.com Thu Apr 5 16:39:26 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Thu, 05 Apr 2007 10:39:26 -0400 Subject: [C++-sig] Embedded Python and calling back into Python from C++ Message-ID: I have what might be a ridiculous question for you all regarding embedded python. Hopefully I can explain it properly. I'm going to explain it within the context of Boost.Python, as that's what we're planning on using. If we write a Python module and also embed Python with Boost.Python, and in the embedded python call back into C++, and then in that C++ call back into Python, what happens? I'll try and explain that a bit more clearly. 1.) Write Python Module 2.) Embed Python Interpreter 3.) Import Python Module 4.) Call C++ function via Boost.Python from embedded Python 5.) Call Python function via Boost.Python from C++ function that was originally called by Python Is that possible? Would Python's thread be busy waiting for the initial C++ function to return? Thanks! -Ron From seefeld at sympatico.ca Thu Apr 5 16:44:30 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 05 Apr 2007 10:44:30 -0400 Subject: [C++-sig] Embedded Python and calling back into Python from C++ In-Reply-To: References: Message-ID: <46150B4E.2090802@sympatico.ca> Ron Brown, Jr. wrote: > I have what might be a ridiculous question for you all regarding > embedded python. Hopefully I can explain it properly. I'm going to > explain it within the context of Boost.Python, as that's what we're > planning on using. > > If we write a Python module and also embed Python with Boost.Python, and > in the embedded python call back into C++, and then in that C++ call > back into Python, what happens? > > I'll try and explain that a bit more clearly. > > 1.) Write Python Module > 2.) Embed Python Interpreter > 3.) Import Python Module > 4.) Call C++ function via Boost.Python from embedded Python > 5.) Call Python function via Boost.Python from C++ function that was > originally called by Python > > Is that possible? Would Python's thread be busy waiting for the initial > C++ function to return? That will work as any other call sequence. Python's Global Interpreter Lock only comes into play when you use multiple threads all accessing the python runtime. (And even then it is you who has to manually control the lock.) HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From dave at boost-consulting.com Sat Apr 7 20:32:43 2007 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 07 Apr 2007 14:32:43 -0400 Subject: [C++-sig] Embedding Python with Boost References: Message-ID: <871wiwujas.fsf@valverde.peloton> on Mon Apr 02 2007, "Ron Brown, Jr." wrote: > Greetings, > > I've been having a bit of trouble in making a simple embedded python > application using boost, and am in need of some guidance. Hopefully I'm > in the right place. All I'm trying to do is just fit the basic pieces > together to see if embedding python is a viable solution for my next > project. > > I'm on Windows using mingw tools, specifically: > g++ (GCC) 3.4.2 (mingw-special) > Boost 1.33.1 > Python 2.5 > > I was able to build Boost after minimal problems, mostly from setting up > my environment for Boost.Jam. All of the examples, including the > embedding.cpp example, seem to all compile/link just fine, and I'm able > to import the example Python extensions and use them. > > My problem lies in getting the most basic embedded Python application > (see below) to link. If the embedding.cpp example links and runs, just invoke bjam with -n -a and look at the commands it is using to make things work. If you can reproduce those commands there's no reason it shouldn't work for you. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From dave at boost-consulting.com Sat Apr 7 20:38:43 2007 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 07 Apr 2007 14:38:43 -0400 Subject: [C++-sig] Embedding Python with Boost References: <7465b6170704031056v55d1ff36ofb5311ee0da24a84@mail.gmail.com> Message-ID: <87veg8t4gc.fsf@valverde.peloton> on Tue Apr 03 2007, "Ron Brown, Jr." wrote: > Roman Yakovenko wrote: >> I suggest you to try other Boost version. Recently there were some >> post, where David Abrahams said build system was broken on this platform. > > Really? I'll certainly have to look into that. Yeah, really? that's news to me. It's especially surprising to hear you say that because Ron says he was successful getting all the examples to work. > That's the most likely thing. I've had some progress, and am able to > get that example I posted to compile, link, and run, but that's > essentially just using the Python C API. Now that I'm trying to > actually use Boost (the examples in this tutorial: > http://members.gamedev.net/sicrane/articles/EmbeddingPythonPart1.html), > I'm running into all kinds of problems during linking. > > Here is my Jamfile: > > -------------------- > project-root ; > > import python ; > > exe embedtest # name of the executable > : #sources > embedtest.cpp > : # requirements > c:/boost/boost_1_33_1/libs/python/build/bin-stage/boost_python.lib > $(PYTHON_PROPERTIES) > BOOST_PYTHON_STATIC_LIB > BOOST_PYTHON_STATIC_MODULE > $(PYTHON_LIB_PATH) > $(PYTHON_EMBEDDED_LIBRARY) ; > -------------------- At least one problem is that you're linking with an import library for boost.python but adding the #defines for a static lib, which would be named libboost_python.lib. ^^^ see http://www.boost.org/more/getting_started.html#Results This all gets a lot easier with BBv2, fortunately. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From roman.yakovenko at gmail.com Sat Apr 7 20:59:09 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sat, 7 Apr 2007 21:59:09 +0300 Subject: [C++-sig] Embedding Python with Boost In-Reply-To: <87veg8t4gc.fsf@valverde.peloton> References: <7465b6170704031056v55d1ff36ofb5311ee0da24a84@mail.gmail.com> <87veg8t4gc.fsf@valverde.peloton> Message-ID: <7465b6170704071159wef18f43u7512d5398797b15c@mail.gmail.com> On 4/7/07, David Abrahams wrote: > > > on Tue Apr 03 2007, "Ron Brown, Jr." wrote: > > > Roman Yakovenko wrote: > >> I suggest you to try other Boost version. Recently there were some > >> post, where David Abrahams said build system was broken on this > platform. > > > > Really? I'll certainly have to look into that. > > Yeah, really? that's news to me. It's especially surprising to hear > you say that because Ron says he was successful getting all the > examples to work. It is possible I've got wrong your post http://mail.python.org/pipermail/c++-sig/2007-March/012080.html -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at boost-consulting.com Sat Apr 7 23:33:02 2007 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 07 Apr 2007 17:33:02 -0400 Subject: [C++-sig] Embedding Python with Boost References: <7465b6170704031056v55d1ff36ofb5311ee0da24a84@mail.gmail.com> <87veg8t4gc.fsf@valverde.peloton> <7465b6170704071159wef18f43u7512d5398797b15c@mail.gmail.com> Message-ID: <87bqhzswdt.fsf@valverde.peloton> on Sat Apr 07 2007, "Roman Yakovenko" wrote: > On 4/7/07, David Abrahams wrote: > > on Tue Apr 03 2007, "Ron Brown, Jr." wrote: > > > Roman Yakovenko wrote: > >> I suggest you to try other Boost version. Recently there were some > >> post, where David Abrahams said build system was broken on this platform. > > > > Really? I'll certainly have to look into that. > > Yeah, really? that's news to me. It's especially surprising to hear > you say that because Ron says he was successful getting all the > examples to work. > > It is possible I've got wrong your post http://mail.python.org/pipermail/c++-sig/2007-March/ > 012080.html Yes, that post was all about BBv2 support. Ron is using BBv1. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From dave at boost-consulting.com Sat Apr 7 23:34:26 2007 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 07 Apr 2007 17:34:26 -0400 Subject: [C++-sig] Embedding Python with Boost References: Message-ID: <876487swbh.fsf@valverde.peloton> on Mon Apr 02 2007, "Ron Brown, Jr." wrote: > All of the examples, including the embedding.cpp example, seem to > all compile/link just fine Hmm, which embedding.cpp example? I can't find a file with that name in my Boost distro. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From dave at boost-consulting.com Sun Apr 8 00:17:21 2007 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 07 Apr 2007 18:17:21 -0400 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr References: Message-ID: <87tzvrrfri.fsf@valverde.peloton> on Mon Apr 02 2007, "Francis Moreau" wrote: > Hi, > > I am passing Python objects to C++ through a shared_ptr smart pointer and > it's work well. However I need to have the same behaviour as shared_ptr but > with my own version of smart pointer. I try to register and expose my custom > smart pointer but I have this compilation error: > > boost/python/pointee.hpp(28) : error C2039: 'element_type': is not a member > of 'CountedObjPtr' > with > [ > T=PyCallEngineState > ] > boost/python/pointee.hpp(38) : see reference to class template > instantiation 'boost::python::detail::pointee_impl::apply' being > compiled > with > [ > T=CountedObjPtr > ] > boost/python/register_ptr_to_python.hpp(17) : see reference to class > template instantiation 'boost::python::pointee' being compiled > with > [ > T=CountedObjPtr > ] > PythonModules.cpp(113) : see reference to function template > instantiation 'void > boost::python::register_ptr_to_python>(void)'being compiled > with > [ > T=PyCallEngineState > ] Please read http://www.boost.org/libs/python/doc/v2/pointee.html However, be aware that you won't get exactly the "magic" shared_ptr behavior described in http://www.boost.org/libs/python/doc/v2/faq.html#xref unless your pointer has a deleter just as shared_ptr does AND you do some extra work to define special converters for it (and perhaps not even then; I haven't looked into the details). -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From dave at boost-consulting.com Sun Apr 8 00:21:34 2007 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 07 Apr 2007 18:21:34 -0400 Subject: [C++-sig] std::string by reference from C++ to Python References: Message-ID: <87odlzrfkh.fsf@valverde.peloton> on Wed Mar 28 2007, "Francis Moreau" wrote: > Hi, > > I would like to know what is the best way to pass a std::string by reference > from C++ to Python. I know this should not work directly since string are > immutable in Python. > > Let's say that I have this C++ base class: > > class State { > virtual bool processEvent(const Event& in_event, std::string& > out_strNextState); > } > > And this C++ base class could be derived from Python, then Python would be > able to decide what is the next state by setting out_strNextState function > parameter. I know this could be much easier by changing the signature of the > processEvent function (as retourning a std::string), but it's important for > me to keep this signature. You can declare a C++ class: struct mystring { std::string x; }; Then expose mystring to class as usual and then register an lvalue from_python converter to std::string&. Naturally, in that case, a wrapped mystring instance is not a Python string. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From dave at boost-consulting.com Sun Apr 8 16:27:05 2007 From: dave at boost-consulting.com (David Abrahams) Date: Sun, 08 Apr 2007 10:27:05 -0400 Subject: [C++-sig] range with virtual functions References: <200703281715.16964.p.jaroszynski@gmail.com> Message-ID: <877isneybq.fsf@valverde.peloton> on Wed Mar 28 2007, Piotr Jaroszynski wrote: > Hello, > > I have yet another "problem" :] I want to expose a begin_foo and end_foo > functions as an iterable object like: > .add_property("foo", range(&Blah:begin_foo, &Blah::end_foo)) > > How can I do it when begin_foo and end_foo are virtual functions? I am > interested in both cases, when they are pure virtual and virtual with > default implementation. > > TIA for help! You don't need to do anything special. Just expose them as above. Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From nick.lee at volition-inc.com Mon Apr 9 16:30:16 2007 From: nick.lee at volition-inc.com (Nick Lee) Date: Mon, 9 Apr 2007 09:30:16 -0500 Subject: [C++-sig] Embedding Python with Boost References: <876487swbh.fsf@valverde.peloton> Message-ID: I think he's referring to the embedding.cpp that's in the libs\python\test directory of the boost distribution. At least, it's in mine. Niq "David Abrahams" wrote in message news:876487swbh.fsf at valverde.peloton... > > on Mon Apr 02 2007, "Ron Brown, Jr." wrote: > >> All of the examples, including the embedding.cpp example, seem to >> all compile/link just fine > > Hmm, which embedding.cpp example? I can't find a file with that name > in my Boost distro. > > -- > Dave Abrahams > Boost Consulting > www.boost-consulting.com > > Don't Miss BoostCon 2007! ==> http://www.boostcon.com From seefeld at sympatico.ca Mon Apr 9 17:31:48 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 09 Apr 2007 11:31:48 -0400 Subject: [C++-sig] Embedding Python with Boost In-Reply-To: References: <876487swbh.fsf@valverde.peloton> Message-ID: <461A5C64.8030702@sympatico.ca> Nick Lee wrote: > I think he's referring to the embedding.cpp that's in the libs\python\test > directory of the boost distribution. At least, it's in mine. > > Niq > > "David Abrahams" wrote in message > news:876487swbh.fsf at valverde.peloton... >> on Mon Apr 02 2007, "Ron Brown, Jr." wrote: >> >>> All of the examples, including the embedding.cpp example, seem to >>> all compile/link just fine >> Hmm, which embedding.cpp example? I can't find a file with that name >> in my Boost distro. I believe that was part of boost until 1.33.1 and then got replaced by exec.cpp. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From ypodpruzhnikov at mcode.ru Mon Apr 9 17:37:51 2007 From: ypodpruzhnikov at mcode.ru (Podpruzhnikov Yuri) Date: Mon, 9 Apr 2007 19:37:51 +0400 Subject: [C++-sig] I want to solve the Py_Finalize problem. Message-ID: If I correctly understood the Py_Finalize problem there was the way of solving the problem which already had been implemented and more or less tested. But I don't know some static and global variables in Boost.Python which should be corrected. That's why I have a request: Please, anyone, who knows Boost very much tell me what I should change. The decision is quite simple: I've created class-wrapper TStaticSmartPtr Actually, it implements two methods GetPtr and SetPtr TStaticSmartPtr makes possible following features: 1) it wraps absolutely any objects 2) on finalization of Python interpreter all wrapped objects will be automatically deleted (calling destructor) 3) it allows every Interpretter to have it own value of wrapped object, it's something like the Interpreter Local Storage (ILS) Note: there won't be any changes in the Python library!!! //------------------------------------------------------------ // Using Example(wrap static object with type SomeType): //------------------------------------------------------------ SomeType* GetSomeType() { static TStaticSmartPtr StaticPtr; // My class SomeType* LocalPtr; // local pointer to object if(!StaticPtr.GetPtr(&LocalPtr)) // Get pointer from ILS { // If pointer is not set - Create new Object LocalPtr = new SomeType(); LocalPtr->SomeInitialize(); StaticPtr.SetPtr(LocalPtr); } // Work with object LocalPtr->SomeAction(); return LocalPtr; } //------------------------------------------------------------ place for intialize data: if(!StaticPtr.GetPtr(&LocalPtr)) // Get pointer from ILS { ... } place for finalize data: destructor of SomeType PS: If I don't understand the exact reason of the Py_Finalize problem, so tell me what it is, please!!! PPS: Sorry for my English From dave at boost-consulting.com Mon Apr 9 17:55:43 2007 From: dave at boost-consulting.com (David Abrahams) Date: Mon, 09 Apr 2007 11:55:43 -0400 Subject: [C++-sig] Embedding Python with Boost References: <876487swbh.fsf@valverde.peloton> Message-ID: <87slb9r18g.fsf@grogan.peloton> on Mon Apr 09 2007, "Nick Lee" wrote: > "David Abrahams" wrote... >> >> on Mon Apr 02 2007, "Ron Brown, Jr." wrote: >> >>> All of the examples, including the embedding.cpp example, seem to >>> all compile/link just fine >> >> Hmm, which embedding.cpp example? I can't find a file with that name >> in my Boost distro. >> >> -- >> Dave Abrahams >> Boost Consulting >> www.boost-consulting.com >> >> Don't Miss BoostCon 2007! ==> http://www.boostcon.com > > I think he's referring to the embedding.cpp that's in the libs\python\test > directory of the boost distribution. At least, it's in mine. > > Niq Ah, I think that Stefan Seefeld replaced that test with a newer one when he added some embedding support for 1.34. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From dave at boost-consulting.com Mon Apr 9 17:57:38 2007 From: dave at boost-consulting.com (David Abrahams) Date: Mon, 09 Apr 2007 11:57:38 -0400 Subject: [C++-sig] Need volunteer to rewrite embedding tutorial Message-ID: <87hcrpr159.fsf@grogan.peloton> I was just reviewing the Boost.Python embedding tutorial and saw so many things wrong with it that it simply has to be rewritten. A great deal of the incorrect information is about building and can be fixed by my new building/testing guide, so it may be easier than it looks. Anyway, I need someone who actually uses embedding to work with me on new content, and I'd like to get this done ASAP so that I can close out the necessary changes for 1.34 before I have to leave for the C++ committee meeting on Thursday. Anybody? -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From rbrown at gamry.com Mon Apr 9 19:28:25 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Mon, 09 Apr 2007 13:28:25 -0400 Subject: [C++-sig] Embedding Python with Boost In-Reply-To: <871wiwujas.fsf@valverde.peloton> References: <871wiwujas.fsf@valverde.peloton> Message-ID: David Abrahams wrote: > If the embedding.cpp example links and runs, just invoke bjam with -n > -a and look at the commands it is using to make things work. If you > can reproduce those commands there's no reason it shouldn't work for > you. That's exactly what I ended up doing, and that's an extremely helpful bit of information. Thanks! David Abrahams wrote: > At least one problem is that you're linking with an import library for > boost.python but adding the > #defines for a static lib, which would be named libboost_python.lib. Yes, once I realized that, it all came together. There was a tutorial linked elsewhere in this thread that originally threw me off a bit. Thanks for all the advice, everyone! -Ron From abelalon at gmail.com Tue Apr 10 12:19:20 2007 From: abelalon at gmail.com (Monio) Date: Tue, 10 Apr 2007 03:19:20 -0700 (PDT) Subject: [C++-sig] pointer_arg_from_python documentation Message-ID: <9917006.post@talk.nabble.com> I'm looking for documentation to convert a pointer python argument to C++. In the boost.python code I found the pointer_arg_from_python template. How this template works? Can I use this template to retrieve a pointer to a C++ object, from a PyObject * parameter? Greets! -- View this message in context: http://www.nabble.com/pointer_arg_from_python-documentation-tf3552174.html#a9917006 Sent from the Python - c++-sig mailing list archive at Nabble.com. From abelalon at gmail.com Tue Apr 10 12:19:20 2007 From: abelalon at gmail.com (Monio) Date: Tue, 10 Apr 2007 03:19:20 -0700 (PDT) Subject: [C++-sig] pointer_arg_from_python documentation Message-ID: <9917006.post@talk.nabble.com> I'm looking for documentation of how to convert a python argument to C++. In the boost.python code I found the pointer_arg_from_python template. How this template works? Can I use this template to retrieve a pointer to a C++ object, from a PyObject * parameter? Greets! -- View this message in context: http://www.nabble.com/pointer_arg_from_python-documentation-tf3552174.html#a9917006 Sent from the Python - c++-sig mailing list archive at Nabble.com. From quant.mind at gmail.com Tue Apr 10 20:08:49 2007 From: quant.mind at gmail.com (Quant Mind) Date: Tue, 10 Apr 2007 20:08:49 +0200 Subject: [C++-sig] automatic conversion of boost date_time to Python date Message-ID: <8315652a0704101108x4294a52fl96ef5cfe0d90cfb9@mail.gmail.com> Here is the problem. I'm using boost date_time library for a calendar C++ application. I exposed my C++ Calendar class to Python using boost python and everything is going smoothly. The class contains several functions which have boost::gregorian::date as arguments. Obviously, this class is not know to Python and I would like to implement a custom converter between boost::gregorian::date and Python datetime.date. What would be the correct way to deal with this problem? Thank You Luca -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Tue Apr 10 20:34:50 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 10 Apr 2007 21:34:50 +0300 Subject: [C++-sig] automatic conversion of boost date_time to Python date In-Reply-To: <8315652a0704101108x4294a52fl96ef5cfe0d90cfb9@mail.gmail.com> References: <8315652a0704101108x4294a52fl96ef5cfe0d90cfb9@mail.gmail.com> Message-ID: <7465b6170704101134q4099777ah53665fef29694378@mail.gmail.com> On 4/10/07, Quant Mind wrote: > > Here is the problem. > I'm using boost date_time library for a calendar C++ application. I > exposed my C++ Calendar class to Python using boost python and everything is > going smoothly. > The class contains several functions which have boost::gregorian::date as > arguments. > Obviously, this class is not know to Python and I would like to implement > a custom converter between boost::gregorian::date and Python datetime.date > . > What would be the correct way to deal with this problem? Let me know if next link helped you: http://language-binding.net/pyplusplus/troubleshooting_guide/automatic_conversion/automatic_conversion.html -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From Jared.McIntyre at medsimulation.com Tue Apr 10 21:02:20 2007 From: Jared.McIntyre at medsimulation.com (Jared McIntyre) Date: Tue, 10 Apr 2007 13:02:20 -0600 Subject: [C++-sig] Reloading Functions Message-ID: I'm sure this should be answered somewhere, but I'm having problems hunting it down. I am working to get embedded Python scripting in the system using boost::python. We already have LUA running. The only issue I'm having is reloading python functions into the runtime. So, I load the file initially: object result = exec_file("C:\\ PythonTest\\HelloFile.py", global, global); object greet = global["greet"]; Now I can run greet() just fine. However, we need to be able to make changes in the greet script while the system is running, and swap the old with the new. Calling the exec_file again won't replace the function with the new version. Is there a way to do this without tearing down the entire python runtime and restarting it? Thanks, Jared -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngoodspeed at solidworks.com Tue Apr 10 21:41:11 2007 From: ngoodspeed at solidworks.com (Nat Goodspeed) Date: Tue, 10 Apr 2007 15:41:11 -0400 Subject: [C++-sig] Reloading Functions In-Reply-To: References: Message-ID: <94F7A8DD4408D8499C6812FE42E2D4B7FA1595@corp-mail4.solidworks.swk> ________________________________________ From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On Behalf Of Jared McIntyre Sent: Tuesday, April 10, 2007 3:02 PM To: c++-sig at python.org Subject: [C++-sig] Reloading Functions Now I can run greet() just fine. However, we need to be able to make changes in the greet script while the system is running, and swap the old with the new. Calling the exec_file again won't replace the function with the new version. Is there a way to do this without tearing down the entire python runtime and restarting it? [Nat] Call the Python reload() builtin? http://docs.python.org/lib/built-in-funcs.html#l2h-61 From seefeld at sympatico.ca Tue Apr 10 22:05:55 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Tue, 10 Apr 2007 16:05:55 -0400 Subject: [C++-sig] Reloading Functions In-Reply-To: References: Message-ID: <461BEE23.1010807@sympatico.ca> Jared McIntyre wrote: > I?m sure this should be answered somewhere, but I?m having problems > hunting it down. I am working to get embedded Python scripting in the > system using boost::python. We already have LUA running. The only issue > I?m having is reloading python functions into the runtime. So, I load > the file initially: > > > > object result = exec_file("C:\\ PythonTest\\HelloFile.py", global, global); > > object greet = global["greet"]; > > > > Now I can run greet() just fine. However, we need to be able to make > changes in the greet script while the system is running, and swap the > old with the new. Calling the exec_file again won?t replace the function > with the new version. Is there a way to do this without tearing down the > entire python runtime and restarting it? exec_file should execute the file (and put all symbols into 'global', each time it is called, no matter whether the file has the same content as the last time you called it. If you don't want to preserve the content of 'global', there is no need to reuse that, i.e. you could use a different dictionary on each invocation. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From Jared.McIntyre at medsimulation.com Tue Apr 10 22:21:00 2007 From: Jared.McIntyre at medsimulation.com (Jared McIntyre) Date: Tue, 10 Apr 2007 14:21:00 -0600 Subject: [C++-sig] Reloading Functions In-Reply-To: <461BEE23.1010807@sympatico.ca> References: <461BEE23.1010807@sympatico.ca> Message-ID: Thanks for the help. I don't know what I was doing when I tested it the first time, but it works now. I'm guessing it was a PEBKAC problem. Jared -----Original Message----- From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On Behalf Of Stefan Seefeld Sent: Tuesday, April 10, 2007 2:06 PM To: Development of Python/C++ integration Subject: Re: [C++-sig] Reloading Functions exec_file should execute the file (and put all symbols into 'global', each time it is called, no matter whether the file has the same content as the last time you called it. If you don't want to preserve the content of 'global', there is no need to reuse that, i.e. you could use a different dictionary on each invocation. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From dave at boost-consulting.com Tue Apr 10 22:32:28 2007 From: dave at boost-consulting.com (David Abrahams) Date: Tue, 10 Apr 2007 16:32:28 -0400 Subject: [C++-sig] pointer_arg_from_python documentation References: <9917006.post@talk.nabble.com> Message-ID: <87y7l02co3.fsf@grogan.peloton> on Tue Apr 10 2007, Monio wrote: > I'm looking for documentation of how to convert a python argument to C++. In > the boost.python code I found the pointer_arg_from_python template. How this > template works? You don't want to know, really. > Can I use this template to retrieve a pointer to a C++ object, from a > PyObject * parameter? No. Foo* x = boost::python::extract(p); HTH, -- Dave Abrahams Boost Consulting http://www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From luca.sbardella at gmail.com Tue Apr 10 23:51:38 2007 From: luca.sbardella at gmail.com (Luca Sbardella) Date: Tue, 10 Apr 2007 23:51:38 +0200 Subject: [C++-sig] automatic conversion of boost date_time to Python date In-Reply-To: <7465b6170704101134q4099777ah53665fef29694378@mail.gmail.com> References: <8315652a0704101108x4294a52fl96ef5cfe0d90cfb9@mail.gmail.com> <7465b6170704101134q4099777ah53665fef29694378@mail.gmail.com> Message-ID: <8315652a0704101451s276f90d1qd12a4bfa3025f0cd@mail.gmail.com> Thanks Roman, it was useful, however I'm getting some specific problem in the implementation (I cannot debug the code since I cannot load the PyDateTime static library). the Python documentation says I should declare PyDateTime_IMPORT but this does not compile!!!! Still trying at the moment but this is what I have done, any comment would be welcome. Best Luca template struct ObjFromPy { ObjFromPy() { boost::python::converter::registry::push_back(&TfromPy::convertible, &TfromPy::construct, boost::python::type_id()); } }; // template struct register_python_conversion { register_python_conversion() { boost::python::to_python_converter(); ObjFromPy(); } }; // // IMPLEMENTATION // struct DateToPy { static PyObject* convert(const qm_date& dte) { return PyDate_FromDate( dte.year(), dte.month(), dte.day()); } }; // struct DateFromPy { // static void* convertible(PyObject* obj_ptr) { if(!PyDate_Check(obj_ptr)) return 0; return obj_ptr; } // static void construct(PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data) { int y = PyDateTime_GET_YEAR(obj_ptr); int m = PyDateTime_GET_MONTH(obj_ptr); int d = PyDateTime_GET_DAY(obj_ptr); qm_date* dte = new qm_date(y,m,d); data->convertible = (void*)dte; } }; // // typedef register_python_conversion date_python_conversion; // On 10/04/07, Roman Yakovenko wrote: > > On 4/10/07, Quant Mind wrote: > > > Here is the problem. > > I'm using boost date_time library for a calendar C++ application. I > > exposed my C++ Calendar class to Python using boost python and everything is > > going smoothly. > > The class contains several functions which have boost::gregorian::date > > as arguments. > > Obviously, this class is not know to Python and I would like to > > implement a custom converter between boost::gregorian::date and Python > > datetime.date. > > What would be the correct way to deal with this problem? > > > Let me know if next link helped you: http://language-binding.net/pyplusplus/troubleshooting_guide/automatic_conversion/automatic_conversion.html > > > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From luca.sbardella at gmail.com Tue Apr 10 23:54:12 2007 From: luca.sbardella at gmail.com (Luca Sbardella) Date: Tue, 10 Apr 2007 23:54:12 +0200 Subject: [C++-sig] automatic conversion of boost date_time to Python date In-Reply-To: <8315652a0704101451s276f90d1qd12a4bfa3025f0cd@mail.gmail.com> References: <8315652a0704101108x4294a52fl96ef5cfe0d90cfb9@mail.gmail.com> <7465b6170704101134q4099777ah53665fef29694378@mail.gmail.com> <8315652a0704101451s276f90d1qd12a4bfa3025f0cd@mail.gmail.com> Message-ID: <8315652a0704101454v3ff196b2wc64e8fb9db9bc1e5@mail.gmail.com> sorry missing typedef boost::gregorian::date qm_date in the code On 10/04/07, Luca Sbardella wrote: > > Thanks Roman, it was useful, however I'm getting some specific problem in > the implementation (I cannot debug the code since I cannot load the > PyDateTime static library). the Python documentation says I should declare > PyDateTime_IMPORT but this does not compile!!!! > Still trying at the moment but this is what I have done, any comment would > be welcome. > Best > Luca > > > template > struct ObjFromPy > { > ObjFromPy() > { > > boost::python::converter::registry::push_back(&TfromPy::convertible, > > &TfromPy::construct, > > boost::python::type_id()); > } > }; > // > template > struct register_python_conversion > { > register_python_conversion() > { > boost::python::to_python_converter(); > ObjFromPy(); > } > }; > // > // IMPLEMENTATION > // > struct DateToPy > { > static PyObject* convert(const qm_date& dte) > { > return PyDate_FromDate( dte.year(), dte.month(), dte.day()); > } > }; > // > struct DateFromPy > { > // > static void* convertible(PyObject* obj_ptr) > { > if(!PyDate_Check(obj_ptr)) return 0; > return obj_ptr; > } > // > static void construct(PyObject* obj_ptr, > > boost::python::converter::rvalue_from_python_stage1_data* data) > { > int y = PyDateTime_GET_YEAR(obj_ptr); > int m = PyDateTime_GET_MONTH(obj_ptr); > int d = PyDateTime_GET_DAY(obj_ptr); > qm_date* dte = new qm_date(y,m,d); > data->convertible = (void*)dte; > } > }; > // > // > typedef register_python_conversion > date_python_conversion; > // > > On 10/04/07, Roman Yakovenko wrote: > > > On 4/10/07, Quant Mind wrote: > > > > > Here is the problem. > > > I'm using boost date_time library for a calendar C++ application. I > > > exposed my C++ Calendar class to Python using boost python and everything is > > > going smoothly. > > > The class contains several functions which have boost::gregorian::date > > > as arguments. > > > Obviously, this class is not know to Python and I would like to > > > implement a custom converter between boost::gregorian::date and Python > > > datetime.date. > > > What would be the correct way to deal with this problem? > > > > > > Let me know if next link helped you: http://language-binding.net/pyplusplus/troubleshooting_guide/automatic_conversion/automatic_conversion.html > > > > > > > > -- > > Roman Yakovenko > > C++ Python language binding > > http://www.language-binding.net/ > > _______________________________________________ > > C++-sig mailing list > > C++-sig at python.org > > http://mail.python.org/mailman/listinfo/c++-sig > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From quant.mind at gmail.com Wed Apr 11 17:56:38 2007 From: quant.mind at gmail.com (Quant Mind) Date: Wed, 11 Apr 2007 17:56:38 +0200 Subject: [C++-sig] automatic conversion of boost date_time to Python date In-Reply-To: <8315652a0704101454v3ff196b2wc64e8fb9db9bc1e5@mail.gmail.com> References: <8315652a0704101108x4294a52fl96ef5cfe0d90cfb9@mail.gmail.com> <7465b6170704101134q4099777ah53665fef29694378@mail.gmail.com> <8315652a0704101451s276f90d1qd12a4bfa3025f0cd@mail.gmail.com> <8315652a0704101454v3ff196b2wc64e8fb9db9bc1e5@mail.gmail.com> Message-ID: <8315652a0704110856s15be2f8fw710ff6f8f8caf9c@mail.gmail.com> OK, I managed to have this working, see the attached file. Is this the correct way of doing it? Especially in the static member "construct" of struct "DateFromPy". Thank You Luca On 10/04/07, Luca Sbardella wrote: > > sorry missing > > typedef boost::gregorian::date qm_date > > in the code > > > > > On 10/04/07, Luca Sbardella < luca.sbardella at gmail.com> wrote: > > > > Thanks Roman, it was useful, however I'm getting some specific problem > > in the implementation (I cannot debug the code since I cannot load the > > PyDateTime static library). the Python documentation says I should declare > > PyDateTime_IMPORT but this does not compile!!!! > > Still trying at the moment but this is what I have done, any comment > > would be welcome. > > Best > > Luca > > > > > > template > > struct ObjFromPy > > { > > ObjFromPy() > > { > > > > boost::python::converter::registry::push_back(&TfromPy::convertible, > > > > &TfromPy::construct, > > > > boost::python::type_id()); > > } > > }; > > // > > template > > struct register_python_conversion > > { > > register_python_conversion() > > { > > boost::python::to_python_converter(); > > ObjFromPy(); > > } > > }; > > // > > // IMPLEMENTATION > > // > > struct DateToPy > > { > > static PyObject* convert(const qm_date& dte) > > { > > return PyDate_FromDate( dte.year(), dte.month(), dte.day()); > > } > > }; > > // > > struct DateFromPy > > { > > // > > static void* convertible(PyObject* obj_ptr) > > { > > if(!PyDate_Check(obj_ptr)) return 0; > > return obj_ptr; > > } > > // > > static void construct(PyObject* obj_ptr, > > > > boost::python::converter::rvalue_from_python_stage1_data* data) > > { > > int y = PyDateTime_GET_YEAR(obj_ptr); > > int m = PyDateTime_GET_MONTH(obj_ptr); > > int d = PyDateTime_GET_DAY(obj_ptr); > > qm_date* dte = new qm_date(y,m,d); > > data->convertible = (void*)dte; > > } > > }; > > // > > // > > typedef register_python_conversion > > date_python_conversion; > > // > > > > On 10/04/07, Roman Yakovenko wrote: > > > > > On 4/10/07, Quant Mind wrote: > > > > > > > Here is the problem. > > > > I'm using boost date_time library for a calendar C++ application. I > > > > exposed my C++ Calendar class to Python using boost python and everything is > > > > going smoothly. > > > > The class contains several functions which have > > > > boost::gregorian::date as arguments. > > > > Obviously, this class is not know to Python and I would like to > > > > implement a custom converter between boost::gregorian::date and Python > > > > datetime.date. > > > > What would be the correct way to deal with this problem? > > > > > > > > > Let me know if next link helped you: http://language-binding.net/pyplusplus/troubleshooting_guide/automatic_conversion/automatic_conversion.html > > > > > > > > > > > > -- > > > Roman Yakovenko > > > C++ Python language binding > > > http://www.language-binding.net/ > > > _______________________________________________ > > > C++-sig mailing list > > > C++-sig at python.org > > > http://mail.python.org/mailman/listinfo/c++-sig > > > > > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- typedef boost::gregorian::date qm_date; template struct ObjFromPy { ObjFromPy() { boost::python::converter::registry::push_back(&TfromPy::convertible, &TfromPy::construct, boost::python::type_id()); } }; template struct register_python_conversion { register_python_conversion() { boost::python::to_python_converter(); ObjFromPy(); } }; // IMPLEMENTATION struct DateToPy { static PyObject* convert(const qm_date& dte) { PyDateTime_IMPORT; return PyDate_FromDate(dte.year(), dte.month(), dte.day()); } }; struct DateFromPy { // static void* convertible(PyObject* obj_ptr) { PyDateTime_IMPORT; if(PyDate_Check(obj_ptr) || PyDateTime_Check(obj_ptr)) return obj_ptr; return 0; } // static void construct(PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data) { PyDateTime_IMPORT; int y = PyDateTime_GET_YEAR(obj_ptr); int m = PyDateTime_GET_MONTH(obj_ptr); int d = PyDateTime_GET_DAY(obj_ptr); qm_date* dte = new qm_date(y,m,d); data->convertible = (void*)dte; } }; typedef register_python_conversion date_python_conversion; From snemeth at houston.rr.com Thu Apr 12 06:25:51 2007 From: snemeth at houston.rr.com (Rocketman@JSC) Date: Wed, 11 Apr 2007 21:25:51 -0700 (PDT) Subject: [C++-sig] Help! Py++ Generated Binding Won't Compile Message-ID: <9952809.post@talk.nabble.com> First of all--thanks for you guys putting in all the work to bring us Py++, pygccxml, and boost.python--lots of hard work and I'm hoping to put it all to good use and save us some tax dollars. Background: ========== I'm trying to write a flight simulation tool with C++ sim code and models with a Python front-end interface. For testing purposes, I put together a simple sim and used SWIG to start with. Basically SWIG has no problem generating the interfaces and all builds fine and works. After some research on Boost and Py++, I came to the conclusion the Boost/Py++ solution would in the end be more robust and easier to maintain--though the learning curve is very steep. I know C++ pretty good but am totally new to Py++/Boost/pygccxml. I searched and searched with nothing really of help--and hope you guys can set me straight. So here I am trying to generate bindings for the same C++ sim code, but I've run into snag. Below I'll post the two header files I think are involved, Py++ script, Py++ messages, and the compiler errors. Executive.hpp =========== #ifndef EXECUTIVE_HPP_INCLUDED #define EXECUTIVE_HPP_INCLUDED #include class Event; // Forward class declarations class Vehicle; class Executive { public: Executive(void); ~Executive(void); bool done; double dt; double getExecTime( void ); void run( void ); void run( double ); void registerEvent( Event *); void registerVehicle( Vehicle *); double time; private: void checkEvents( void ); std::vector< Event * > eventList; std::vector< Vehicle * > vehicleList; }; #endif // EXECUTIVE_HPP_INCLUDED Event.hpp ======== #ifndef EVENT_HPP_INCLUDED #define EVENT_HPP_INCLUDED #include #include typedef enum { TGO_NONE, TRIP, REGULA_FALSI } tgo_t; typedef enum { SLOPE_NONE, DECREASING, ANY, INCREASING } slope_t; typedef enum { EVENT_NONE, FIXED, FLOATING, FREE }event_t; class Executive; // Forward class declaration class Event { public: Event(void); Event( Executive * ); Event( Executive *, PyObject *, PyObject *); ~Event(void); // bool setSimExec(Executive *); PyObject * setCondition(PyObject *self, PyObject *args); PyObject * setAction(PyObject *self, PyObject *args); bool check(void); bool repeating; std::string name; tgo_t tgo_type; slope_t slope; event_t event_type; bool beenFired; bool active; PyObject *condition; PyObject *action; private: Executive *exec; }; #endif // EVENT_HPP_INCLUDED Py++ generate_code.py file ====================== #! /usr/local/bin/python # Copyright 2004 Roman Yakovenko. # Distributed under the Boost Software License, Version 1.0. (See # accompanying file LICENSE_1_0.txt or copy at # http://www.boost.org/LICENSE_1_0.txt) import os import sys sys.path.append( '../../../..' ) # Not sure about this next line #from environment import gccxml from pyplusplus import module_builder mb = module_builder.module_builder_t( files=['Integrator.hpp','Ball.hpp', 'Executive.hpp','Event.hpp'] , include_paths=['/usr/include/python2.4', '/work/scott/python/testPy++'] , gccxml_path='/usr/local/bin/gccxml' ) #path to gccxml executable #I want to exclude all class methods with name starts with getPtr getPtr_methods = mb.mem_funs( lambda decl: decl.name.startswith( 'getPtr' ) ) getPtr_methods.exclude() ptr_to_double = mb.vars( type='double *') ptr_to_double.exclude() ptr_to_doubles = mb.vars( type='double * *') ptr_to_doubles.exclude() #I can print declarations to see what is going on #mb.print_declarations() #Added mb.classes().always_expose_using_scope = True #Now it is the time to give a name to our module mb.build_code_creator( module_name='Pyxis' ) #It is common requirement in software world - each file should have license mb.code_creator.license = '//Boo Hoo' #I don't want absolute includes within code mb.code_creator.user_defined_directories.append( os.path.abspath('.') ) #And finally we can write code to the disk mb.write_module( os.path.join( os.path.abspath('.'), 'Pyxis_wrap.cpp' ) ) Py++ screen messages =================== [scott at localhost testPy++]$ generate_code.py INFO Parsing source file "Integrator.hpp" ... INFO gccxml cmd: /usr/local/bin/gccxml -I"." -I"/usr/include/python2.4" -I"/work/scott/python/testPy++" "Integrator.hpp" -fxml="/tmp/tmpNlvtn3.xml" INFO Parsing source file "Ball.hpp" ... INFO gccxml cmd: /usr/local/bin/gccxml -I"." -I"/usr/include/python2.4" -I"/work/scott/python/testPy++" "Ball.hpp" -fxml="/tmp/tmp9hV6e8.xml" INFO Parsing source file "Executive.hpp" ... INFO gccxml cmd: /usr/local/bin/gccxml -I"." -I"/usr/include/python2.4" -I"/work/scott/python/testPy++" "Executive.hpp" -fxml="/tmp/tmpciXxqc.xml" INFO Parsing source file "Event.hpp" ... INFO gccxml cmd: /usr/local/bin/gccxml -I"." -I"/usr/include/python2.4" -I"/work/scott/python/testPy++" "Event.hpp" -fxml="/tmp/tmpcFbVO_.xml" WARNING: Event [class] > warning W1025: Py++ will generate class wrapper - class contains T* member > variable WARNING: Event [class] > warning W1025: Py++ will generate class wrapper - class contains T* member > variable WARNING: BallForce [class] > warning W1040: The declaration is unexposed, but there are other > declarations, which refer to it. This could > cause "no to_python converter found" run time error. Declarations: > Ball::force [variable] WARNING: _object [struct] > warning W1040: The declaration is unexposed, but there are other > declarations, which refer to it. This could > cause "no to_python converter found" run time error. Declarations: > Event::Event(Executive * arg0, PyObject *> arg1, PyObject * arg2) > [constructor] Event::Event(Executive * arg0, PyObject * arg1, PyObject * > arg2) > [constructor] PyObject * Event::setAction(PyObject * self, PyObject * > args) [member function] PyObject * > Event::setAction(PyObject * self, PyObject * args) [member function] > PyObject * Event::setAction(PyObject * > self, PyObject * args) [member function] PyObject * > Event::setCondition(PyObject * self, PyObject * args) > [member function] PyObject * Event::setCondition(PyObject * self, > PyObject * args) [member function] > PyObject * Event::setCondition(PyObject * self, PyObject * args) [member > function] Event::action [variable] > Event::condition [variable] WARNING: Executive [class declaration] > warning W1040: The declaration is unexposed, but there are other > declarations, which refer to it. This could > cause "no to_python converter found" run time error. Declarations: > Event::Event(Executive * arg0) > [constructor] Event::Event(Executive * arg0, PyObject * arg1, PyObject * > arg2) [constructor] WARNING: BallState [class] > warning W1040: The declaration is unexposed, but there are other > declarations, which refer to it. This could > cause "no to_python converter found" run time error. Declarations: > Ball::state [variable] WARNING: Vehicle [class] > warning W1040: The declaration is unexposed, but there are other > declarations, which refer to it. This could > cause "no to_python converter found" run time error. Declarations: Ball > [class] WARNING: Vehicle [class declaration] > warning W1040: The declaration is unexposed, but there are other > declarations, which refer to it. This could > cause "no to_python converter found" run time error. Declarations: void > Executive::registerVehicle(Vehicle *> arg0) [member function] WARNING: Event [class declaration] > warning W1040: The declaration is unexposed, but there are other > declarations, which refer to it. This could > cause "no to_python converter found" run time error. Declarations: void > Executive::registerEvent(Event * > arg0) [member function] INFO: file "Pyxis_wrap.cpp" - updated( 0.000000 seconds ) bjam compiler message =================== [scott at localhost testPy++]$ bjam ...found 1632 targets... ...updating 2 targets... gcc-C++-action bin/testPy++/Pyxis.so/gcc/debug/shared-linkable-true/Pyxis_wrap.o /apps/oss/boost_1_33_1/boost/python/object/pointer_holder.hpp: In instantiation of ?boost::python::objects::pointer_holder<_object*, _object>?: /apps/oss/boost_1_33_1/boost/type_traits/alignment_of.hpp:52: instantiated from ?const size_t boost::detail::alignment_of_impl >::value? /apps/oss/boost_1_33_1/boost/type_traits/alignment_of.hpp:61: instantiated from ?boost::alignment_of >? /apps/oss/boost_1_33_1/boost/python/object/instance.hpp:30: instantiated from ?boost::python::objects::instance >? /apps/oss/boost_1_33_1/boost/python/object/instance.hpp:44: instantiated from ?const size_t boost::python::objects::additional_instance_size >::value? /apps/oss/boost_1_33_1/boost/python/object/make_instance.hpp:32: instantiated from ?static PyObject* boost::python::objects::make_instance_impl::execute(Arg&) [with Arg = _object*, T = _object, Holder = boost::python::objects::pointer_holder<_object*, _object>, Derived = boost::python::objects::make_ptr_instance<_object, boost::python::objects::pointer_holder<_object*, _object> >]? /apps/oss/boost_1_33_1/boost/python/to_python_indirect.hpp:96: instantiated from ?static PyObject* boost::python::detail::make_reference_holder::execute(T*) [with T = _object]? /apps/oss/boost_1_33_1/boost/python/to_python_indirect.hpp:60: instantiated from ?PyObject* boost::python::to_python_indirect::execute(const U&, mpl_::false_) const [with U = _object, T = PyObject*, MakeHolder = boost::python::detail::make_reference_holder]? /apps/oss/boost_1_33_1/boost/python/to_python_indirect.hpp:48: instantiated from ?PyObject* boost::python::to_python_indirect::execute(U*, mpl_::true_) const [with U = PyObject, T = PyObject*, MakeHolder = boost::python::detail::make_reference_holder]? /apps/oss/boost_1_33_1/boost/python/to_python_indirect.hpp:37: instantiated from ?PyObject* boost::python::to_python_indirect::operator()(const U&) const [with U = PyObject*, T = PyObject*, MakeHolder = boost::python::detail::make_reference_holder]? /apps/oss/boost_1_33_1/boost/python/detail/invoke.hpp:75: instantiated from ?PyObject* boost::python::detail::invoke(boost::python::detail::invoke_tag_, const RC&, F&, AC0&) [with RC = boost::python::to_python_indirect, F = PyObject* (*)(const Event&), AC0 = boost::python::arg_from_python]? /apps/oss/boost_1_33_1/boost/python/detail/caller.hpp:199: instantiated from ?PyObject* boost::python::detail::caller_arity<1u>::impl::operator()(PyObject*, PyObject*) [with F = PyObject* (*)(const Event&), Policies = boost::python::return_value_policy, Sig = boost::mpl::vector2]? /apps/oss/boost_1_33_1/boost/python/object/py_function.hpp:38: instantiated from ?PyObject* boost::python::objects::caller_py_function_impl::operator()(PyObject*, PyObject*) [with Caller = boost::python::detail::caller, boost::mpl::vector2 >]? Pyxis_wrap.cpp:333: instantiated from here /apps/oss/boost_1_33_1/boost/python/object/pointer_holder.hpp:176: error: ?boost::python::objects::pointer_holder::pointer_holder(PyObject*) [with Pointer = _object*, Value = _object]? cannot be overloaded /apps/oss/boost_1_33_1/boost/python/object/pointer_holder.hpp:111: error: with ?boost::python::objects::pointer_holder::pointer_holder(Pointer) [with Pointer = _object*, Value = _object]? set -e "g++" -c -Wall -ftemplate-depth-255 -DBOOST_PYTHON_DYNAMIC_LIB -g -O0 -fno-inline -fPIC -I"bin/testPy++" -I "/usr/include/python2.4" -I "/apps/oss/boost_1_33_1" -o "bin/testPy++/Pyxis.so/gcc/debug/shared-linkable-true/Pyxis_wrap.o" "Pyxis_wrap.cpp" "/usr/bin/objcopy" --set-section-flags .debug_str=contents,debug "bin/testPy++/Pyxis.so/gcc/debug/shared-linkable-true/Pyxis_wrap.o" ...failed gcc-C++-action bin/testPy++/Pyxis.so/gcc/debug/shared-linkable-true/Pyxis_wrap.o... ...skipped <@testPy++/Pyxis.so/gcc/debug/shared-linkable-true>Pyxis.so for lack of <@testPy++/Pyxis.so/gcc/debug/shared-linkable-true>Pyxis_wrap.o... ...failed updating 1 target... ...skipped 1 target... I don't have any clue where the problem is from these messages. Note the compiler mentions the 2nd to the last } of the generated wrapper Pyxis_wrap.cpp file and all the rest of the compiler messages don't appear to be in my code. If I remove the Event and Executive class--all builds up fine. Thanks in advance for any help. Scott p.s. RH FC5, gcc version 4.1.0 20060304 (Red Hat 4.1.0-3), python 2.4, boost_1_33_1, and Py++ 0.8.3 -- View this message in context: http://www.nabble.com/Help%21--Py%2B%2B-Generated-Binding-Won%27t-Compile-tf3563358.html#a9952809 Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Thu Apr 12 11:15:13 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 12 Apr 2007 12:15:13 +0300 Subject: [C++-sig] Help! Py++ Generated Binding Won't Compile In-Reply-To: <9952809.post@talk.nabble.com> References: <9952809.post@talk.nabble.com> Message-ID: <7465b6170704120215x3b8c3f8s55130a3f357fee23@mail.gmail.com> On 4/12/07, Rocketman at JSC wrote: > > First of all--thanks for you guys putting in all the work to bring us Py++, > pygccxml, and boost.python--lots of hard work and I'm hoping to put it all > to good use and save us some tax dollars. :-) > So here I am trying to generate bindings for the same C++ sim code, but I've > run into snag. Below I'll post the two header files I think are involved, > Py++ script, Py++ messages, and the compiler errors. Few comments 1. You didn't specified call policies for functions registerEvent and registerVehicle. If you just saves references to the objects I suggest you to take a look on http://boost.org/libs/python/doc/v2/with_custodian_and_ward.html call policy and to read tutorials. If Executive class also manages their life-time, than this is other story. Call policies are **very** important. 2. Event class is using raw PyObject type. There is a much better way: http://boost.org/libs/python/doc/tutorial/doc/html/python/object.html 3. You don't really have to exclude getPtr methods. You can use call policies to deal with them, even if this is a pointer to an array. Take a look on Py++ defined call policies: http://language-binding.net/pyplusplus/documentation/functions/call_policies.html 4. Py++ warnings is there for you. For example > WARNING: BallForce [class] > > warning W1040: The declaration is unexposed, but there are other > > declarations, which refer to it. This could > > cause "no to_python converter found" run time error. Declarations: > > Ball::force [variable] Py++ says you that you are exposing Ball::force member variable, while you don't expose BallForce class( the type of the variable ). So when user will try to use "force" variable he will get an error. Of course you can disable them: http://language-binding.net/pyplusplus/documentation/feedback.html Now about your errors. My guess is that they are caused by missing call policies for functions setAction and setCondition. In order to be sure I need to see generated code. > I don't have any clue where the problem is from these messages. Note the > compiler mentions the 2nd to the last } of the generated wrapper > Pyxis_wrap.cpp file and all the rest of the compiler messages don't appear > to be in my code. If I remove the Event and Executive class--all builds up > fine. > > Thanks in advance for any help. HTH. P.S. It is much better to post small and complete hand-written example + error message. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From rbrown at gamry.com Thu Apr 12 23:01:01 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Thu, 12 Apr 2007 17:01:01 -0400 Subject: [C++-sig] Calling member function of object created in C++ via embedded Python Message-ID: Please forgive me if this is a ridiculously simple question, but I'm new to both Python and Boost.Python, and I'm having hard time getting my bearings. Essentially, I have a C++ application with Python embedded via Boost.Python. I have a class which I instantiate on the C++ side. This class is also exposed to Python via a BOOST_PYTHON_MODULE. Now normally (at least according to any examples I've found), you would expose that class to Python, and instantiate that class on the Python side. But as this is an embedded Python application, I'd like to instantiate the object on the C++ side, and somehow be able to call that object's member functions from Python. Is this possible, and if so, how would one go about doing it? I'll try and explain using a modified version of the example from http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html Thanks! -Ron ---------------------------------------- #include using namespace boost::python; class World { public: void set(std::string msg) { this->msg = msg; } std::string greet() { return msg; } std::string msg; }; BOOST_PYTHON_MODULE(hello) { class_("World") .def("greet", &World::greet) .def("set", &World::set) ; } int main(int argc, char *argv[]) { Py_Initialize(); World worldObject; worldObject.set("Hello, World!"); try { inithello(); PyRun_SimpleString("import hello"); // ******************** // How would I call the equivalent of: // worldObject.greet(); // from Python? // ******************** } catch (error_already_set) { PyErr_Print(); } Py_Finalize(); return 0; } ---------------------------------------- From snemeth at houston.rr.com Fri Apr 13 05:01:12 2007 From: snemeth at houston.rr.com (Rocketman@JSC) Date: Thu, 12 Apr 2007 20:01:12 -0700 (PDT) Subject: [C++-sig] Help! Py++ Generated Binding Won't Compile In-Reply-To: <7465b6170704120215x3b8c3f8s55130a3f357fee23@mail.gmail.com> References: <9952809.post@talk.nabble.com> <7465b6170704120215x3b8c3f8s55130a3f357fee23@mail.gmail.com> Message-ID: <9972164.post@talk.nabble.com> Thanks for the help Roman. Not totally sure what I did, but this added code to the generate_code.py file seemed to fix the problem: event_class = mb.class_( 'Event' ) event_class.opaque = True event_class_setCon_ptr = event_class.member_function( 'setCondition', recursive=False ) event_class_setCon_ptr.call_policies = module_builder.call_policies.return_value_policy( call_policies.return_opaque_pointer ) event_class_setAct_ptr = event_class.member_function( 'setAction', recursive=False ) event_class_setAct_ptr.call_policies = module_builder.call_policies.return_value_policy( call_policies.return_opaque_pointer ) Roman Yakovenko wrote: > > On 4/12/07, Rocketman at JSC wrote: >> >> First of all--thanks for you guys putting in all the work to bring us > Py++, >> pygccxml, and boost.python--lots of hard work and I'm hoping to put it >> all >> to good use and save us some tax dollars. > > :-) > >> So here I am trying to generate bindings for the same C++ sim code, but > I've >> run into snag. Below I'll post the two header files I think are >> involved, >> Py++ script, Py++ messages, and the compiler errors. > > Few comments > > 1. You didn't specified call policies for functions registerEvent and > registerVehicle. If you just saves references to the objects I suggest you > to take a look on > http://boost.org/libs/python/doc/v2/with_custodian_and_ward.html call > policy > and to read tutorials. If Executive class also manages their life-time, > than > this is other story. Call policies are **very** important. > > 2. Event class is using raw PyObject type. There is a much better way: > http://boost.org/libs/python/doc/tutorial/doc/html/python/object.html > > 3. You don't really have to exclude getPtr methods. You can use call > policies to deal with them, even if this is a pointer to an array. Take a > look on Py++ defined call policies: > http://language-binding.net/pyplusplus/documentation/functions/call_policies.html > > 4. Py++ warnings is there for you. For example > >> WARNING: BallForce [class] >> > warning W1040: The declaration is unexposed, but there are other >> > declarations, which refer to it. This could >> > cause "no to_python converter found" run time error. Declarations: >> > Ball::force [variable] > > Py++ says you that you are exposing Ball::force member variable, while you > don't expose BallForce class( the type of the variable ). So when user > will > try to use "force" variable he will get an error. > > Of course you can disable them: > http://language-binding.net/pyplusplus/documentation/feedback.html > > Now about your errors. My guess is that they are caused by missing call > policies for functions setAction and setCondition. In order to be sure I > need to see generated code. > >> I don't have any clue where the problem is from these messages. Note the >> compiler mentions the 2nd to the last } of the generated wrapper >> Pyxis_wrap.cpp file and all the rest of the compiler messages don't >> appear >> to be in my code. If I remove the Event and Executive class--all builds > up >> fine. >> >> Thanks in advance for any help. > > HTH. > > P.S. It is much better to post small and complete hand-written example + > error message. > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > -- View this message in context: http://www.nabble.com/Help%21--Py%2B%2B-Generated-Binding-Won%27t-Compile-tf3563358.html#a9972164 Sent from the Python - c++-sig mailing list archive at Nabble.com. From ndbecker2 at gmail.com Fri Apr 13 15:20:28 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Fri, 13 Apr 2007 09:20:28 -0400 Subject: [C++-sig] howto check for an attr from c++? Message-ID: I need to check if an object has an attribute from c++. I'm using this low-level approach: (r is an object): if (PyObject_HasAttrString (r.ptr(), "__len__")) { Nothing wrong with that, but is there something in the boost::python api to handle this? From seefeld at sympatico.ca Fri Apr 13 15:33:30 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 13 Apr 2007 09:33:30 -0400 Subject: [C++-sig] howto check for an attr from c++? In-Reply-To: References: Message-ID: <461F86AA.2020306@sympatico.ca> Neal Becker wrote: > I need to check if an object has an attribute from c++. > > I'm using this low-level approach: > (r is an object): > if (PyObject_HasAttrString (r.ptr(), "__len__")) { > > Nothing wrong with that, but is there something in the boost::python api to > handle this? boost::python does provide getattr(), setattr(), and delattr(). HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From ndbecker2 at gmail.com Fri Apr 13 15:45:55 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Fri, 13 Apr 2007 09:45:55 -0400 Subject: [C++-sig] howto check for an attr from c++? References: <461F86AA.2020306@sympatico.ca> Message-ID: Stefan Seefeld wrote: > Neal Becker wrote: >> I need to check if an object has an attribute from c++. >> >> I'm using this low-level approach: >> (r is an object): >> if (PyObject_HasAttrString (r.ptr(), "__len__")) { >> >> Nothing wrong with that, but is there something in the boost::python api >> to handle this? > > boost::python does provide getattr(), setattr(), and delattr(). > > HTH, > Stefan > If I call getattr() on a nonexistent attribute, what happens? Does python throw an exception? (I believe it does). If so, how do I intercept it from c++? I'm trying to test is an attribute is defined. From seefeld at sympatico.ca Fri Apr 13 15:57:23 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 13 Apr 2007 09:57:23 -0400 Subject: [C++-sig] howto check for an attr from c++? In-Reply-To: References: <461F86AA.2020306@sympatico.ca> Message-ID: <461F8C43.2040809@sympatico.ca> Neal Becker wrote: > Stefan Seefeld wrote: > >> Neal Becker wrote: >>> I need to check if an object has an attribute from c++. >>> >>> I'm using this low-level approach: >>> (r is an object): >>> if (PyObject_HasAttrString (r.ptr(), "__len__")) { >>> >>> Nothing wrong with that, but is there something in the boost::python api >>> to handle this? >> boost::python does provide getattr(), setattr(), and delattr(). >> >> HTH, >> Stefan >> > > If I call getattr() on a nonexistent attribute, what happens? Does python > throw an exception? (I believe it does). If so, how do I intercept it from > c++? I'm trying to test is an attribute is defined. Unfortunately I couldn't find any documentation for the above. (David ?) However, I did find two versions of getattr(), one taking two arguments, one taking three. The latter allows to pass a default return value, as supported by python's own "getattr(o, key, default)". So, at least that last one clearly doesn't throw (and I would expect the former to simply use a default of None, i.e. object(). Just try it ! HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From j.reid at mail.cryst.bbk.ac.uk Fri Apr 13 18:34:33 2007 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Fri, 13 Apr 2007 17:34:33 +0100 Subject: [C++-sig] length of boost.python error messages Message-ID: Hi, When developing or using boost.python modules I frequently get Python argument type mismatches. Boost.python tells me that the arguments I have passed do not match/cannot be converted to the expected types. Unfortunately normally these C++ types are templates and are almost impossible to decipher from the error message. Perhaps it would be possible to place a newline in between the type of each argument that was passed and also in between the type of each argument that was expected. This could improve the readability of the error messages. A more general scheme for pretty printing templated types would be nice but I'm sure would be a lot more work. For example recently I found it difficult to determine which argument was incorrect from this error message: Traceback (most recent call last): File "temp.py", line 12, in distance_map = distances Boost.Python.ArgumentError: Python argument types in boost.graph._graph.dijkstra_shortest_paths(Graph, Vertex) did not match C++ signature: dijkstra_shortest_paths(class boost::graph::python::basic_graph graph, struct boost::graph::python::basic_descriptor root_vertex, class boost::vector_property_map,str uct boost::graph::python::basic_index_map,struct boost::adj_list_vertex_prope rty_map,struct boost::property,struct boost::no_property,struct boos t::listS>,unsigned int,unsigned int const &,enum boost::vertex_index_t> > > * pr edecessor_map=None, class boost::vector_property_map,struct boost::adj_list_vertex_property_map,struct boost::property,struct boost::no_property,struct boost::listS>,unsigned int,unsigned int const &,enum boost::vertex_index_t> > > * distance_map=None, c lass boost::vector_property_map,struct boost::bidirectionalS>,struc t boost::adj_list_edge_property_map const ,enum boost::edge_index_t> > > {lvalu e} weight_map, class boost::python::api::object visitor=None, class boost::vecto r_property_map,struct boost::adj_list_vertex_property_map,struct boost::property,struct boost::no_property,struct boost::listS>,unsigned int,unsigned int const &,enum boost::vertex_index_t> > > * color_map=None) dijkstra_shortest_paths(class boost::graph::python::basic_graph graph, struct boost::graph::python::basic_descriptor root_vertex, class boost::vector_property_map,struct boost ::graph::python::basic_index_map,struct boost::adj_list_vertex_property_map,struct boost::property,struct boost::no_property,struct boost::listS>,unsig ned int,unsigned int const &,enum boost::vertex_index_t> > > * predecessor_map=N one, class boost::vector_property_map,struct boost::adj_list_vertex_property_map,stru ct boost::property,struct boost::no_property,struct boost::listS>,unsigned int,unsigned int con st &,enum boost::vertex_index_t> > > * distance_map=None, class boost::vector_pr operty_map,struct boost::undirectedS>,struct boost::adj_list_edge_prop erty_map const ,enum boost::edge_index_t> > > {lvalue} weight_map, class boost::py thon::api::object visitor=None, class boost::vector_property_map,struct boost::adj_l ist_vertex_property_map,struct boost::property,struct boost::no_propert y,struct boost::listS>,unsigned int,unsigned int const &,enum boost::vertex_inde x_t> > > * color_map=None) Apologies for the line breaks in the formatting above. Windows seems to want to make my life difficult. Thanks, John. From roman.yakovenko at gmail.com Fri Apr 13 19:43:24 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 13 Apr 2007 20:43:24 +0300 Subject: [C++-sig] Calling member function of object created in C++ via embedded Python In-Reply-To: References: Message-ID: <7465b6170704131043r44fa7fa3m7f3bd6a88316f5ad@mail.gmail.com> On 4/13/07, Ron Brown, Jr. wrote: > Please forgive me if this is a ridiculously simple question, but I'm new > to both Python and Boost.Python, and I'm having hard time getting my > bearings. There are few resources to look for information. One of them is: http://wiki.python.org/moin/boost.python/EmbeddingPython You can find more complete list here: http://language-binding.net/pyplusplus/links.html#help-resources > > I'll try and explain using a modified version of the example from > http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html You definitely should continue to read tutorials: http://www.boost.org/libs/python/doc/tutorial/doc/html/python/object.html And finally the answer to your question is here: http://www.boost.org/libs/python/doc/v2/call_method.html > ---------------------------------------- > #include > using namespace boost::python; > > class World > { > public: > void set(std::string msg) { this->msg = msg; } > std::string greet() { return msg; } > std::string msg; > }; > > BOOST_PYTHON_MODULE(hello) > { > class_("World") > .def("greet", &World::greet) > .def("set", &World::set) > ; > } > > int main(int argc, char *argv[]) > { > > Py_Initialize(); > > World worldObject; > worldObject.set("Hello, World!"); > > try { > inithello(); > PyRun_SimpleString("import hello"); > // ******************** > // How would I call the equivalent of: > // worldObject.greet(); > // from Python? > // ******************** namespace bpl = boost::python; bpl::object py_wo( World() ); bpl::call_method(py_wo, "set", "Hello World"); I think this should work -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Fri Apr 13 19:49:29 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 13 Apr 2007 20:49:29 +0300 Subject: [C++-sig] Help! Py++ Generated Binding Won't Compile In-Reply-To: <9972164.post@talk.nabble.com> References: <9952809.post@talk.nabble.com> <7465b6170704120215x3b8c3f8s55130a3f357fee23@mail.gmail.com> <9972164.post@talk.nabble.com> Message-ID: <7465b6170704131049q546513b1ld5000f1a28f42678@mail.gmail.com> On 4/13/07, Rocketman at JSC wrote: > > Thanks for the help Roman. Not totally sure what I did, but this added code > to the generate_code.py file seemed to fix the problem: > > event_class = mb.class_( 'Event' ) > event_class.opaque = True > > event_class_setCon_ptr = event_class.member_function( 'setCondition', > recursive=False ) > event_class_setCon_ptr.call_policies = > module_builder.call_policies.return_value_policy( > call_policies.return_opaque_pointer ) > > event_class_setAct_ptr = event_class.member_function( 'setAction', > recursive=False ) > event_class_setAct_ptr.call_policies = > module_builder.call_policies.return_value_policy( > call_policies.return_opaque_pointer ) Well you set call policies. I am not sure they are the best one. Does it work for you? ( I mean run-time ) -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From rbrown at gamry.com Fri Apr 13 21:23:26 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Fri, 13 Apr 2007 15:23:26 -0400 Subject: [C++-sig] Calling member function of object created in C++ via embedded Python In-Reply-To: <7465b6170704131043r44fa7fa3m7f3bd6a88316f5ad@mail.gmail.com> References: <7465b6170704131043r44fa7fa3m7f3bd6a88316f5ad@mail.gmail.com> Message-ID: Roman, Thanks for the reply. Believe me, I looked at all of those links before posting. ;) However, regarding your response of: Roman Yakovenko wrote: > namespace bpl = boost::python; > bpl::object py_wo( World() ); > bpl::call_method(py_wo, "set", "Hello World"); > > I think this should work Now unless I'm just misunderstanding, what you're doing there is not exactly what I'm trying to do. Wouldn't that code just create ANOTHER object of class World on the Python side? Again, I'm embedding Python in a C++ app, and what I'm trying to do is just pass an already-instantiated object from the C++ through to Python, and then in Python, call one of that particular object's member functions. I want to use the SAME object in both C++ and Python. - Ron From roman.yakovenko at gmail.com Fri Apr 13 22:24:59 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 13 Apr 2007 23:24:59 +0300 Subject: [C++-sig] Calling member function of object created in C++ via embedded Python In-Reply-To: References: <7465b6170704131043r44fa7fa3m7f3bd6a88316f5ad@mail.gmail.com> Message-ID: <7465b6170704131324g228e4737m236f0880d7da752@mail.gmail.com> On 4/13/07, Ron Brown, Jr. wrote: > Roman, > However, regarding your response of: > > Roman Yakovenko wrote: > > namespace bpl = boost::python; > > bpl::object py_wo( World() ); > > bpl::call_method(py_wo, "set", "Hello World"); > > > > I think this should work > > Now unless I'm just misunderstanding, what you're doing there is not > exactly what I'm trying to do. Wouldn't that code just create ANOTHER > object of class World on the Python side? Again, I'm embedding Python > in a C++ app, and what I'm trying to do is just pass an > already-instantiated object from the C++ through to Python, and then in > Python, call one of that particular object's member functions. I want > to use the SAME object in both C++ and Python. Now I understand. I think you are looking for next functionality: http://boost.org/libs/python/doc/v2/ptr.html -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From snemeth at houston.rr.com Sat Apr 14 05:03:45 2007 From: snemeth at houston.rr.com (Rocketman@JSC) Date: Fri, 13 Apr 2007 20:03:45 -0700 (PDT) Subject: [C++-sig] Help! Py++ Generated Binding Won't Compile In-Reply-To: <7465b6170704131049q546513b1ld5000f1a28f42678@mail.gmail.com> References: <9952809.post@talk.nabble.com> <7465b6170704120215x3b8c3f8s55130a3f357fee23@mail.gmail.com> <9972164.post@talk.nabble.com> <7465b6170704131049q546513b1ld5000f1a28f42678@mail.gmail.com> Message-ID: <9989600.post@talk.nabble.com> Roman, Well earlier I posted too soon. Apparently the only reason it built up was cause the Event class was excluded from the wrapper file, but now I finally do have it working--yea! The clip below was used sucessfully in the generate_code.py file, but the "event_class.opaque = True" was removed. One other big thing had to be fixed and I only found it by trial and error: Basically somewhere along the lines, either boost or py++ didn't like the two data members, PyObject *condition and PyObject *action in the Event class definition. Once I made those private, all built up without the compiler error. My simple sim now runs and gives the same results as the SWIG version. I really have lots to learn. Thanks Again for the help, Scott Roman Yakovenko wrote: > > On 4/13/07, Rocketman at JSC wrote: >> >> Thanks for the help Roman. Not totally sure what I did, but this added >> code >> to the generate_code.py file seemed to fix the problem: >> >> event_class = mb.class_( 'Event' ) >> event_class.opaque = True >> >> event_class_setCon_ptr = event_class.member_function( 'setCondition', >> recursive=False ) >> event_class_setCon_ptr.call_policies = >> module_builder.call_policies.return_value_policy( >> call_policies.return_opaque_pointer ) >> >> event_class_setAct_ptr = event_class.member_function( 'setAction', >> recursive=False ) >> event_class_setAct_ptr.call_policies = >> module_builder.call_policies.return_value_policy( >> call_policies.return_opaque_pointer ) > > Well you set call policies. I am not sure they are the best one. Does > it work for you? > ( I mean run-time ) > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > -- View this message in context: http://www.nabble.com/Help%21--Py%2B%2B-Generated-Binding-Won%27t-Compile-tf3563358.html#a9989600 Sent from the Python - c++-sig mailing list archive at Nabble.com. From p.jaroszynski at gmail.com Sun Apr 15 01:51:46 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Sun, 15 Apr 2007 01:51:46 +0200 Subject: [C++-sig] Methods returning *this Message-ID: <200704150151.47151.p.jaroszynski@gmail.com> Hello, I have a problem with wrapping such methods: struct Foo { Foo & blah(....) { (....) return *this; } }; What call policy should I use? I have really (blindly) tried quite a few of them and I can't get rid of the segfault when trying to access the object after calling its blah method. TIA for help! -- Best Regards, Piotr Jaroszynski From roman.yakovenko at gmail.com Sun Apr 15 05:56:38 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 15 Apr 2007 06:56:38 +0300 Subject: [C++-sig] Methods returning *this In-Reply-To: <200704150151.47151.p.jaroszynski@gmail.com> References: <200704150151.47151.p.jaroszynski@gmail.com> Message-ID: <7465b6170704142056y53318707g5a2bd8b9b2e03abf@mail.gmail.com> On 4/15/07, Piotr Jaroszynski wrote: > Hello, > > I have a problem with wrapping such methods: > struct Foo { > Foo & blah(....) { > (....) > return *this; > } > }; > > What call policy should I use? I have really (blindly) tried quite a few of > them and I can't get rid of the segfault when trying to access the object > after calling its blah method. return_self: http://boost.org/libs/python/doc/v2/return_arg.html#return_self-spec by the way segfault could be caused by the function too and may be has nothing to do with boost.python -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From p.jaroszynski at gmail.com Sun Apr 15 13:05:00 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Sun, 15 Apr 2007 13:05:00 +0200 Subject: [C++-sig] Methods returning *this In-Reply-To: <7465b6170704142056y53318707g5a2bd8b9b2e03abf@mail.gmail.com> References: <200704150151.47151.p.jaroszynski@gmail.com> <7465b6170704142056y53318707g5a2bd8b9b2e03abf@mail.gmail.com> Message-ID: <200704151305.00637.p.jaroszynski@gmail.com> On Sunday 15 of April 2007 05:56:38 Roman Yakovenko wrote: > return_self: > http://boost.org/libs/python/doc/v2/return_arg.html#return_self-spec Thank you very much, worked! Btw. the example in the spec makes a good job in hiding where this policy is really needed. > by the way segfault could be caused by the function too and may be has > nothing to do with boost.python Yeah, I know, but the function call was "working", the segfault occurred only after when trying to access one of the object attributes, so I figured out that must be something with policies :] -- Best Regards, Piotr Jaroszynski From quant.mind at gmail.com Sun Apr 15 23:09:51 2007 From: quant.mind at gmail.com (Quant Mind) Date: Sun, 15 Apr 2007 23:09:51 +0200 Subject: [C++-sig] abstract class and virtual function Message-ID: <8315652a0704151409m1f0b15f5v747baeba97d230e6@mail.gmail.com> I'm running into some problem in exposing an abstract class with some virtual functions with default implementations (no problem with pure virtuals). The problem seems quite trivial but I cannot get to the bottom of it. struct rateWrap : public irate, public boost::python::wrapper { bool isScalar() const; double value() const; // bool default_isScalar() { return this->irate::isScalar(); } double default_value() { return this->irate::value(); } }; // inline bool rateWrap::isScalar() const { if(boost::python::override overfun = this->get_override("isScalar")) #ifdef _MSC_VER return boost::python::call(overfun.ptr()); #else return overfun(); #endif // _MSC_VER return irate::isScalar(); } // inline double rateWrap::value() const { if(boost::python::override overfun = this->get_override("value")) #ifdef _MSC_VER return boost::python::call(overfun.ptr()); #else return overfun(); #endif // _MSC_VER return irate::realvalue(); } While the first function, returning bool, works fine :) the second one, returning double, does not work!!! :( I'm compiling with Microsoft Visual Studio 2005 which returns the following error 'return' cannot convert from 'const char *' to 'double' I don't know what is the problem, any advise? Luca -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Mon Apr 16 07:25:07 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 16 Apr 2007 08:25:07 +0300 Subject: [C++-sig] abstract class and virtual function In-Reply-To: <8315652a0704151409m1f0b15f5v747baeba97d230e6@mail.gmail.com> References: <8315652a0704151409m1f0b15f5v747baeba97d230e6@mail.gmail.com> Message-ID: <7465b6170704152225u2bd0850amb9aec95bf060d243@mail.gmail.com> On 4/16/07, Quant Mind wrote: > > I'm running into some problem in exposing an abstract class with some > virtual functions with default implementations (no problem with pure > virtuals). > The problem seems quite trivial but I cannot get to the bottom of it. > > struct rateWrap : public irate, public boost::python::wrapper > { > bool isScalar() const; > double value() const; > // > bool default_isScalar() { return this->irate::isScalar(); } > double default_value() { return this->irate::value(); } > }; > // > inline bool rateWrap::isScalar() const > { > if(boost::python::override overfun = this->get_override("isScalar")) > > #ifdef _MSC_VER > return boost::python::call(overfun.ptr()); > #else > return overfun(); > #endif // _MSC_VER > return irate::isScalar(); > } > // > inline double rateWrap::value() const > { > if(boost::python::override overfun = this->get_override("value")) > #ifdef _MSC_VER > return boost::python::call(overfun.ptr()); > #else > return overfun(); > #endif // _MSC_VER > return irate::realvalue(); > } > > While the first function, returning bool, works fine :) the second one, > returning double, does not work!!! :( > I'm compiling with Microsoft Visual Studio 2005 which returns the > following error > > 'return' cannot convert from 'const char *' to 'double' > > I don't know what is the problem, any advise? > In both cases you return "char const*" , probably it should be : return boost::python::call(overfun.ptr()); and return boost::python::call(overfun.ptr()); -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From pierre.bourgault at autodesk.com Mon Apr 16 21:33:37 2007 From: pierre.bourgault at autodesk.com (Pierre Bourgault) Date: Mon, 16 Apr 2007 15:33:37 -0400 Subject: [C++-sig] Boost.Python with Python 2.5 64-bit compile error (Linux, AMD, x86_64) Message-ID: <0543829BDF24954791B86D31F121EE0B59390797@msgusaebk01.autodesk.com> Hi, I was compiling Boost.Python against Python 2.5 in 64-bit (AMD, Linux) and was getting the following error: /var/tmp/boost_1_33_1/libs/python/build/../src/object_protocol.cpp: In function `PyObject* boost::python::api::::apply_slice(PyObject*, PyObject*, PyObject*)': /var/tmp/boost_1_33_1/libs/python/build/../src/object_protocol.cpp:110: error: cannot convert `int*' to `Py_ssize_t*' for argument `2' to `int _PyEval_SliceIndex(PyObject*, Py_ssize_t*)' Looking into object_protocol.cpp, a comment says that apply_slice() and assign_slice() were copy/pasted from Python source. Comparing Python 2.5 apply_slice with boost apply_slice shows the following difference: Boost: int ilow = 0, ihigh = INT_MAX; if (!_PyEval_SliceIndex(v, &ilow)) return -1; Python 2.5: Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX; if (!_PyEval_SliceIndex(v, &ilow)) return -1; I fixed the boost file and it worked. I suppose there are good reasons for the copy/paste but it was not for that, the error would have never shown. Pierre Bourgault --- FYI, I use bjam (3.1.13) but I had to edit jam.h so that OSPLAT is also defined for x86_64. -------------- next part -------------- An HTML attachment was scrubbed... URL: From quant.mind at gmail.com Tue Apr 17 13:37:50 2007 From: quant.mind at gmail.com (Quant Mind) Date: Tue, 17 Apr 2007 13:37:50 +0200 Subject: [C++-sig] abstract class and virtual function In-Reply-To: <7465b6170704152225u2bd0850amb9aec95bf060d243@mail.gmail.com> References: <8315652a0704151409m1f0b15f5v747baeba97d230e6@mail.gmail.com> <7465b6170704152225u2bd0850amb9aec95bf060d243@mail.gmail.com> Message-ID: <8315652a0704170437n21b3a8ebl4c9701046b024d5e@mail.gmail.com> yep, late at night I guess. Thanks On 16/04/07, Roman Yakovenko wrote: > > On 4/16/07, Quant Mind wrote: > > > I'm running into some problem in exposing an abstract class with some > > virtual functions with default implementations (no problem with pure > > virtuals). > > The problem seems quite trivial but I cannot get to the bottom of it. > > > > struct rateWrap : public irate, public boost::python::wrapper > > { > > bool isScalar() const; > > double value() const; > > // > > bool default_isScalar() { return this->irate::isScalar(); } > > double default_value() { return this->irate::value(); } > > }; > > // > > inline bool rateWrap::isScalar() const > > { > > if(boost::python::override overfun = > > this->get_override("isScalar")) > > #ifdef _MSC_VER > > return boost::python::call(overfun.ptr()); > > #else > > return overfun(); > > #endif // _MSC_VER > > return irate::isScalar(); > > } > > // > > inline double rateWrap::value() const > > { > > if(boost::python::override overfun = this->get_override("value")) > > #ifdef _MSC_VER > > return boost::python::call(overfun.ptr()); > > #else > > return overfun(); > > #endif // _MSC_VER > > return irate::realvalue(); > > } > > > > While the first function, returning bool, works fine :) the second one, > > returning double, does not work!!! :( > > I'm compiling with Microsoft Visual Studio 2005 which returns the > > following error > > > > 'return' cannot convert from 'const char *' to 'double' > > > > I don't know what is the problem, any advise? > > > > In both cases you return "char const*" , probably it should be : > > return boost::python::call(overfun.ptr()); > and > return boost::python::call(overfun.ptr()); > > > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ndbecker2 at gmail.com Tue Apr 17 14:48:48 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Tue, 17 Apr 2007 08:48:48 -0400 Subject: [C++-sig] which return policy? Message-ID: In this: A free_func (B& b); Where A is a class that maintains a reference to b, class_ ... .def ("get_A", free_func, return_arg<1>()) Is this the correct return policy? Looking at python ref counts, I don't think so. From roman.yakovenko at gmail.com Tue Apr 17 14:52:30 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 17 Apr 2007 15:52:30 +0300 Subject: [C++-sig] which return policy? In-Reply-To: References: Message-ID: <7465b6170704170552n7f7f204crc1c32708a85ba068@mail.gmail.com> On 4/17/07, Neal Becker wrote: > In this: > > A free_func (B& b); > > Where A is a class that maintains a reference to b, > > class_ ... > .def ("get_A", free_func, return_arg<1>()) > > Is this the correct return policy? > I guess with_custodian_and_ward_postcall: http://boost.org/libs/python/doc/v2/with_custodian_and_ward.html#with_custodian_and_ward_postcall-spec -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From ndbecker2 at gmail.com Tue Apr 17 15:06:58 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Tue, 17 Apr 2007 09:06:58 -0400 Subject: [C++-sig] which return policy? References: <7465b6170704170552n7f7f204crc1c32708a85ba068@mail.gmail.com> Message-ID: Roman Yakovenko wrote: > On 4/17/07, Neal Becker wrote: >> In this: >> >> A free_func (B& b); >> >> Where A is a class that maintains a reference to b, >> >> class_ ... >> .def ("get_A", free_func, return_arg<1>()) >> >> Is this the correct return policy? >> > I guess with_custodian_and_ward_postcall: > http://boost.org/libs/python/doc/v2/with_custodian_and_ward.html#with_custodian_and_ward_postcall-spec > Thanks. Looks like with_cusotdian_and_ward_postcall<0,1> is correct. From rwgk at yahoo.com Tue Apr 17 21:11:44 2007 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Tue, 17 Apr 2007 12:11:44 -0700 (PDT) Subject: [C++-sig] Boost.Python with Python 2.5 64-bit compile error (Linux, AMD, x86_64) Message-ID: <858500.27717.qm@web31112.mail.mud.yahoo.com> I checked in Py_ssize_t support even before Python 2.5 was released. Unfortunately there hasn't been a boost release since then. Until there is a new release, the best approach IMO is to work with the current boost CVS (I'm doing this all the time with very little occasional problems). Ralf __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From rbrown at gamry.com Wed Apr 18 17:58:49 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Wed, 18 Apr 2007 11:58:49 -0400 Subject: [C++-sig] Calling member function of object created in C++ via embedded Python In-Reply-To: References: <7465b6170704131043r44fa7fa3m7f3bd6a88316f5ad@mail.gmail.com> Message-ID: I'm still having quite a bit of trouble wrapping my mind around this problem, and feel like I'm just missing something obvious. Again, what I'm trying to do is have an instance of an object in C++ and call one of that particular object's member functions from Python. In the example below, I create an instance of the "World" class on the C++ side. It's then COPIED into a new object on the Python side. This is not how I want my application to behave. How can I change this example so that Python doesn't create a NEW object and copy it, but instead uses the object created by C++? I read through the documentation on "ptr()", but I'm not really sure how to apply that to this problem. I'm still quite new to both Python and Boost.Python, so please keep that in mind. Thanks again! -Ron ---------------------------------------- #include #include using namespace boost::python; class World { public: void set(std::string msg) { this->msg = msg; } std::string greet() { return msg; } std::string msg; }; BOOST_PYTHON_MODULE(hello) { class_("World") .def("greet", &World::greet) .def("set", &World::set) ; } int main(int argc, char *argv[]) { Py_Initialize(); World worldObject, worldObject2; worldObject.set("Hello from C++!"); try { inithello(); PyRun_SimpleString("import hello"); object module(handle<>(borrowed(PyImport_AddModule("__main__")))); object dictionary = module.attr("__dict__"); dictionary["pyWorldObject"] = worldObject; PyRun_SimpleString("pyWorldObject.set('Hello from Python!')"); object resultObject = dictionary["pyWorldObject"]; worldObject2 = extract(resultObject); } catch (error_already_set) { PyErr_Print(); } std::cout << "worldObject.greet(): " << worldObject.greet() << std::endl; std::cout << "worldObject2.greet(): " << worldObject2.greet() << std::endl; Py_Finalize(); return 0; } ---------------------------------------- Output: worldObject.greet(): Hello from C++! worldObject2.greet(): Hello from Python! ---------------------------------------- From j.reid at mail.cryst.bbk.ac.uk Wed Apr 18 18:31:50 2007 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Wed, 18 Apr 2007 17:31:50 +0100 Subject: [C++-sig] Calling member function of object created in C++ via embedded Python In-Reply-To: References: <7465b6170704131043r44fa7fa3m7f3bd6a88316f5ad@mail.gmail.com> Message-ID: If I have you right you might try this: typedef boost::shared_ptr< World > world_ptr; world_ptr worldObject( new World ), worldObject2; ... worldObject2 = extract(resultObject); If you pass pointers to objects around instead of objects by value you will probably have more luck. The following line as it stands forces use of the copy constructor (if worldObject is of type World) dictionary["pyWorldObject"] = worldObject For boost.python to work with world_ptrs you will need to tell it about them, so add this to the BOOST_PYTHON_MODULE: register_ptr_to_python( world_ptr ); There may be typos in the above but I think this is along the right sort of lines.... John. From rbrown at gamry.com Wed Apr 18 19:31:14 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Wed, 18 Apr 2007 13:31:14 -0400 Subject: [C++-sig] Calling member function of object created in C++ via embedded Python In-Reply-To: References: <7465b6170704131043r44fa7fa3m7f3bd6a88316f5ad@mail.gmail.com> Message-ID: Thanks John, I'll give that a shot! -Ron John Reid wrote: > If I have you right you might try this: > > typedef boost::shared_ptr< World > world_ptr; > world_ptr worldObject( new World ), worldObject2; > > ... > > worldObject2 = extract(resultObject); > > > If you pass pointers to objects around instead of objects by value you > will probably have more luck. The following line as it stands forces use > of the copy constructor (if worldObject is of type World) > dictionary["pyWorldObject"] = worldObject > > For boost.python to work with world_ptrs you will need to tell it about > them, so add this to the BOOST_PYTHON_MODULE: > > register_ptr_to_python( world_ptr ); > > > There may be typos in the above but I think this is along the right sort > of lines.... > > John. From roman.yakovenko at gmail.com Wed Apr 18 19:47:00 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 18 Apr 2007 20:47:00 +0300 Subject: [C++-sig] Calling member function of object created in C++ via embedded Python In-Reply-To: References: <7465b6170704131043r44fa7fa3m7f3bd6a88316f5ad@mail.gmail.com> Message-ID: <7465b6170704181047m41bbbb75vc4d6d81b3cd9a3da@mail.gmail.com> On 4/18/07, Ron Brown, Jr. wrote: > I'm still having quite a bit of trouble wrapping my mind around this > problem, and feel like I'm just missing something obvious. Again, what > I'm trying to do is have an instance of an object in C++ and call one of > that particular object's member functions from Python. > > In the example below, I create an instance of the "World" class on the > C++ side. It's then COPIED into a new object on the Python side. This > is not how I want my application to behave. How can I change this > example so that Python doesn't create a NEW object and copy it, but > instead uses the object created by C++? > > I read through the documentation on "ptr()", but I'm not really sure how > to apply that to this problem. > > I'm still quite new to both Python and Boost.Python, so please keep that > in mind. Thanks again! > > > -Ron > > ---------------------------------------- > #include > #include > using namespace boost::python; > > class World > { > public: > void set(std::string msg) { this->msg = msg; } > std::string greet() { return msg; } > std::string msg; > }; > > BOOST_PYTHON_MODULE(hello) > { > class_("World") > .def("greet", &World::greet) > .def("set", &World::set) > ; > } > > int main(int argc, char *argv[]) > { > > Py_Initialize(); > > World worldObject, worldObject2; > worldObject.set("Hello from C++!"); > > > try { > inithello(); > PyRun_SimpleString("import hello"); > object module(handle<>(borrowed(PyImport_AddModule("__main__")))); > object dictionary = module.attr("__dict__"); > > dictionary["pyWorldObject"] = worldObject; I think dictionary["pyWorldObject"] = boost::python::ptr( &worldObject ); should work. > > PyRun_SimpleString("pyWorldObject.set('Hello from Python!')"); > > object resultObject = dictionary["pyWorldObject"]; > worldObject2 = extract(resultObject); > > } catch (error_already_set) { > PyErr_Print(); > } > > std::cout << "worldObject.greet(): " << worldObject.greet() << std::endl; > std::cout << "worldObject2.greet(): " << worldObject2.greet() << std::endl; > > Py_Finalize(); > > return 0; > } > ---------------------------------------- > Output: > worldObject.greet(): Hello from C++! > worldObject2.greet(): Hello from Python! > ---------------------------------------- > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From mikeowen at llnl.gov Wed Apr 18 19:49:10 2007 From: mikeowen at llnl.gov (J. Michael Owen) Date: Wed, 18 Apr 2007 10:49:10 -0700 Subject: [C++-sig] Possible Boost.Python (1.33.1) source code problem Message-ID: <3b2f8ce193f79c7ecba355476e9850b0@llnl.gov> Hi, Recently we've had the IBM xlC team trying to get the last release of Boost (1.33.1) to build with xlC, and they have found one problem that they believe is an error in the Boost source. They suggest that line 158 of boost/python/detail/def_helper.hpp should be changed from , keywords<0> to the fully qualified form , boost::python::detail::keywords<0> because they believe the original form to be ambiguous. The (not too detailed) message they sent me is as follows: > --- > This one we think is a boost source error. The access to keywords need > to be fully qualified to distinguish it from it earlier use. > You need to patch your boost header as follows: Looking at the file I don't necessarily see why the original form is ambiguous myself. Does someone more knowledgeable believe this change is required, or should I push the xlC guys and say we believe this is still a weakness in their compiler? Mike Owen. From giuott at tin.it Wed Apr 18 21:03:00 2007 From: giuott at tin.it (Giuseppe Ottaviano) Date: Wed, 18 Apr 2007 21:03:00 +0200 Subject: [C++-sig] bases<> from an MPL sequence Message-ID: [I sent this message to the Boost ML but had no answer, hope here I have more luck :) ] Hi, I have the base class types of a given class in an MPL sequence (a mpl::vector). I have to expose this class via Boost.Python, passing a bases<...> argument to the class_ declaration. For example, if I have mpl::vector I should pass bases. Is there a way to convert a generic MPL sequence to a bases<> sequence? I haven't found an easy way to do it (push_back<> on bases<> doesn't return a bases<> type, so boost::python::detail::specifies_bases<> fails) Thanks. -- Giuseppe Ottaviano From rbrown at gamry.com Wed Apr 18 20:59:37 2007 From: rbrown at gamry.com (Ron Brown, Jr.) Date: Wed, 18 Apr 2007 14:59:37 -0400 Subject: [C++-sig] Calling member function of object created in C++ via embedded Python In-Reply-To: References: <7465b6170704131043r44fa7fa3m7f3bd6a88316f5ad@mail.gmail.com> Message-ID: John, Thanks for the help, that's exactly what I was looking for! Below is the modified code that shows what I was looking to do. This was also helpful: http://www.boost.org/libs/python/doc/v2/register_ptr_to_python.html Thanks again! - Ron -------------------------------------------------- #include #include using namespace boost::python; class World { public: void set(std::string msg) { this->msg = msg; } std::string greet() { return msg; } std::string msg; }; typedef boost::shared_ptr< World > world_ptr; BOOST_PYTHON_MODULE(hello) { class_("World") .def("greet", &World::greet) .def("set", &World::set) ; register_ptr_to_python(); } int main(int argc, char *argv[]) { Py_Initialize(); world_ptr worldObjectPtr (new World); worldObjectPtr->set("Hello from C++!"); try { inithello(); PyRun_SimpleString("import hello"); object module(handle<>(borrowed(PyImport_AddModule("__main__")))); object dictionary = module.attr("__dict__"); dictionary["pyWorldObjectPtr"] = worldObjectPtr; PyRun_SimpleString("pyWorldObjectPtr.set('Hello from Python!')"); } catch (error_already_set) { PyErr_Print(); } std::cout << "worldObjectPtr->greet(): " << worldObjectPtr->greet() << std::endl; Py_Finalize(); return 0; } -------------------------------------------------- Output: worldObjectPtr->greet(): Hello from Python! -------------------------------------------------- From ccambly at ca.ibm.com Wed Apr 18 20:49:59 2007 From: ccambly at ca.ibm.com (Christopher Cambly) Date: Wed, 18 Apr 2007 14:49:59 -0400 Subject: [C++-sig] Possible Boost.Python (1.33.1) source code problem In-Reply-To: <3b2f8ce193f79c7ecba355476e9850b0@llnl.gov> Message-ID: c++-sig-bounces at python.org wrote on 04/18/2007 01:49:10 PM: > Hi, > > Recently we've had the IBM xlC team trying to get the last release of > Boost (1.33.1) to build with xlC, and they have found one problem that > they believe is an error in the Boost source. They suggest that line > 158 of boost/python/detail/def_helper.hpp should be changed from > > , keywords<0> > > to the fully qualified form > > , boost::python::detail::keywords<0> > > because they believe the original form to be ambiguous. The (not too > detailed) message they sent me is as follows: > > > --- > > This one we think is a boost source error. The access to keywords need > > to be fully qualified to distinguish it from it earlier use. > > You need to patch your boost header as follows: > > Looking at the file I don't necessarily see why the original form is > ambiguous myself. Does someone more knowledgeable believe this change > is required, or should I push the xlC guys and say we believe this is > still a weakness in their compiler? The patched boost source code is no longer required with the development branch of xlC. Your compiler version will require the use of the patched boost source in the interim. > > Mike Owen. > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig Chris Cambly XL C++ Compiler Development -------------- next part -------------- An HTML attachment was scrubbed... URL: From aljaz.fajmut at siol.net Thu Apr 19 01:16:13 2007 From: aljaz.fajmut at siol.net (Aljaz) Date: Thu, 19 Apr 2007 01:16:13 +0200 Subject: [C++-sig] application size Message-ID: Im considering about embeding python in my windows application.. How much would this affect it's size? I use MSVC 2005 on Win Xp. I dont want it to be much bigger or dependent on any other files.. Thanks for help Regards From aljaz.fajmut at siol.net Thu Apr 19 01:14:56 2007 From: aljaz.fajmut at siol.net (Aljaz) Date: Thu, 19 Apr 2007 01:14:56 +0200 Subject: [C++-sig] question about api names Message-ID: Hello Are you going to change python api names in future to something more c++ oriented? For instance: Py_Initialize(); -> boost::python::initialize(); Regards From snemeth at houston.rr.com Thu Apr 19 04:09:27 2007 From: snemeth at houston.rr.com (Rocketman@JSC) Date: Wed, 18 Apr 2007 19:09:27 -0700 (PDT) Subject: [C++-sig] Supplying a Python base class when wrapping a C++ class... In-Reply-To: <4548E141.8030206@pixar.com> References: <4548E141.8030206@pixar.com> Message-ID: <10070530.post@talk.nabble.com> Has anyone figured out how to do this? Alex Mohr-2 wrote: > > I'd like to wrap a C++ class and have the generated python wrapper class > derive from a Python class. > > I know I can use bases<> to have my wrapped class derive from the class > objects associated with the C++ types I specify in bases<>, but in this > instance, I want to derive my wrapper class from a Python class that was > not generated by boost.python. > > From looking at the code, it seems like it would be some work to add > this capability. (Is anyone working on something like this?) > > As a workaround, does anyone know a way that I could perhaps hack this > in after the Python class is created? That is, can I change the Python > base classes of a Python class at runtime? > > Thanks, > > Alex > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > -- View this message in context: http://www.nabble.com/-C%2B%2B-sig--Supplying-a-Python-base-class-when-wrapping-a-C%2B%2B-class...-tf2554848.html#a10070530 Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Thu Apr 19 07:52:26 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 19 Apr 2007 08:52:26 +0300 Subject: [C++-sig] Supplying a Python base class when wrapping a C++ class... In-Reply-To: <10070530.post@talk.nabble.com> References: <4548E141.8030206@pixar.com> <10070530.post@talk.nabble.com> Message-ID: <7465b6170704182252o1d54bfb9kf9d5494f4ee07e4@mail.gmail.com> On 4/19/07, Rocketman at JSC wrote: > > Has anyone figured out how to do this? I recently spent some time on this issue. I was supposed to expose library defined exceptions and make them play well with "try-except" functionality. I found that it is not possible to create Python class which derives from Boost.Python exposed class and Exception. I've got TypeError: "Error when calling the metaclass bases multiple bases have instance lay-out conflict". So I doubt this issue could be solved on C++ level too. Fortunately I found pretty simple( for my case ) work around: http://language-binding.net/pyplusplus/troubleshooting_guide/exceptions/exceptions.html . The page also contains full source code and unit tests. Let me know if this helped you. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From j.reid at mail.cryst.bbk.ac.uk Thu Apr 19 18:39:12 2007 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Thu, 19 Apr 2007 17:39:12 +0100 Subject: [C++-sig] Extract from numpy uint32 Message-ID: Hi, Anyone know why the following extract< unsigned >() fail? C++: int test_unsigned_extraction( boost::python::object i ) { return boost::python::extract< unsigned >( i ); } int test_signed_extraction( boost::python::object i ) { return boost::python::extract< int >( i ); } export ... Python: import ... In [5]: test_unsigned_extraction( numpy.uint32( 1 ) ) --------------------------------------------------------------------------- Traceback (most recent call last) C:\Dev\MyProjects\Bio\hmm\python\ in () : No registered converter was able to produce a C++ rvalue of type unsigned int from this Python object of type numpy.uint32 In [6]: test_unsigned_extraction( numpy.int32( 1 ) ) Out[6]: 1 In [7]: test_signed_extraction( numpy.uint32( 1 ) ) --------------------------------------------------------------------------- Traceback (most recent call last) C:\Dev\MyProjects\Bio\hmm\python\ in () : No registered converter was able to produce a C++ rvalue of type int from this Python object of type numpy.uint32 In [8]: test_signed_extraction( numpy.int32( 1 ) ) Out[8]: 1 I guess this is something to do with the numpy int32 and uint32 types but I thought it must be worth asking here. I had a quick look at extract.hpp but didn't manage to make sense of it yet. Thanks, John. From p.jaroszynski at gmail.com Fri Apr 20 14:10:13 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Fri, 20 Apr 2007 14:10:13 +0200 Subject: [C++-sig] std::tr1::shared_ptr as boost::shared_ptr Message-ID: <200704201410.14020.p.jaroszynski@gmail.com> Hello, I am exposing a C++ app, which makes heavy use of tr1::shared_ptr, which seems to be exactly(?) the same as boost::shared_ptr. I have managed to make most of the stuff work just as it would be boost::shared_ptr ( additionally adding support for shared_ptr - thanks to [1] ): namespace my_app_namespace { //Make Boost.Python work with std::tr1::shared_ptr<> template inline T_ * get_pointer(std::tr1::shared_ptr const & p) { return p.get(); } //Make Boost.Python work with std::tr1::shared_ptr template inline T_ * get_pointer(std::tr1::shared_ptr const & p) { return const_cast(p.get()); } } namespace boost { namespace python { //Make Boost.Python work with std::tr1::shared_ptr<> template struct pointee > { typedef T_ type; }; //Make Boost.Python work with std::tr1::shared_ptr template struct pointee > { typedef T_ type; }; } } What is still not working is the implicit conversion of Foo and Derived for functions taking std::tr1::shared_ptr. With boost::shared_ptr it just works when you add the bp::bases to the Derived: func(boost::shared_ptr) ... bp::class_ ... bp::class_ > ... and func can take both Foo and Derived. But with std::tr1::shared_ptr I have to use: func(std::tr1::shared_ptr) ... bp::class_ > ... bp::class_, bp::bases > ... bp::implicitly_convertible, std::tr1::shared_ptr >(); How can I make it working just as with boost::shared_ptr? Btw. I have seen [2] already, but it doesn't seem to cover that. [1] - http://language-binding.net/pyplusplus/troubleshooting_guide/shared_ptr/shared_ptr.html [2] - http://language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html -- Best Regards, Piotr Jaroszynski From roman.yakovenko at gmail.com Fri Apr 20 18:53:32 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 20 Apr 2007 19:53:32 +0300 Subject: [C++-sig] std::tr1::shared_ptr as boost::shared_ptr In-Reply-To: <200704201410.14020.p.jaroszynski@gmail.com> References: <200704201410.14020.p.jaroszynski@gmail.com> Message-ID: <7465b6170704200953g562d72d7vacfcc7bb9e71fdd3@mail.gmail.com> On 4/20/07, Piotr Jaroszynski wrote: > bp::class_ > ... > bp::class_, bp::bases > ... > bp::implicitly_convertible, > std::tr1::shared_ptr >(); > > How can I make it working just as with boost::shared_ptr? There is no implicit conversion from a base class to the dervied. The other way will work: bp::implicitly_convertible, std::tr1::shared_ptr >(); > Btw. I have seen [2] already, but it doesn't seem to cover that. It does :-) > > [1] - > http://language-binding.net/pyplusplus/troubleshooting_guide/shared_ptr/shared_ptr.html > [2] - > http://language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html Glad you found this useful > -- > Best Regards, > Piotr Jaroszynski > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From p.jaroszynski at gmail.com Fri Apr 20 19:21:41 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Fri, 20 Apr 2007 19:21:41 +0200 Subject: [C++-sig] std::tr1::shared_ptr as boost::shared_ptr In-Reply-To: <7465b6170704200953g562d72d7vacfcc7bb9e71fdd3@mail.gmail.com> References: <200704201410.14020.p.jaroszynski@gmail.com> <7465b6170704200953g562d72d7vacfcc7bb9e71fdd3@mail.gmail.com> Message-ID: <200704201921.42275.p.jaroszynski@gmail.com> On Friday 20 of April 2007 18:53:32 Roman Yakovenko wrote: > There is no implicit conversion from a base class to the dervied. > The other way will work: > > bp::implicitly_convertible, > std::tr1::shared_ptr >(); Err I made a "typo", that's what I meant as a work around. What I asked about was whether it's possible to make it work exactly as with boost::shared_ptr, so: func(std::tr1::shared_ptr) ... bp::class_ ... bp::class_ > ... and func can take both Foo and Derived. I suppose it's a matter of making some "mirror" templates for std::tr1::shared_ptr so boost.python will treat it exactly as it treats boost::shared_ptr. -- Best Regards, Piotr Jaroszynski From roman.yakovenko at gmail.com Fri Apr 20 19:26:43 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 20 Apr 2007 20:26:43 +0300 Subject: [C++-sig] std::tr1::shared_ptr as boost::shared_ptr In-Reply-To: <200704201921.42275.p.jaroszynski@gmail.com> References: <200704201410.14020.p.jaroszynski@gmail.com> <7465b6170704200953g562d72d7vacfcc7bb9e71fdd3@mail.gmail.com> <200704201921.42275.p.jaroszynski@gmail.com> Message-ID: <7465b6170704201026o6ea7c183see85896905606961@mail.gmail.com> On 4/20/07, Piotr Jaroszynski wrote: > > On Friday 20 of April 2007 18:53:32 Roman Yakovenko wrote: > > There is no implicit conversion from a base class to the dervied. > > The other way will work: > > > > bp::implicitly_convertible, > > std::tr1::shared_ptr >(); > > Err I made a "typo", that's what I meant as a work around. What I asked > about > was whether it's possible to make it work exactly as with > boost::shared_ptr, > so: > func(std::tr1::shared_ptr) ... > bp::class_ ... > bp::class_ > ... > > and func can take both Foo and Derived. > > I suppose it's a matter of making some "mirror" templates for > std::tr1::shared_ptr so boost.python will treat it exactly as it treats > boost::shared_ptr. I am not sure. What you can do is to search Boost.Python library for shared_ptr usage. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From p.jaroszynski at gmail.com Sat Apr 21 13:10:25 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Sat, 21 Apr 2007 13:10:25 +0200 Subject: [C++-sig] std::tr1::shared_ptr as boost::shared_ptr In-Reply-To: <7465b6170704201026o6ea7c183see85896905606961@mail.gmail.com> References: <200704201410.14020.p.jaroszynski@gmail.com> <200704201921.42275.p.jaroszynski@gmail.com> <7465b6170704201026o6ea7c183see85896905606961@mail.gmail.com> Message-ID: <200704211310.25859.p.jaroszynski@gmail.com> On Friday 20 of April 2007 19:26:43 Roman Yakovenko wrote: > I am not sure. What you can do is to search Boost.Python library for > shared_ptr usage. If you are not sure then there is only one person left :] I will love your input David and tia! In the meantime I will indeed try searching the library... -- Best Regards, Piotr Jaroszynski From maillists at gauckler.ch Sat Apr 21 23:12:14 2007 From: maillists at gauckler.ch (Michael Gauckler) Date: Sat, 21 Apr 2007 23:12:14 +0200 Subject: [C++-sig] Multithreaded Python embedding Message-ID: <000c01c78459$bc87a330$3596e990$@ch> Dear List, I'd like to write a multi-threaded C++ program where each thread contains its own python interpreter. The code should act as a servant in a distributed environment and several (#threads) requests are handled in the (separate) interpreters running concurrently (on several CPUs). Is it possible to do this? What's the best way to do it? I read about the issues of the GIL, but as I understand it, this only applies to multi-threading within a single python interpreter instance. Thank you for hints, links and explanations. Regards, Michael Gauckler -------------- next part -------------- An HTML attachment was scrubbed... URL: From snemeth at houston.rr.com Sun Apr 22 21:09:35 2007 From: snemeth at houston.rr.com (Rocketman@JSC) Date: Sun, 22 Apr 2007 12:09:35 -0700 (PDT) Subject: [C++-sig] Difficult problem with GCCXML typedef of templated class Message-ID: <10128674.post@talk.nabble.com> I'm trying to Py++ wrap a templated class and seem to have a problem with GCCXML. I get the error "xml_find_template_parm encountered unsupported type non_lvalue_expr" and it seems to be instigated from a typedef statement "typedef Units< 0, 1, 0, 0 >::Units Length;". The full source code of the header file is in: http://enbridge.kundert.ca/units/units_t.h I know there may not be a fix for this but does anyone have a work around. TIA, Scott p.s. GCCXML is the latest built from a CVS checkout. -- View this message in context: http://www.nabble.com/Difficult-problem-with-GCCXML-typedef-of-templated-class-tf3627343.html#a10128674 Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Sun Apr 22 21:30:28 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 22 Apr 2007 22:30:28 +0300 Subject: [C++-sig] Difficult problem with GCCXML typedef of templated class In-Reply-To: <10128674.post@talk.nabble.com> References: <10128674.post@talk.nabble.com> Message-ID: <7465b6170704221230w26f13eb1vbd2a367bcee9515d@mail.gmail.com> On 4/22/07, Rocketman at JSC wrote: > > > I'm trying to Py++ wrap a templated class and seem to have a problem with > GCCXML. I get the error "xml_find_template_parm encountered unsupported > type non_lvalue_expr" and it seems to be instigated from a typedef > statement > "typedef Units< 0, 1, 0, 0 >::Units Length;". > > The full source code of the header file is in: > http://enbridge.kundert.ca/units/units_t.h > > I know there may not be a fix for this but does anyone have a work around. I believe that you found a bug in GCCXML, but I was not able to reproduce is. I built XML from your header file. May be you excluded too much? I attached the generated file. Can you report this bug to gccxml mailing list( http://www.gccxml.org/mailman/listinfo/gccxml)? All you need is to be able to reproduce the bug, than run gccxml with "-E" option. This will create preprocessed file, that you can send( attach ) to the bug report. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: units_t.h.xml.bz2 Type: application/x-bzip2 Size: 4400 bytes Desc: not available URL: From roman.yakovenko at gmail.com Sun Apr 22 21:32:14 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 22 Apr 2007 22:32:14 +0300 Subject: [C++-sig] Py++ - code freeze Message-ID: <7465b6170704221232j593502dbme65ca272a4ac5c04@mail.gmail.com> Hi all. I am going to freeze source code and to prepare it for another release. I believe it will take some time. The code itself in a good shape - it passes almost all unit tests and few external big projects. There were a lot of changes and I will try to document all of them. Recently I worked on performance. I believe Py++ is now 30-50% faster. The first improvement comes from pygccxml - standard XML SAX parser was replaced with cElementTree iterparse functionality. The old parser is still available and will be used if cElementTree is not available. Here you can find some information about cElementTree benchmarks: http://effbot.org/zone/celementtree.htm#benchmarks The second one comes from caching some intermediate result during code generation + creating a repository of md5 sum of generated file. Thus next time you generate code, Py++ can use the repository and compare md5 sums, instead of file content. You have to explicitly to enable this functionality: mb = module_builder_t( ... ) ... mb.split_module( ..., use_files_sum_repository=True ) I ask you to try SVN version of Py++ and report any problem with it. Thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From p.jaroszynski at gmail.com Sun Apr 22 22:31:47 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Sun, 22 Apr 2007 22:31:47 +0200 Subject: [C++-sig] Policies... Message-ID: <200704222231.47962.p.jaroszynski@gmail.com> Hello, I still have problems with choosing appropriate policies: class Foo { private: std::tr1::shared_ptr _blah; (...) public: std::tr1::shared_ptr get_blah() { return _blah; } }; What policy should I use for the get_blah function? TIA! -- Best Regards, Piotr Jaroszynski From roman.yakovenko at gmail.com Sun Apr 22 22:37:37 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 22 Apr 2007 23:37:37 +0300 Subject: [C++-sig] Policies... In-Reply-To: <200704222231.47962.p.jaroszynski@gmail.com> References: <200704222231.47962.p.jaroszynski@gmail.com> Message-ID: <7465b6170704221337p757f7df6sbc34d4f3732b67d3@mail.gmail.com> On 4/22/07, Piotr Jaroszynski wrote: > > Hello, > > I still have problems with choosing appropriate policies: > class Foo { > private: > std::tr1::shared_ptr _blah; > (...) > public: > std::tr1::shared_ptr get_blah() { > return _blah; > } > }; > > What policy should I use for the get_blah function? The default one. In this case you don't specify the call policy for the function. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From p.jaroszynski at gmail.com Sun Apr 22 22:50:53 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Sun, 22 Apr 2007 22:50:53 +0200 Subject: [C++-sig] Policies... In-Reply-To: <7465b6170704221337p757f7df6sbc34d4f3732b67d3@mail.gmail.com> References: <200704222231.47962.p.jaroszynski@gmail.com> <7465b6170704221337p757f7df6sbc34d4f3732b67d3@mail.gmail.com> Message-ID: <200704222250.53668.p.jaroszynski@gmail.com> On Sunday 22 of April 2007 22:37:37 Roman Yakovenko wrote: > The default one. In this case you don't specify the call policy for the > function. Thanks! I have another two cases tho :] 1) Would there be any difference if a raw pointer instead of shared_ptr was used for Blah? 2) What is the proper policy if the Blah object has a reference to Foo, so blah = Foo().get_blah() will result in dangling reference in blah as the object Foo will be destroyed after the function call? -- Best Regards, Piotr Jaroszynski From snemeth at houston.rr.com Mon Apr 23 06:14:49 2007 From: snemeth at houston.rr.com (Rocketman@JSC) Date: Sun, 22 Apr 2007 21:14:49 -0700 (PDT) Subject: [C++-sig] Difficult problem with GCCXML typedef of templated class In-Reply-To: <7465b6170704221230w26f13eb1vbd2a367bcee9515d@mail.gmail.com> References: <10128674.post@talk.nabble.com> <7465b6170704221230w26f13eb1vbd2a367bcee9515d@mail.gmail.com> Message-ID: <10133594.post@talk.nabble.com> Roman, By any chance did you set "#define CHECK_UNITS 1" in the header or command line? Apparently if CHECK_UNITS is not defined all the template code is bypassed. Scott Roman Yakovenko wrote: > > On 4/22/07, Rocketman at JSC wrote: >> >> >> I'm trying to Py++ wrap a templated class and seem to have a problem with >> GCCXML. I get the error "xml_find_template_parm encountered unsupported >> type non_lvalue_expr" and it seems to be instigated from a typedef >> statement >> "typedef Units< 0, 1, 0, 0 >::Units Length;". >> >> The full source code of the header file is in: >> http://enbridge.kundert.ca/units/units_t.h >> >> I know there may not be a fix for this but does anyone have a work >> around. > > > I believe that you found a bug in GCCXML, but I was not able to reproduce > is. I built XML from your header file. May be you excluded too much? I > attached the generated file. > > Can you report this bug to gccxml mailing list( > http://www.gccxml.org/mailman/listinfo/gccxml)? All you need is to be able > to reproduce the bug, than run gccxml with "-E" option. This will create > preprocessed file, that you can send( attach ) to the bug report. > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > -- View this message in context: http://www.nabble.com/Difficult-problem-with-GCCXML-typedef-of-templated-class-tf3627343.html#a10133594 Sent from the Python - c++-sig mailing list archive at Nabble.com. From s_sourceforge at nedprod.com Mon Apr 23 19:23:38 2007 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Mon, 23 Apr 2007 18:23:38 +0100 Subject: [C++-sig] Multithreaded Python embedding In-Reply-To: <000c01c78459$bc87a330$3596e990$@ch> References: <000c01c78459$bc87a330$3596e990$@ch> Message-ID: <462CF9AA.29526.293CC236@s_sourceforge.nedprod.com> On 21 Apr 2007 at 23:12, Michael Gauckler wrote: > I'd like to write a multi-threaded C++ program where each thread contains > its own python interpreter. > > The code should act as a servant in a distributed environment and several > (#threads) requests are handled in the (separate) interpreters running > concurrently (on several CPUs). > > Is it possible to do this? What's the best way to do it? I read about the > issues of the GIL, but as I understand it, this only applies to > multi-threading within a single python interpreter instance. Unless something substantial has changed in recent Python versions, there is still one GIL for the entire of Python. This reduces concurrency somewhat, but not fatally. Check this list's archives for how to patch Boost.Python to unlock the GIL while in C++ code. Cheers, Niall From Jared.McIntyre at medsimulation.com Mon Apr 23 19:31:47 2007 From: Jared.McIntyre at medsimulation.com (Jared McIntyre) Date: Mon, 23 Apr 2007 11:31:47 -0600 Subject: [C++-sig] Packaging up parameters Message-ID: I have a library that abstracts the details of running scripts from the users using boost::python. They simply define script call points, and the lib takes care of the rest. Optimally, I'd like the users not to have to include any python headers of libraries. I was wondering if there was a way to package up all the parameters in someway before passing them into a python function call. Here is an extreme over simplification of what I'm trying to do: boost::python::object PythonFunction; **** Publicly exposed header file ***** void CallFunction(Package &package); template void Trigger(T0 t0, T1 t1) { CallFunction(Package(t0,t1)); } **** Library specific cpp file **** void CallFunction(Package &package) { PythonFunction(package); } In this way users of the Trigger function don't have to know anything about the python library because those details are hidden inside the library. Is there any way to package them up this way? Otherwise I have to put them up in the header with the template, which I can get away with, but would rather not. Jared -------------- next part -------------- An HTML attachment was scrubbed... URL: From ext at sidvind.com Mon Apr 23 21:40:46 2007 From: ext at sidvind.com (David Sveningsson) Date: Mon, 23 Apr 2007 21:40:46 +0200 Subject: [C++-sig] Instantiate a wrapped class in C++ and get the PyObject pointer Message-ID: <462D0BBE.8070309@sidvind.com> Hi I have a class wrapping an STL map which is exposed to python using boost::python. The class can automatically be converted to and from a python dictionary. This works fine as long as the dictionary doesn't contain any class that I have exposed to python. For instance, I have a vector class exposed to python. But if I put a vector in the map and try to send it to python it fails because the vector cannot be converted to a PyObject*. The conversion works like this: 1) A to_python_converter is registered. 2) When a conversion is need the toPython method in the class is called, which returns a PyObject*. 3) My map class creates a new PyDict and iterates over the map calling toPython on each key value pair, and add them to the dictionary. 4) The PyObject* is given to python. I have some other classes like strings and numbers which all works as expected. My question is how to instantiate a wrapped class and get the PyObject pointer. Do I need to know the type of the class to convert it or could I write a general toPython method that would work on many classes? From seefeld at sympatico.ca Mon Apr 23 22:11:15 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 23 Apr 2007 16:11:15 -0400 Subject: [C++-sig] Instantiate a wrapped class in C++ and get the PyObject pointer In-Reply-To: <462D0BBE.8070309@sidvind.com> References: <462D0BBE.8070309@sidvind.com> Message-ID: <462D12E3.9080308@sympatico.ca> David Sveningsson wrote: > My question is how to instantiate a wrapped class and get the PyObject > pointer. Do I need to know the type of the class to convert it or could > I write a general toPython method that would work on many classes? class_ a_type(...); // Define the mapping (and implied conversion operator) // ... object a_instance = A(...); // Create an A instance and wrap it in a python object PyObject *ptr = a_instance.ptr(); // access a C API pointer from the instance Is that all you want ? HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From ext at sidvind.com Mon Apr 23 22:42:42 2007 From: ext at sidvind.com (David Sveningsson) Date: Mon, 23 Apr 2007 22:42:42 +0200 (CEST) Subject: [C++-sig] Instantiate a wrapped class in C++ and get the PyObject pointer In-Reply-To: <462D12E3.9080308@sympatico.ca> References: <462D0BBE.8070309@sidvind.com> <462D12E3.9080308@sympatico.ca> Message-ID: <35900.83.209.20.148.1177360962.squirrel@mail.sidvind.com> > > object a_instance = A(...); // Create an A instance and wrap it in a > python object > > PyObject *ptr = a_instance.ptr(); // access a C API pointer from the > instance > But if I return a_instance.ptr() the object will be corrupt right? But if I allocate on the heap it will leak memory? From seefeld at sympatico.ca Mon Apr 23 22:47:45 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 23 Apr 2007 16:47:45 -0400 Subject: [C++-sig] Instantiate a wrapped class in C++ and get the PyObject pointer In-Reply-To: <35900.83.209.20.148.1177360962.squirrel@mail.sidvind.com> References: <462D0BBE.8070309@sidvind.com> <462D12E3.9080308@sympatico.ca> <35900.83.209.20.148.1177360962.squirrel@mail.sidvind.com> Message-ID: <462D1B71.8090706@sympatico.ca> David Sveningsson wrote: >> object a_instance = A(...); // Create an A instance and wrap it in a >> python object >> >> PyObject *ptr = a_instance.ptr(); // access a C API pointer from the >> instance >> > > But if I return a_instance.ptr() the object will be corrupt right? But if > I allocate on the heap it will leak memory? Can you rephrase the question, please ? The underlaying pointer is owned by the boost::python::object instance, no matter how often you access it via the ptr() member function. Thus, if you want to hand a reference to it to someone else, you have to make sure the reference counter is appropriately incremented (Read boost.python's reference guide for more on the topic.) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From ext at sidvind.com Mon Apr 23 23:22:36 2007 From: ext at sidvind.com (David Sveningsson) Date: Mon, 23 Apr 2007 23:22:36 +0200 (CEST) Subject: [C++-sig] Instantiate a wrapped class in C++ and get the PyObject pointer In-Reply-To: <462D1B71.8090706@sympatico.ca> References: <462D0BBE.8070309@sidvind.com> <462D12E3.9080308@sympatico.ca> <35900.83.209.20.148.1177360962.squirrel@mail.sidvind.com> <462D1B71.8090706@sympatico.ca> Message-ID: <35253.83.209.20.148.1177363356.squirrel@mail.sidvind.com> > > Can you rephrase the question, please ? The underlaying pointer is owned > by > the boost::python::object instance, no matter how often you access it via > the ptr() member function. Thus, if you want to hand a reference to it to > someone else, you have to make sure the reference counter is appropriately > incremented (Read boost.python's reference guide for more on the topic.) > PyObject* foo(){ boost::python::object bar = Fred(1,2,3); // <-- Stack allocated return bar.ptr(); } Since bar is allocated on the stack it will be destroyed once we return from bar. Won't the pointer returned by ptr() only point to garbage after returning? PyObject* foo(){ boost::python::object bar = new boost::python::object(Fred(1,2,3)); return bar->ptr(); } This time bar is allocated on the heap, so the pointer would still be valid after returning from bar. But it will leak memory. From seefeld at sympatico.ca Mon Apr 23 23:29:39 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 23 Apr 2007 17:29:39 -0400 Subject: [C++-sig] Instantiate a wrapped class in C++ and get the PyObject pointer In-Reply-To: <35253.83.209.20.148.1177363356.squirrel@mail.sidvind.com> References: <462D0BBE.8070309@sidvind.com> <462D12E3.9080308@sympatico.ca> <35900.83.209.20.148.1177360962.squirrel@mail.sidvind.com> <462D1B71.8090706@sympatico.ca> <35253.83.209.20.148.1177363356.squirrel@mail.sidvind.com> Message-ID: <462D2543.3070505@sympatico.ca> David Sveningsson wrote: > PyObject* foo(){ > boost::python::object bar = Fred(1,2,3); // <-- Stack allocated > return bar.ptr(); > } > > Since bar is allocated on the stack it will be destroyed once we return > from bar. Won't the pointer returned by ptr() only point to garbage after > returning? python's objects are all heap-allocated (and ref-counted). The above boost::python::object is only a tiny wrapper around a PyObject pointer, conveniently handling the ref counting for you. I'd think the construction itself thus already implies a copy construction to move the Fred instance into the heap. You are right that simply returning bar.ptr() like this is wrong, due to python potentially (or even likely) deleting the underlaying object, as soon as bar's destructor decreases the ref counter at the end-of-scope. Thus, you need to increment the counter before returning. However, the whole point of boost.python is to discourage you to use PyObject pointers directly (given how error-prone a business this is). > PyObject* foo(){ > boost::python::object bar = new boost::python::object(Fred(1,2,3)); > return bar->ptr(); > } > > This time bar is allocated on the heap, so the pointer would still be > valid after returning from bar. But it will leak memory. Right. Don't do that. boost::python::object instances should be allocated on the stack, and always be passed by-value. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From ext at sidvind.com Tue Apr 24 00:00:21 2007 From: ext at sidvind.com (David Sveningsson) Date: Tue, 24 Apr 2007 00:00:21 +0200 (CEST) Subject: [C++-sig] Instantiate a wrapped class in C++ and get the PyObject pointer In-Reply-To: <462D2543.3070505@sympatico.ca> References: <462D0BBE.8070309@sidvind.com> <462D12E3.9080308@sympatico.ca> <35900.83.209.20.148.1177360962.squirrel@mail.sidvind.com> <462D1B71.8090706@sympatico.ca> <35253.83.209.20.148.1177363356.squirrel@mail.sidvind.com> <462D2543.3070505@sympatico.ca> Message-ID: <51666.83.209.20.148.1177365621.squirrel@mail.sidvind.com> > > python's objects are all heap-allocated (and ref-counted). The above > boost::python::object is only a tiny wrapper around a PyObject pointer, > conveniently handling the ref counting for you. > I'd think the construction itself thus already implies a copy construction > to move the Fred instance into the heap. > You are right that simply returning bar.ptr() like this is wrong, due to > python potentially (or even likely) deleting the underlaying object, as > soon > as bar's destructor decreases the ref counter at the end-of-scope. > Thus, you need to increment the counter before returning. Ok, so I just increase the ref-count before returning and I'm all safe. > However, the > whole point of boost.python is to discourage you to use PyObject pointers > directly (given how error-prone a business this is). Yeah, but I need to fill a PyDict with keys and values. (And decrease the ref-count after adding to the PyDict, as the dictionary should hold the last reference) I think I understand how this works now (the code seems to work too), thanks! From fdelizy at unfreeze.net Thu Apr 26 07:50:08 2007 From: fdelizy at unfreeze.net (DELIZY Florian) Date: Thu, 26 Apr 2007 07:50:08 +0200 Subject: [C++-sig] boost.python class inheritance Message-ID: <46303D90.8090006@unfreeze.net> Hi, I am using boost.python and wxPython together and I am experiencing some troubles getting both interacting. wxPython (www.wxpython.org) is a SWIG wrapped GUI C++ extension for python. My problem is I want to expose a class using boost.python class_ template to python but this class is derived from a wx C++ class, to be more precise, say I have this code : class MyFrame : public wxFrame { public: void SomeOperations( ... ); ... }; the class wxFrame is a C++ wx class already exposed to python using SWIG as wx.wxFrame, how do I expose the class MyFrame, informing python that MyFrame is derived from wxFrame using boost.python ? is there a way to do such things ? Regards, Florian Delizy From roman.yakovenko at gmail.com Thu Apr 26 08:13:13 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 26 Apr 2007 09:13:13 +0300 Subject: [C++-sig] boost.python class inheritance In-Reply-To: <46303D90.8090006@unfreeze.net> References: <46303D90.8090006@unfreeze.net> Message-ID: <7465b6170704252313l5621b261x18092c96754470e1@mail.gmail.com> On 4/26/07, DELIZY Florian wrote: > Hi, > > I am using boost.python and wxPython together and I am experiencing some > troubles getting both interacting. > wxPython (www.wxpython.org) is a SWIG wrapped GUI C++ extension for python. > > My problem is I want to expose a class using boost.python class_ > template to python but this class is derived from a wx C++ class, to be > more precise, say I have this code : > > > class MyFrame : public wxFrame > { > > public: > void SomeOperations( ... ); > ... > }; > > the class wxFrame is a C++ wx class already exposed to python using SWIG > as wx.wxFrame, how do I expose the class MyFrame, informing python that > MyFrame is derived from wxFrame using boost.python ? > > is there a way to do such things ? Basically what you want is to expose your class and say that one of its base classes is defined in Python. Right now, Boost.Python does not provide this functionality. In some cases it is possible to create work-around to the problem, but I am not sure about your use-case. Take a look on next link http://www.language-binding.net/pyplusplus/troubleshooting_guide/exceptions/exceptions.html- it contains solution to similar problem. P.S. Python-Ogre project, exposed using Boost.Python, makes an extensive use of wxPython. May be they have some knowledge or "ready" solution for you. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From florian.delizy at unfreeze.net Thu Apr 26 10:34:17 2007 From: florian.delizy at unfreeze.net (Florian Delizy) Date: Thu, 26 Apr 2007 10:34:17 +0200 Subject: [C++-sig] boost.python class inheritance In-Reply-To: <7465b6170704252313l5621b261x18092c96754470e1@mail.gmail.com> References: <46303D90.8090006@unfreeze.net> <7465b6170704252313l5621b261x18092c96754470e1@mail.gmail.com> Message-ID: <46306409.5060904@unfreeze.net> Roman Yakovenko wrote: > On 4/26/07, DELIZY Florian wrote: >> Hi, >> >> I am using boost.python and wxPython together and I am experiencing some >> troubles getting both interacting. >> wxPython (www.wxpython.org) is a SWIG wrapped GUI C++ extension for > python. >> >> My problem is I want to expose a class using boost.python class_ >> template to python but this class is derived from a wx C++ class, to be >> more precise, say I have this code : >> >> >> class MyFrame : public wxFrame >> { >> >> public: >> void SomeOperations( ... ); >> ... >> }; >> >> the class wxFrame is a C++ wx class already exposed to python using SWIG >> as wx.wxFrame, how do I expose the class MyFrame, informing python that >> MyFrame is derived from wxFrame using boost.python ? >> >> is there a way to do such things ? > > Basically what you want is to expose your class and say that one of > its base > classes is defined in Python. well, not exactly, I want to expose a C++ class using python, given the fact that this C++ class is derived (in C++) from another C++ class itself exposed in python using SWIG... so basically I want to export this existing C++ inheritance link into python, but I don't want to rewrite all wxPython using boost.python (quite an intensive job I think). How does boost.python create this inheritance link (internally) ? I was thinking of something like retrieving a class descriptor from python/swig/wxPython and feeding it to boost.python class_ template ... (ok I could read the whole boost.python code for that ... but I don't know yet what to loof for ...) > Right now, Boost.Python does not provide this > functionality. In some cases it is possible to create work-around to the > problem, but I am not sure about your use-case. I understand Python -> C++ inheritance is something tricky, but I am speaking about symetric C++->C++/Python->Python inheritance ... I thought it would work that way, would it ? > Take a look on next link > http://www.language-binding.net/pyplusplus/troubleshooting_guide/exceptions/exceptions.html- > > it contains solution to similar problem. I read it thanks, but that method does not apply > > P.S. Python-Ogre project, exposed using Boost.Python, makes an > extensive use > of wxPython. May be they have some knowledge or "ready" solution for you. all I found about it is wxPython + wxOgre usage (all together), but no integration between the two libs ... From luca.sbardella at gmail.com Thu Apr 26 12:35:03 2007 From: luca.sbardella at gmail.com (Luca Sbardella) Date: Thu, 26 Apr 2007 12:35:03 +0200 Subject: [C++-sig] pure virtuals and properties Message-ID: <8315652a0704260335t653bf836tb71c5471860d0e44@mail.gmail.com> Hi, just a quick question on the use o "wrapper.hpp". I have the following class and its correspondent wrapper class class blah { public: virtual double ciao() const = 0; }; class blah_wrapper : public blah, public boost::python::wrapper { public: double ciao() const { return boost::python::call(this->get_override("ciao").ptr()); } }; I would like to expose the "ciao" method as a property class_("blah") .add_property("ciao", pure_virtual(&blah::ciao)) ; but it doesn't compile... lots of template errors (by the way is there a way to use "textfilt" http://textfilt.sourceforge.net/ in visual studio?) This, on the other hand, does compile as expected class_("blah") .def("ciao", pure_virtual(&blah::ciao)) ; I guess we cannot expose e pure virtual function as property, or is there something I'm missing? Thanks Luca -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Thu Apr 26 14:55:40 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 26 Apr 2007 15:55:40 +0300 Subject: [C++-sig] boost.python class inheritance In-Reply-To: <46306409.5060904@unfreeze.net> References: <46303D90.8090006@unfreeze.net> <7465b6170704252313l5621b261x18092c96754470e1@mail.gmail.com> <46306409.5060904@unfreeze.net> Message-ID: <7465b6170704260555g4192f770w365b52e8d4d5aa72@mail.gmail.com> On 4/26/07, Florian Delizy wrote: > > Basically what you want is to expose your class and say that one of > > its base > > classes is defined in Python. > well, not exactly, I want to expose a C++ class using python, given the > fact that this C++ class is derived (in C++) from another C++ class > itself exposed in python using SWIG... Boost.Python requires base class to be exposed before derived. Because you don't want to expose wxFrame ( I perfectly understand why ) the only choice left is to derive from class defined in Python. > so basically I want to export this existing C++ inheritance link into > python, but I don't want to rewrite all wxPython using boost.python > (quite an intensive job I think). Py++ could make it to be a pleasure :-) > How does boost.python create this inheritance link (internally) ? I was > thinking of something like retrieving a class descriptor from > python/swig/wxPython and feeding it to boost.python class_ template ... > (ok I could read the whole boost.python code for that ... but I don't > know yet what to loof for ...) Me too. P.S. May be you should consider to use SWIG to expose the class. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From florian.delizy at unfreeze.net Thu Apr 26 16:27:37 2007 From: florian.delizy at unfreeze.net (Florian Delizy) Date: Thu, 26 Apr 2007 16:27:37 +0200 Subject: [C++-sig] boost.python class inheritance In-Reply-To: <7465b6170704260555g4192f770w365b52e8d4d5aa72@mail.gmail.com> References: <46303D90.8090006@unfreeze.net> <7465b6170704252313l5621b261x18092c96754470e1@mail.gmail.com> <46306409.5060904@unfreeze.net> <7465b6170704260555g4192f770w365b52e8d4d5aa72@mail.gmail.com> Message-ID: <4630B6D9.5010407@unfreeze.net> > Boost.Python requires base class to be exposed before derived. Because > you > don't want to expose wxFrame ( I perfectly understand why ) the only > choice > left is to derive from class defined in Python. OK I see, there is still an ugly solution: having a method return the wxObject version of the object, I think that's the only easy way for now. > P.S. May be you should consider to use SWIG to expose the class. If you ask me, SWIG binding really looks ugly, it even look worse than the conversion method ... I'll do that so Thanks, Florian From ndbecker2 at gmail.com Thu Apr 26 16:33:22 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 26 Apr 2007 10:33:22 -0400 Subject: [C++-sig] expose class that does it's own ref counting Message-ID: I'm interested to play with exposing blitz++ arrays. They do there own ref counting. Do I need to do anything special? I'm thinking that I can just ignore this fact, and nothing bad will happen (except maybe some inefficiency). Is this correct? From mitch at niftyneato.com Thu Apr 26 19:21:20 2007 From: mitch at niftyneato.com (Mitch Jones) Date: Thu, 26 Apr 2007 10:21:20 -0700 Subject: [C++-sig] Problem exposing container iterator with boost::iterator_adaptor Message-ID: I've been experimenting with the boost::python library and slowly getting the hang of it, although I've run into a problem I don't quite understand. I have a container class that provides begin and end functions that are implemented with the help of boost::iterator_adaptor. The iterator is forward only. This class and the thing it contains is then exported to python via boost::python. A snippet of the definitions I have: class_ >("BaseWidget", no_init) .def("__repr__", &Widget::name); class_("BigWidget", no_init); class_(" LittleWidget", no_init); class_ >("Widgets", no_init) .def(vector_indexing_suite, true>()); class_, noncopyable >(" WidgetContainer", no_init) .def("__iter__", range(&Container::begin, &Container::end) //the iterator returns shared_ptr .def("contents", &WidgetContainer::contents); //returns vector When used in python, I get a "pure virtual method called" error followed by a terminate/abort when I try to iterate over WidgetContainer. widgets = container_factory() # factory that supplies a WidgetContainer for some_widget in widgets : print some_widget If I ignore iteration and instead return a list, I have no problem: for some_widget in widgets.contents(): print some_widget Is there some more scaffolding I still need to define to support iteration? It's not really clear to me what the error really means. The Widget base class has no virtual functions other than it's destructor. Is it referring to the iterator_adaptor? Does the iterator_adaptor class need to be exposed to python? Is the iterator_adaptor a red herring and the real issue in Widget and its descendants and I've missed something obvious? -- Mitch Jones mitch at niftyneato.com From oanjao at yahoo.com Thu Apr 26 23:27:05 2007 From: oanjao at yahoo.com (Craig Finch) Date: Thu, 26 Apr 2007 14:27:05 -0700 (PDT) Subject: [C++-sig] Need strategic advice designing Boost/Python program Message-ID: <42831.62164.qm@web30315.mail.mud.yahoo.com> As a fairly new boost.python user, I would like some expert advice on which strategy to use in using Boost and Python, so that I don't go down a dead end. A clean interface is more important to me than efficiency. I am writing a numerical solver. I want most of the code to be in Python for ease of development. However, I want to implement some objects in C++ for speed, because they use iterative solution methods. I envision my Python code as creating a mesh on a domain, calling a C++ solver, and performing post-processing such as plotting. The cleanest way to do this seems to be to create a complex "mesh" object in Python, and then pass all the mesh data to C++ at once. However, I want to implement the methods for creating and modifying the mesh in Python, because flexibility is more important than speed. After reading a lot, there seem to be three main approaches to doing this. I need help choosing between: 1. Create the data portions of the mesh class in C++ and use Boost to expose it to Python as a module. Then use Python to add methods, as shown in the Tutorial, and pass the whole object back to C++. Finally, use to get C++ values for computation. One downside to this approach is that apparently Python can't be used to implement a constructor method for a wrapped C++ class. 2. Similar to #1, but instead create Python classes that inherit from the C++ base classes, and then add Python methods. Not sure how to get the resulting objects back into C++. 3. Create the data and methods in Python, and then define custom lvalue converters to get the data into C++. This seems messy because I have to use the classic Python/C API in the converter functions, which seems to kind of defeat the purpose of using Boost. Your input is greatly appreciated! If I have misunderstood any thing, please let me know. The resulting code will be made available as an example to others. Craig Finch -------------- Please reply to cfinch at ieee.org __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From seefeld at sympatico.ca Thu Apr 26 23:54:46 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 26 Apr 2007 17:54:46 -0400 Subject: [C++-sig] Need strategic advice designing Boost/Python program In-Reply-To: <42831.62164.qm@web30315.mail.mud.yahoo.com> References: <42831.62164.qm@web30315.mail.mud.yahoo.com> Message-ID: <46311FA6.5050003@sympatico.ca> Craig Finch wrote: > As a fairly new boost.python user, I would like some expert advice on > which strategy to use in using Boost and Python, so that I don't go > down a dead end. A clean interface is more important to me than > efficiency. (Depending on what 'clean' means to you, it may not actually be in conflict with efficient at all.) > I am writing a numerical solver. I want most of the code to be in > Python for ease of development. However, I want to implement some > objects in C++ for speed, because they use iterative solution methods. > I envision my Python code as creating a mesh on a domain, calling a C++ > solver, and performing post-processing such as plotting. The cleanest > way to do this seems to be to create a complex "mesh" object in Python, > and then pass all the mesh data to C++ at once. However, I want to > implement the methods for creating and modifying the mesh in Python, > because flexibility is more important than speed. After reading a lot, > there seem to be three main approaches to doing this. I need help > choosing between: > > 1. Create the data portions of the mesh class in C++ and use Boost to > expose it to Python as a module. Then use Python to add methods, as > shown in the Tutorial, and pass the whole object back to C++. Finally, > use to get C++ values for computation. One downside to this > approach is that apparently Python can't be used to implement a > constructor method for a wrapped C++ class. I'm not sure what you mean by 'implement a constructor'. Can you elaborate ? As a minor point, if you go this route, there is no need to use extract<> to access values, as within C++, you'll always deal with the (native) C++ interface directly. extract<> is useful to pull out embedded C++ objects from python (wrapper) objects, or to require an explicit conversion / cast. > 2. Similar to #1, but instead create Python classes that inherit from > the C++ base classes, and then add Python methods. Not sure how to get > the resulting objects back into C++. If you add new methods, or, more generally, attributes, you'll have to access them through the boost.python 'attr()' method that is part of the object protocol. However, if these attributes are only added for convenience (i.e. used within python, but not necessary for the C++ solvers), there isn't any need to access them from within C++, right ? > 3. Create the data and methods in Python, and then define custom lvalue > converters to get the data into C++. This seems messy because I have > to use the classic Python/C API in the converter functions, which seems > to kind of defeat the purpose of using Boost. What would worry me more is that each time you cross the language boundary you have to copy the data, which hurts performance. I understand that efficiency isn't of great concern to you (right now), but there is no reason to require that extra copy. I believe solutions 1 or 2 are in fact superior. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From rwgk at yahoo.com Fri Apr 27 05:01:32 2007 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Thu, 26 Apr 2007 20:01:32 -0700 (PDT) Subject: [C++-sig] Need strategic advice designing Boost/Python program Message-ID: <629114.174.qm@web31102.mail.mud.yahoo.com> > As a fairly new boost.python user, I would like some expert advice on > which strategy to use in using Boost and Python, so that I don't go > down a dead end. A clean interface is more important to me than > efficiency. I think you can have both. Only the implementations tendy to become dirty if you go for speed. > I am writing a numerical solver. I want most of the code to be in > Python for ease of development. However, I want to implement some > objects in C++ for speed, because they use iterative solution methods. This is exactly our situation. You may want to look at the "scitbx", which is a library for a bigger project (cctbx.sf.net). It is built from day one on top of Boost.Python. There is a little bit of an introduction here: http://cci.lbl.gov/~hohn/scitbx-tour.html The backbone of our system is the array family, with the most important types being reference-counted C++ arrays (af::shared, af::versa): http://cci.lbl.gov/~hohn/array-family-tour.html The pages are a bit rough and slightly out of date. You can get standalone scitbx bundles here: http://cci.lbl.gov/scitbx_bundles/ They come with and without Python included and have no external dependencies (other than an OS and a C++ compiler). Ask questions here or at: http://www.phenix-online.org/mailman/listinfo/cctbxbb Ralf __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From bruce.who.hk at gmail.com Fri Apr 27 07:38:23 2007 From: bruce.who.hk at gmail.com (Bruce Who) Date: Fri, 27 Apr 2007 13:38:23 +0800 Subject: [C++-sig] How to wrap a existing DLL to a python module? Message-ID: Hi, all I have a DLL and related .lib and .h files provided by 3rd party, and I want to make a python extension module from this dll I tried swig but failed. At first , I just used this command: swig -python -module TEVLib TEVLib.h I got a TEVLib.py file, but it did not work of course. So I tried another method: I wrote TEV.c and TEV.i, this TEV.c just wrappered all functions from TEVLib and included all necessary .h files and also included this statement #pragma comment(lib, "TEVLib.lib") Then I compiled all of these and got TEVLib.py and _TEVLib.pyd, but when I tried to import it, error occured, it's said something like that the DLL could not be imported. I know that we can use ctypes to call functions in dll, but I just get bored with defining function prototypes with CFUNCTYPE. That's why I choose swig. As a newbie, I just do not know why it doesnot work. Could somebody help me out?Thanks in advance. Bruce From ext at sidvind.com Fri Apr 27 10:35:21 2007 From: ext at sidvind.com (David Sveningsson) Date: Fri, 27 Apr 2007 10:35:21 +0200 Subject: [C++-sig] expose class that does it's own ref counting In-Reply-To: References: Message-ID: <4631B5C9.8030103@sidvind.com> Neal Becker skrev: > I'm interested to play with exposing blitz++ arrays. They do there own ref > counting. Do I need to do anything special? I'm thinking that I can just > ignore this fact, and nothing bad will happen (except maybe some > inefficiency). Is this correct? > Try using boost::intrusive_ptr as container for the classes. Worked for me when I did something similar. boost::intrusive_ptr requires you to write two additional functions, intrusive_ptr_add_ref and intrusive_ptr_release which takes a pointer as parameter should increases and decrease the refcount. -- //*David Sveningsson [eXt]* Freelance coder | Game Development Student http://sidvind.com Thou shalt make thy program's purpose and structure clear to thy fellow man by using the One True Brace Style, even if thou likest it not, for thy creativity is better used in solving problems than in creating beautiful new impediments to understanding. From ndbecker2 at gmail.com Fri Apr 27 11:35:02 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Fri, 27 Apr 2007 05:35:02 -0400 Subject: [C++-sig] expose class that does it's own ref counting References: <4631B5C9.8030103@sidvind.com> Message-ID: David Sveningsson wrote: > Neal Becker skrev: >> I'm interested to play with exposing blitz++ arrays. They do there own >> ref >> counting. Do I need to do anything special? I'm thinking that I can >> just ignore this fact, and nothing bad will happen (except maybe some >> inefficiency). Is this correct? >> > Try using boost::intrusive_ptr as container for the classes. Worked for > me when I did something similar. boost::intrusive_ptr requires you to > write two additional functions, intrusive_ptr_add_ref and > intrusive_ptr_release which takes a pointer as parameter should > increases and decrease the refcount. > Thanks. That sounds like good advice. I'm still wondering, though, what happens if I don't do anything (just expose the object as is). I'm thinking that it won't break anything, just result in some inefficiency. From s.ramacher at gmx.at Fri Apr 27 13:24:22 2007 From: s.ramacher at gmx.at (Sebastian Ramacher) Date: Fri, 27 Apr 2007 13:24:22 +0200 Subject: [C++-sig] [Boost.Python] inconsistent use of function pointer typedef Message-ID: I just wanted to repost a patch I've submitted to the SourceForge tracker some days ago. The patch fixes the inconsistent use of the function pointer typedef 'convertible_function' at the declaration and definition of 'boost::python::converter::registry::insert' and 'boost::python::converter::registry::push_back'. The link to the SoruceForge tracker item is: https://sourceforge.net/tracker/?func=detail&atid=307586&aid=1707377&group_id=7586 Regards, Sebastian Ramacher -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: python_registry.patch URL: From robert.stewart at sig.com Fri Apr 27 15:55:57 2007 From: robert.stewart at sig.com (Rob Stewart) Date: Fri, 27 Apr 2007 09:55:57 -0400 Subject: [C++-sig] [Swig-user] How to wrap a existing DLL to a python module? In-Reply-To: (bruce.who.hk@gmail.com) References: Message-ID: <200704271355.l3RDtvqP027211@vanzandt.balstatdev.susq.com> From: "Bruce Who" > > I have a DLL and related .lib and .h files provided by 3rd party, and > I want to make a python extension module from this dll I tried swig > but failed. [snip] > Then I compiled all of these and got TEVLib.py and _TEVLib.pyd, but > when I tried to import it, error occured, it's said something like > that the DLL could not be imported. Where is the DLL? Is it on your path? -- Rob Stewart robert.stewart at sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer; From roman.yakovenko at gmail.com Fri Apr 27 20:36:50 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 27 Apr 2007 21:36:50 +0300 Subject: [C++-sig] pure virtuals and properties In-Reply-To: <8315652a0704260335t653bf836tb71c5471860d0e44@mail.gmail.com> References: <8315652a0704260335t653bf836tb71c5471860d0e44@mail.gmail.com> Message-ID: <7465b6170704271136u7be4f62eg73a19412e7510233@mail.gmail.com> On 4/26/07, Luca Sbardella wrote: > Hi, just a quick question on the use o "wrapper.hpp". > I have the following class and its correspondent wrapper class > > class blah > { > public: > virtual double ciao() const = 0; > }; > > class blah_wrapper : public blah, public boost::python::wrapper > { > public: > double ciao() const { return > boost::python::call(this->get_override("ciao").ptr()); > } > }; > > I would like to expose the "ciao" method as a property > > class_("blah") > .add_property("ciao", pure_virtual(&blah::ciao)) > ; > > but it doesn't compile... lots of template errors (by the way is there a way > to use "textfilt" http://textfilt.sourceforge.net/ in > visual studio?) > This, on the other hand, does compile as expected > > class_("blah") > .def("ciao", pure_virtual(&blah::ciao)) > ; > > I guess we cannot expose e pure virtual function as property, or is there > something I'm missing? I could be wrong, but it looks to me like a mistake to make pure virtual function to behave like a property. May be you can do something with "make_function", but this will surprise the users. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Fri Apr 27 20:47:11 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 27 Apr 2007 21:47:11 +0300 Subject: [C++-sig] Problem exposing container iterator with boost::iterator_adaptor In-Reply-To: References: Message-ID: <7465b6170704271147m4c3a1crb5530c0713eece7c@mail.gmail.com> On 4/26/07, Mitch Jones wrote: > I've been experimenting with the boost::python library and slowly getting > the hang of it, although I've run into a problem I don't quite understand. > > I have a container class that provides begin and end functions that are > implemented with the help of boost::iterator_adaptor. The iterator is > forward only. > > This class and the thing it contains is then exported to python via > boost::python. > > A snippet of the definitions I have: > > class_ >("BaseWidget", no_init) > .def("__repr__", &Widget::name); > class_("BigWidget", no_init); > class_(" LittleWidget", no_init); > > class_ >("Widgets", no_init) > .def(vector_indexing_suite, true>()); > > class_, noncopyable >(" > WidgetContainer", no_init) > .def("__iter__", range(&Container::begin, &Container::end) //the iterator > returns shared_ptr > .def("contents", &WidgetContainer::contents); //returns vector > > When used in python, I get a "pure virtual method called" error followed by > a terminate/abort when I try to iterate over WidgetContainer. > > widgets = container_factory() # factory that supplies a WidgetContainer > > for some_widget in widgets : > print some_widget > > If I ignore iteration and instead return a list, I have no problem: > > for some_widget in widgets.contents(): > print some_widget > > Is there some more scaffolding I still need to define to support iteration? > It's not really clear to me what the error really means. The Widget base > class has no virtual functions other than it's destructor. Is it referring > to the iterator_adaptor? Does the iterator_adaptor class need to be exposed > to python? Is the iterator_adaptor a red herring and the real issue in > Widget and its descendants and I've missed something obvious? Your code looks correct, can you create small example, which reproduce the problem? Also what happens if you replace .def("__iter__", range(&Container::begin, &Container::end) with .def("__iter__", range(&WidgetContainer::begin, &WidgetContainer::end) ? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From bruce.who.hk at gmail.com Sat Apr 28 02:46:56 2007 From: bruce.who.hk at gmail.com (Bruce Who) Date: Sat, 28 Apr 2007 08:46:56 +0800 Subject: [C++-sig] Fwd: [Swig-user] How to wrap a existing DLL to a python module? In-Reply-To: References: <200704271355.l3RDtvqP027211@vanzandt.balstatdev.susq.com> Message-ID: ---------- Forwarded message ---------- From: Bruce Who Date: Apr 28, 2007 8:46 AM Subject: Re: [Swig-user] How to wrap a existing DLL to a python module? To: Rob Stewart OMG, just call me dumb, I put the DLL to the same directory and it works fine! Thanks! On 4/27/07, Rob Stewart wrote: > From: "Bruce Who" > > > > I have a DLL and related .lib and .h files provided by 3rd party, and > > I want to make a python extension module from this dll I tried swig > > but failed. > [snip] > > Then I compiled all of these and got TEVLib.py and _TEVLib.pyd, but > > when I tried to import it, error occured, it's said something like > > that the DLL could not be imported. > > Where is the DLL? Is it on your path? > > -- > Rob Stewart robert.stewart at sig.com > Software Engineer http://www.sig.com > Susquehanna International Group, LLP using std::disclaimer; > Bruce From bruce.who.hk at gmail.com Sat Apr 28 03:39:47 2007 From: bruce.who.hk at gmail.com (Bruce Who) Date: Sat, 28 Apr 2007 09:39:47 +0800 Subject: [C++-sig] How to wrap a existing DLL to a python module? In-Reply-To: References: Message-ID: Hi, Another question, what i do now is wrap all functions in DLL again like this: // this is header file for DLL void func(void) { ... } // this is my .cpp file for swig #pragma comment(lib, "TEVlib.lib") void _func(void) { func() // this is the func of DLL } // this is .i file %inline{ extern void _func(void); %} I just wonder if it is possible that we can generate a .py file directly from the header file of DLL with all functions exposed? I think that can save a lot of time. I have tried without success. On 4/27/07, Bruce Who wrote: > Hi, all > > I have a DLL and related .lib and .h files provided by 3rd party, and > I want to make a python extension module from this dll I tried swig > but failed. > > At first , I just used this command: > > swig -python -module TEVLib TEVLib.h > > I got a TEVLib.py file, but it did not work of course. So I tried > another method: I wrote TEV.c and TEV.i, this TEV.c just wrappered all > functions from TEVLib and included all necessary .h files and also > included this statement > > #pragma comment(lib, "TEVLib.lib") > > Then I compiled all of these and got TEVLib.py and _TEVLib.pyd, but > when I tried to import it, error occured, it's said something like > that the DLL could not be imported. > > I know that we can use ctypes to call functions in dll, but I just get > bored with defining function prototypes with CFUNCTYPE. That's why I > choose swig. > > As a newbie, I just do not know why it doesnot work. Could somebody > help me out?Thanks in advance. > Bruce From wsf at fultondesigns.co.uk Sun Apr 29 10:09:47 2007 From: wsf at fultondesigns.co.uk (William S Fulton) Date: Sun, 29 Apr 2007 09:09:47 +0100 Subject: [C++-sig] [Swig-user] How to wrap a existing DLL to a python module? In-Reply-To: References: Message-ID: <463452CB.5030205@fultondesigns.co.uk> Bruce Who wrote: > Hi, > > Another question, what i do now is wrap all functions in DLL again like this: > > // this is header file for DLL > void func(void) > { > ... > } > > // this is my .cpp file for swig > #pragma comment(lib, "TEVlib.lib") > > void _func(void) > { > func() // this is the func of DLL > } > > // this is .i file > %inline{ > extern void _func(void); > %} > > I just wonder if it is possible that we can generate a .py file > directly from the header file of DLL with all functions exposed? I > think that can save a lot of time. I have tried without success. > Er, this is one of the things that SWIG is designed to do. Take a look at %include. William From bruce.who.hk at gmail.com Sun Apr 29 17:09:35 2007 From: bruce.who.hk at gmail.com (Bruce Who) Date: Sun, 29 Apr 2007 23:09:35 +0800 Subject: [C++-sig] [Swig-user] How to wrap a existing DLL to a python module? In-Reply-To: <463452CB.5030205@fultondesigns.co.uk> References: <463452CB.5030205@fultondesigns.co.uk> Message-ID: Hi, William Thanks! I have tried %include, and it works! This is my code: %module pyTE %include "TEVLib.h" %inline %{ #include "TEVLib.h" %} and I find that the #include statement is necessary too, what's the difference between %include and #include? On 4/29/07, William S Fulton wrote: > > > Er, this is one of the things that SWIG is designed to do. Take a look > at %include. > > William > Bruce From oanjao at yahoo.com Mon Apr 30 21:20:49 2007 From: oanjao at yahoo.com (Craig Finch) Date: Mon, 30 Apr 2007 12:20:49 -0700 (PDT) Subject: [C++-sig] Need strategic advice designing Boost/Python program In-Reply-To: <46311FA6.5050003@sympatico.ca> Message-ID: <240313.54382.qm@web30312.mail.mud.yahoo.com> Stefan, Thank you for your input. To answer your questions: > I'm not sure what you mean by 'implement a constructor'. Can you > elaborate ? > As a minor point, if you go this route, there is no need to use > extract<> > to access values, as within C++, you'll always deal with the (native) > C++ > interface directly. extract<> is useful to pull out embedded C++ > objects > from python (wrapper) objects, or to require an explicit conversion / > cast. Let's say that I have created classes in C++ that represent the data needed for my program. However, I want to build the methods in Python because they need to be modified easily. I have gotten this approach to work for methods except for __init__. If I try to add a Python constructor to the class, it can't access the data members of the C++ class. I don't want to write the constructor in C++ because it's potentially very complicated, defeating the purpose of writing methods in Python. What do you suggest? Also, thank you for your explanation of . It seems like I will still need to use extract, because I want the Python interface to use arrays in a Python-like manner. On Ralf's suggestion, I will look at scitbx to see how he handles it. > > 2. Similar to #1, but instead create Python classes that inherit > from > > the C++ base classes, and then add Python methods. Not sure how to > get > > the resulting objects back into C++. > > If you add new methods, or, more generally, attributes, you'll have > to > access them through the boost.python 'attr()' method that is part of > the > object protocol. However, if these attributes are only added for > convenience > (i.e. used within python, but not necessary for the C++ solvers), > there > isn't any need to access them from within C++, right ? Correct--the methods I am building in Python should not need to be accessible to the C++ solvers. So, if I have a C++ base class containing all the "core" data, I can then derive Python classes which add methods and "extra" data. In this case, how do I access the "core" data from C++, since it is now a derived class? Also--Ralf, thanks for the information about scitbx, and I may have more questions once I've studied it. I'll take a look at it and try to learn from its design, but currently the server http://cci.lbl.gov/ seems to be unreachable. Craig Finch -------------- Please reply to cfinch at ieee.org __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From seefeld at sympatico.ca Mon Apr 30 21:40:43 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 30 Apr 2007 15:40:43 -0400 Subject: [C++-sig] Need strategic advice designing Boost/Python program In-Reply-To: <240313.54382.qm@web30312.mail.mud.yahoo.com> References: <240313.54382.qm@web30312.mail.mud.yahoo.com> Message-ID: <4636463B.8090709@sympatico.ca> Craig Finch wrote: > Stefan, > Thank you for your input. To answer your questions: > >> I'm not sure what you mean by 'implement a constructor'. Can you >> elaborate ? >> As a minor point, if you go this route, there is no need to use >> extract<> >> to access values, as within C++, you'll always deal with the (native) >> C++ >> interface directly. extract<> is useful to pull out embedded C++ >> objects >> from python (wrapper) objects, or to require an explicit conversion / >> cast. > > Let's say that I have created classes in C++ that represent the data > needed for my program. However, I want to build the methods in Python > because they need to be modified easily. I have gotten this approach > to work for methods except for __init__. If I try to add a Python > constructor to the class, it can't access the data members of the C++ > class. I don't want to write the constructor in C++ because it's > potentially very complicated, defeating the purpose of writing methods > in Python. What do you suggest? This, together with your point further down that you want to add python methods that are only ever used inside python make it sound as if a C++ base class derived from in python may be the best approach. I don't understand what you mean by "I can't access the data members of the C++ class". Of course, you have to expose them to python via accessors (say, as 'properties'), but as soon as that is done you can easily derive and extend that base class from within python. > Also, thank you for your explanation of . It seems like I > will still need to use extract, because I want the Python interface to > use arrays in a Python-like manner. On Ralf's suggestion, I will look > at scitbx to see how he handles it. I'm not sure I understand. extract<> is a C++ API, that has nothing to do with a Python interface. The only context in which you may need extract<> here is when talking to a Python object (such as an instance of the derived Python class) from within C++, which, as you stated earlier, you won't. > Correct--the methods I am building in Python should not need to be > accessible to the C++ solvers. So, if I have a C++ base class > containing all the "core" data, I can then derive Python classes which > add methods and "extra" data. In this case, how do I access the "core" > data from C++, since it is now a derived class? The same IS-A relationship that you would expect from a pure C++ program still holds in this hybrid setting. If you have exposed a C++ function (or method) expecting a parent reference (or pointer), a child reference (or pointer) will be accepted as well. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From rwgk at yahoo.com Mon Apr 30 23:28:42 2007 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 30 Apr 2007 14:28:42 -0700 (PDT) Subject: [C++-sig] Need strategic advice designing Boost/Python program Message-ID: <109106.40902.qm@web31101.mail.mud.yahoo.com> > Also, thank you for your explanation of . It seems like I > will still need to use extract, because I want the Python interface to > use arrays in a Python-like manner. On Ralf's suggestion, I will look > at scitbx to see how he handles it. It all centers on the idea of storing repetetive data in C++ reference-counted arrays, which are wrapped with a central facility (huge template, flex_wrapper.h>). Custom data are stored in specific C++ classes which are wrapped with Boost.Python. A simple example is scitbx/histogram.h, where the repetetive data are in an array of floating point values, and the custom data in the histogram class. The interesting point after a few years of working experience is: Most people are covered with the existing array types (element types double, size_t, std::complex, etc.). It is fairly rare that someone needs other array types. I'm also trying to keep the number of wrapped array types low because the compile-time overhead and resulting .so file sizes are quite significant. This consideration leads to designs where we have two independent arrays of type A and B, instead of one array of a type C which composes A and B. That's often a compromise, but, again working experience, most of the time it really doesn't matter all that much, and you can hide it, e.g. behind a thin Python layer. If it becomes a problem, you can still use that huge template, and in a couple of lines define a new array type with element type C, with a complete Python interface modeled after Python's builtin list. So when I approach a new problem I typically try to fit it into the system by designing a C++ class for the core calculation, reusing our existing Python-exposed C++ arrays (flex arrays) as inputs. If this solution is too raw for general consumption, I stick a Python layer on top that makes it look truly pythonic. It is very similar to the idea behind Numeric/Numpy/numarray, only that the scitbx array types are C++ arrays with a "nice" std::vector like interface and fully automatic life-time management, and that you can easily make arrays of user-defined types if you feel it is necessary. Not everything fits into this scheme of using C++ as the "vector unit" and Python as the slower but more versatile "scalar unit", but we got a long way with this approach. The only time it didn't work out for me was when I had to handle a very deeply nested hierarchical data structure (six levels!), with two-way parent-child references. But numerical data usually come as arrays anyway, so often it is a no-brainer to design the C++ class processing the data. > Also--Ralf, thanks for the information about scitbx, and I may have > more questions once I've studied it. I'll take a look at it and try to > learn from its design, but currently the server > http://cci.lbl.gov/ seems to be unreachable. Sorry, that was an accident. It is back online now. (The unusual instability is because we just moved to new hardware.) Ralf __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From rwgk at yahoo.com Mon Apr 30 23:45:10 2007 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 30 Apr 2007 14:45:10 -0700 (PDT) Subject: [C++-sig] Need strategic advice designing Boost/Python program Message-ID: <148457.12444.qm@web31106.mail.mud.yahoo.com> > This, together with your point further down that you want to add python > methods that are only ever used inside python make it sound as if > a C++ base class derived from in python may be the best approach. FWIW: there is a very powerful alternative to this: injecting. For example: class _histogram(boost.python.injector, ext.histogram): def show(self, f=None, prefix="", format_cutoffs="%.8g"): if (f is None): f = sys.stdout fmt = "%s" + format_cutoffs + " - " + format_cutoffs + ": %d" for info in self.slot_infos(): print >> f, fmt % (prefix, info.low_cutoff, info.high_cutoff, info.n) The boost.python.injector is a small utility class defined in boost_adaptbx (adaptor toolbox), boost/python.py. The difference is that objects of ext.histogram will have the .show() method, even if they are created in C++. I.e. even if you only have a C++ constructor, you can have pure Python methods. To give proper credit: this trick is due to David Abrahams. It was one of the big aha moments in my life when he explained it to me. Ralf P.S.: you can also do it like this: def histogram_show(self, f=None, prefix="", format_cutoffs="%.8g"): # exact same code ext.histogram.show = histogram_show but it doesn't look nearly as nice. You can also make it look nice with decorators if you don't care about Python 2.2 and 2.3 compatibility. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From rwgk at yahoo.com Mon Apr 30 23:45:10 2007 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 30 Apr 2007 14:45:10 -0700 (PDT) Subject: [C++-sig] Need strategic advice designing Boost/Python program Message-ID: <148457.12444.qm@web31106.mail.mud.yahoo.com> > This, together with your point further down that you want to add python > methods that are only ever used inside python make it sound as if > a C++ base class derived from in python may be the best approach. FWIW: there is a very powerful alternative to this: injecting. For example: class _histogram(boost.python.injector, ext.histogram): def show(self, f=None, prefix="", format_cutoffs="%.8g"): if (f is None): f = sys.stdout fmt = "%s" + format_cutoffs + " - " + format_cutoffs + ": %d" for info in self.slot_infos(): print >> f, fmt % (prefix, info.low_cutoff, info.high_cutoff, info.n) The boost.python.injector is a small utility class defined in boost_adaptbx (adaptor toolbox), boost/python.py. The difference is that objects of ext.histogram will have the .show() method, even if they are created in C++. I.e. even if you only have a C++ constructor, you can have pure Python methods. To give proper credit: this trick is due to David Abrahams. It was one of the big aha moments in my life when he explained it to me. Ralf P.S.: you can also do it like this: def histogram_show(self, f=None, prefix="", format_cutoffs="%.8g"): # exact same code ext.histogram.show = histogram_show but it doesn't look nearly as nice. You can also make it look nice with decorators if you don't care about Python 2.2 and 2.3 compatibility. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: