From lists_ravi at lavabit.com Sun Nov 1 21:40:26 2009 From: lists_ravi at lavabit.com (Ravi) Date: Sun, 1 Nov 2009 15:40:26 -0500 Subject: [C++-sig] [python] python + phoenix In-Reply-To: <4AEBCA00.5010902@resophonic.com> References: <200910101158.46996.lists_ravi@lavabit.com> <200910310002.37065.lists_ravi@lavabit.com> <4AEBCA00.5010902@resophonic.com> Message-ID: <200911011540.26664.lists_ravi@lavabit.com> On Saturday 31 October 2009 01:24:16 troy d. straszheim wrote: > I take it that you have a use-case where it is difficult to specify > > as(thing) > > and easy to specify > > as >(thing) > > Could you elaborate? I have some code that takes member function pointers of the form R Obj::*( A1, A2, A3, ..., An ) and converts them to a function object Q with the following signature: Rnew Q( Obj&, B1, B2, ..., Bn ) where Rnew = result_conversion_metafunction::type Bi = arg_conversion_metafunction::type and Q is exposed via boost.python as a member of Obj. For example, Ai could be a fixed-point number, with Bi being a double so that the python side does not know anything about fixed-point numbers. An instance of Q would convert eh floating point numbers passed from the python side into fixed-point numbers, call the member function, and convert the returned value to a double for use on the python side. For these "converters", metafunctions from boost.function_types are used to obtain the mpl::vector specifying result type and argument types. Extending the technique above to function objects which take Obj* as their first argument, I have a protocol which relies on the presence of a typedef called 'components' in the function object so that I can use the converter when exposing via boost.python: struct my_functor { typedef mpl::vector components; R operator()( Obj*, A1, A2, A3, A4 ) { ... } }; // my_converter uses the components typedef typedef typename my_converter::type Q; Q q; class_( "Obj" ) .def( "my_func", q ) ; Regards, Ravi From anders.e.e.wallin at gmail.com Sun Nov 1 23:28:42 2009 From: anders.e.e.wallin at gmail.com (Anders Wallin) Date: Mon, 2 Nov 2009 00:28:42 +0200 Subject: [C++-sig] cannot find -lboost_python after upgrade to Ubuntu 9.10 Message-ID: <5ce676910911011428l36e8cbbdxbfcf8591e4c38719@mail.gmail.com> This example from the tutorial http://pastebin.ca/1652154 used to compile on my 9.04 system with this makefile: http://pastebin.ca/1652155 now after the 9.10 upgrade I get: /usr/bin/ld: cannot find -lboost_python what could be wrong? I do have 'libboost-python-dev' installed as well as 'libboost-python1.38.0' and 'libboost1.38-dev' From blake at hailmail.net Sun Nov 1 23:52:58 2009 From: blake at hailmail.net (Dane Springmeyer) Date: Sun, 1 Nov 2009 14:52:58 -0800 Subject: [C++-sig] cannot find -lboost_python after upgrade to Ubuntu 9.10 In-Reply-To: <5ce676910911011428l36e8cbbdxbfcf8591e4c38719@mail.gmail.com> References: <5ce676910911011428l36e8cbbdxbfcf8591e4c38719@mail.gmail.com> Message-ID: I think that recent boost packages on ubuntu have added a '-py25' or '- py26' at the end of the library, essentially providing two libraries linked to each of the python versions available on ubuntu. Dane On Nov 1, 2009, at 2:28 PM, Anders Wallin wrote: > This example from the tutorial > http://pastebin.ca/1652154 > > used to compile on my 9.04 system with this makefile: > http://pastebin.ca/1652155 > > now after the 9.10 upgrade I get: > /usr/bin/ld: cannot find -lboost_python > > > what could be wrong? > I do have 'libboost-python-dev' installed as well as > 'libboost-python1.38.0' and 'libboost1.38-dev' > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From anders.e.e.wallin at gmail.com Mon Nov 2 07:25:18 2009 From: anders.e.e.wallin at gmail.com (Anders Wallin) Date: Mon, 2 Nov 2009 08:25:18 +0200 Subject: [C++-sig] cannot find -lboost_python after upgrade to Ubuntu 9.10 In-Reply-To: References: <5ce676910911011428l36e8cbbdxbfcf8591e4c38719@mail.gmail.com> Message-ID: <5ce676910911012225w3f396727k7da83a2fcafcb125@mail.gmail.com> > I think that recent boost packages on ubuntu have added a '-py25' or '-py26' > at the end of the library, essentially providing two libraries linked to > each of the python versions available on ubuntu. looking in usr/lib I have at least these, but changing the makefile to one of these names does not seem to help: /usr/lib/libboost_python-mt.a /usr/lib/libboost_python-mt.so /usr/lib/libboost_python-mt-py25.a /usr/lib/libboost_python-mt-py25.so /usr/lib/libboost_python-mt-py25.so.1.38.0 /usr/lib/libboost_python-mt-py26.a /usr/lib/libboost_python-mt-py26.so /usr/lib/libboost_python-mt-py26.so.1.38.0 AW From micdestefano at gmail.com Mon Nov 2 08:55:51 2009 From: micdestefano at gmail.com (Michele De Stefano) Date: Mon, 2 Nov 2009 08:55:51 +0100 Subject: [C++-sig] Python converters for raw pointers: request for help In-Reply-To: <200910302353.28002.lists_ravi@lavabit.com> References: <200910302353.28002.lists_ravi@lavabit.com> Message-ID: Thank you very much. I had not thought to this solution. And I don't think there's a better one. Thank you again. Michele 2009/10/31 Ravi : > On Friday 23 October 2009 04:38:30 Michele De Stefano wrote: >> typedef struct _p_Mat* ? ? ? ? ? Mat; > > Creating a converter to/from a raw pointer is rather tricky (due to the way > argument type deduction handles pointers & references seamlessly, IIUC). You > should either expose _p_Mat or wrap the raw pointer in a struct and then > expose converters to it: > ?struct Mat { struct _p_Mat *ptr; }; > If you come up with a better way, I'd be interested. > > Regards, > Ravi > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Michele De Stefano http://www.linkedin.com/in/micdestefano http://xoomer.virgilio.it/michele_de_stefano From troy at resophonic.com Mon Nov 2 16:08:34 2009 From: troy at resophonic.com (troy d. straszheim) Date: Mon, 02 Nov 2009 10:08:34 -0500 Subject: [C++-sig] [python] python + phoenix In-Reply-To: <200911011540.26664.lists_ravi@lavabit.com> References: <200910101158.46996.lists_ravi@lavabit.com> <200910310002.37065.lists_ravi@lavabit.com> <4AEBCA00.5010902@resophonic.com> <200911011540.26664.lists_ravi@lavabit.com> Message-ID: <4AEEF5F2.2070403@resophonic.com> Ravi wrote: > On Saturday 31 October 2009 01:24:16 troy d. straszheim wrote: >> I take it that you have a use-case where it is difficult to specify >> >> as(thing) >> >> and easy to specify >> >> as >(thing) >> >> Could you elaborate? > > I have some code that takes member function pointers of the form > R Obj::*( A1, A2, A3, ..., An ) > and converts them to a function object Q with the following signature: > Rnew Q( Obj&, B1, B2, ..., Bn ) > where > Rnew = result_conversion_metafunction::type > Bi = arg_conversion_metafunction::type > and Q is exposed via boost.python as a member of Obj. > > For example, Ai could be a fixed-point number, with Bi being a double so that > the python side does not know anything about fixed-point numbers. I gather that custom converters aren't preferred because: 1. It is a hassle to talk directly to the underlying raw PyObject pointers and manage storage in the converter methods 2. The converted type still leaks out to python in docstrings (Both effects are visible in the custom string example: http://www.boost.org/doc/libs/1_40_0/libs/python/doc/v2/faq.html#custom_string) Are there others? Or am I completely off? > An instance > of Q would convert eh floating point numbers passed from the python side into > fixed-point numbers, call the member function, and convert the returned value > to a double for use on the python side. > > For these "converters", metafunctions from boost.function_types are used to > obtain the mpl::vector specifying result type and argument types. Extending > the technique above to function objects which take Obj* as their first > argument, I have a protocol which relies on the presence of a typedef called > 'components' in the function object so that I can use the converter when > exposing via boost.python: > > struct my_functor { > typedef mpl::vector components; > R operator()( Obj*, A1, A2, A3, A4 ) { ... } > }; To be sure I'm following you, correct me: * for each member function signature that you wrap, you have a matching my_functor that calls it * my_functor does nothing but call the member function it wraps * my_converter::type does the conversion from Rnew (B1, B2, ... Bn) to R (A1, A2, ...An) and calls my_functor. Have I got it? It is all very interesting, and to a certain degree duplicates some functionality in detail/caller.hpp. How do you generate these my_functors... preprocessor? fusion? I'm wondering if there isn't motivation here to cleanly integrate a general facility for additional c++-side type conversions. The following came to mind, which imitates a function type style that boost::proto uses extensively: struct hidden_type; // python shouldn't see this at all struct int_2_hidden // converts python-side 'int' to hidden { typedef hidden_type result_type; hidden_type operator()(int) const; }; // fnobj takes a hidden, doesn't know it is wrapped in python struct fnobj { typedef void result_type; void operator()(float, hidden_type); }; def("myfunctor", as(fnobj())); where int_to_hidden(int) is a function type (not a function call, but it later becomes a function call), indicating that what python passes should be converted to int, then the int converted to hidden_type via an instance of int_to_hidden, then the hidden_type passed to the underlying instance of fnobj. I realize this doesn't involve using the mpl::vectors you've already calculated, just throwing it out there. > // my_converter uses the components typedef > typedef typename my_converter::type Q; so Q could have a nested typedef (note I say Rnew, not R): typedef mpl::vector components; or use that in combination with function_types to synthesize typedef Rnew(Obj*, B1, B2, B3, B4) signature; > Q q; > class_( "Obj" ) > .def( "my_func", q ) > ; -t From lists_ravi at lavabit.com Mon Nov 2 21:33:14 2009 From: lists_ravi at lavabit.com (lists_ravi at lavabit.com) Date: Mon, 2 Nov 2009 15:33:14 -0500 (EST) Subject: [C++-sig] [python] python + phoenix Message-ID: <55154.65.220.96.226.1257193994.squirrel@lavabit.com> >> I have some code that takes member function pointers of the form >> R Obj::*( A1, A2, A3, ..., An ) >> and converts them to a function object Q with the following signature: >> Rnew Q( Obj&, B1, B2, ..., Bn ) >> where >> Rnew = result_conversion_metafunction::type >> Bi = arg_conversion_metafunction::type >> and Q is exposed via boost.python as a member of Obj. >> >> For example, Ai could be a fixed-point number, with Bi being a double so >> that >> the python side does not know anything about fixed-point numbers. > > I gather that custom converters aren't preferred because: > > 1. It is a hassle to talk directly to the underlying raw PyObject > pointers and manage storage in the converter methods > > 2. The converted type still leaks out to python in docstrings > > Are there others? Or am I completely off? Neither of the above is a big issue. In fact, python-side knowledge of the converted type would even be mildly useful. The main problem is the number of types: my_fixed_point_type< int bit_width, // 1 to 63 int binary_pt_location, // -63 to 63 bool signed, property_tag // policies for rounding, etc. > std::complex< my_fixed_point_type<...> > boost::numeric::ublas::array< ... > boost::numeric::ublas::matrix< ... > There are simply far too many types to create converters for and to register explicitly. Therefore, I use inline functions/functors to do all the conversions on the C++ side, and python knows only about real and complex doubles. > > An instance >> of Q would convert eh floating point numbers passed from the python side >> into >> fixed-point numbers, call the member function, and convert the returned >> value >> to a double for use on the python side. >> >> For these "converters", metafunctions from boost.function_types are used >> to >> obtain the mpl::vector specifying result type and argument types. >> Extending >> the technique above to function objects which take Obj* as their first >> argument, I have a protocol which relies on the presence of a typedef >> called >> 'components' in the function object so that I can use the converter when >> exposing via boost.python: >> >> struct my_functor { >> typedef mpl::vector components; >> R operator()( Obj*, A1, A2, A3, A4 ) { ... } >> }; > > To be sure I'm following you, correct me: > > * for each member function signature that you wrap, you have a matching > my_functor that calls it > * my_functor does nothing but call the member function it wraps > * my_converter::type does the conversion from Rnew (B1, B2, > ... Bn) to R (A1, A2, ...An) and calls my_functor. > > Have I got it? Yes. > It is all very interesting, and to a certain degree duplicates some > functionality in detail/caller.hpp. How do you generate these > my_functors... preprocessor? fusion? Mostly boost.preprocessor (with some template metaprogramming using mpl). Fusion doesn't work very well for me since the effort needed to get my functors into a form usable from fusion is greater than just coding my own invocation functions via boost.preprocessor; pseudo-code: template struct my_converter { Func f_; typedef typename ft::components::type components; typedef typename arg_converter::type arg_types; typedef typename result_converter::type result_type; // Shown for non-void return with one argument; can be generalized // using enable_if for void types and using boost.preprocessor for // different numbers of arguments result_type operator()( typename at_c<1,arg_types>::type a1 ) { return convert_result( f_( convert_arg::type>( a1 ) ) ); } }; template my_converter make_converted_func( Func f ) { return my_converter( f ); } > I'm wondering if there isn't motivation here to cleanly integrate a > general facility for additional c++-side type conversions. The > following came to mind, which imitates a function type style that > boost::proto uses extensively: > > struct hidden_type; // python shouldn't see this at all > > struct int_2_hidden // converts python-side 'int' to hidden > { > typedef hidden_type result_type; > > hidden_type operator()(int) const; > }; > > // fnobj takes a hidden, doesn't know it is wrapped in python > struct fnobj { > typedef void result_type; > void operator()(float, hidden_type); > }; > > def("myfunctor", as(fnobj())); > > where int_to_hidden(int) is a function type (not a function call, but it > later becomes a function call), indicating that what python passes > should be converted to int, then the int converted to hidden_type via an > instance of int_to_hidden, then the hidden_type passed to the underlying > instance of fnobj. > > I realize this doesn't involve using the mpl::vectors you've already > calculated, just throwing it out there. Such a general facility would probably be better than my code and would be greatly useful for me, but are there enough people out there with this use case? I don't care about using mpl::vector if there is a more general solution. >> // my_converter uses the components typedef >> typedef typename my_converter::type Q; > > so Q could have a nested typedef (note I say Rnew, not R): > > typedef mpl::vector components; > > or use that in combination with function_types to synthesize > > typedef Rnew(Obj*, B1, B2, B3, B4) signature; Yes. Regards, Ravi From amundson at fnal.gov Wed Nov 4 22:34:01 2009 From: amundson at fnal.gov (James Amundson) Date: Wed, 04 Nov 2009 15:34:01 -0600 Subject: [C++-sig] How do I wrap a static member function? In-Reply-To: <200910302343.31622.lists_ravi@lavabit.com> References: <4AEB5F01.2070503@fnal.gov> <200910302343.31622.lists_ravi@lavabit.com> Message-ID: <4AF1F349.2070608@fnal.gov> On 10/30/2009 10:43 PM, Ravi wrote: > Use class_.staticmethod(...) as shown below: > > Thanks. That's what I needed. Best, Jim Amundson From thomasd57 at yahoo.com Fri Nov 6 08:09:34 2009 From: thomasd57 at yahoo.com (Thomas Daniel) Date: Thu, 05 Nov 2009 23:09:34 -0800 Subject: [C++-sig] Iterators for heterogeneous container Message-ID: <4AF3CBAE.4080701@yahoo.com> I am having trouble wrapping a container that can hold multiple object types and therefore has multiple iterator types. My C++ library has these classes: class Tomato; class Potato; class Garden { Iter get_tomatoes(); Iter get_potatoes(); }; template class Iter { T get_next(); }; which allows to write: Iter potatoes = garden.get_potatoes(); while (Potato potato = potatoes.get_next()) { cout << potato.name(); } I am trying to use boost to wrap it so that I can write in python: for potato in garden.get_potatoes(): print potato.name() Any pointers how to achieve this with boost? From micdestefano at gmail.com Fri Nov 6 08:42:15 2009 From: micdestefano at gmail.com (Michele De Stefano) Date: Fri, 6 Nov 2009 08:42:15 +0100 Subject: [C++-sig] Iterators for heterogeneous container In-Reply-To: <4AF3CBAE.4080701@yahoo.com> References: <4AF3CBAE.4080701@yahoo.com> Message-ID: Thomas, I think the answer is here: http://www.boost.org/doc/libs/1_40_0/libs/python/doc/tutorial/doc/html/python/iterators.html but you should modify your "Garden" class in order to support "begin" and "end" iterators for "tomatoes" and "potatoes". With these modifications: class Garden { public: typedef .... Tomato_Iter; typedef .... Potato_Iter; Tomato_Iter tomatoes_begin(); Tomato_Iter tomatoes_end(); Potato_Iter potatoes_begin(); Potato_Iter potatoes_end(); }; you can expose your Garden class as here: class_("Garden") .property("tomatoes", range(&Garden::tomatoes_begin, &Garden::tomatoes_end)) .property("potatoes", range(&Garden::potatoes_begin, &Garden::potatoes_end)); At this point, in Python, you should be able to write: for potato in garden.potatoes: .... and do here whatever you want ... It's the best I can suggest 2009/11/6 Thomas Daniel : > I am having trouble wrapping a container that can hold multiple object types > and therefore has multiple iterator types. > > My C++ library has these classes: > > class Tomato; > class Potato; > class Garden { > ? Iter ? get_tomatoes(); > ? Iter ? get_potatoes(); > }; > > template > class Iter { > ? T get_next(); > }; > > which allows to write: > > Iter potatoes = garden.get_potatoes(); > while (Potato potato = potatoes.get_next()) { > ?cout << potato.name(); > } > > I am trying to use boost to wrap it so that I can write in python: > > for potato in garden.get_potatoes(): > ?print potato.name() > > Any pointers how to achieve this with boost? > > > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Michele De Stefano http://www.linkedin.com/in/micdestefano http://xoomer.virgilio.it/michele_de_stefano http://code.google.com/p/mds-utils From czg at eyesee360.com Fri Nov 6 19:40:42 2009 From: czg at eyesee360.com (Chang Glasgow) Date: Fri, 6 Nov 2009 13:40:42 -0500 (EST) Subject: [C++-sig] Python extension for Windows failing on import Message-ID: <49847.67.86.36.175.1257532842.squirrel@webmail7.pair.com> Hi folks, I'm attempting to build a Python extension and seem to be running repeatedly into a wall. The module compiles and links successfully, but attempting to import from a Python script kicks out the following error: ImportError: DLL load failed: The specified module could not be found. I'm using Boost 1.4 and Rapidmind 4.0. I've attempted building from the sample project provided by python.org, and from the quickstart sample provided by boost, always to the same result. Thanks, Chang From george_edison55 at hotmail.com Fri Nov 6 20:19:21 2009 From: george_edison55 at hotmail.com (Nathan Osman) Date: Fri, 6 Nov 2009 11:19:21 -0800 Subject: [C++-sig] Python extension for Windows failing on import In-Reply-To: <49847.67.86.36.175.1257532842.squirrel@webmail7.pair.com> References: <49847.67.86.36.175.1257532842.squirrel@webmail7.pair.com> Message-ID: Have you ensured that all paths are set up properly? (Can Python find your module?) Also, does your module have the right extension? - George > Date: Fri, 6 Nov 2009 13:40:42 -0500 > From: czg at eyesee360.com > To: cplusplus-sig at python.org > Subject: [C++-sig] Python extension for Windows failing on import > > Hi folks, > > I'm attempting to build a Python extension and seem to be running > repeatedly into a wall. > > The module compiles and links successfully, but attempting to import from > a Python script kicks out the following error: > > ImportError: DLL load failed: The specified module could not be found. > > I'm using Boost 1.4 and Rapidmind 4.0. I've attempted building from the > sample project provided by python.org, and from the quickstart sample > provided by boost, always to the same result. > > Thanks, > > > Chang > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig _________________________________________________________________ Windows Live: Make it easier for your friends to see what you?re up to on Facebook. http://go.microsoft.com/?linkid=9691816 -------------- next part -------------- An HTML attachment was scrubbed... URL: From czg at eyesee360.com Fri Nov 6 20:27:45 2009 From: czg at eyesee360.com (Chang Glasgow) Date: Fri, 6 Nov 2009 14:27:45 -0500 (EST) Subject: [C++-sig] Python extension for Windows failing on import In-Reply-To: References: <49847.67.86.36.175.1257532842.squirrel@webmail7.pair.com> Message-ID: <49878.67.86.36.175.1257535665.squirrel@webmail7.pair.com> The module is built as _somemodule.pyd, located in the same directory as the test script which attempts to import with the following line: from _somemodule import * This works in the case of the trivial examples, at least. It also occurs to me that I should mention that I'm using Python 2.6.4. From troy at resophonic.com Fri Nov 6 20:52:49 2009 From: troy at resophonic.com (troy d. straszheim) Date: Fri, 06 Nov 2009 14:52:49 -0500 Subject: [C++-sig] function types as user-defined converters In-Reply-To: <55154.65.220.96.226.1257193994.squirrel@lavabit.com> References: <55154.65.220.96.226.1257193994.squirrel@lavabit.com> Message-ID: <4AF47E91.9070503@resophonic.com> >> I'm wondering if there isn't motivation here to cleanly integrate a >> general facility for additional c++-side type conversions. I got distracted with boost.cmake stuff for a while but just got some things working. Simple example: // type hidden from python struct Hidden { double value; }; // // c++ interface that uses hidden type // std::string takes_hidden(Hidden h) { return std::string("Got an Hidden containing ") + boost::lexical_cast(h.value); } // // converter object // struct dbl_to_Hidden { typedef Hidden result_type; result_type operator()(double v) const { Hidden h; h.value = v; return a; } }; // the module BOOST_PYTHON_MODULE(udconversions_ext) { def("takes_hidden", as(&takes_hidden)); } ^^^^^^^^^^^^^^^^^^^^^ And the python: >>> from udconversions_ext import * >>> takes_a(13) 'Got an A containing 13' Here's another contrived example: // Here we have a class that takes optional arguments that // python ought not know about: struct S { void set(optional v) { i = v; } void add(boost::optional oi) { if (i && oi) (*i) += (*oi); } std::string show() { if (i) return boost::lexical_cast(*i); else return ""; } optional i; }; // And a converter function object that converts a python::object // to an optional (holding a T) if the object is // convertible to T, otherwise it returns an empty optional: template struct to_optional { typedef optional result_type; result_type operator()(const bp::object& o) { bp::extract ex(o); if (ex.check()) return optional(ex()); else return optional(); } }; BOOST_PYTHON_MODULE(opt_ext) { bp::class_("S") .def("set", bp::as(bp::object))>(&S::set)) .def("add", bp::as(bp::object))>(&S::add)) .def("show", &S::show) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ; }; And the result: >>> from opt_ext import * >>> s = S() >>> s.set(13) >>> s.show() '13' >>> s.set(None) >>> s.show() '' >>> s.set(None) >>> s.add(13) >>> s.show() '' >>> s.set(13) >>> s.add(13) >>> s.show() '26' There are plenty of issues yet. Currently, the converter type still leaks out to python in the signature: class S(Boost.Python.instance) | add(...) | add( (S)arg1, (object)arg2) -> None : | | C++ signature : | void add(S*,to_optional (*)(boost::python::api::object)) converters won't nest: def("foo", as(&somefn)); // nah I dont have a mechanism for hooking a user defined converter into the *return* type. I'm going to have to think about that one a bit more. specifying a function type, whose return value is a pointer to a function, is kinda nasty anyway: def("foo", as<(converter(*)(int))(bool, converter(float))>(&fn)); I'm just not sure it gets one anything other than terseness. Maybe a special return value policy is in order. Also, there isn't a way to use an object's constructor as a converter, That is, I'd like to support this syntax: struct X { X(double); }; void takes_X(X x); def("takes_x", as(&takes_x)); But I think this involves adding some scaffolding so that boost.python can differentiate between what in proto-ese is a Callable Transform (what now works) and an Object Transform (convert via constructor). -t From thomasd57 at yahoo.com Sat Nov 7 07:57:16 2009 From: thomasd57 at yahoo.com (Thomas Daniel) Date: Fri, 06 Nov 2009 22:57:16 -0800 Subject: [C++-sig] Iterators for heterogeneous container In-Reply-To: References: <4AF3CBAE.4080701@yahoo.com> Message-ID: <4AF51A4C.7010900@yahoo.com> Hi Michele, Thanks for the pointers, but unfortunately I don't think they apply in my case. It seems that boost support for iterators was written *only* with STL-like iterators in mind. You have to have begin() and end() and to be able to dereference the iterator. I can't do it (or I don't know how to do it). In contrast, my iterators are more like python iteration sequence, with only one method: get_next(), which returns the next object in the container *and* advances the iterator at the same time. I can't dereference this iterator, as you would do in STL. In fact, this seems to be so close to the python iteration model, that a simple approach should be doable: BOOST_PYTHON_MODULE(vegetables) { class_("Garden") .def("get_potatoes", &Garden::get_potatoes) .def("get_tomatoes", &Garden::get_tomatoes) ; class_("TomatoIter") .def("__iter__", &TomatoIter::get_next) ; } That at least compiles - unlike all my previous attempts that generate three pages of template errors - but python complain: TypeError: iter() returned non-iterator of type 'Tomato' so now I am trying to figure out how to tell boost that get_tomatoes returns an iterator ... Thomas Michele De Stefano wrote: > Thomas, > > I think the answer is here: > http://www.boost.org/doc/libs/1_40_0/libs/python/doc/tutorial/doc/html/python/iterators.html > > but you should modify your "Garden" class in order to support "begin" > and "end" iterators for "tomatoes" and "potatoes". > > With these modifications: > > class Garden { > public: > typedef .... Tomato_Iter; > typedef .... Potato_Iter; > > Tomato_Iter tomatoes_begin(); > Tomato_Iter tomatoes_end(); > Potato_Iter potatoes_begin(); > Potato_Iter potatoes_end(); > }; > > you can expose your Garden class as here: > > class_("Garden") > .property("tomatoes", range(&Garden::tomatoes_begin, &Garden::tomatoes_end)) > .property("potatoes", range(&Garden::potatoes_begin, > &Garden::potatoes_end)); > > At this point, in Python, you should be able to write: > > for potato in garden.potatoes: > .... and do here whatever you want ... > > It's the best I can suggest > > > 2009/11/6 Thomas Daniel : > >> I am having trouble wrapping a container that can hold multiple object types >> and therefore has multiple iterator types. >> >> My C++ library has these classes: >> >> class Tomato; >> class Potato; >> class Garden { >> Iter get_tomatoes(); >> Iter get_potatoes(); >> }; >> >> template >> class Iter { >> T get_next(); >> }; >> >> which allows to write: >> >> Iter potatoes = garden.get_potatoes(); >> while (Potato potato = potatoes.get_next()) { >> cout << potato.name(); >> } >> >> I am trying to use boost to wrap it so that I can write in python: >> >> for potato in garden.get_potatoes(): >> print potato.name() >> >> Any pointers how to achieve this with boost? >> >> >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> >> > > > > From blackmet.darkangel at gmail.com Sat Nov 7 14:43:33 2009 From: blackmet.darkangel at gmail.com (blackmet blackmet) Date: Sat, 7 Nov 2009 18:43:33 +0500 Subject: [C++-sig] using windows api functions Message-ID: <2b8ad48b0911070543h5b5a7c13va94703fe39e0c07@mail.gmail.com> Hello, World!))) I'm try to generate pyd file, which simple should create one window, and it's necessary to use windiws api functions. I just take example from msdn: #include // Global variable HINSTANCE hinst; // Function prototypes. //int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int); BOOL InitApplication(HINSTANCE); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); // Application entry point. int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; if (!InitApplication(hinstance)) return FALSE; if (!InitInstance(hinstance, nCmdShow)) return FALSE; BOOL fGotMessage; while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; UNREFERENCED_PARAMETER(lpCmdLine); } BOOL InitApplication(HINSTANCE hinstance) { WNDCLASSEX wcx; // Fill in the window class structure with parameters // that describe the main window. wcx.cbSize = sizeof(wcx); // size of structure wcx.style = CS_HREDRAW | CS_VREDRAW; // redraw if size changes wcx.lpfnWndProc = MainWndProc; // points to window procedure wcx.cbClsExtra = 0; // no extra class memory wcx.cbWndExtra = 0; // no extra window memory wcx.hInstance = hinstance; // handle to instance wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION); // predefined app. icon wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // predefined arrow wcx.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH); // white background brush wcx.lpszMenuName = "MainMenu"; // name of menu resource wcx.lpszClassName = "MainWClass"; // name of window class wcx.hIconSm = (HICON)LoadImage(hinstance, // small class icon MAKEINTRESOURCE(5), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); // Register the window class. return RegisterClassEx(&wcx); } BOOL InitInstance(HINSTANCE hinstance, int nCmdShow) { HWND hwnd; // Save the application-instance handle. hinst = hinstance; // Create the main window. hwnd = CreateWindow( "MainWClass", // name of window class "Sample", // title-bar string WS_OVERLAPPEDWINDOW, // top-level window CW_USEDEFAULT, // default horizontal position CW_USEDEFAULT, // default vertical position CW_USEDEFAULT, // default width CW_USEDEFAULT, // default height (HWND) NULL, // no owner window (HMENU) NULL, // use class menu hinstance, // handle to application instance (LPVOID) NULL); // no window-creation data if (!hwnd) return FALSE; // Show the window and send a WM_PAINT message to the window // procedure. ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); return TRUE; } LRESULT CALLBACK MainWndProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch(msg) { case WM_PAINT: ValidateRgn(hwnd, NULL); break; case WM_DESTROY: { PostQuitMessage(0); return(0); } break; } return(DefWindowProc(hwnd, msg, wparam, lparam)); } and in compile process I get this errors: c:/boost_1_40_0/boost/python/make_function.hpp: In function `boost::python::api: :object boost::python::make_function(F) [with F = int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, int)]': c:/boost_1_40_0/boost/python/def.hpp:82: instantiated from `boost::python::api ::object boost::python::detail::make_function1(T, ...) [with T = int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, int)]' c:/boost_1_40_0/boost/python/def.hpp:91: instantiated from `void boost::python ::def(const char*, Fn) [with Fn = int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, int)]' windowmsdn.cpp:141: instantiated from here c:/boost_1_40_0/boost/python/make_function.hpp:103: error: invalid conversion from `int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, int)' to `int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, int)' c:/boost_1_40_0/boost/python/make_function.hpp:103: error: initializing argument 1 of `boost::mpl::vector5 boost::python::detail::get_signature(RT (*)(T0, T1, T2, T3), void*) [with RT = int, T0 = HINSTANCE__*, T1 = HINSTANCE__*, T2 = CHAR*, T3 = int]' I will be very grateful for any help. email: blackmet.darkangel at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Sat Nov 7 15:41:51 2009 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Sat, 07 Nov 2009 09:41:51 -0500 Subject: [C++-sig] using windows api functions In-Reply-To: <2b8ad48b0911070543h5b5a7c13va94703fe39e0c07@mail.gmail.com> References: <2b8ad48b0911070543h5b5a7c13va94703fe39e0c07@mail.gmail.com> Message-ID: <4AF5872F.9000400@sympatico.ca> On 11/07/2009 08:43 AM, blackmet blackmet wrote: > > c:/boost_1_40_0/boost/python/make_function.hpp:103: error: > invalid conversion from `int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, > int)' to `int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, int)' > c:/boost_1_40_0/boost/python/make_function.hpp:103: error: > initializing argument 1 of `boost::mpl::vector5 > boost::python::detail::get_signature(RT (*)(T0, T1, T2, T3), void*) > [with RT = int, T0 = HINSTANCE__*, T1 = HINSTANCE__*, T2 = CHAR*, T3 = > int]' That is a confusing error message, isn't it. I seem to recall having seen similar errors in the past, though I don't recall what caused it. Can you show the declaration of the function that you are attempting to wrap ? Could it be that it is using non-standard calling conventions (stdcall, fastcall, or some such), causing the compiler to detect a mismatch, without being able to report a clear error message ? Just a shot in the dark... Stefan -- ...ich hab' noch einen Koffer in Berlin... From blackmet.darkangel at gmail.com Sat Nov 7 15:58:10 2009 From: blackmet.darkangel at gmail.com (blackmet blackmet) Date: Sat, 7 Nov 2009 19:58:10 +0500 Subject: [C++-sig] using windows api functions In-Reply-To: <4AF5872F.9000400@sympatico.ca> References: <2b8ad48b0911070543h5b5a7c13va94703fe39e0c07@mail.gmail.com> <4AF5872F.9000400@sympatico.ca> Message-ID: <2b8ad48b0911070658k25702f93rc3c9607874e32bd5@mail.gmail.com> 2009/11/7 Stefan Seefeld > On 11/07/2009 08:43 AM, blackmet blackmet wrote: > >> >> c:/boost_1_40_0/boost/python/make_function.hpp:103: error: >> invalid conversion from `int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, int)' >> to `int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, int)' >> c:/boost_1_40_0/boost/python/make_function.hpp:103: error: >> initializing argument 1 of `boost::mpl::vector5 >> boost::python::detail::get_signature(RT (*)(T0, T1, T2, T3), void*) >> [with RT = int, T0 = HINSTANCE__*, T1 = HINSTANCE__*, T2 = CHAR*, T3 = >> int]' >> > > That is a confusing error message, isn't it. I seem to recall having seen > similar errors in the past, though I don't recall what caused it. Can you > show the declaration of the function that you are attempting to wrap ? Could > it be that it is using non-standard calling conventions (stdcall, fastcall, > or some such), causing the compiler to detect a mismatch, without being able > to report a clear error message ? > > It is quite simple: BOOST_PYTHON_MODULE(_winmain) { using namespace boost::python; def("WinMain", WinMain); } -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Sat Nov 7 17:00:56 2009 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Sat, 07 Nov 2009 11:00:56 -0500 Subject: [C++-sig] using windows api functions In-Reply-To: <2b8ad48b0911070658k25702f93rc3c9607874e32bd5@mail.gmail.com> References: <2b8ad48b0911070543h5b5a7c13va94703fe39e0c07@mail.gmail.com> <4AF5872F.9000400@sympatico.ca> <2b8ad48b0911070658k25702f93rc3c9607874e32bd5@mail.gmail.com> Message-ID: <4AF599B8.4020109@sympatico.ca> On 11/07/2009 09:58 AM, blackmet blackmet wrote: > > > 2009/11/7 Stefan Seefeld > > > On 11/07/2009 08:43 AM, blackmet blackmet wrote: > > > c:/boost_1_40_0/boost/python/make_function.hpp:103: error: > invalid conversion from `int (*)(HINSTANCE__*, HINSTANCE__*, > CHAR*, int)' to `int (*)(HINSTANCE__*, HINSTANCE__*, CHAR*, int)' > c:/boost_1_40_0/boost/python/make_function.hpp:103: error: > initializing argument 1 of `boost::mpl::vector5 T3, T4> > boost::python::detail::get_signature(RT (*)(T0, T1, T2, T3), > void*) > [with RT = int, T0 = HINSTANCE__*, T1 = HINSTANCE__*, T2 = > CHAR*, T3 = int]' > > > That is a confusing error message, isn't it. I seem to recall > having seen similar errors in the past, though I don't recall what > caused it. Can you show the declaration of the function that you > are attempting to wrap ? Could it be that it is using non-standard > calling conventions (stdcall, fastcall, or some such), causing the > compiler to detect a mismatch, without being able to report a > clear error message ? > > It is quite simple: > > BOOST_PYTHON_MODULE(_winmain) > { > using namespace boost::python; > def("WinMain", WinMain); What is "WinMain" ? Oh, and looking again at the error I wonder what HINSTANCE__ is, and how you want to convert that to Python. But that is an entirely different issue, unrelated to the error you are getting. Stefan -- ...ich hab' noch einen Koffer in Berlin... From blackmet.darkangel at gmail.com Sat Nov 7 18:59:46 2009 From: blackmet.darkangel at gmail.com (blackmet blackmet) Date: Sat, 7 Nov 2009 22:59:46 +0500 Subject: [C++-sig] using windows api functions In-Reply-To: <4AF599B8.4020109@sympatico.ca> References: <2b8ad48b0911070543h5b5a7c13va94703fe39e0c07@mail.gmail.com> <4AF5872F.9000400@sympatico.ca> <2b8ad48b0911070658k25702f93rc3c9607874e32bd5@mail.gmail.com> <4AF599B8.4020109@sympatico.ca> Message-ID: <2b8ad48b0911070959g405b5091p3c88b265f4c2cf60@mail.gmail.com> Thanks, Stefan. Sorry for my stupid question. -------------- next part -------------- An HTML attachment was scrubbed... URL: From troy at resophonic.com Sat Nov 7 20:45:22 2009 From: troy at resophonic.com (troy d. straszheim) Date: Sat, 07 Nov 2009 14:45:22 -0500 Subject: [C++-sig] Iterators for heterogeneous container In-Reply-To: <4AF51A4C.7010900@yahoo.com> References: <4AF3CBAE.4080701@yahoo.com> <4AF51A4C.7010900@yahoo.com> Message-ID: <4AF5CE52.40402@resophonic.com> Thomas Daniel wrote: > > BOOST_PYTHON_MODULE(vegetables) > { > class_("Garden") > .def("get_potatoes", &Garden::get_potatoes) > .def("get_tomatoes", &Garden::get_tomatoes) > ; > class_("TomatoIter") > .def("__iter__", &TomatoIter::get_next) > ; > } > > That at least compiles - unlike all my previous attempts that generate > three pages of template errors - but python complain: > > TypeError: iter() returned non-iterator of type 'Tomato' > > so now I am trying to figure out how to tell boost that get_tomatoes > returns an iterator ... > Please don't top-post, guys As you know, python iterators have a function next() that returns the next object in the iteree, or throw StopIteration when they're at the end. As you also know, python expects member functions __iter__() to return an iterator. Is the thing returned by TomatoIter::get_next an iterator, ie does it implement the iterator interface? -t From blackmet.darkangel at gmail.com Sat Nov 7 21:38:51 2009 From: blackmet.darkangel at gmail.com (blackmet blackmet) Date: Sun, 8 Nov 2009 01:38:51 +0500 Subject: [C++-sig] connecting library Message-ID: <2b8ad48b0911071238w383189c7h104e9372f8d71a88@mail.gmail.com> Hi, everyone. Prompt me please, how can I connect extern library to bjam. For example: connect libd3d9.a from mingw package. Thankful in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From thomasd57 at yahoo.com Sat Nov 7 23:25:41 2009 From: thomasd57 at yahoo.com (Thomas Daniel) Date: Sat, 07 Nov 2009 14:25:41 -0800 Subject: [C++-sig] Iterators for heterogeneous container In-Reply-To: <4AF5CE52.40402@resophonic.com> References: <4AF3CBAE.4080701@yahoo.com> <4AF51A4C.7010900@yahoo.com> <4AF5CE52.40402@resophonic.com> Message-ID: <4AF5F3E5.2010006@yahoo.com> troy d. straszheim wrote: > Thomas Daniel wrote: >> >> BOOST_PYTHON_MODULE(vegetables) >> { >> class_("Garden") >> .def("get_potatoes", &Garden::get_potatoes) >> .def("get_tomatoes", &Garden::get_tomatoes) >> ; >> class_("TomatoIter") >> .def("__iter__", &TomatoIter::get_next) >> ; >> } >> >> That at least compiles - unlike all my previous attempts that >> generate three pages of template errors - but python complain: >> >> TypeError: iter() returned non-iterator of type 'Tomato' >> >> so now I am trying to figure out how to tell boost that get_tomatoes >> returns an iterator ... >> > > Please don't top-post, guys > > As you know, python iterators have a function next() that returns the > next object in the iteree, or throw StopIteration when they're at the > end. As you also know, python expects member functions __iter__() to > return an iterator. Is the thing returned by TomatoIter::get_next an > iterator, ie does it implement the iterator interface? > > -t > > Thank you very much for the email, you did put me on the right track and I finally figured out how to do it, with help from here also: http://wiki.python.org/moin/boost.python/iterator The solution involves creating a wrapper class around get_next(), which throws StopIteration when done and a "pass_through" function to bind to __iter__: inline TomatoIter pass_through(const TomatoIter& iter) { return iter; } Tomato next(TomatoIter& iter) { Tomato tomato = iter.get_next(); if (!tomato) { PyErr_SetString(PyExc_StopIteration, "No more data."); throw_error_already_set(); } return tomato; } Now, I can do this: BOOST_PYTHON_MODULE(Garden) { ...... class_("TomatoIter", no_init) .def("next", next) .def("__iter__", pass_through) ; and it works. Thomas From troy at resophonic.com Sun Nov 8 02:35:06 2009 From: troy at resophonic.com (troy d. straszheim) Date: Sat, 07 Nov 2009 20:35:06 -0500 Subject: [C++-sig] connecting library In-Reply-To: <2b8ad48b0911071238w383189c7h104e9372f8d71a88@mail.gmail.com> References: <2b8ad48b0911071238w383189c7h104e9372f8d71a88@mail.gmail.com> Message-ID: <4AF6204A.3050200@resophonic.com> blackmet blackmet wrote: > Hi, everyone. > > Prompt me please, how can I connect extern library to bjam. > For example: > connect libd3d9.a from mingw package. > The wrong list I think, asking on you are. -t From thomasd57 at yahoo.com Mon Nov 9 00:20:03 2009 From: thomasd57 at yahoo.com (Thomas Daniel) Date: Sun, 08 Nov 2009 15:20:03 -0800 Subject: [C++-sig] unfriendly python error message (no line number) In-Reply-To: <4AF6204A.3050200@resophonic.com> References: <2b8ad48b0911071238w383189c7h104e9372f8d71a88@mail.gmail.com> <4AF6204A.3050200@resophonic.com> Message-ID: <4AF75223.8010103@yahoo.com> No, it is not about template-related error messages :-)... I am wrapping a class: #include struct X { X() : _x(0) {} int get() const { return _x; } int _x; }; BOOST_PYTHON_MODULE(Test) { class_("X") .def("get", &X::get) ; } and testing it with python: x = X(); print x.get() print x.get Obviously, the error is that I forgot '()' in the second get(). I am getting this error message: 0 > Is there a way to give user a more friendly error message in such case, with at least a line number where it happened? Note that if mistype the attribute name, I am getting something I much nicer: Traceback (most recent call last): File "test.py", line 5, in print x.foo AttributeError: 'X' object has no attribute 'foo' From amohr at pixar.com Mon Nov 9 01:01:49 2009 From: amohr at pixar.com (amohr at pixar.com) Date: Sun, 08 Nov 2009 16:01:49 -0800 Subject: [C++-sig] unfriendly python error message (no line number) In-Reply-To: <4AF75223.8010103@yahoo.com> References: <2b8ad48b0911071238w383189c7h104e9372f8d71a88@mail.gmail.com> <4AF6204A.3050200@resophonic.com> <4AF75223.8010103@yahoo.com> Message-ID: > No, it is not about template-related error messages :-)... > > I am wrapping a class: > Obviously, the error is that I forgot '()' in the second get(). I > am > getting this error message: > > 0 > > > > Is there a way to give user a more friendly error message in such > case, > with at least a line number where it happened? This has nothing to do with boost.python. It's how python works. For example: >>> class X(object): def __init__(self): self._x = 0 def get(self): return self._x >>> x = X() >>> print x.get() 0 >>> print x.get > And accessing 'x.get' is not in itself an error. You may want to pass that bound method to a function expecting a callable, for instance. Alex From boost-python at lonely-star.org Tue Nov 10 13:45:34 2009 From: boost-python at lonely-star.org (Nathan Huesken) Date: Tue, 10 Nov 2009 13:45:34 +0100 Subject: [C++-sig] getting shape of numpy array Message-ID: <20091110124534.GA4108@esrom> Hi, I am trying to interaface a numpy array using boost::python::numeric::array. void function(numeric::array& a) { tuple shape=extract(a.getshape()); double x; int i,j; for(i=0;i(a[make_tuple(i,j)]); } Now I had to add numeric::array::set_module_and_type("numpy", "ndarray"); to the module, hope that was correct. When I call this function in python, I get: AttributeError: 'numpy.ndarray' object has no attribute 'getshape' What is the correct way to do it? Thanks! Nathan -- From micdestefano at gmail.com Tue Nov 10 14:35:13 2009 From: micdestefano at gmail.com (Michele De Stefano) Date: Tue, 10 Nov 2009 14:35:13 +0100 Subject: [C++-sig] getting shape of numpy array In-Reply-To: <20091110124534.GA4108@esrom> References: <20091110124534.GA4108@esrom> Message-ID: Hello Nathan. Find my answers below. 2009/11/10 Nathan Huesken : > Hi, > > I am trying to interaface a numpy array using boost::python::numeric::array. > > void function(numeric::array& a) > { > ? ? ? ?tuple shape=extract(a.getshape()); > ? ? ? ?double x; > ? ? ? ?int i,j; > ? ? ? ?for(i=0;i ? ? ? ? ? ? ? ?for(j=0;j ? ? ? ? ? ? ? ? ? ? ? ?x=extract(a[make_tuple(i,j)]); > } > > Now I had to add > numeric::array::set_module_and_type("numpy", "ndarray"); > > to the module, hope that was correct. > > When I call this function in python, I get: > AttributeError: 'numpy.ndarray' object has no attribute 'getshape' The error comes out because, actually, numpy.ndarray has no getshape method. > > What is the correct way to do it? > Within your function, declare const tuple &shape = extract(in.attr("shape")); I suggest you to have a look also at my open source library (mds-utils): http://code.google.com/p/mds-utils/ I've written some from/to Python converters that are able to convert a Python sequence directly into a Boost uBLAS vector or matrix. If you download the software, you can build the doxygen documentation (follow the instructions) and you will find an example for each C++ class or function. In other words, using my library, you could write your function as: void function(const boost::numeric::ublas::matrix& m) { ... } and export it to Python and pass, from Python, a Numpy 2D array. Have a look at mds-utils and at its usage examples. Bye, Michele > Thanks! > Nathan > -- > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Michele De Stefano http://www.linkedin.com/in/micdestefano http://code.google.com/p/mds-utils http://xoomer.virgilio.it/michele_de_stefano From charles at stardock.com Tue Nov 10 22:36:41 2009 From: charles at stardock.com (Charles Lentz) Date: Tue, 10 Nov 2009 16:36:41 -0500 Subject: [C++-sig] [Boost.Python] tutorial python extension fails on import with ImportError: No module named hello_ext Message-ID: <004a01ca624d$e43c95a0$acb5c0e0$@com> Hey all, When using boost_1_38_0\libs\python\example\tutorial, I try to import the extension that is created (hello_ext) but get the exception, ImportError: No module named hello_ext Here's some details that might be needed: OS: Windows Vista Boost version: 1.38.0 Bjam version: 3.1.17 Python version: 2.6.2 My user-config.jam looks like this: # MSVC configuration using msvc : 9.0 ; # Python configuration using python : 2.6 #version : C:/Python26 #location : C:/projects/external/python/Python-2.6.2/Include #includes : C:/projects/external/python/Python-2.6.2/Lib #libraries ; I am working with the tutorial example located here: boost_1_38_0\libs\python\example\tutorial. When I run Bjam after I cd to this directory, this tutorial example says that it passes all the tests and I can check the output in the resulting files within the hello.test directory. That's all well and good that the test can get this extension module to work, but I want to be able to import it myself in a python session or python file. That way I can see it work and then move on to creating my own extensions. I modify Jamroot so that the last few lines where it creates tests are commented out. When I run bjam again, hello_ext.pyd remains (previously it was being created, used for the test, and then deleted). I try to run hello.py (which is the script that the test uses) from IDLE (which comes with the installation of python) and it throws an exception for my import statement Traceback (most recent call last): File "C:\Users\Charles\Desktop\boost_1_38_0\boost_1_38_0\libs\python\example\tuto rial\hello.py", line 6, in import hello_ext ImportError: No module named hello_ext Hrmm. Okay. So I move the hello.py file into the same directory as the hello_ext.pyd file. Run it again using IDLE, still get the exception Traceback (most recent call last): File "C:\Users\Charles\Desktop\boost_1_38_0\boost_1_38_0\libs\python\example\tuto rial\bin\msvc-9.0\debug\threading-multi\hello.py", line 6, in import hello_ext ImportError: No module named hello_ext So I guess I need some help here. I want to be able to create an extension module and then import it in a script. This is what I was led to believe I could do, but I do not see how. I have tried to provide all information that I think is necessary. Any help is greatly appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mg262 at cam.ac.uk Thu Nov 19 13:30:16 2009 From: mg262 at cam.ac.uk (Mohan Ganesalingam) Date: 19 Nov 2009 12:30:16 +0000 Subject: [C++-sig] Efficient string passing Message-ID: Dear all, I recently sent an e-mail which (I think) didn't reach the list, about trying to return large strings from C++ into Python without copying them. By looking at the C/Python API I've subsequently figured out roughly how to do this... something like Class World { object return_object() { PyObject *result = PyString_FromStringAndSize(0, 7); // initialise the underlying string using PyString_AsString handle<> h(result); return object(); } ... main_namespace["World"] = class_("World") .def("return_object", &World::return_object); Unfortunately this doesn't work; when I call w.return_object(), I get back None. Returning a raw PyObject * instead of a boost::python::object works, but I can't tell if that involves a copy... and using return_value_policy() with a PyObject * return crashes. Any advice here would be very much appreciated! Thank you and best wishes, Mohan From seefeld at sympatico.ca Thu Nov 19 14:06:07 2009 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 19 Nov 2009 08:06:07 -0500 Subject: [C++-sig] Efficient string passing In-Reply-To: References: Message-ID: <4B0542BF.4040000@sympatico.ca> On 11/19/2009 07:30 AM, Mohan Ganesalingam wrote: > Dear all, > > I recently sent an e-mail which (I think) didn't reach the list, about > trying to return large strings from C++ into Python without copying them. I believe this is impossible: Python treats strings as immutable, which means you can't manipulate them in-place, and there is no C API function that allows you to pass in a string by reference, as that would make memory management impossibly complex. > By looking at the C/Python API I've subsequently figured out roughly > how to do this... something like > > Class World > { > object return_object() > { > PyObject *result = PyString_FromStringAndSize(0, 7); This creates a new string, copying whatever you pass as input. > // initialise the underlying string using PyString_AsString > handle<> h(result); > return object(); I'm not sure what your intent is, but returning a default-constructed object is the C++ equivalent of None. > } > > ... > > > main_namespace["World"] = class_("World") > .def("return_object", &World::return_object); > > Unfortunately this doesn't work; when I call w.return_object(), I get > back None. ...because above you "return object();", presumably. > Returning a raw PyObject * instead of a boost::python::object works, > but I can't tell if that involves a copy... and using > return_value_policy() with a PyObject * return > crashes. > > Any advice here would be very much appreciated! I don't think you can get what you want using strings directly. You may create and expose a wrapper type that provides a string-like interface, with appropriate conversions. Stefan -- ...ich hab' noch einen Koffer in Berlin... From mg262 at cam.ac.uk Thu Nov 19 14:30:45 2009 From: mg262 at cam.ac.uk (Mohan Ganesalingam) Date: 19 Nov 2009 13:30:45 +0000 Subject: [C++-sig] Efficient string passing In-Reply-To: <4B0542BF.4040000@sympatico.ca> References: <4B0542BF.4040000@sympatico.ca> Message-ID: Thank you for the reply! As you spotted, I was making an embarrassingly idiotic typo (due to too much cut and pasting)... it should of course have been handle<> h(result); return object(h); Once that was fixed, everything worked fine. >I believe this is impossible: Python treats strings as immutable, which >means you can't manipulate them in-place, and there is no C API function There is apparently one exception to this, which I was utilising: char* PyString_AsString(PyObject *string) Returns a NUL-terminated representation of the contents of string. The pointer refers to the internal buffer of string, not a copy. The data must not be modified in any way, unless the string was just created using PyString_FromStringAndSize(NULL, size). I.e. if you create a string using PyString_FromStringAndSize(0, n), then you can immediately modify its contents using PyString_AsString. AFAICS this functionality is (reasonably enough) not wrapped by boost::python::str, so you have to use the underlying API. Thanks again for spotting that! Best wishes, Mohan From seefeld at sympatico.ca Thu Nov 19 14:56:39 2009 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 19 Nov 2009 08:56:39 -0500 Subject: [C++-sig] Efficient string passing In-Reply-To: References: <4B0542BF.4040000@sympatico.ca> Message-ID: <4B054E97.2060101@sympatico.ca> On 11/19/2009 08:30 AM, Mohan Ganesalingam wrote: >> I believe this is impossible: Python treats strings as immutable, >> which means you can't manipulate them in-place, and there is no C API >> function > > There is apparently one exception to this, which I was utilising: > char* PyString_AsString(PyObject *string) Returns a NUL-terminated > representation of the contents of string. The pointer refers to the > internal buffer of string, not a copy. The data must not be modified > in any way, unless the string was just created using > PyString_FromStringAndSize(NULL, size). > > I.e. if you create a string using PyString_FromStringAndSize(0, n), > then you can immediately modify its contents using PyString_AsString. > AFAICS this functionality is (reasonably enough) not wrapped by > boost::python::str, so you have to use the underlying API. OK, perhaps I misunderstood your use-case. Indeed, you may allocate a string using the Python C API, and have a small window within which you can manipulate its content, before handing control back to the Python runtime. If that is what you need, great. For avoidance of doubt: I don't think it would be valid to modify that string after you have handed control back to Python. The Python runtime may well create hashes from it for efficient lookup, which build on the immutability of this type of object. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From dtcaciuc at gmail.com Thu Nov 19 22:54:51 2009 From: dtcaciuc at gmail.com (Dimitri Tcaciuc) Date: Thu, 19 Nov 2009 13:54:51 -0800 Subject: [C++-sig] Boost.Python + OpenGL segmentation faults Message-ID: <9bdc02d30911191354v2603db13i15c5030aa16b076c@mail.gmail.com> Hello everyone, I've recently posted a problem to SO (http://stackoverflow.com/questions/1751408/boost-python-opengl-segmentation-faults), where I haven't had much luck with answer. My problem is that if I import OpenGL python libraries before importing my B.P bindings, I get strange segmentation faults for no apparent reason. I isolated a small test case which I'm attaching below. The current boost library version I'm using is 1.37; you'll have to make a change to Makefile if you have any other version installed. I'd greatly appreciate if anyone can give some insight on what's happening. Dimitri. -------------- next part -------------- A non-text attachment was scrubbed... Name: foo.cpp Type: application/octet-stream Size: 1796 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: glcrash.py Type: application/octet-stream Size: 184 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Makefile Type: application/octet-stream Size: 140 bytes Desc: not available URL: From troy at resophonic.com Thu Nov 19 23:13:26 2009 From: troy at resophonic.com (troy d. straszheim) Date: Thu, 19 Nov 2009 17:13:26 -0500 Subject: [C++-sig] Boost.Python + OpenGL segmentation faults In-Reply-To: <9bdc02d30911191354v2603db13i15c5030aa16b076c@mail.gmail.com> References: <9bdc02d30911191354v2603db13i15c5030aa16b076c@mail.gmail.com> Message-ID: <4B05C306.8050604@resophonic.com> Dimitri Tcaciuc wrote: > Hello everyone, > > I've recently posted a problem to SO > (http://stackoverflow.com/questions/1751408/boost-python-opengl-segmentation-faults), > where I haven't had much luck with answer. > > My problem is that if I import OpenGL python libraries before > importing my B.P bindings, I get strange segmentation faults for no > apparent reason. I isolated a small test case which I'm attaching > below. The current boost library version I'm using is 1.37; you'll > have to make a change to Makefile if you have any other version > installed. > > I'd greatly appreciate if anyone can give some insight on what's happening. > The only thing that strikes me is that I'd just use the vector_indexing_suite on the 'vector elements'. Beyond that, the next thing you'd want is a --with-pydebug --without-pymalloc build of python (with associated opengl and boost.python built against this library), get some stacktraces from running under gdb, and run things under valgrind with the valgrind-python.supp list from the Misc directory of the python distribution. It is a bit of work to set up but you get a lot more debuggability. -t From rwgk at yahoo.com Fri Nov 20 02:02:37 2009 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Thu, 19 Nov 2009 17:02:37 -0800 (PST) Subject: [C++-sig] Boost.Python + OpenGL segmentation faults In-Reply-To: <9bdc02d30911191354v2603db13i15c5030aa16b076c@mail.gmail.com> References: <9bdc02d30911191354v2603db13i15c5030aa16b076c@mail.gmail.com> Message-ID: <525801.22167.qm@web111409.mail.gq1.yahoo.com> Just a remark: We are heavily using Boost.Python extensions together with OpenGL, and have so for years. It works for us without problems under Windows, Mac OS X, Linux. We are not using PyOpenGL, though, but have our own Boost.Python-based wrappers for the entire OpenGL library (http://cci.lbl.gov/gltbx/). Back in 2005/2006 all that worked even with PyOpenGL imported under Windows. I'd try running with valgrind to debug. ----- Original Message ---- From: Dimitri Tcaciuc To: cplusplus-sig at python.org Sent: Thu, November 19, 2009 1:54:51 PM Subject: [C++-sig] Boost.Python + OpenGL segmentation faults Hello everyone, I've recently posted a problem to SO (http://stackoverflow.com/questions/1751408/boost-python-opengl-segmentation-faults), where I haven't had much luck with answer. My problem is that if I import OpenGL python libraries before importing my B.P bindings, I get strange segmentation faults for no apparent reason. I isolated a small test case which I'm attaching below. The current boost library version I'm using is 1.37; you'll have to make a change to Makefile if you have any other version installed. I'd greatly appreciate if anyone can give some insight on what's happening. Dimitri. From eilif at gmx.de Fri Nov 20 11:50:01 2009 From: eilif at gmx.de (Eilif Mueller) Date: Fri, 20 Nov 2009 11:50:01 +0100 Subject: [C++-sig] dynamic compile and "to-Python converter ... second conversion method ignored" Message-ID: <20091120105001.305150@gmx.net> Hi, I'm using boost.python to interface with some dynamically generated C++ code, employing some scipy.weave facilities to cache the results. When I force a recompile and import, I get the following message: RuntimeWarning: to-Python converter for __randint already registered; second conversion method ignored. where __randint is the dynamically generated class. As such, the changes to the code (__randint) are not represented in the newly imported module. Is it possible to override this behaviour so that the new class replaces the registration for the old one? Thanks. cheers, Eilif -- GRATIS f?r alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01 From seefeld at sympatico.ca Fri Nov 20 14:00:56 2009 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 20 Nov 2009 08:00:56 -0500 Subject: [C++-sig] dynamic compile and "to-Python converter ... second conversion method ignored" In-Reply-To: <20091120105001.305150@gmx.net> References: <20091120105001.305150@gmx.net> Message-ID: <4B069308.8030501@sympatico.ca> On 11/20/2009 05:50 AM, Eilif Mueller wrote: > Hi, > > I'm using boost.python to interface with some dynamically generated C++ code, employing some scipy.weave facilities to cache the results. When I force a recompile and import, I get the following message: > > RuntimeWarning: to-Python converter for __randint already registered; second conversion method ignored. > > where __randint is the dynamically generated class. As such, the changes to the code (__randint) are not represented in the newly imported module. > > Is it possible to override this behaviour so that the new class replaces the registration for the old one? > I very much doubt that what you want is possible: All extension modules using boost.python share a single global 'registry' that holds type-conversion information. Thus it is impossible to have that loaded while a separate process is adding new entries to it. (It's a global symbol that is seen by all DSOs. Having two DSOs provide that symbol, but with different content, is illegal.) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From troy at resophonic.com Fri Nov 20 17:47:09 2009 From: troy at resophonic.com (troy d. straszheim) Date: Fri, 20 Nov 2009 11:47:09 -0500 Subject: [C++-sig] dynamic compile and "to-Python converter ... second conversion method ignored" In-Reply-To: <4B069308.8030501@sympatico.ca> References: <20091120105001.305150@gmx.net> <4B069308.8030501@sympatico.ca> Message-ID: <4B06C80D.8010502@resophonic.com> Stefan Seefeld wrote: > On 11/20/2009 05:50 AM, Eilif Mueller wrote: >> Hi, >> >> I'm using boost.python to interface with some dynamically generated >> C++ code, employing some scipy.weave facilities to cache the >> results. When I force a recompile and import, I get the following >> message: >> >> RuntimeWarning: to-Python converter for __randint already >> registered; second conversion method ignored. >> >> where __randint is the dynamically generated class. As such, the >> changes to the code (__randint) are not represented in the newly >> imported module. >> >> Is it possible to override this behaviour so that the new class >> replaces the registration for the old one? >> > > I very much doubt that what you want is possible: All extension > modules using boost.python share a single global 'registry' that > holds type-conversion information. Thus it is impossible to have that > loaded while a separate process is adding new entries to it. > > (It's a global symbol that is seen by all DSOs. Having two DSOs > provide that symbol, but with different content, is illegal.) > I'd like to fix this, now, it is starting to cause me pain. For instance, it also rears its ugly head if you import two libraries that both wrap std::vector, for instance if you try to use the IceTray physics analysis suite together with Paul Kunz's wonderful Hippodraw. Another situation that came up recently was that the pair inside std::map and another container of T,U (that used pair internally) collided. I think it should be possible to specify that a type's wrapper is replaceable (if you know, for instance, that two projects wrap std::pair the same way). I haven't thought about the interface much yet. Here's something I just decided against, a per-type global 'replacability' flag, that warns or not: template void replacable(bool quietly); BOOST_PYTHON_MODULE(mymod) { replacable >(true); } But this doesn't help the situation where you're trying to use two different python modules together, neither of which specifies that pairs are replacable. It seems that boost.python should expose some way to specify replacability at runtime. How about this. A type's converters are 'locked', 'verbose', and 'nonthrowing' by default (like they are now). Boost python modules expose one function def __lock__(type, lock, verbose, throw): ... at module scope that allows you to specify whether - types are locked down or replacable - collisions should be complained about to stdout - collision events should throw e.g. >>> import M1 # silently replace T's >>> M1.__lock__(M1.T, lock=False, verbose=False, throw=False) # verbosely refuse replacement of tees >>> M1.__lock__(M1.T, lock=True, verbose=True, throw=False) And that this __lock__ function would also be available on the C++ side for setting defaults: BOOST_PYTHON_MODULE(M1) { class_("T") ... lock(false, false, false); // silent, replacable, nonthrowing } Just a sketch. Thoughts? -t From seefeld at sympatico.ca Fri Nov 20 18:29:27 2009 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 20 Nov 2009 12:29:27 -0500 Subject: [C++-sig] dynamic compile and "to-Python converter ... second conversion method ignored" In-Reply-To: <4B06C80D.8010502@resophonic.com> References: <20091120105001.305150@gmx.net> <4B069308.8030501@sympatico.ca> <4B06C80D.8010502@resophonic.com> Message-ID: <4B06D1F7.10501@sympatico.ca> On 11/20/2009 11:47 AM, troy d. straszheim wrote: > Stefan Seefeld wrote: >> >> (It's a global symbol that is seen by all DSOs. Having two DSOs >> provide that symbol, but with different content, is illegal.) >> > > I'd like to fix this, now, it is starting to cause me pain. > > For instance, it also rears its ugly head if you import two libraries > that both wrap std::vector, for instance if you try to use the > IceTray physics analysis suite together with Paul Kunz's wonderful > Hippodraw. > > Another situation that came up recently was that the pair inside > std::map and another container of T,U (that used pair > internally) collided. I think it should be possible to specify that a > type's wrapper is replaceable (if you know, for instance, that two > projects wrap std::pair the same way). > > I haven't thought about the interface much yet. It's an interesting thought, but it raises a lot of questions, and opens a huge can of worms. (Welcome in the world of 'DLL hell' !) I'm not sure what angle to attack this problem from. In particular, I believe before thinking about interface issues, we should clarify the problem domain, and in particular, what semantics we want. For example, what does 'replaceable' mean ? Does it mean two bindings are identical or equivalent ? Does it merely mean the user allows the mapping to change ? (This would lead into undefined behavior, if you don't carefully control in which order things are loaded / initialized.) A different approach might be to avoid collisions by 'scoping' conversion operators, so they are only available to specific extension modules. May a converter registry be explicitly imported into a module, during initialization ? That would involve making dependency between extension modules explicit ("module X imports converters provided by module Y"). In general, I think that would be cleanest. "Explicit is better than implicit." I'm sure David would like that. ;-) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From troy at resophonic.com Fri Nov 20 21:23:43 2009 From: troy at resophonic.com (troy d. straszheim) Date: Fri, 20 Nov 2009 15:23:43 -0500 Subject: [C++-sig] dynamic compile and "to-Python converter ... second conversion method ignored" In-Reply-To: <4B06D1F7.10501@sympatico.ca> References: <20091120105001.305150@gmx.net> <4B069308.8030501@sympatico.ca> <4B06C80D.8010502@resophonic.com> <4B06D1F7.10501@sympatico.ca> Message-ID: <4B06FACF.6020002@resophonic.com> Stefan Seefeld wrote: > > It's an interesting thought, but it raises a lot of questions, and opens > a huge can of worms. (Welcome in the world of 'DLL hell' !) > I'm not sure what angle to attack this problem from. In particular, I > believe before thinking about interface issues, we should clarify the > problem domain, and in particular, what semantics we want. I thought this was obvious, sorry. The 'worst' and most common scenario is, precisely put: more than one python extension module over which we have no control has wrapped type T. We need to use them simultaneously. For instance, here are two modules that both wrap vector. With assertions enabled, this happens: >>> import converter_collisions1_ext >>> import converter_collisions2_ext python: /home/troy/Projects/boost/src/libs/python/src/converter/registry.cpp:212: void boost::python::converter::registry::insert(PyObject* (*)(const void*), boost::python::type_info, const PyTypeObject* (*)()): Assertion `slot->m_to_python == 0' failed. zsh: abort python delightfully, without assertions there is only a warning: >>> import converter_collisions1_ext >>> import converter_collisions2_ext __main__:1: RuntimeWarning: to-Python converter for std::vector > already registered; second conversion method ignored. So one resolution is to remove the 'assert' that triggers this crash and leave the semantics as-is: second and later converter registrations are warned about and ignored. I'm simply suggesting we give the user more control over this behavior, because in a number of situations (it is up to the user to determine what they are) such control would be useful. > For example, what does 'replaceable' mean ? Does it mean two bindings > are identical or equivalent ? No it means only that the behavior in the situation above is modified. > Does it merely mean the user allows the > mapping to change? (This would lead into undefined behavior, if you > don't carefully control in which order things are loaded / initialized.) UB as in what happens when there is an ODR violation? How? > A different approach might be to avoid collisions by 'scoping' > conversion operators, so they are only available to specific extension > modules. Interesting idea. As I interpret "scoping conversion operators", I'm not sure the cost in runtime and complexity would be worth it, OTOH I don't have much to go on here. > May a converter registry be explicitly imported into a module, > during initialization? Now I'm sure I've interpreted "scoping conversion operators" differently than you meant it. Can you clarify? > That would involve making dependency between extension modules explicit > ("module X imports converters provided by module Y"). But, you can do that now; "module X imports the converter for T provided by module Y" just means "module X doesn't wrap T and won't run without Y", right? -t From seefeld at sympatico.ca Fri Nov 20 21:44:19 2009 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 20 Nov 2009 15:44:19 -0500 Subject: [C++-sig] dynamic compile and "to-Python converter ... second conversion method ignored" In-Reply-To: <4B06FACF.6020002@resophonic.com> References: <20091120105001.305150@gmx.net> <4B069308.8030501@sympatico.ca> <4B06C80D.8010502@resophonic.com> <4B06D1F7.10501@sympatico.ca> <4B06FACF.6020002@resophonic.com> Message-ID: <4B06FFA3.3040401@sympatico.ca> On 11/20/2009 03:23 PM, troy d. straszheim wrote: > >> For example, what does 'replaceable' mean ? Does it mean two bindings >> are identical or equivalent ? > > No it means only that the behavior in the situation above is modified. > >> Does it merely mean the user allows the mapping to change? (This >> would lead into undefined behavior, if you don't carefully control in >> which order things are loaded / initialized.) > > UB as in what happens when there is an ODR violation? How? I'm only talking about user-visible behavior, and assuming that it could be implemented in a way that would avoid ODR violations. (Whether that's actually possible is a different matter. I'm only talking about semantics.) > >> A different approach might be to avoid collisions by 'scoping' >> conversion operators, so they are only available to specific >> extension modules. > > Interesting idea. As I interpret "scoping conversion operators", I'm > not sure the cost in runtime and complexity would be worth it, OTOH I > don't have much to go on here. > > > May a converter registry be explicitly imported into a module, >> during initialization? > > Now I'm sure I've interpreted "scoping conversion operators" > differently than you meant it. Can you clarify? This is mostly only hand-waving, sorry. I was wondering whether it was possible to make the registry not an application-global object, but a per-module object. And if that's possible (and thus, conversions are by default only available to the defining module), would it be possible to chain them explicitly, to get both, well-defined semantics, as well as no ODR violations. I haven't thought about whether this has to involve runtime overhead, and if so, how much. At least without the chaining it may actually be cheap. (Just hide the registry symbol from other modules.) And even with chaining, this is merely a lookup in a chain of converter maps, which likely can be reordered / optimized at the point where one registry is explicitly added to another. Still, this is just a rough idea. I don't have any implementation ideas yet. > >> That would involve making dependency between extension modules >> explicit ("module X imports converters provided by module Y"). > > But, you can do that now; "module X imports the converter for T > provided by module Y" just means "module X doesn't wrap T and won't > run without Y", right? Yes. I'm talking about the case where module X and module Y provide a converter for T, so the user who wishes to use X and Y needs to specify the desired behavior, which could be "each uses its own" or "either of the two will be used globally". Stefan -- ...ich hab' noch einen Koffer in Berlin... From mg262 at cam.ac.uk Fri Nov 20 22:34:17 2009 From: mg262 at cam.ac.uk (Mohan Ganesalingam) Date: 20 Nov 2009 21:34:17 +0000 Subject: [C++-sig] Boost.Python + OpenGL segmentation faults In-Reply-To: <4B054E97.2060101@sympatico.ca> References: <4B0542BF.4040000@sympatico.ca> <4B054E97.2060101@sympatico.ca> Message-ID: > Back in 2005/2006 all that worked even with PyOpenGL imported under > Windows. I'm using boost 1.39 to embed Python 2.6 + PyOpenGL 3 in MSVC 2008 under Windows XP right now, without any difficulties. I'm not using PyOpenGL directly from C++, but have all the relevant code in a file pygl.py, and run exec("from pygl import *"); in my C++ code. I'm not sure if any of this is helpful, but you're very welcome to the code samples if they would be of any use. Best wishes, Mohan From roman.yakovenko at gmail.com Sat Nov 21 20:05:04 2009 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sat, 21 Nov 2009 21:05:04 +0200 Subject: [C++-sig] dynamic compile and "to-Python converter ... second conversion method ignored" In-Reply-To: <4B06FFA3.3040401@sympatico.ca> References: <20091120105001.305150@gmx.net> <4B069308.8030501@sympatico.ca> <4B06C80D.8010502@resophonic.com> <4B06D1F7.10501@sympatico.ca> <4B06FACF.6020002@resophonic.com> <4B06FFA3.3040401@sympatico.ca> Message-ID: <7465b6170911211105o18a274e5j2c2e908e2b07b429@mail.gmail.com> On Fri, Nov 20, 2009 at 10:44 PM, Stefan Seefeld wrote: >> Now I'm sure I've interpreted "scoping conversion operators" differently >> than you meant it. ?Can you clarify? > > This is mostly only hand-waving, sorry. I was wondering whether it was > possible to make the registry not an application-global object, but a > per-module object. And if that's possible (and thus, conversions are by > default only available to the defining module), would it be possible to > chain them explicitly, to get both, well-defined semantics, as well as no > ODR violations. > > I haven't thought about whether this has to involve runtime overhead, and if > so, how much. At least without the chaining it may actually be cheap. (Just > hide the registry symbol from other modules.) > And even with chaining, this is merely a lookup in a chain of converter > maps, which likely can be reordered / optimized at the point where one > registry is explicitly added to another. > Still, this is just a rough idea. I don't have any implementation ideas yet. IMHO, this could be the way to go. I already meat this problem in the past: * std containers were exposed using different suites * during the testing - initially all my tests run in the same process, but after 30 tests it became very difficult to name C++ classes :-), so I was forced to run every test in its own process. The "per-module" registry could be a solution to the problem. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From trigves at yahoo.com Sun Nov 22 17:55:38 2009 From: trigves at yahoo.com (Trigve Siver) Date: Sun, 22 Nov 2009 08:55:38 -0800 (PST) Subject: [C++-sig] multithreading and interpreters Message-ID: <851160.7250.qm@web110413.mail.gq1.yahoo.com> Hi, I'm working on project where we used boost.python (embedded). In general I have the "modules" where each module is something like sandbox, is isolated from others modules and run in solo thread. I want to be able to use python (and boost python functions of course) from each module thread. Something like that each thread could have own interpreter without need to take care of GIL and like. Is it possible? I know there could be some problems with GIL and so. I've found in python C API docs Py_NewInterpreter() function which could be of help (or not?). So will this configuration work? Could be there some problems with boost.python when using multiple threads? Thanks Trigve From ma.hed at bluewin.ch Mon Nov 23 13:05:36 2009 From: ma.hed at bluewin.ch (Martin Hediger) Date: Mon, 23 Nov 2009 13:05:36 +0100 Subject: [C++-sig] Getting Boost.Python Working on MacOS X Message-ID: <4B0A7A90.5080008@bluewin.ch> Dear All I have been trying to get Boost.Python working on my MBP with Mac OS 10.5, since I need it to compile the chemical modeling software "AVOGADRO" for modification reasons. I pretty much followed the instructions on both the Boost, Getting Started guide and the Boost.Python Introduction. I managed to get the "Lambda" and the "Regex" examples working perfectly, but unfortunatly not the python "embedding" and "extending" examples, neither as well the "hello world". Apparently, no embedding/extending module is being built for me to import in the python-test script following the build. My setup is as follows: Boost root: /usr/local/boost_1_40_0/ (so I call every command with sudo). The lambda and regex example files (.cpp) were perfectly compiling in a directory ~/programming_tests/, so my compiler is very likely to work. In order to do so, I issued the command: cpp+ -I example.cpp -o example /usr/local/boost_1_40_0/lib/libboost_regex-mt.a for some (possibly to the overall problem related) reason, it does not work with the dynamic library .dylib. What would be the basic procedure to get the boost.python examples running on Mac OS X? Thanks a lot for any suggestions. From Matthew.Scouten at tradingtechnologies.com Tue Nov 24 15:57:37 2009 From: Matthew.Scouten at tradingtechnologies.com (Matthew Scouten (TT)) Date: Tue, 24 Nov 2009 08:57:37 -0600 Subject: [C++-sig] multithreading and interpreters In-Reply-To: <851160.7250.qm@web110413.mail.gq1.yahoo.com> References: <851160.7250.qm@web110413.mail.gq1.yahoo.com> Message-ID: <32490DFF7774554A85D65D23A9F0F9380BD9FC58@chiex01> Multiple threads is do-able, but you have to manage the GIL manually. Multiple interpreters is more difficult. I think that it has been done before by someone who massively BP, but I have never tried it. -----Original Message----- From: cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org [mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at py thon.org] On Behalf Of Trigve Siver Sent: Sunday, November 22, 2009 10:56 AM To: cplusplus-sig at python.org Subject: [C++-sig] multithreading and interpreters Hi, I'm working on project where we used boost.python (embedded). In general I have the "modules" where each module is something like sandbox, is isolated from others modules and run in solo thread. I want to be able to use python (and boost python functions of course) from each module thread. Something like that each thread could have own interpreter without need to take care of GIL and like. Is it possible? I know there could be some problems with GIL and so. I've found in python C API docs Py_NewInterpreter() function which could be of help (or not?). So will this configuration work? Could be there some problems with boost.python when using multiple threads? Thanks Trigve _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig From Matthew.Scouten at tradingtechnologies.com Tue Nov 24 16:11:36 2009 From: Matthew.Scouten at tradingtechnologies.com (Matthew Scouten (TT)) Date: Tue, 24 Nov 2009 09:11:36 -0600 Subject: [C++-sig] multithreading and interpreters In-Reply-To: <32490DFF7774554A85D65D23A9F0F9380BD9FC58@chiex01> References: <851160.7250.qm@web110413.mail.gq1.yahoo.com> <32490DFF7774554A85D65D23A9F0F9380BD9FC58@chiex01> Message-ID: <32490DFF7774554A85D65D23A9F0F9380BD9FC77@chiex01> I'm sorry, I meant massively *patched* BP -----Original Message----- From: cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org [mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at py thon.org] On Behalf Of Matthew Scouten (TT) Sent: Tuesday, November 24, 2009 8:58 AM To: Development of Python/C++ integration Subject: Re: [C++-sig] multithreading and interpreters Multiple threads is do-able, but you have to manage the GIL manually. Multiple interpreters is more difficult. I think that it has been done before by someone who massively BP, but I have never tried it. -----Original Message----- From: cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org [mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at py thon.org] On Behalf Of Trigve Siver Sent: Sunday, November 22, 2009 10:56 AM To: cplusplus-sig at python.org Subject: [C++-sig] multithreading and interpreters Hi, I'm working on project where we used boost.python (embedded). In general I have the "modules" where each module is something like sandbox, is isolated from others modules and run in solo thread. I want to be able to use python (and boost python functions of course) from each module thread. Something like that each thread could have own interpreter without need to take care of GIL and like. Is it possible? I know there could be some problems with GIL and so. I've found in python C API docs Py_NewInterpreter() function which could be of help (or not?). So will this configuration work? Could be there some problems with boost.python when using multiple threads? Thanks Trigve _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig From sipickles at googlemail.com Wed Nov 25 14:11:06 2009 From: sipickles at googlemail.com (Simon Pickles) Date: Wed, 25 Nov 2009 13:11:06 +0000 Subject: [C++-sig] building modules with MSVC Message-ID: <4B0D2CEA.1080409@googlemail.com> Hi, Is it possible to build boost::python modules using MSVC as opposed to bjam? I'd like MSVC to output a pyd of course. Thanks Simon From jfuchs at esigma-technology.com Wed Nov 25 17:16:58 2009 From: jfuchs at esigma-technology.com (Jahn Fuchs) Date: Wed, 25 Nov 2009 17:16:58 +0100 Subject: [C++-sig] boost python and weak_ptr from a shared_ptr argument In-Reply-To: References: Message-ID: <4B0D587A.7040004@esigma-technology.com> Hello boost python users, I really like boost python but I stumbled in a problem I don't know how to solve, maybe you have an idea: I have a class KnowledgeBase that holds an shared_ptr to an KBObject and also a weak_ptr to an KBObject. If I set the shared_ptr with the set_shared_ptr function it works fine, but it doesn't work with the set_weak_ptr function (which takes a shared_ptr as an argument) ... see the following C++ code, python export code and python test code to see what i mean. I made a simple example which I also tested and it shows the problem I have. Thank you in advance. jahn. // C++ CODE: ------------------------------------------------------------------------- class KBObject { public: std::string name; }; class KnowledgeBase { public: boost::shared_ptr obj_shared_ptr; boost::weak_ptr obj_weak_ptr; void set_weak_ptr(boost::shared_ptr obj_ptr) { obj_weak_ptr = boost::weak_ptr(obj_ptr); }; void set_shared_ptr(boost::shared_ptr obj_ptr) { obj_shared_ptr = obj_ptr; }; boost::shared_ptr get_shared_ptr() { return obj_shared_ptr; }; boost::shared_ptr get_weak_ptr() { return obj_weak_ptr.lock(); }; void print_weak_ptr() { if (boost::shared_ptr obj_ptr = obj_weak_ptr.lock()) std::cout << obj_ptr.get()->name << std::endl; else std::cout << "NO OBJECT" << std::endl; }; void print_shared_ptr() { if (obj_shared_ptr.get()!=0) std::cout << obj_shared_ptr.get()->name << std::endl; else std::cout << "NO OBJECT" << std::endl; }; }; // PYTHON MODULE EXPORT ---------------------------------------------- BOOST_PYTHON_MODULE(kbpython) { using namespace boost::python; class_ >("KBObject") .def_readwrite("name", &KBObject::name) ; class_("KnowledgeBase") .def("set_shared_ptr", &KnowledgeBase::set_shared_ptr) .def("set_weak_ptr", &KnowledgeBase::set_weak_ptr) .def("get_shared_ptr", &KnowledgeBase::get_shared_ptr) .def("get_weak_ptr", &KnowledgeBase::get_weak_ptr) .def("print_weak_ptr", &KnowledgeBase::print_weak_ptr) .def("print_shared_ptr", &KnowledgeBase::print_shared_ptr) ; } // PYTHON TEST PROGRAM ----------------------------------------------- #!/usr/bin/env python import kbpython kb = kbpython.KnowledgeBase() obj = kbpython.KBObject() obj.name = "myObject" kb.set_shared_ptr(obj) kb.print_shared_ptr() obj_2 = kb.get_shared_ptr() obj_2.name = "change_name" kb.print_shared_ptr() kb.set_weak_ptr(obj_2) kb.print_weak_ptr() ------------------------------------------------------------------------ From brokenn at gmail.com Wed Nov 25 19:41:51 2009 From: brokenn at gmail.com (Brian O'Kennedy) Date: Wed, 25 Nov 2009 18:41:51 +0000 Subject: [C++-sig] building modules with MSVC In-Reply-To: <4B0D2CEA.1080409@googlemail.com> References: <4B0D2CEA.1080409@googlemail.com> Message-ID: It's certainly possible. * Create a project in Visual Studio, set the output type to DLL. * Go to Linker->Output and name the DLL as mymodule.pyd (pyd's are just DLLs on Windows). * Set the Additional Library paths to where you keep your boost python stub lib for linking. * You might need to specify the folder to the python lib too. * Including the boost python headers will automatically cause the stub lib to be linked against. This is from memory so might be missing out a step or two. Hope this helps, Brian 2009/11/25 Simon Pickles > Hi, > > Is it possible to build boost::python modules using MSVC as opposed to > bjam? > > I'd like MSVC to output a pyd of course. > > Thanks > > Simon > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rwgk at yahoo.com Wed Nov 25 21:13:50 2009 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Wed, 25 Nov 2009 12:13:50 -0800 (PST) Subject: [C++-sig] boost python and weak_ptr from a shared_ptr argument In-Reply-To: <4B0D587A.7040004@esigma-technology.com> References: <4B0D587A.7040004@esigma-technology.com> Message-ID: <786610.71356.qm@web111402.mail.gq1.yahoo.com> This is a known problem. Boost.Python builds shared_ptr instances when calling your functions, using a custom deleter. I think internally it just uses regular Python reference counting to manage the lifetime of your KBObject. Unfortunately I don't have a nice suggestion for you. A few years ago I worked around the issue in this way: struct KBOject_data { std::string name; }; struct KBOject { boost::shared_ptr data; }; I.e. by hiding the shared_ptr from Boost.Python. It isn't pretty. Maybe someone else has a better solution? (I've always been wondering if enable_shared_from_this could make this nice.) ----- Original Message ---- From: Jahn Fuchs To: cplusplus-sig at python.org Sent: Wed, November 25, 2009 8:16:58 AM Subject: [C++-sig] boost python and weak_ptr from a shared_ptr argument Hello boost python users, I really like boost python but I stumbled in a problem I don't know how to solve, maybe you have an idea: I have a class KnowledgeBase that holds an shared_ptr to an KBObject and also a weak_ptr to an KBObject. If I set the shared_ptr with the set_shared_ptr function it works fine, but it doesn't work with the set_weak_ptr function (which takes a shared_ptr as an argument) ... see the following C++ code, python export code and python test code to see what i mean. I made a simple example which I also tested and it shows the problem I have. Thank you in advance. jahn. From rfritz333 at gmail.com Thu Nov 26 00:44:52 2009 From: rfritz333 at gmail.com (Randolph Fritz) Date: Wed, 25 Nov 2009 23:44:52 +0000 (UTC) Subject: [C++-sig] Getting Boost.Python Working on MacOS X References: <4B0A7A90.5080008@bluewin.ch> Message-ID: On 2009-11-23, Martin Hediger wrote: > > I have been trying to get Boost.Python working on my MBP with Mac OS > 10.5, since I need it to compile the chemical modeling software > "AVOGADRO" for modification reasons. There's something wrong with the boost.python test script on Mac OS X; see . That's probably what's making problems for you. Boost.python does in fact work on Mac, despite the test script problem; here's what I did to make it go: -- Randolph Fritz design machine group, architecture department, university of washington rfritz at u.washington.edu -or- rfritz333 at gmail.com From sohail at taggedtype.net Mon Nov 30 01:50:13 2009 From: sohail at taggedtype.net (Sohail Somani) Date: Sun, 29 Nov 2009 19:50:13 -0500 Subject: [C++-sig] Embedding Python - creating Python objects from C++ Message-ID: I have a class in C++ as follows: class Base { virtual ~Base() /* ideally like = 0 but doesn't seem to work */; }; I export this class to Python: class_("Base",no_init); The user is expected to create a derived class: class UserClass(Base): ... stuff here ... What I want to do then (in C++) is create instances of this Python class that are stored using a shared pointer: // constructs a new instance of UserClass in Python using magic // but uses shared_ptr to keep alive shared_ptr base(/* magic here */); I understand that I can do something like module.attr("UserClass")() to create an instance but that will return an object instance which doesn't seem like it would play nice if I extracted a Base pointer from it. So does anyone know how to implement the above referenced magic? It sounds like it should be obvious, but I'm not sure. Thanks, and sorry if this is a newb question! Sohail From sohail at taggedtype.net Mon Nov 30 20:09:48 2009 From: sohail at taggedtype.net (Sohail Somani) Date: Mon, 30 Nov 2009 14:09:48 -0500 Subject: [C++-sig] Embedding Python - creating Python objects from C++ In-Reply-To: References: Message-ID: On 29/11/09 7:50 PM, Sohail Somani wrote: > // constructs a new instance of UserClass in Python using magic > // but uses shared_ptr to keep alive > shared_ptr base(/* magic here */); This seemed to work: // Boost Python/C++ class_ >("Base") .def(...) ; # Python class UserClass(Base): ... // Back in C++ object = module["UserClass"] shared_ptr ptr = extract >(object()); I'll find out soon enough if it works like I'm expecting ;-) -- Sohail Somani http://uint32t.blogspot.com