From hapopen at gmail.com Sun Sep 2 15:55:35 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 2 Sep 2012 21:55:35 +0800 Subject: [C++-sig] two question from boost.python Message-ID: The first question: The class_ class has four parameters,class template class_. I don't understand the parameters of Bases and HeldType.How to use the parameters. this doc: http://www.boost.org/doc/libs/1_51_0/libs/python/doc/v2/class.html The second question: I have to make a python function as callback of a c++ function.How to do?Where can I find some examples?? From hapopen at gmail.com Sun Sep 2 17:00:38 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 2 Sep 2012 23:00:38 +0800 Subject: [C++-sig] How to make a python function as callback of a c++ function Message-ID: I have to make a python function as callback of a c++ function.How to do?Where can I find some examples?? I want to use boost.python. I want my code look something like this: In C++ : typedef void (*MyCallback_t) (CallbackInfo); > class MyClass > {... > void setcallback(MyCallback_t cb); > ... > } And to use it in python : import mylib class mypythonclass: def myCallback(mylib_CallbackInfo): .... def mywork(): t = mylib.MyClass() t.setcallback(self.myCallback) -------------- next part -------------- An HTML attachment was scrubbed... URL: From Holger.Joukl at LBBW.de Mon Sep 3 10:33:54 2012 From: Holger.Joukl at LBBW.de (Holger Joukl) Date: Mon, 3 Sep 2012 10:33:54 +0200 Subject: [C++-sig] How to make a python function as callback of a c++ function In-Reply-To: References: Message-ID: Hi, "Cplusplus-sig" schrieb am 02.09.2012 17:00:38: > I have to make a python function as callback of a c++ function.How > to do?Where can I find some examples?? I want to use boost.python. > I want my code look something like this: In C++ : > ?typedef void (*MyCallback_t) (CallbackInfo); > ?class MyClass > ?{... > ? ? void setcallback(MyCallback_t cb); > ? ... > ?} > > And to use it in python : > import mylib > class mypythonclass: > ? ? def myCallback(mylib_CallbackInfo): > ? ? ? ?.... > ? ? def mywork(): > ? ? ? ? t = mylib.MyClass() > ? ? ? ? t.setcallback(self.myCallback) A function pointer can not be wrapped in a way so that an arbitrary python function can be used as a (callback) argument. This is due to C/C++ restrictions, see http://www.boost.org/doc/libs/1_51_0/libs/python/doc/v2/faq.html#funcptr Basically, a C/C++ function pointer needs to exist at compile time and doesn't have state while Python functions are stateful objects that come to life at runtime. If you have control over the C++ code you could change the signature to take a boost::python::object as an argument and call that, something like //Instead of actually wrapping these... char const * call(char const * (*func)()) { std::cout << "--> call" << std::endl; return (*func)(); } char const * call_int(char const * (*func)(int), int i) { std::cout << "--> call_int" << std::endl; return (*func)(i); } // ...we expose these *instead* - note that the original call()/call_int() aren't used at all namespace bp = boost::python; char const * call_wrap(bp::object &func) { bp::object result = func(); return bp::extract(result); } char const * call_int_wrap(bp::object &func, int i) { bp::object result = func(i); return bp::extract(result); } BOOST_PYTHON_MODULE(func_ptr) { bp::def("call", &call_wrap); bp::def("call_int", &call_int_wrap); }; See also http://boost.2283326.n4.nabble.com/Boost-Python-C-struct-with-a-function-pointer-td2699880.html for some hints/discussion. Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart From legordian at gmx.net Tue Sep 4 09:04:10 2012 From: legordian at gmx.net (Charly Bicker) Date: Tue, 04 Sep 2012 09:04:10 +0200 Subject: [C++-sig] Inheriting static overloaded properties Message-ID: <20120904070410.58300@gmx.net> Dear all, I am working in the following scenario: I have a class "Derived" which inherits from "Base" and wrappers for both. Both have their own static debug flag and getters and setters for it (see code at the end of the mail). I try to use ".add_static_property" to make the debug flags visible in python, however if I do this for both Base and Derived, I get: python -c "import testInheritance" Traceback (most recent call last): File "", line 1, in Boost.Python.ArgumentError: Python argument types in None.None(Boost.Python.StaticProperty) did not match C++ signature: None(bool) Already when trying to import the module. If I do not add the property for Derived, I can import the module but then using debug on the Derived set the debug for Base instead. What am I doing wrong? Is this a bug? Does anybody know a solution? Any help would be greatly appreciated! Best regards, Karl Bicker PS: I would also like to make you aware of a stackoverflow question of mine: http://stackoverflow.com/questions/12202763/boost-python-inheriting-from-wrapped-classes Code: ---------------------------------------------------------------------------- #include namespace bp = boost::python; struct Base { Base() { }; static bool debug() { return _debug; }; static void setDebug(const bool debug = true) { _debug = debug; }; private: static bool _debug; }; struct BaseWrapper : Base, bp::wrapper { BaseWrapper() : Base(), bp::wrapper() { }; BaseWrapper(const Base& base) : Base(base), bp::wrapper() { }; }; struct Derived : Base { Derived() : Base() { }; static bool debug() { return _debug; }; static void setDebug(const bool debug = true) { _debug = debug; }; private: static bool _debug; }; struct DerivedWrapper : Derived, bp::wrapper { DerivedWrapper() : Derived(), bp::wrapper() { }; DerivedWrapper(const Derived derived) : Derived(derived), bp::wrapper() { }; }; bool Base::_debug = false; bool Derived::_debug = false; BOOST_PYTHON_MODULE(testInheritance){ bp::class_("Base") .add_static_property("debug", &BaseWrapper::debug, &BaseWrapper::setDebug); bp::class_ >("Derived") .add_static_property("debug", &DerivedWrapper::debug, &DerivedWrapper::setDebug); } From brandsmeier at gmx.de Tue Sep 4 10:13:07 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Tue, 4 Sep 2012 10:13:07 +0200 Subject: [C++-sig] Inheriting static overloaded properties In-Reply-To: <20120904070410.58300@gmx.net> References: <20120904070410.58300@gmx.net> Message-ID: Dear Karl, your problem does not occur if you do the following, bp::class_("Base") .add_static_property("debug", &Base::debug, &Base::setDebug); bp::class_ >("Derived") .add_static_property("debug2", &Derived::debug, &Derived::setDebug); Note that I called the property "debug2" in the drived class. I am not sure if this is an option for you though. What I think is the problem is easier to explain for another method, `staticmethod`, which declares that a member function is static. In the documentation it explicitly states: "Note: Attempting to invoke def(name,...) after invoking staticmethod(name) will raise a RuntimeError." http://www.boost.org/doc/libs/1_41_0/libs/python/doc/v2/class.html Boost python can at the moment not support if you declare something a static method under some name in python, and later you try to overload this function with something else in python. Your error makes me believe that the same thing is true for add_static_property. -Holger On Tue, Sep 4, 2012 at 9:04 AM, Charly Bicker wrote: > Dear all, > > I am working in the following scenario: I have a class "Derived" which inherits from "Base" and wrappers for both. Both have their own static debug flag and getters and setters for it (see code at the end of the mail). I try to use ".add_static_property" to make the debug flags visible in python, however if I do this for both Base and Derived, I get: > > python -c "import testInheritance" > Traceback (most recent call last): > File "", line 1, in > Boost.Python.ArgumentError: Python argument types in > None.None(Boost.Python.StaticProperty) > did not match C++ signature: > None(bool) > > Already when trying to import the module. If I do not add the property for Derived, I can import the module but then using debug on the Derived set the debug for Base instead. What am I doing wrong? Is this a bug? Does anybody know a solution? Any help would be greatly appreciated! > > Best regards, > Karl Bicker > > PS: I would also like to make you aware of a stackoverflow question of mine: http://stackoverflow.com/questions/12202763/boost-python-inheriting-from-wrapped-classes > > Code: > ---------------------------------------------------------------------------- > #include > > namespace bp = boost::python; > > struct Base { > > Base() { }; > > static bool debug() { return _debug; }; > static void setDebug(const bool debug = true) { _debug = debug; }; > > private: > static bool _debug; > > }; > > struct BaseWrapper : Base, > bp::wrapper > { > > BaseWrapper() : > Base(), > bp::wrapper() { }; > > BaseWrapper(const Base& base) : > Base(base), > bp::wrapper() { }; > > }; > > struct Derived : Base > { > > Derived() : Base() { }; > > static bool debug() { return _debug; }; > static void setDebug(const bool debug = true) { _debug = debug; }; > > private: > static bool _debug; > > > }; > > struct DerivedWrapper : Derived, > bp::wrapper > { > > DerivedWrapper() : > Derived(), > bp::wrapper() { }; > > DerivedWrapper(const Derived derived) : > Derived(derived), > bp::wrapper() { }; > > }; > > bool Base::_debug = false; > bool Derived::_debug = false; > > BOOST_PYTHON_MODULE(testInheritance){ > > bp::class_("Base") > .add_static_property("debug", &BaseWrapper::debug, &BaseWrapper::setDebug); > > bp::class_ >("Derived") > .add_static_property("debug", &DerivedWrapper::debug, &DerivedWrapper::setDebug); > > } > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From legordian at gmx.net Tue Sep 4 19:40:09 2012 From: legordian at gmx.net (Charly Bicker) Date: Tue, 04 Sep 2012 19:40:09 +0200 Subject: [C++-sig] More inheritance problems Message-ID: <20120904174009.175350@gmx.net> Dear all, first of all thanks to Holger for pointing me to a workaround with the add_static_property problem! Unfortunately, I already ran into the next problem, which seems very weird to me: I have again my old friends Base and Derived, with the obvious inheritance (source file attached). Now, there is a third class, named "Third", which has as a data member a boost::shared_ptr which is initialized in the constructor. For this reason, the constructor takes such a pointer as an argument. Now, the point where stuff goes wrong is this: the argument to the constructor has as a default value a NULL-boost::shared_ptr. The behavior now works as expected if the argument is provided in python: >>> import testInheritance >>> t = testInheritance.Third(testInheritance.Derived()) >>> t.get_base().name() 'Derived' >>> However, this fails if one does not provide an argument: >>> t = testInheritance.Third() >>> t.get_base().name() Traceback (most recent call last): File "", line 1, in Boost.Python.ArgumentError: Python argument types in Derived.name(Derived) did not match C++ signature: name(DerivedWrapper {lvalue}) name(DerivedWrapper {lvalue}) >>> I cannot wrap my head around this, can anybody point me to what I am doing wrong? I would be very grateful! Best regards, Karl Bicker -------------- next part -------------- A non-text attachment was scrubbed... Name: testInheritance.cc Type: text/x-c++src Size: 3124 bytes Desc: not available URL: From brandsmeier at gmx.de Tue Sep 4 20:14:57 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Tue, 4 Sep 2012 20:14:57 +0200 Subject: [C++-sig] More inheritance problems In-Reply-To: <20120904174009.175350@gmx.net> References: <20120904174009.175350@gmx.net> Message-ID: Dear Karl, the problem is fixed if you do the following: bp::class_("Base", bp::no_init) .def("do_stuff", bp::pure_virtual(&BaseWrapper::do_stuff)) .def("name", &BaseWrapper::name, &BaseWrapper::default_name) .def("name", &Base::name) ; bp::class_ >("Derived") .def("do_stuff", &DerivedWrapper::do_stuff, &DerivedWrapper::default_do_stuff) .def("name", &DerivedWrapper::name, &DerivedWrapper::default_name) .def("name", &Derived::name) ; Note the two added definitions of `name`. Why is that needed? >>> t = testInheritance.Third(testInheritance.Derived()) her in reality an instance of DerivedWrapper is created, and then passed to C++. When you then do `t.get_base()` and instance of DerivedWrapper will be returned (there is actually some "magic" going on that boost python can associate your `boost::shared_ptr` with the PyObject that is a DerivedWrapper). Because that object is a `DerivedWrapper` the `.name` calls DerivedWrapper::name. In your second example the object is create with the call `_base = boost::shared_ptr(new Derived());`. This class is no subclass of `DerivedWrapper` nor `BaseWrapper`, so you can not call a member function of the wrappers. That is why you get the error. By exporting .def("name", &Base::name) you are able to call name if you have no Wrapper. The reason why you have to do .def("name", &DerivedWrapper::name, &DerivedWrapper::default_name) .def("name", &Derived::name) in the derived class is that the first definition overrides both definitions for `&BaseWrapper::name` and `&Base::name` -Holger On Tue, Sep 4, 2012 at 7:40 PM, Charly Bicker wrote: > Dear all, > > first of all thanks to Holger for pointing me to a workaround with the add_static_property problem! > > Unfortunately, I already ran into the next problem, which seems very weird to me: > > I have again my old friends Base and Derived, with the obvious inheritance (source file attached). Now, there is a third class, named "Third", which has as a data member a boost::shared_ptr which is initialized in the constructor. For this reason, the constructor takes such a pointer as an argument. > > Now, the point where stuff goes wrong is this: the argument to the constructor has as a default value a NULL-boost::shared_ptr. The behavior now works as expected if the argument is provided in python: > >>>> import testInheritance >>>> t = testInheritance.Third(testInheritance.Derived()) >>>> t.get_base().name() > 'Derived' >>>> > > However, this fails if one does not provide an argument: > >>>> t = testInheritance.Third() >>>> t.get_base().name() > Traceback (most recent call last): > File "", line 1, in > Boost.Python.ArgumentError: Python argument types in > Derived.name(Derived) > did not match C++ signature: > name(DerivedWrapper {lvalue}) > name(DerivedWrapper {lvalue}) >>>> > > I cannot wrap my head around this, can anybody point me to what I am doing wrong? I would be very grateful! > > Best regards, > Karl Bicker > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From legordian at gmx.net Wed Sep 5 09:34:52 2012 From: legordian at gmx.net (Charly Bicker) Date: Wed, 05 Sep 2012 09:34:52 +0200 Subject: [C++-sig] More inheritance problems Message-ID: <20120905073452.85450@gmx.net> Dear Holger, thank you for your reply! > Why is that needed? > >>> t = testInheritance.Third(testInheritance.Derived()) > her in reality an instance of DerivedWrapper is created, and then > passed to C++. When you then do `t.get_base()` and instance of > DerivedWrapper will be returned (there is actually some "magic" going > on that boost python can associate your `boost::shared_ptr` > with > the PyObject that is a DerivedWrapper). Because that object is a > `DerivedWrapper` the `.name` calls DerivedWrapper::name. > > In your second example the object is create with the call `_base = > boost::shared_ptr(new Derived());`. This class is no subclass > of > `DerivedWrapper` nor `BaseWrapper`, so you can not call a member > function of the wrappers. That is why you get the error. I see. I was working under the assumption, that the boost python magic included the treatment of classes Derived as their DerivedWrapper. Unfortunately, this leads me to another problem. In my simple example, the problem is fixed by just adding another function definition. However, for various reasons, some of my wrappers do more than just handle the function overloading: they convert certain objects to and from python. The conversions in question cannot be done with the usual converter classes, due to the use of an external C++ library with python bindings which is somewhat special (and might warrant another mail at some point). Now, wrapping like this with the behavior you describe will of course have disadvantageous effects because the conversions will only be done if the object was instantiated from python. Is there a way around this? Can I convince boost python to treat (i.e. convert) all instances of Derived as DerivedWrapper? If the answer is no, then that would obviously mean that the wrappers must not contain more than code for overloading and I would have solve the problems with the converters some other way. Best regards, Karl From herzog at mpi-cbg.de Wed Sep 5 11:44:19 2012 From: herzog at mpi-cbg.de (Ronny Herzog) Date: Wed, 05 Sep 2012 11:44:19 +0200 Subject: [C++-sig] building 32 Bit and 64 Bit extensions on the same machine In-Reply-To: <20120831013410.GK622624@lenin.acc.umu.se> References: <20120831013410.GK622624@lenin.acc.umu.se> Message-ID: <50471EF3.5020004@mpi-cbg.de> Aha! Thank you. This made it work. Ronny Am 8/31/2012 3:34 AM, schrieb Lars Viklund: > On Mon, Aug 27, 2012 at 04:23:14PM +0200, Ronny Herzog wrote: >> Thank you, >> >> but this does not seem to work. If I do this with >> "bjam --address-model=64 -n" > > Note that the previous poster did not say "--address-model=64". > He said "address-model=64". It's a feature, not an option. > Features do not have a leading dashdash. > You may of course have to point out a suitable Python path anyway, but > it helps if you actually instruct the build to be 64-bit in the first > place. > From hapopen at gmail.com Fri Sep 7 18:54:55 2012 From: hapopen at gmail.com (simon zhang) Date: Sat, 8 Sep 2012 00:54:55 +0800 Subject: [C++-sig] How to compile static library with -fPIC from boost.python Message-ID: Hi all Default,libboostpython.a has be compiled without -fPIC.But I have to make a python extending and it is a dynamic library with -fPIC that link to static library. How to compile static library (libboostpython.a) with -fPIC from boost.python simon -------------- next part -------------- An HTML attachment was scrubbed... URL: From j.reid at mail.cryst.bbk.ac.uk Tue Sep 18 10:39:35 2012 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Tue, 18 Sep 2012 09:39:35 +0100 Subject: [C++-sig] using debug python with boost build Message-ID: Hi, I'm setting up my environment on a fresh machine with boost 1.51 and I can't get my python debug builds to work. I've got this in my user-config.jam using python ; using python : : /home/john/local/Python-2.7.3-dbg/bin/python : : : on ; I've built a python debug and installed it into /home/john/local/Python-2.7.3-dbg/. I've also tried with the Ubuntu python debug package. I've set on in the project I'm building but I get these error messages: capture-output /home/john/Dev/Bio/bin/Biopsy/build_svg_test.test/gcc-4.6/debug/python-debugging-on/build_svg_test ====== BEGIN OUTPUT ====== /home/john/local/Python-2.7.3-dbg/bin/python: can't open file 'python': [Errno 2] No such file or directory EXIT STATUS: 2 ====== END OUTPUT ====== LD_LIBRARY_PATH="/home/john/Dev/Bio/bin/Bio/gcc-4.6/debug/python-debugging-on:/home/john/Dev/Bio/bin/Biopsy/gcc-4.6/debug/python-debugging-on:/home/john/src/bin:/home/john/src/boost/boost_1_51_0/bin.v2/libs/chrono/build/gcc-4.6/debug/python-debugging-on/threading-multi:/home/john/src/boost/boost_1_51_0/bin.v2/libs/date_time/build/gcc-4.6/debug/python-debugging-on:/home/john/src/boost/boost_1_51_0/bin.v2/libs/filesystem/build/gcc-4.6/debug/python-debugging-on:/home/john/src/boost/boost_1_51_0/bin.v2/libs/program_options/build/gcc-4.6/debug/python-debugging-on:/home/john/src/boost/boost_1_51_0/bin.v2/libs/python/build/gcc-4.6/debug/python-debugging-on:/home/john/src/boost/boost_1_51_0/bin.v2/libs/regex/build/gcc-4.6/debug/python-debugging-on:/home/john/src/boost/boost_1_51_0/bin.v2/libs/serialization/build/gcc-4.6/debug/python-debugging-on:/home/john/src/boost/boost_1_51_0/bin.v2/libs/system/build/gcc-4.6/debug/python-debugging-on:/home/john/src/boost/boost_1_51_0/bin.v2/libs/system/bui ld/gcc-4.6/debug/python-debugging-on/threading-multi:/home/john/src/boost/boost_1_51_0/bin.v2/libs/thread/build/gcc-4.6/debug/python-debugging-on/threading-multi:/home/john/src/lib:/home/john/src/lib32:/home/john/src/lib64:/tmp/xercesc/lib:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH PYTHONPATH="/home/john/Dev/Bio/bin/Biopsy/gcc-4.6/debug/python-debugging-on" export PYTHONPATH "/home/john/local/Python-2.7.3-dbg/bin/python" "python" "python_test/test_build_svg.py" > "/home/john/Dev/Bio/bin/Biopsy/build_svg_test.test/gcc-4.6/debug/python-debugging-on/build_svg_test.output" 2>&1 status=$? echo >> "/home/john/Dev/Bio/bin/Biopsy/build_svg_test.test/gcc-4.6/debug/python-debugging-on/build_svg_test.output" echo EXIT STATUS: $status >> "/home/john/Dev/Bio/bin/Biopsy/build_svg_test.test/gcc-4.6/debug/python-debugging-on/build_svg_test.output" if test $status -eq 0 ; then cp "/home/john/Dev/Bio/bin/Biopsy/build_svg_test.test/gcc-4.6/debug/python-debugging-on/build_svg_test.output" "/home/john/Dev/Bio/bin/Biopsy/build_svg_test.test/gcc-4.6/debug/python-debugging-on/build_svg_test" fi verbose=0 if test $status -ne 0 ; then verbose=1 fi if test $verbose -eq 1 ; then echo ====== BEGIN OUTPUT ====== cat "/home/john/Dev/Bio/bin/Biopsy/build_svg_test.test/gcc-4.6/debug/python-debugging-on/build_svg_test.output" echo ====== END OUTPUT ====== fi exit $status Any ideas where I'm going wrong? Thanks, John. From giuseppe.corbelli at copanitalia.com Mon Sep 24 16:21:54 2012 From: giuseppe.corbelli at copanitalia.com (Giuseppe Corbelli) Date: Mon, 24 Sep 2012 16:21:54 +0200 Subject: [C++-sig] Weird problem with opaque pointers and pointers by reference Message-ID: <50606C82.1070804@copanitalia.com> Hi all I am facing an "interesting" problem. I have factory functions implemented in C++ that create opaque pointers. Such pointers are tossed around in python and finally freed in C++. The module is implemented using boost::python. Platform gcc 4.6.3/debian/boost 1.46, but also tried on msvc8/win7/boost 1.43 The opaque pointer refers to an IplImage, a C struct defined by OpenCV image processing library. I'm attaching a complete test case, however, in short: IplImage* CreateImage() { IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); std::cout << "Created " << img << std::endl; return img; } void ReleaseImage(IplImage*& img) { std::cout << "Release Image " << img << std::endl; ::cvReleaseImage(&img); } When I run: img = CameraConnect_Fake_Python.CreateImage() CameraConnect_Fake_Python.ReleaseImage(img) here's the output: Created 0x8e34970 Release Image 0x70 Segmentation fault For some reason that I don't grasp the reference-to-pointer is not passed around correctly. Still more interesting, if I try: struct OPAQUE {int unused;}; OPAQUE* factory() { return new OPAQUE(); } void destroyer(OPAQUE*& x) { delete (x); x = NULL; } img = CameraConnect_Fake_Python.FACTORY() CameraConnect_Fake_Python.DESTROYER(img) It runs correctly! Hope someone can help... -- Giuseppe Corbelli WASP Software Engineer, Copan Italia S.p.A Phone: +390303666318 Fax: +390302659932 E-mail: giuseppe.corbelli at copanitalia.com -------------- next part -------------- A non-text attachment was scrubbed... Name: main.cpp Type: text/x-c++src Size: 1437 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: test.py Type: text/x-python Size: 367 bytes Desc: not available URL: