From mharidev at qualcomm.com Thu Mar 1 01:39:34 2007 From: mharidev at qualcomm.com (Haridev, Meghana) Date: Wed, 28 Feb 2007 16:39:34 -0800 Subject: [C++-sig] Running Py++ unittests using mb.split_module() Message-ID: <5383A81D5EB8F14FBDB487D9B5FBEA8802379E97@NAEX11.na.qualcomm.com> Hi Folke, This is regarding the Py++ unittests: Currently all the tests (except for the split_module_* tests) are run such that all the Py++ generated boost.python bindings are written to a single file (using mb.write_module in fundamental_tester_base_t.generate_source_files() ). Regarding our development environment, we always invoke Py++ to write to multiple files (using mb.split_module). I plan to test the sanity of some internal patches that I made to Py++ file_writer: multiple_files.py by running them against the Py++ unittests (by writing the bindings to multiple files). So I modified fundamental_tester_base_t.generate_source_files() and fundamental_tester_base_t.get_source_files() appropriately, and use a flag defined in autoconfig.py to switch from using "single file" to "multiple file" generation. Most of the tests pass, but some tests fail with linking errors due to "multiple definitions" when I generate code to multiple files. For example, abstract_tester fails with the following error: square.pypp.os: In function `abstract::call(abstract::shape*)': square.pypp.cpp:(.text+0x0): multiple definition of `abstract::call(abstract::shape*)' shape.pypp.os:shape.pypp.cpp:(.text+0x0): first defined here abstract_enumerations.pypp.os: In function `abstract::call(abstract::shape*)': abstract_enumerations.pypp.cpp:(.text+0x0): multiple definition of `abstract::call(abstract::shape*)' shape.pypp.os:shape.pypp.cpp:(.text+0x0): first defined here abstract_free_functions.pypp.os: In function `abstract::call(abstract::shape*)': abstract_free_functions.pypp.cpp:(.text+0x0): multiple definition of `abstract::call(abstract::shape*)' shape.pypp.os:shape.pypp.cpp:(.text+0x0): first defined here abstract.main.os: In function `abstract::call(abstract::shape*)': abstract.main.cpp:(.text+0x0): multiple definition of `abstract::call(abstract::shape*)' shape.pypp.os:shape.pypp.cpp:(.text+0x0): first defined here collect2: ld returned 1 exit status scons: *** [abstract.so] Error 1 Some of the other tests that fail due to "multiple definitions" are: - free_operators_tester - special_operators_tester - temporary_variable_tester - unnamed_classes_tester - overloads_macro_tester I know that this can be resolved by splitting the associated "data" files to separate .hpp and .cpp files. I was wondering if anybody had tried running the Py++ unittests for multiple generated files? I think it would be a good idea to split these data files into .hpp and .cpp files so that we can switch between testing for "single file" and "multiple file" generation seamlessly. Thanks, -Meghana. -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Thu Mar 1 03:33:40 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 28 Feb 2007 21:33:40 -0500 Subject: [C++-sig] [python] Fix for boost::python::import Message-ID: <45E63B84.2040705@sympatico.ca> The attached patch fixes the boost::python::import implementation. It used to work fine for modules already ('physically') imported into the process, but not for new ones. I'd like to apply to HEAD, and would like to ask whether it is OK for the R_1_34 branch, too. The problem is that the 'import' function is new (well, it's actually two years old, but not in any official release), and no unit test exists. Thus, part of this patch is a unit test that demonstrates the failure on the existing implementation, as well as the fix. OK to commit to HEAD and R_1_34 ? Thanks, Stefan -- ...ich hab' noch einen Koffer in Berlin... -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: patch URL: From roman.yakovenko at gmail.com Thu Mar 1 07:56:29 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 1 Mar 2007 08:56:29 +0200 Subject: [C++-sig] Running Py++ unittests using mb.split_module() In-Reply-To: <5383A81D5EB8F14FBDB487D9B5FBEA8802379E97@NAEX11.na.qualcomm.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA8802379E97@NAEX11.na.qualcomm.com> Message-ID: <7465b6170702282256t4f335506p2047824fafc9547a@mail.gmail.com> On 3/1/07, Haridev, Meghana wrote: > Hi Folke, > > This is regarding the Py++ unittests: > > Currently all the tests (except for the split_module_* tests) are run such > that all the Py++ generated boost.python bindings are written to a single > file (using mb.write_module in > fundamental_tester_base_t.generate_source_files() ). The situation even worse :-), every compiled and tested ( imported module ) stays in the memory. So, I have to be very creative when I give names to class or function. > I plan to test the sanity of some > internal patches that I made to Py++ file_writer: multiple_files.py by > running them against the Py++ unittests (by writing the bindings to multiple > files). It could be nice if you can share your patches. If you cannot, than I suggest you to implement another solution: module_builder_t class expose "code_creator" - the root of code creators tree. You can create new class, which derives from multiple_files_t one and add the missing\adjusted functionality to it. This is how "class_multiple_files_t" file writer is implemented. After this you, from your main script, you can construct your custom writer and pass to it reference to the "code_creator". This approach is a little bit better, because you will not have to merge your changes every time you get new Py++ version. > So I modified > fundamental_tester_base_t.generate_source_files() and > fundamental_tester_base_t.get_source_files() appropriately, > and use a flag defined in autoconfig.py to switch from using "single file" > to "multiple file" generation. Most of the tests pass, but some tests fail > with linking errors due to "multiple definitions" when I generate code to > multiple files. > > For example, abstract_tester fails with the following error: > > > > square.pypp.os: In function > `abstract::call(abstract::shape*)': > ... > > > I know that this can be resolved by splitting the associated "data" files to > separate .hpp and .cpp files. I was wondering if anybody had tried running > the Py++ unittests for multiple generated files? I think it would be a good > idea to split these data files into .hpp and .cpp files so that we can > switch between testing for "single file" and "multiple file" generation > seamlessly. I'll be glad if you will help me to fix this situation. I can give you SVN access or you can send me the patches. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From dave at boost-consulting.com Thu Mar 1 16:00:47 2007 From: dave at boost-consulting.com (David Abrahams) Date: Thu, 01 Mar 2007 10:00:47 -0500 Subject: [C++-sig] [python] Fix for boost::python::import References: <45E63B84.2040705@sympatico.ca> Message-ID: <871wk9knz4.fsf@valverde.peloton> Stefan Seefeld writes: > The attached patch fixes the boost::python::import implementation. > It used to work fine for modules already ('physically') imported > into the process, but not for new ones. > > I'd like to apply to HEAD, Fine with me. > and would like to ask whether it is OK > for the R_1_34 branch, too. I recommend that Thomas accept this patch for RC_1_34_0 -- Dave Abrahams Boost Consulting www.boost-consulting.com From tim at klingt.org Thu Mar 1 17:17:09 2007 From: tim at klingt.org (Tim Blechmann) Date: Thu, 01 Mar 2007 17:17:09 +0100 Subject: [C++-sig] boost.python question Message-ID: <1172765829.22871.13.camel@localhost> hi all, i've got a question regarding the following setup: i've got a c++ class foo, that's wrapped into a python class and a class bar, that stores a pointer to an instance of foo... class foo{}; class bar{ void set(foo * newf) { f = newf;} foo * get(void) { return f;} virtual void override_me_from_python() { } protected: foo * f; }; so ... i'm a bit puzzled, how to wrap the class bar, so that it can access bar::f when implementing the virtual function from python ... any hints / pointers to examples or documentation? thanks in advance ... tim -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org I must say I find television very educational. The minute somebody turns it on, I go to the library and read a good book. Groucho Marx -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From seefeld at sympatico.ca Thu Mar 1 17:49:33 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 01 Mar 2007 11:49:33 -0500 Subject: [C++-sig] boost.python question In-Reply-To: <1172765829.22871.13.camel@localhost> References: <1172765829.22871.13.camel@localhost> Message-ID: <45E7041D.505@sympatico.ca> Tim Blechmann wrote: > hi all, > > i've got a question regarding the following setup: > > i've got a c++ class foo, that's wrapped into a python class and a class > bar, that stores a pointer to an instance of foo... > > class foo{}; > > class bar{ > void set(foo * newf) { f = newf;} > foo * get(void) { return f;} > > virtual void override_me_from_python() > { > } > > protected: > foo * f; > }; > > so ... i'm a bit puzzled, how to wrap the class bar, so that it can > access bar::f when implementing the virtual function from python ... ...by exporting the bar::get method such that you can call it from within python code ? HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From tim at klingt.org Thu Mar 1 19:22:38 2007 From: tim at klingt.org (Tim Blechmann) Date: Thu, 01 Mar 2007 19:22:38 +0100 Subject: [C++-sig] boost.python question In-Reply-To: <45E7041D.505@sympatico.ca> References: <1172765829.22871.13.camel@localhost> <45E7041D.505@sympatico.ca> Message-ID: <1172773358.22871.16.camel@localhost> On Thu, 2007-03-01 at 11:49 -0500, Stefan Seefeld wrote: > Tim Blechmann wrote: > > hi all, > > > > i've got a question regarding the following setup: > > > > i've got a c++ class foo, that's wrapped into a python class and a class > > bar, that stores a pointer to an instance of foo... > > > > class foo{}; > > > > class bar{ > > void set(foo * newf) { f = newf;} > > foo * get(void) { return f;} > > > > virtual void override_me_from_python() > > { > > } > > > > protected: > > foo * f; > > }; > > > > so ... i'm a bit puzzled, how to wrap the class bar, so that it can > > access bar::f when implementing the virtual function from python ... > > ...by exporting the bar::get method such that you can call it from > within python code ? ah, cool ... didn't think, that it's that easy .... thanks ... tim -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org I must say I find television very educational. The minute somebody turns it on, I go to the library and read a good book. Groucho Marx -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From gabriel.becedillas at corest.com Thu Mar 1 20:10:52 2007 From: gabriel.becedillas at corest.com (Gabriel Becedillas) Date: Thu, 01 Mar 2007 16:10:52 -0300 Subject: [C++-sig] boost::python and custom smart pointer Message-ID: <45E7253C.6010806@corest.com> Hi, I'd like to have my custom smart_ptr behave like boost::shared_ptr regarding from_python conversions. To_python converstions work fine, but the problem arises when I have an instance to a derived class via a smart_ptr to the base class MySmartPtr and this has to be passed to a function receiving MySmartPtr. If I use boost::shared_ptr this works just fine. I think I'm doing something wrong in MySmartPtr_from_python. I'm pasting a simplified version of my code: ------ BEGIN C++ CODE ------ // Dummy smart pointer class. template class MySmartPtr { public: MySmartPtr() : m_P(NULL) { } MySmartPtr(T* a_P) : m_P(a_P) { } T* m_P; }; namespace boost { namespace python { template T* get_pointer(MySmartPtr const& a_P) { return a_P.m_P; } template struct pointee< MySmartPtr > { typedef T type; }; } // namespace python } // namespace boost template struct MySmartPtr_from_python { static void* convertible(PyObject* p) { if (p == Py_None) return p; return boost::python::converter::get_lvalue_from_python(p, boost::python::converter::registered< MySmartPtr >::converters); } static void construct(PyObject* source, boost::python::converter::rvalue_from_python_stage1_data* data) { void* const storage = ((boost::python::converter::rvalue_from_python_storage >*)data)->storage.bytes; if (data->convertible == source) // This is because convertible returned p if p was Py_None. new (storage) MySmartPtr(); else new (storage) MySmartPtr( *((MySmartPtr*)data->convertible) ); data->convertible = storage; } }; class Base { public: virtual ~Base() {} }; class Derived : public Base { }; boost::shared_ptr build_ok() { return boost::shared_ptr(new Derived()); } MySmartPtr build_fail() { return MySmartPtr(new Derived()); } void test_ok(boost::shared_ptr a_Derived) {} void test_fail(MySmartPtr a_Derived) {} template void register_MySmartPtr_conversions() { boost::python::register_ptr_to_python< MySmartPtr >(); boost::python::converter::registry::insert( &MySmartPtr_from_python::convertible, &MySmartPtr_from_python::construct, boost::python::type_id< MySmartPtr >() ); } BOOST_PYTHON_MODULE(bptest) { boost::python::class_ class_Base("Base", boost::python::no_init); boost::python::register_ptr_to_python< boost::shared_ptr >(); register_MySmartPtr_conversions(); boost::python::class_ > class_Derived("Derived", boost::python::no_init); boost::python::register_ptr_to_python< boost::shared_ptr >(); register_MySmartPtr_conversions(); boost::python::def("build_ok", build_ok); boost::python::def("test_ok", test_ok); boost::python::def("build_fail", build_fail); boost::python::def("test_fail", test_fail); } ------ END C++ CODE ------ This python code fails to work: import bptest bptest.test_ok(bptest.build_ok()) bptest.test_fail(bptest.build_fail()) And this is the error that I get: Boost.Python.ArgumentError: Python argument types in bptest.test_fail(Derived) did not match C++ signature: test_fail(class MySmartPtr) Thanks in advance. -- Gabriel Becedillas Developer CORE SECURITY TECHNOLOGIES From gabriel.becedillas at corest.com Thu Mar 1 21:03:31 2007 From: gabriel.becedillas at corest.com (Gabriel Becedillas) Date: Thu, 01 Mar 2007 17:03:31 -0300 Subject: [C++-sig] boost::python and custom smart pointer In-Reply-To: <45E7253C.6010806@corest.com> References: <45E7253C.6010806@corest.com> Message-ID: <45E73193.2050008@corest.com> Gabriel Becedillas wrote: > Hi, > I'd like to have my custom smart_ptr behave like boost::shared_ptr > regarding from_python conversions. To_python converstions work fine, but > the problem arises when I have an instance to a derived class via a > smart_ptr to the base class MySmartPtr and this has to be passed > to a function receiving MySmartPtr. If I use boost::shared_ptr > this works just fine. I think I'm doing something wrong in > MySmartPtr_from_python. > I'm pasting a simplified version of my code: > > ------ BEGIN C++ CODE ------ > > // Dummy smart pointer class. > template > class MySmartPtr > { > public: > MySmartPtr() : > m_P(NULL) > { > } > > MySmartPtr(T* a_P) : > m_P(a_P) > { > } > > T* m_P; > }; > > namespace boost { > namespace python { > > template > T* get_pointer(MySmartPtr const& a_P) > { > return a_P.m_P; > } > > template > struct pointee< MySmartPtr > > { > typedef T type; > }; > > } // namespace python > } // namespace boost > > template > struct MySmartPtr_from_python > { > static void* convertible(PyObject* p) > { > if (p == Py_None) > return p; > > return boost::python::converter::get_lvalue_from_python(p, > boost::python::converter::registered< MySmartPtr > >::converters); > } > > static void construct(PyObject* source, > boost::python::converter::rvalue_from_python_stage1_data* data) > { > void* const storage = > ((boost::python::converter::rvalue_from_python_storage > >*)data)->storage.bytes; > > if (data->convertible == source) // This is because convertible > returned p if p was Py_None. > new (storage) MySmartPtr(); > else > new (storage) MySmartPtr( > *((MySmartPtr*)data->convertible) ); > > data->convertible = storage; > } > }; > > class Base > { > public: > virtual ~Base() > {} > }; > > class Derived : > public Base > { > }; > > boost::shared_ptr build_ok() > { > return boost::shared_ptr(new Derived()); > } > > MySmartPtr build_fail() > { > return MySmartPtr(new Derived()); > } > > void test_ok(boost::shared_ptr a_Derived) > {} > > void test_fail(MySmartPtr a_Derived) > {} > > template > void register_MySmartPtr_conversions() > { > boost::python::register_ptr_to_python< MySmartPtr >(); > > boost::python::converter::registry::insert( > &MySmartPtr_from_python::convertible, > &MySmartPtr_from_python::construct, > boost::python::type_id< MySmartPtr >() > ); > } > > BOOST_PYTHON_MODULE(bptest) > { > boost::python::class_ class_Base("Base", > boost::python::no_init); > boost::python::register_ptr_to_python< boost::shared_ptr >(); > register_MySmartPtr_conversions(); > > > boost::python::class_ boost::python::bases > class_Derived("Derived", > boost::python::no_init); > boost::python::register_ptr_to_python< boost::shared_ptr >(); > register_MySmartPtr_conversions(); > > boost::python::def("build_ok", build_ok); > boost::python::def("test_ok", test_ok); > > boost::python::def("build_fail", build_fail); > boost::python::def("test_fail", test_fail); > } > > ------ END C++ CODE ------ > > This python code fails to work: > > import bptest > > bptest.test_ok(bptest.build_ok()) > bptest.test_fail(bptest.build_fail()) > > And this is the error that I get: > > Boost.Python.ArgumentError: Python argument types in > bptest.test_fail(Derived) > did not match C++ signature: > test_fail(class MySmartPtr) > > Thanks in advance. > This function should be like this: template void register_MySmartPtr_conversions() { boost::python::register_ptr_to_python< MySmartPtr >(); boost::python::converter::registry::insert( &MySmartPtr_from_python::convertible, &MySmartPtr_from_python::construct, boost::python::type_id< MySmartPtr >() ); } But its still failing. -- Gabriel Becedillas Developer CORE SECURITY TECHNOLOGIES From roman.yakovenko at gmail.com Thu Mar 1 22:07:41 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 1 Mar 2007 23:07:41 +0200 Subject: [C++-sig] boost::python and custom smart pointer In-Reply-To: <45E73193.2050008@corest.com> References: <45E7253C.6010806@corest.com> <45E73193.2050008@corest.com> Message-ID: <7465b6170703011307j3be95721p5450b13bdc746449@mail.gmail.com> On 3/1/07, Gabriel Becedillas wrote: > > Gabriel Becedillas wrote: > > Hi, > > I'd like to have my custom smart_ptr behave like boost::shared_ptr > > regarding from_python conversions. To_python converstions work fine, but > > the problem arises when I have an instance to a derived class via a > > smart_ptr to the base class MySmartPtr and this has to be passed > > to a function receiving MySmartPtr. If I use boost::shared_ptr > > this works just fine. I think I'm doing something wrong in > > MySmartPtr_from_python. > > I'm pasting a simplified version of my code: For one of my project I also needed such functionality. I didn't find the answer. So I implemented pretty quick and simple work-around. I had a factory class, which constructs objects and returns them using custom smart pointer. So, instead of exposing "create" function directly I created small wrapper and exposed it instead: boost::python object create_wrapper( std::string name ){ custom_ptr inst = create( name ); //call original function if( dynamic_cast< derived1_t*>( inst.get() ) ){ return boost::python::object( custom_ptr_dynamic_cast( inst ) ) } else if( ... ){ ... } } This will give your users the behavior you want. Now when I think about this, the better solution would be to introduce small call policy, which derives from the default_call_policies class and contains the if-else+ casting logic. If you use Py++, than it is pretty simple to apply the new call policy on all relevant functions. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim at klingt.org Thu Mar 1 22:09:40 2007 From: tim at klingt.org (Tim Blechmann) Date: Thu, 01 Mar 2007 22:09:40 +0100 Subject: [C++-sig] boost.python metaclass problem Message-ID: <1172783380.22871.47.camel@localhost> hi all, i've got a problem with a python class, that i'm deriving from two classes. one of the base classes is a class from pyqt, the other one is a wrapped boost.python class. now, when deriving my class from these two classes, i'm getting the message: TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases any idea, how to solve the metaclass conflict? my boost.python class has the structure: class_("my_class") .def("my_fun", &my_wrapper::my_fun); thanks ... tim -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org Nothing exists until or unless it is observed. An artist is making something exist by observing it. And his hope for other people is that they will also make it exist by observing it. I call it 'creative observation.' Creative viewing. William S. Burroughs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From seefeld at sympatico.ca Thu Mar 1 23:54:32 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 01 Mar 2007 17:54:32 -0500 Subject: [C++-sig] boost.python metaclass problem In-Reply-To: <1172783380.22871.47.camel@localhost> References: <1172783380.22871.47.camel@localhost> Message-ID: <45E759A8.1050006@sympatico.ca> Tim Blechmann wrote: > hi all, > > i've got a problem with a python class, that i'm deriving from two > classes. one of the base classes is a class from pyqt, the other one is > a wrapped boost.python class. > > now, when deriving my class from these two classes, i'm getting the > message: > TypeError: Error when calling the metaclass bases metaclass conflict: > the metaclass of a derived class must be a (non-strict) subclass of the > metaclasses of all its bases > > any idea, how to solve the metaclass conflict? my boost.python class has > the structure: > class_("my_class") > .def("my_fun", &my_wrapper::my_fun); The problem you are facing is that there are certain constraints on the metaclasses of your (multiple) base classes: http://www.python.org/download/releases/2.2/descrintro/#metaclasses I don't know pyqt, and I'm not sure you get access to boost.python's own metaclass. (David ?) You may need to derive a new metaclass from boost.python's one as well as the pyqt metaclass. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From tim at klingt.org Fri Mar 2 00:54:34 2007 From: tim at klingt.org (Tim Blechmann) Date: Fri, 02 Mar 2007 00:54:34 +0100 Subject: [C++-sig] boost.python metaclass problem In-Reply-To: <45E759A8.1050006@sympatico.ca> References: <1172783380.22871.47.camel@localhost> <45E759A8.1050006@sympatico.ca> Message-ID: <1172793274.22871.76.camel@localhost> hi stefan ... > > now, when deriving my class from these two classes, i'm getting the > > message: > > TypeError: Error when calling the metaclass bases metaclass conflict: > > the metaclass of a derived class must be a (non-strict) subclass of the > > metaclasses of all its bases > > > > any idea, how to solve the metaclass conflict? my boost.python class has > > the structure: > > class_("my_class") > > .def("my_fun", &my_wrapper::my_fun); > > The problem you are facing is that there are certain constraints on > the metaclasses of your (multiple) base classes: > > http://www.python.org/download/releases/2.2/descrintro/#metaclasses > > I don't know pyqt, and I'm not sure you get access to boost.python's > own metaclass. (David ?) You may need to derive a new metaclass from > boost.python's one as well as the pyqt metaclass. i see ... in the meanwhile i've been trying to use the metaclass conflict resolver found at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204197. however with the result of giving me a TypeError: multiple bases have instance lay-out conflict i would prefer to find a clean solution for this, avoiding the multiple inheritance would add an additional indirection in my code, that i'd like to avoid ... thanks ... tim -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org Your mind will answer most questions if you learn to relax and wait for the answer. William S. Burroughs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From meine at informatik.uni-hamburg.de Fri Mar 2 11:07:22 2007 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Fri, 2 Mar 2007 11:07:22 +0100 Subject: [C++-sig] boost.python metaclass problem In-Reply-To: <1172793274.22871.76.camel@localhost> References: <1172783380.22871.47.camel@localhost> <45E759A8.1050006@sympatico.ca> <1172793274.22871.76.camel@localhost> Message-ID: <200703021107.22957.meine@informatik.uni-hamburg.de> Am Freitag, 02. M?rz 2007 00:54 schrieb Tim Blechmann: > i would prefer to find a clean solution for this, avoiding the multiple > inheritance would add an additional indirection in my code, that i'd > like to avoid ... Yes, but I can hardly imagine how a SIP/BPL mixture class should behave (think of memory management etc.) - I am quite sure that this simply won't work! -- Ciao, / / /--/ / / ANS From gabriel.becedillas at corest.com Fri Mar 2 15:11:50 2007 From: gabriel.becedillas at corest.com (Gabriel Becedillas) Date: Fri, 02 Mar 2007 11:11:50 -0300 Subject: [C++-sig] boost::python and custom smart pointer In-Reply-To: <45E7253C.6010806@corest.com> References: <45E7253C.6010806@corest.com> Message-ID: <45E830A6.1050106@corest.com> Gabriel Becedillas wrote: > Hi, > I'd like to have my custom smart_ptr behave like boost::shared_ptr > regarding from_python conversions. To_python converstions work fine, but > the problem arises when I have an instance to a derived class via a > smart_ptr to the base class MySmartPtr and this has to be passed > to a function receiving MySmartPtr. If I use boost::shared_ptr > this works just fine. I think I'm doing something wrong in > MySmartPtr_from_python. > I'm pasting a simplified version of my code: > > ------ BEGIN C++ CODE ------ > > // Dummy smart pointer class. > template > class MySmartPtr > { > public: > MySmartPtr() : > m_P(NULL) > { > } > > MySmartPtr(T* a_P) : > m_P(a_P) > { > } > > T* m_P; > }; > > namespace boost { > namespace python { > > template > T* get_pointer(MySmartPtr const& a_P) > { > return a_P.m_P; > } > > template > struct pointee< MySmartPtr > > { > typedef T type; > }; > > } // namespace python > } // namespace boost > > template > struct MySmartPtr_from_python > { > static void* convertible(PyObject* p) > { > if (p == Py_None) > return p; > > return boost::python::converter::get_lvalue_from_python(p, > boost::python::converter::registered< MySmartPtr > >::converters); > } > > static void construct(PyObject* source, > boost::python::converter::rvalue_from_python_stage1_data* data) > { > void* const storage = > ((boost::python::converter::rvalue_from_python_storage > >*)data)->storage.bytes; > > if (data->convertible == source) // This is because convertible > returned p if p was Py_None. > new (storage) MySmartPtr(); > else > new (storage) MySmartPtr( > *((MySmartPtr*)data->convertible) ); > > data->convertible = storage; > } > }; > > class Base > { > public: > virtual ~Base() > {} > }; > > class Derived : > public Base > { > }; > > boost::shared_ptr build_ok() > { > return boost::shared_ptr(new Derived()); > } > > MySmartPtr build_fail() > { > return MySmartPtr(new Derived()); > } > > void test_ok(boost::shared_ptr a_Derived) > {} > > void test_fail(MySmartPtr a_Derived) > {} > > template > void register_MySmartPtr_conversions() > { > boost::python::register_ptr_to_python< MySmartPtr >(); > > boost::python::converter::registry::insert( > &MySmartPtr_from_python::convertible, > &MySmartPtr_from_python::construct, > boost::python::type_id< MySmartPtr >() > ); > } > > BOOST_PYTHON_MODULE(bptest) > { > boost::python::class_ class_Base("Base", > boost::python::no_init); > boost::python::register_ptr_to_python< boost::shared_ptr >(); > register_MySmartPtr_conversions(); > > > boost::python::class_ boost::python::bases > class_Derived("Derived", > boost::python::no_init); > boost::python::register_ptr_to_python< boost::shared_ptr >(); > register_MySmartPtr_conversions(); > > boost::python::def("build_ok", build_ok); > boost::python::def("test_ok", test_ok); > > boost::python::def("build_fail", build_fail); > boost::python::def("test_fail", test_fail); > } > > ------ END C++ CODE ------ > > This python code fails to work: > > import bptest > > bptest.test_ok(bptest.build_ok()) > bptest.test_fail(bptest.build_fail()) > > And this is the error that I get: > > Boost.Python.ArgumentError: Python argument types in > bptest.test_fail(Derived) > did not match C++ signature: > test_fail(class MySmartPtr) > > Thanks in advance. > I also forgot to say that upcasts aren't working either. If I return a MySmartPtr and the concrete type is Derived, boost::python let me use the Derived interface (which is great!), but when this instance has to be passed back to C++ it'll only match an exact MySmartPtr type. It won't do neither an upcast nor a downcast. After checking shared_ptr_from_python a litte bit I realize that convertible(...) is returning a raw T* to the object (when the passed PyObject is not Py_None) and that construct(...) is creating a new boost::shared_ptr from the raw T*. converter::get_lvalue_from_python(p, registered::converters) is used to extract the raw T* from the PyObject. This won't work for me because I need to extract the whole MySmartPtr*, not just the raw T*, in order to copy the MySmartPtr in construct(...). Is there a way to do that ? Please keep in mind that I need to check both for upcasts and downcasts. My PyObject could hold a MySmartPtr instance and I'd like to pass it to C++ as a MySmartPtr, and the other way around (previously checking if a downcast is possible). I really need to make MySmartPtr to behave as boost::shared_ptr regarding conversions. Thanks a lot. From rwgk at yahoo.com Fri Mar 2 16:55:31 2007 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Fri, 2 Mar 2007 07:55:31 -0800 (PST) Subject: [C++-sig] boost.python metaclass problem Message-ID: <967169.61129.qm@web31107.mail.mud.yahoo.com> > I don't know pyqt, and I'm not sure you get access to boost.python's > own metaclass. (David ?) Simply use the __class__ attribute of a Boost.Python wrapped class. I.e. if the class is "bar" (and you construct objects with "bar()"), bar.__class__ will give you the metaclass. See also: boost/libs/python/doc/tutorial/doc/tutorial.qbk Look for: __metaclass__ But, like Hans I'm also a bit skeptical ... Ralf ____________________________________________________________________________________ We won't tell. Get more on shows you hate to love (and love to hate): Yahoo! TV's Guilty Pleasures list. http://tv.yahoo.com/collections/265 -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Fri Mar 2 17:02:56 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 02 Mar 2007 11:02:56 -0500 Subject: [C++-sig] boost.python metaclass problem In-Reply-To: <967169.61129.qm@web31107.mail.mud.yahoo.com> References: <967169.61129.qm@web31107.mail.mud.yahoo.com> Message-ID: <45E84AB0.1070307@sympatico.ca> Ralf W. Grosse-Kunstleve wrote: >> I don't know pyqt, and I'm not sure you get access to boost.python's >> own metaclass. (David ?) > > Simply use the __class__ attribute of a Boost.Python wrapped class. > I.e. if the class is "bar" (and you construct objects with "bar()"), > bar.__class__ > will give you the metaclass. > See also: boost/libs/python/doc/tutorial/doc/tutorial.qbk > Look for: __metaclass__ Yeah, I certainly know how to access an object's metaclass. What I don't know is how to access that metaclass directly to derive from it in order to generate a new metaclass suitable for the new class... Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From roman.yakovenko at gmail.com Fri Mar 2 19:25:22 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 2 Mar 2007 20:25:22 +0200 Subject: [C++-sig] boost::python and custom smart pointer In-Reply-To: <45E830A6.1050106@corest.com> References: <45E7253C.6010806@corest.com> <45E830A6.1050106@corest.com> Message-ID: <7465b6170703021025h60a3b35fgc4100cf89b0d4925@mail.gmail.com> On 3/2/07, Gabriel Becedillas wrote: > > I also forgot to say that upcasts aren't working either. > If I return a MySmartPtr and the concrete type is Derived, > boost::python let me use the Derived interface (which is great!), but > when this instance has to be passed back to C++ it'll only match an > exact MySmartPtr type. It won't do neither an upcast nor a downcast. Upcast is solvable, look documentation for implicitly_convertible. You also can take a look on complete example of exposing custom smart pointer here: http://language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html > After checking shared_ptr_from_python a litte bit I realize that > convertible(...) is returning a raw T* to the object (when the passed > PyObject is not Py_None) and that construct(...) is creating a new > boost::shared_ptr from the raw T*. > converter::get_lvalue_from_python(p, registered::converters) is used > to extract the raw T* from the PyObject. This won't work for me because > I need to extract the whole MySmartPtr*, not just the raw T*, in > order to copy the MySmartPtr in construct(...). > > Is there a way to do that ? Please keep in mind that I need to check > both for upcasts and downcasts. My PyObject could hold a > MySmartPtr instance and I'd like to pass it to C++ as a > MySmartPtr, and the other way around (previously checking if a > downcast is possible). > I really need to make MySmartPtr to behave as boost::shared_ptr > regarding conversions. There is only one person who can help you here: Dave Abrahams. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From tim at klingt.org Sat Mar 3 20:05:25 2007 From: tim at klingt.org (Tim Blechmann) Date: Sat, 03 Mar 2007 20:05:25 +0100 Subject: [C++-sig] c++ to python object conversion problem Message-ID: <1172948725.19770.83.camel@localhost> hi all, i've wrapped a simple c++ class with boost python, which looks like this: class simple: boost::noncopyable{}; an instance of this class (or a derived one) is allocated from python and a pointer is stored in a class with a virtual function: class callback: boost::noncopyable { virtual void overloadme(simple const &) = 0 void run(void) { this->overloadme(s_); } void set(simple * s) {s_ = s;} simple * s_; }; this callback class is wrapped like it's described in the tutorial: class cbwrapper: callback, wrapper { virtual void overloadme(simple const & s) { this->get_override("overloadme")(s); } }; callback::run is called from the c++ code later in the program asynchronously. however, when trying to call the overridden function, the program crashes: > 0 PyErr_Restore PC=0xb7f30a9f, FP=0xb45461c8 [/usr/lib/libpython2.4.so.1.0] 1 PyErr_SetObject PC=0xb7f30b30, FP=0xb45461c8 [/usr/lib/libpython2.4.so.1.0] 2 _ZNK5boost6python9converter12registration9to_pythonEPVKv PC=0xb519c4d6, FP=0xb45461c8 [/usr/lib/libboost_python-mt.so.1.33.1] 3 _ZN5boost6python9converter6detail18arg_to_python_baseC2EPVKvRKNS1_12registrationE PC=0xb51accdf, FP=0xb45461e8 [/usr/lib/libboost_python-mt.so.1.33.1] 4 boost::python::converter::detail::value_arg_to_python::not-in-charge value_arg_to_python PC=0xb678648b, FP=0xb4546208 [/usr/include/boost/python/converter/arg_to_python.hpp#209] 5 boost::python::converter::arg_to_python::arg_to_python PC=0xb67864b5, FP=0xb4546228 [/usr/include/boost/python/converter/arg_to_python.hpp#256] 6 boost::python::override::operator () PC=0xb67864e0, FP=0xb4546268 [/usr/include/boost/python/override.hpp#138] the instance of the wrapped object is still valid, but the conversion from the c++ object to the python object seems to be the reason of the crash ... any idea, what i am missing? thanks, tim -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org Just what the hell is the experimental tradition? Morton Feldman -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From roman.yakovenko at gmail.com Sat Mar 3 20:21:25 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sat, 3 Mar 2007 21:21:25 +0200 Subject: [C++-sig] c++ to python object conversion problem In-Reply-To: <1172948725.19770.83.camel@localhost> References: <1172948725.19770.83.camel@localhost> Message-ID: <7465b6170703031121k5e4d3b75l9d831fb46b45e825@mail.gmail.com> On 3/3/07, Tim Blechmann wrote: > hi all, > > i've wrapped a simple c++ class with boost python, which looks like > this: > > class simple: boost::noncopyable{}; > > an instance of this class (or a derived one) is allocated from python > and a pointer is stored in a class with a virtual function: > > class callback: boost::noncopyable > { > virtual void overloadme(simple const &) = 0 > void run(void) { this->overloadme(s_); } > void set(simple * s) {s_ = s;} > simple * s_; > }; > > this callback class is wrapped like it's described in the tutorial: > > class cbwrapper: callback, wrapper > { > virtual void overloadme(simple const & s) { > this->get_override("overloadme")(s); I think that the problem in this line. By default Boost.Python pass copy of the object to the "override" function. In this case "simple" is noncopyable. ( I don't understand why you didn't get compile time error ). In my opinion you should change this->get_override("...")(s) to this->get_override( ... )( boost::ref(s) ) P.S. Also you didn't specified how you exposed the classes to Python. P.S. Py++ is aware of this and would generate the right from the beginning -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From tim at klingt.org Sun Mar 4 14:37:01 2007 From: tim at klingt.org (Tim Blechmann) Date: Sun, 04 Mar 2007 14:37:01 +0100 Subject: [C++-sig] c++ to python object conversion problem In-Reply-To: <7465b6170703031121k5e4d3b75l9d831fb46b45e825@mail.gmail.com> References: <1172948725.19770.83.camel@localhost> <7465b6170703031121k5e4d3b75l9d831fb46b45e825@mail.gmail.com> Message-ID: <1173015421.17148.20.camel@localhost> hi roman ... thanks for your hint i've added the wrapper code below the classes: > > class simple: boost::noncopyable{}; class_("simple"); > > an instance of this class (or a derived one) is allocated from python > > and a pointer is stored in a class with a virtual function: > > > > class callback: boost::noncopyable > > { > > virtual void overloadme(simple const &) = 0 > > void run(void) { this->overloadme(s_); } > > void set(simple * s) {s_ = s;} > > simple * s_; > > }; > > > > this callback class is wrapped like it's described in the tutorial: > > > > class cbwrapper: callback, wrapper > > { > > virtual void overloadme(simple const & s) { > > this->get_override("overloadme")(s); class_("callback") .def("set", &cbwrapper::set) .def("overloadme", &cbwrapper::overloadme) ; > I think that the problem in this line. By default Boost.Python pass copy of the > object to the "override" function. In this case "simple" is noncopyable. > ( I don't understand why you didn't get compile time error ). > In my opinion you should change > this->get_override("...")(s) > to > this->get_override( ... )( boost::ref(s) ) i tried this ... however python is crashing now: > 0 PyFrame_New PC=0xb7e2c81a, FP=0xb44aee68 [/usr/lib/libpython2.4.so.1.0] 1 PyEval_EvalCodeEx PC=0xb7e79b74, FP=0xb44aeee8 [/usr/lib/libpython2.4.so.1.0] 2 PyClassMethod_New PC=0xb7e2d81d, FP=0xb44aef48 [/usr/lib/libpython2.4.so.1.0] 3 PyObject_Call PC=0xb7e14a35, FP=0xb44aef68 [/usr/lib/libpython2.4.so.1.0] 4 PyClass_IsSubclass PC=0xb7e1b05d, FP=0xb44af1b8 [/usr/lib/libpython2.4.so.1.0] 5 PyObject_Call PC=0xb7e14a35, FP=0xb44af1d8 [/usr/lib/libpython2.4.so.1.0] 6 PyEval_CallObjectWithKeywords PC=0xb7e73d26, FP=0xb44af1f8 [/usr/lib/libpython2.4.so.1.0] 7 PyEval_CallFunction PC=0xb7e94207, FP=0xb44af228 [/usr/lib/libpython2.4.so.1.0] 8 boost::python::override::operator () > PC=0xb66f0712, FP=0xb44af268 [/usr/include/boost/python/override.hpp#138] > P.S. Py++ is aware of this and would generate the right from the beginning ... unfortunately, gccxml doesn't like my c++ code :/ any other hints or workarounds? thanks ... tim -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org A paranoid is a man who knows a little of what's going on. William S. Burroughs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From roman.yakovenko at gmail.com Sun Mar 4 15:21:29 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 4 Mar 2007 16:21:29 +0200 Subject: [C++-sig] c++ to python object conversion problem In-Reply-To: <1173015421.17148.20.camel@localhost> References: <1172948725.19770.83.camel@localhost> <7465b6170703031121k5e4d3b75l9d831fb46b45e825@mail.gmail.com> <1173015421.17148.20.camel@localhost> Message-ID: <7465b6170703040621w392c2e29ve2c2a35d1d748a9d@mail.gmail.com> On 3/4/07, Tim Blechmann wrote: > hi roman ... thanks for your hint > > i've added the wrapper code below the classes: > > > > class simple: boost::noncopyable{}; > > class_("simple"); > > > > an instance of this class (or a derived one) is allocated from python > > > and a pointer is stored in a class with a virtual function: > > > > > > class callback: boost::noncopyable > > > { > > > virtual void overloadme(simple const &) = 0 > > > void run(void) { this->overloadme(s_); } > > > void set(simple * s) {s_ = s;} > > > simple * s_; > > > }; > > > > > > this callback class is wrapped like it's described in the tutorial: > > > > > > class cbwrapper: callback, wrapper > > > { > > > virtual void overloadme(simple const & s) { > > > this->get_override("overloadme")(s); > > class_("callback") > .def("set", &cbwrapper::set) A problem could also be here, you have to set call policies: http://boost.org/libs/python/doc/tutorial/doc/html/python/functions.html#python.call_policies Also, creating small and **complete** is much better way to get help. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From tim at klingt.org Sun Mar 4 15:48:25 2007 From: tim at klingt.org (Tim Blechmann) Date: Sun, 04 Mar 2007 15:48:25 +0100 Subject: [C++-sig] c++ to python object conversion problem In-Reply-To: <7465b6170703040621w392c2e29ve2c2a35d1d748a9d@mail.gmail.com> References: <1172948725.19770.83.camel@localhost> <7465b6170703031121k5e4d3b75l9d831fb46b45e825@mail.gmail.com> <1173015421.17148.20.camel@localhost> <7465b6170703040621w392c2e29ve2c2a35d1d748a9d@mail.gmail.com> Message-ID: <1173019706.17148.26.camel@localhost> hi roman, i just figured out, that it was a simple python threading issue, because the c++ thread hasn't been synchronized with python. after guarding the calls to the override object by a PyGILState_Ensure()/Release() pair, it works flawless! cheers ... tim On Sun, 2007-03-04 at 16:21 +0200, Roman Yakovenko wrote: > On 3/4/07, Tim Blechmann wrote: > > hi roman ... thanks for your hint > > > > i've added the wrapper code below the classes: > > > > > > class simple: boost::noncopyable{}; > > > > class_("simple"); > > > > > > an instance of this class (or a derived one) is allocated from python > > > > and a pointer is stored in a class with a virtual function: > > > > > > > > class callback: boost::noncopyable > > > > { > > > > virtual void overloadme(simple const &) = 0 > > > > void run(void) { this->overloadme(s_); } > > > > void set(simple * s) {s_ = s;} > > > > simple * s_; > > > > }; > > > > > > > > this callback class is wrapped like it's described in the tutorial: > > > > > > > > class cbwrapper: callback, wrapper > > > > { > > > > virtual void overloadme(simple const & s) { > > > > this->get_override("overloadme")(s); > > > > class_("callback") > > .def("set", &cbwrapper::set) > > A problem could also be here, you have to set call policies: > http://boost.org/libs/python/doc/tutorial/doc/html/python/functions.html#python.call_policies > > Also, creating small and **complete** is much better way to get help. > -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org Life is really simple, but we insist on making it complicated. Confucius -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From tim at klingt.org Sun Mar 4 18:50:04 2007 From: tim at klingt.org (Tim Blechmann) Date: Sun, 04 Mar 2007 18:50:04 +0100 Subject: [C++-sig] yet another conversion problem (was: Re: c++ to python object conversion problem) In-Reply-To: <1173019706.17148.26.camel@localhost> References: <1172948725.19770.83.camel@localhost> <7465b6170703031121k5e4d3b75l9d831fb46b45e825@mail.gmail.com> <1173015421.17148.20.camel@localhost> <7465b6170703040621w392c2e29ve2c2a35d1d748a9d@mail.gmail.com> <1173019706.17148.26.camel@localhost> Message-ID: <1173030604.18489.8.camel@localhost> ok ... now i'm facing the problem, that a class derived from a c++ class is converted to the c++ base class, in a python callback: the c++ classes are: struct simple: boost::noncopyable { }; struct holder: boost::noncopyable { simple * s_; void set(simple * s) { s_ = s; } void run(void) { this->ping(s_); } virtual void ping(simple * s) = 0; }; struct holder_wrapper: holder, wrapper { virtual void ping(simple * s) { this->get_override("ping")(boost::ref(s)); } }; which are wrapped: class_("simple"); class_("holder") .def("set", &holder_wrapper::set) .def("run", &holder_wrapper::run) .def("ping", pure_virtual(&holder_wrapper::ping)) ; from python, i derive new classes and implement the virtual function holder::ping: class simple_d(simple): def __init__(self, i): simple.__init__(self) self.i = i class holder_d(holder): def __init__(self): holder.__init__(self) def ping(self, s): assert(isinstance(s, simple)) assert(isinstance(s, simple_d)) print s.i when i call it like this the instant s in the python code, the second assert statement fails. the object is not handled as simple_d, but as simple: s = simple_d(53) h = holder_d() h.set(s) h.run() is there any way to "upcast" the instance s from simple to simple_d? thanks again! tim -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org Linux is like a wigwam: no windows, no gates, apache inside, stable. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From ndbecker2 at gmail.com Mon Mar 5 14:21:13 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 05 Mar 2007 08:21:13 -0500 Subject: [C++-sig] module destructor? Message-ID: It is easy to put some code to run when a module is loaded: BOOST_PYTHON_MODULE(foo) { do_some_init(); ... But how can I do some cleanup when module is unloaded? From seefeld at sympatico.ca Mon Mar 5 14:29:36 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 05 Mar 2007 08:29:36 -0500 Subject: [C++-sig] module destructor? In-Reply-To: References: Message-ID: <45EC1B40.8000805@sympatico.ca> Neal Becker wrote: > It is easy to put some code to run when a module is loaded: > > BOOST_PYTHON_MODULE(foo) { > do_some_init(); > ... > > But how can I do some cleanup when module is unloaded? Export a 'cleanup' type and instantiate it as a 'global object' in your module. If nothing else is referring to it it will be deleted with the module... HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From ndbecker2 at gmail.com Mon Mar 5 14:43:35 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 05 Mar 2007 08:43:35 -0500 Subject: [C++-sig] module destructor? References: <45EC1B40.8000805@sympatico.ca> Message-ID: Stefan Seefeld wrote: > Neal Becker wrote: >> It is easy to put some code to run when a module is loaded: >> >> BOOST_PYTHON_MODULE(foo) { >> do_some_init(); >> ... >> >> But how can I do some cleanup when module is unloaded? > > Export a 'cleanup' type and instantiate it as a 'global object' > in your module. If nothing else is referring to it it will be > deleted with the module... > Thanks. I've never tried to instantiate an object from c++. Any examples/hints? From seefeld at sympatico.ca Mon Mar 5 15:04:58 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 05 Mar 2007 09:04:58 -0500 Subject: [C++-sig] module destructor? In-Reply-To: References: <45EC1B40.8000805@sympatico.ca> Message-ID: <45EC238A.3040508@sympatico.ca> Neal Becker wrote: > Thanks. I've never tried to instantiate an object from c++. Any > examples/hints? ----- #include #include struct Guard { Guard() { std::cout << "default constructor" << std::endl;} Guard(Guard const &) { std::cout << "copy constructor" << std::endl;} ~Guard() { std::cout << "destructor" << std::endl;} }; using namespace boost::python; BOOST_PYTHON_MODULE(bpl) { class_ a("Guard"); scope().attr("_hidden") = Guard(); } ----- (As you will see running this code, the object here is passed by value, so you'll actually get two. You may want to change that...) HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From sgwoodjr at gmail.com Tue Mar 6 01:10:07 2007 From: sgwoodjr at gmail.com (Simon Wood) Date: Mon, 5 Mar 2007 19:10:07 -0500 Subject: [C++-sig] C++ assertion Message-ID: I am in the process of wrapping a C++ class using Boost.Python. Things are progressing well, however the underlying C++ library uses assert() for various error checks. Out of curiousity I triggerred one of the asserts() on an exported C++ function from within Python. Sure enough the assertion error printed (o.k.) AND the Python interpreter quit (not o.k.). So, is there a way to implement some Python code to capture the underlying C++ assertions, or do I have to modify the underlying library code? -Simon -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Tue Mar 6 01:19:46 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 05 Mar 2007 19:19:46 -0500 Subject: [C++-sig] C++ assertion In-Reply-To: References: Message-ID: <45ECB3A2.2030700@sympatico.ca> Simon Wood wrote: > I am in the process of wrapping a C++ class using Boost.Python. Things > are progressing well, however the underlying C++ library uses assert() > for various error checks. Out of curiousity I triggerred one of the > asserts() on an exported C++ function from within Python. Sure enough > the assertion error printed ( o.k.) AND the Python interpreter quit (not > o.k.). > > So, is there a way to implement some Python code to capture the > underlying C++ assertions, or do I have to modify the underlying library > code? The latter. An assertion is just that. It asserts a certain statement to be true, else aborts the running process. (If an assertion is false your program is likely to be in an invalid state, so there is no point continuing.) If you want something else you may consider throwing an exception. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From ext at sidvind.com Tue Mar 6 09:36:40 2007 From: ext at sidvind.com (David Sveningsson) Date: Tue, 06 Mar 2007 09:36:40 +0100 Subject: [C++-sig] Memory issues In-Reply-To: <062201c74ef3$ed136840$6c01a8c0@paul> References: <062201c74ef3$ed136840$6c01a8c0@paul> Message-ID: <45ED2818.9040107@sidvind.com> Paul Guse skrev: > Hello. I have a few boost::python issues I need help with . > > Firstly, I'm getting some memory leaks by just including the boost > libraries. (Dynamically linked dll, VS2005. Using boost to embed > python in c++) > From what I gather this is still an outstanding issue with no, as of > yet, solution? Is that correct? > > Second, this is also causing some issues as I need to free the > resources allocated during the running of scripts. Specifically, I'm > using the intrusive_ptr to wrap my own object's reference counting. > When these objects are referenced in a python script the reference > count is incremented, but not decremented when the script ends. As > expected, this is causing some issues. > Is there a way to clean up the variables allocated in a namespace? Or > to effectively clear the main namespace? > > Thanks. > Paul > > ------------------------------------------------------------------------ > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > Hi, did you make any progress? I have the same problem, it seems like the release function is never called when python destroys the references. Even setting a variable to None does not call the release function. The problem does not seems to be specific to a compiler since I'm having these issues with gcc4 on GNU/Linux and also tried compiling with VS2005 -- //*David Sveningsson [eXt]* Freelance coder | Game Development Student http://sidvind.com Thou shalt make thy program's purpose and structure clear to thy fellow man by using the One True Brace Style, even if thou likest it not, for thy creativity is better used in solving problems than in creating beautiful new impediments to understanding. From sgwoodjr at gmail.com Tue Mar 6 14:46:20 2007 From: sgwoodjr at gmail.com (Simon Wood) Date: Tue, 6 Mar 2007 08:46:20 -0500 Subject: [C++-sig] C++ assertion In-Reply-To: <45ECB3A2.2030700@sympatico.ca> References: <45ECB3A2.2030700@sympatico.ca> Message-ID: > > The latter. An assertion is just that. It asserts a certain statement > to be true, else aborts the running process. (If an assertion is false > your program is likely to be in an invalid state, so there is no point > continuing.) > If you want something else you may consider throwing an exception. > > Regards, > Stefan > > This is more or less what I figured to be true. Thanks for the confirmation. -Simon -------------- next part -------------- An HTML attachment was scrubbed... URL: From kir at lapshin.net Wed Mar 7 17:26:20 2007 From: kir at lapshin.net (Kirill Lapshin) Date: Wed, 07 Mar 2007 19:26:20 +0300 Subject: [C++-sig] passing python derived instance back to C++ Message-ID: I've spent couple of hours trying to diagnose quite a basic problem, finally solved it, and just for reference documenting it here, since my googling around did not reveal any solution. Hopefully it will save someone a bit of time. Suppose you have abstract C++ interface Base properly exposed to python via wrapper, etc. Suppose you have C++ function taking pointer to Base. Now suppose you want to implement Base interface in Python and pass it to C++ function. // C++ struct Base { virtual ~Base(){} virtual void foo() =0; }; void bar(Base * b) { } // Python class Derived(module.Base): def __init__(self): pass def foo(): print 'Derived.foo' module.bar(Derived()) At this point call to module.bar will fail complaining that it can't find matching C++ function. It turns out that solution is quite obvious -- just call base class constructor in Derived constructor, i.e. class Derived(module.Base): def __init__(self): module.Base.__init__(self) def foo(): print 'Derived.foo' From paul at mustagh.com Thu Mar 8 18:12:49 2007 From: paul at mustagh.com (Paul Guse) Date: Thu, 8 Mar 2007 10:12:49 -0700 Subject: [C++-sig] Memory issues References: <062201c74ef3$ed136840$6c01a8c0@paul> <45ED2818.9040107@sidvind.com> Message-ID: <01a201c761a5$00f20e70$6c01a8c0@paul> David wrote: >> > Hi, did you make any progress? I have the same problem, it seems like > the release function is never called when python destroys the > references. Even setting a variable to None does not call the release > function. > > The problem does not seems to be specific to a compiler since I'm having > these issues with gcc4 on GNU/Linux and also tried compiling with VS2005 > I haven't made any progress on the eliminating the memory leak but have accepted that it is a known one time memory deallocation problem. Which is important as it's not technically a leak as it doesn't continuously leak or grow with time. It's not pretty to see a dump at the end of a debug session, but it seems worth the trade off. As for the other issue I was having with intrusive pointers, I am now using a copy of the main module dictionary for all script runs. At the end of each run I clear and destroy the dictionary copy so all memory from it gets freed. Paul > > > //*David Sveningsson [eXt]* > > Freelance coder | Game Development Student > http://sidvind.com > > Thou shalt make thy program's purpose and structure clear to thy fellow > man by using the One True Brace Style, even if thou likest it not, for thy > creativity is better used in solving problems than in creating beautiful > new impediments to understanding. > > From p.jaroszynski at gmail.com Fri Mar 9 23:06:34 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Fri, 9 Mar 2007 23:06:34 +0100 Subject: [C++-sig] operator<< problems Message-ID: <200703092306.34922.p.jaroszynski@gmail.com> Hello, I have written a simple hello.cpp - [1], which doesn't compile - [2]. After some "investigation" I have tracked down the problem to boost/python/str.hpp header. Removing str.hpp and numeric.hpp, which includes it, from python.hpp make the hello.cpp compile and work just fine: >>> import hello >>> blah = hello.World() >>> blah.set("foo") >>> print blah foo System info: boost=1.33.1, gcc-4.1.2, glibc-2.5, kernel 2.6.20-gentoo x86_64, python-2.4.4 [1] = http://dev.gentooexperimental.org/~peper/boost/hello.cpp [2] - http://dev.gentooexperimental.org/~peper/boost/hello.error -- Best Regards, Piotr Jaroszynski From roman.yakovenko at gmail.com Sat Mar 10 11:33:09 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sat, 10 Mar 2007 12:33:09 +0200 Subject: [C++-sig] operator<< problems In-Reply-To: <200703092306.34922.p.jaroszynski@gmail.com> References: <200703092306.34922.p.jaroszynski@gmail.com> Message-ID: <7465b6170703100233j2a9317a7u7f665139dfa85240@mail.gmail.com> On 3/10/07, Piotr Jaroszynski wrote: > Hello, > > I have written a simple hello.cpp - [1], which doesn't compile - [2]. After > some "investigation" I have tracked down the problem to boost/python/str.hpp > header. Removing str.hpp and numeric.hpp, which includes it, from python.hpp > make the hello.cpp compile and work just fine: > >>> import hello > >>> blah = hello.World() > >>> blah.set("foo") > >>> print blah > foo > > System info: > boost=1.33.1, gcc-4.1.2, glibc-2.5, kernel 2.6.20-gentoo x86_64, python-2.4.4 > > [1] = http://dev.gentooexperimental.org/~peper/boost/hello.cpp > [2] - http://dev.gentooexperimental.org/~peper/boost/hello.error This is a known problem/bug. The work around is pretty simple: use boost::python::self_ns::str instead of boost::python::str. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From p.jaroszynski at gmail.com Sat Mar 10 12:29:57 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Sat, 10 Mar 2007 12:29:57 +0100 Subject: [C++-sig] operator<< problems In-Reply-To: <7465b6170703100233j2a9317a7u7f665139dfa85240@mail.gmail.com> References: <200703092306.34922.p.jaroszynski@gmail.com> <7465b6170703100233j2a9317a7u7f665139dfa85240@mail.gmail.com> Message-ID: <200703101229.58012.p.jaroszynski@gmail.com> > This is a known problem/bug. The work around is pretty simple: use > boost::python::self_ns::str instead of boost::python::str. If only I had known this earlier :D Many thanks! Btw. How would you bind copy constructor? To __copy__? If so please advise how. I tried: class_("VersionSpec", init()) .def(init()) but then the string constructor doesn't work: TypeError: No registered converter was able to extract a C++ reference to type std::string from this Python object of type str -- Best Regards, Piotr Jaroszynski From tim at klingt.org Sun Mar 11 01:36:40 2007 From: tim at klingt.org (Tim Blechmann) Date: Sun, 11 Mar 2007 01:36:40 +0100 Subject: [C++-sig] polymorphic class question Message-ID: <1173573400.9664.44.camel@localhost> hi all, i'm using lots of polymorphic classes in a mixed c++/python environment. the biggest problem for me are noncopyable c++ classes, that are derived from python and stored (via pointers) in c++ data structures. my_class: boost::noncopyable {}; in order to keep the reference, i introduced the following helper class: template struct self_containing_wrapper: public T { self_containing_wrapper(PyObject * self): self(self) { } virtual ~self_containing_wrapper(void){} PyObject * self; }; which is used to wrap classes in the following way: class_, boost::noncopyable>("my_class"); now i'm trying to write a c++-to-python converter in order to get the correct reference when wrapping functions that return pointers to instances of my_class: template struct self_containing_wrapper_converter { static PyObject* convert(T const & x) { self_containing_wrapper const & wrapper = static_cast const &>(x); return wrapper.self; } }; to be used like: to_python_converter >(); now i have two questions: - is this correct or do i need to take care in terms of reference counting? - when wrapped c++ functions return my_class*, will i have to specify the return_value_policy? if so, what policy should i use? thanks in advance ... tim -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org Which is more musical, a truck passing by a factory or a truck passing by a music school? John Cage -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From roman.yakovenko at gmail.com Sun Mar 11 07:49:57 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 11 Mar 2007 08:49:57 +0200 Subject: [C++-sig] polymorphic class question In-Reply-To: <1173573400.9664.44.camel@localhost> References: <1173573400.9664.44.camel@localhost> Message-ID: <7465b6170703102249h4a239d2fy8c7fbe99514880c9@mail.gmail.com> On 3/11/07, Tim Blechmann wrote: > hi all, > > i'm using lots of polymorphic classes in a mixed c++/python environment. > the biggest problem for me are noncopyable c++ classes, that are derived > from python and stored (via pointers) in c++ data structures. > > my_class: boost::noncopyable {}; > > in order to keep the reference, i introduced the following helper class: > > template > struct self_containing_wrapper: > public T > { > self_containing_wrapper(PyObject * self): > self(self) > { > } > > virtual ~self_containing_wrapper(void){} > > PyObject * self; > }; > > which is used to wrap classes in the following way: > > class_, boost::noncopyable>("my_class"); > > now i'm trying to write a c++-to-python converter in order to get the > correct reference when wrapping functions that return pointers to > instances of my_class: > > template > struct self_containing_wrapper_converter > { > static PyObject* convert(T const & x) > { > self_containing_wrapper const & wrapper = > static_cast const &>(x); > return wrapper.self; > } > }; > > to be used like: > > to_python_converter >(); > > now i have two questions: > - is this correct or do i need to take care in terms of reference > counting? > > - when wrapped c++ functions return my_class*, will i have to specify > the return_value_policy? if so, what policy should i use? What is wrong with Boost.Python wrapper class?( http://boost.org/libs/python/doc/v2/wrapper.html ) You also can get reference to the Python object: http://boost.org/libs/python/doc/v2/faq.html#xref -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From tim at klingt.org Sun Mar 11 10:17:04 2007 From: tim at klingt.org (Tim Blechmann) Date: Sun, 11 Mar 2007 10:17:04 +0100 Subject: [C++-sig] polymorphic class question In-Reply-To: <7465b6170703102249h4a239d2fy8c7fbe99514880c9@mail.gmail.com> References: <1173573400.9664.44.camel@localhost> <7465b6170703102249h4a239d2fy8c7fbe99514880c9@mail.gmail.com> Message-ID: <1173604624.21351.8.camel@localhost> On Sun, 2007-03-11 at 08:49 +0200, Roman Yakovenko wrote: > On 3/11/07, Tim Blechmann wrote: > > hi all, > > > > i'm using lots of polymorphic classes in a mixed c++/python environment. > > the biggest problem for me are noncopyable c++ classes, that are derived > > from python and stored (via pointers) in c++ data structures. > > > > my_class: boost::noncopyable {}; > > > > in order to keep the reference, i introduced the following helper class: > > > > template > > struct self_containing_wrapper: > > public T > > { > > self_containing_wrapper(PyObject * self): > > self(self) > > { > > } > > > > virtual ~self_containing_wrapper(void){} > > > > PyObject * self; > > }; > > > > which is used to wrap classes in the following way: > > > > class_, boost::noncopyable>("my_class"); > > > > now i'm trying to write a c++-to-python converter in order to get the > > correct reference when wrapping functions that return pointers to > > instances of my_class: > > > > template > > struct self_containing_wrapper_converter > > { > > static PyObject* convert(T const & x) > > { > > self_containing_wrapper const & wrapper = > > static_cast const &>(x); > > return wrapper.self; > > } > > }; > > > > to be used like: > > > > to_python_converter >(); > > > > now i have two questions: > > - is this correct or do i need to take care in terms of reference > > counting? > > > > - when wrapped c++ functions return my_class*, will i have to specify > > the return_value_policy? if so, what policy should i use? > > What is wrong with Boost.Python wrapper class?( > http://boost.org/libs/python/doc/v2/wrapper.html ) isn't Boost.Python wrapper only useful, when implementing virtual functions from python? get_override is the only documented member > You also can get reference to the Python object: > http://boost.org/libs/python/doc/v2/faq.html#xref that's what i was trying to implement without the need to wrap function explicitly but by using an implicit converter ... tim -- tim at klingt.org ICQ: 96771783 http://tim.klingt.org Silence is only frightening to people who are compulsively verbalizing. William S. Burroughs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From dialtone at gmail.com Sun Mar 11 15:03:09 2007 From: dialtone at gmail.com (Valentino Volonghi) Date: Sun, 11 Mar 2007 15:03:09 +0100 Subject: [C++-sig] Boost python and import error Message-ID: hi all, I'm trying to use boost to write python extensions in C++ under OSX. I'm trying to use the most basic example of a greet function. The extension module is inside a reactor package and it's called _reactor. Unfortunately after a compilation (that successfully finishes) I get the following error: >>> from reactor import _reactor Traceback (most recent call last): File "", line 1, in File "reactor/__init__.py", line 11, in from reactor import _reactor ImportError: dynamic module does not define init function (init_reactor) I've searched for this on google and found many answers but none worked. Actually after looking at boost/python.hpp source I think that init_reactor should be there since it's inserted by BOOST_PYTHON_MODULE macro. Do I really have to use boost.jam to compile this small example? (I ask because I tried hard to use it under osx last week and failed miserably, I simply can't use it and documentation is lacking at best). Thanks for information! Here is the full source: #include using namespace boost::python; char const* greet() { return "hello, world"; } BOOST_PYTHON_MODULE(_reactor) { def("greet", greet); } and here's the setup.py: #!/usr/bin/python import sys, os from os.path import join from distutils.core import setup from distutils.extension import Extension from distutils import sysconfig # break abstraction to set g++ as linker - is there better way? sysconfig._init_posix() compiler = "g++ -ftemplate-depth-50" #compiler += " -fkeep-inline-functions" # for debugging # workaround for bugs in Redhat 7.3 compiler if os.popen("g++ --version").read().strip() == "2.96": compiler += " -fno-inline " sysconfig._config_vars["CC"] = compiler if sys.platform == 'darwin': sysconfig._config_vars["LDSHARED"] = "g++ -undefined dynamic_lookup" else: sysconfig._config_vars["LDSHARED"] = "g++ -shared" ext_modules = [] include_dirs = ["./include", "/usr/local/include", "/opt/local/include/"] if os.environ.has_key("BOOST_INCLUDE"): include_dirs.append(os.environ["BOOST_INCLUDE"]) libraries = [] for libpath in (os.environ.get("LD_LIBRARY_PATH", "").split(":") + ["/usr/lib", "/lib", "/usr/local/lib", "/opt/local/lib/"]): paths = [os.path.join(libpath, libname) for libname in ("libboost_python.so", "libboost_python.dylib")] for path in paths: if os.path.exists(path): libraries.append("boost_python") break if not "boost_python" in libraries: libraries.append("boost_python-gcc-mt") ext_modules.append(Extension("reactor._reactor", [join("reactor", "%s.cpp" % name) for name in ("_reactor",)], libraries=libraries, library_dirs=["/usr/local/lib", "/opt/local/lib"], include_dirs=include_dirs)) setup( name = "reactor", packages=["reactor"], ext_modules=ext_modules, ) -- Valentino Volonghi aka Dialtone Now running MacOS X 10.4 TechBlog: http://vvolonghi.blogspot.com Home Page: http://www.twisted.it From dialtone at gmail.com Sun Mar 11 15:23:36 2007 From: dialtone at gmail.com (Valentino Volonghi) Date: Sun, 11 Mar 2007 15:23:36 +0100 Subject: [C++-sig] Boost python and import error In-Reply-To: References: Message-ID: On 3/11/07, Valentino Volonghi wrote: > and here's the setup.py: > > #!/usr/bin/python > import sys, os > from os.path import join > from distutils.core import setup > from distutils.extension import Extension > from distutils import sysconfig > > # break abstraction to set g++ as linker - is there better way? > sysconfig._init_posix() > compiler = "g++ -ftemplate-depth-50" > #compiler += " -fkeep-inline-functions" # for debugging > > # workaround for bugs in Redhat 7.3 compiler > if os.popen("g++ --version").read().strip() == "2.96": > compiler += " -fno-inline " > > sysconfig._config_vars["CC"] = compiler > if sys.platform == 'darwin': > sysconfig._config_vars["LDSHARED"] = "g++ -undefined dynamic_lookup" > else: > sysconfig._config_vars["LDSHARED"] = "g++ -shared" Ok, I guess this code (that I took from another example) didn't actually work at all. Removing it fixes many of the problems except a RuntimeWarning: /Users/dialtone/.pythonstartup.py:1: RuntimeWarning: Python C API version mismatch for module _reactor: This Python has API version 1013, module _reactor has version 1012. I guess this is because python 2.5 was released after boost 1.33. Thanks for all anyway :) -- Valentino Volonghi aka Dialtone Now running MacOS X 10.4 TechBlog: http://vvolonghi.blogspot.com Home Page: http://www.twisted.it From roman.yakovenko at gmail.com Sun Mar 11 16:56:36 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 11 Mar 2007 17:56:36 +0200 Subject: [C++-sig] Boost python and import error In-Reply-To: References: Message-ID: <7465b6170703110856t4f59ec95ua815c21748e14684@mail.gmail.com> On 3/11/07, Valentino Volonghi wrote: > Do I really have to use > boost.jam to compile this small example? (I ask because I tried hard > to use it under > osx last week and failed miserably, I simply can't use it and > documentation is lacking at best). No. It is important to compile Boost.Python shared library with bjam, but after this you can switch to your favorite build system. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From rwgk at yahoo.com Mon Mar 12 02:21:35 2007 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Sun, 11 Mar 2007 18:21:35 -0700 (PDT) Subject: [C++-sig] Boost python and import error Message-ID: <79964.74735.qm@web31112.mail.mud.yahoo.com> > No. It is important to compile Boost.Python shared library with bjam, > but after this you can switch to your favorite build system. You can build the boost_python runtime with any build system you like. E.g. I have been using scons for years. You just have to get the compile & link commands right. In the past there was a way to get bjam (version 1) show the commands it was issuing, I think it was -d2. I guess something similar if not the same should also work with the current bjam. Ralf ____________________________________________________________________________________ Sucker-punch spam with award-winning protection. Try the free Yahoo! Mail Beta. http://advision.webevents.yahoo.com/mailbeta/features_spam.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Mon Mar 12 06:07:46 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 12 Mar 2007 07:07:46 +0200 Subject: [C++-sig] Boost python and import error In-Reply-To: <79964.74735.qm@web31112.mail.mud.yahoo.com> References: <79964.74735.qm@web31112.mail.mud.yahoo.com> Message-ID: <7465b6170703112207w4fd9867o84a3118ad93b82ba@mail.gmail.com> On 3/12/07, Ralf W. Grosse-Kunstleve wrote: > > > No. It is important to compile Boost.Python shared library with bjam, > > > but after this you can switch to your favorite build system. > > You can build the boost_python runtime with any build system you like. E.g. > I have been using scons for years. You just have to get the compile & link > commands right. :-). Can you post your scons script? Thank you -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From p.jaroszynski at gmail.com Mon Mar 12 22:31:06 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Mon, 12 Mar 2007 22:31:06 +0100 Subject: [C++-sig] std::tr1::shared_ptr and C++ Singletons Message-ID: <200703122231.06711.p.jaroszynski@gmail.com> Hello, I have 2 problems: 1) class_ > doesn't work=compile errors while boost::shared_ptr does. In the code I am writing bindings for std::tr1::shared_ptr is used. 2) I am not sure how to handle C++ Singletons. I thought that the proper way is to use return_value_policy() for functions returning pointers to them as described in [1], but at the bottom of [2] Dave seems to disagree or I am missing something. Btw. how would you handle the situation when the get_it() is a method of object you want to get? I used: def("get_it", &Foo::get_instance, return_value_policy()); Is it the best solution? [1] - http://www.boost.org/libs/python/doc/v2/reference_existing_object.html [2] - http://mail.python.org/pipermail/c++-sig/2005-January/008481.html -- Best Regards, Piotr Jaroszynski From rwgk at yahoo.com Tue Mar 13 02:57:14 2007 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 12 Mar 2007 18:57:14 -0700 (PDT) Subject: [C++-sig] Boost python and import error Message-ID: <953236.7545.qm@web31111.mail.mud.yahoo.com> > :-). Can you post your scons script? Thank you There are two SConscripts which need a few supporting Python scripts. It is all part of the source bundles here: http://cci.lbl.gov/cctbx_build/all.html There are two approaches: 1. perl cctbx_python_25_bundle.selfx Capture the compile and link commands from the output and throw away everything else. Configure your own build system to spit out the same commands. The only complication is under Mac OS X, where a helper script is used: libtbx/libtbx/command_line/mac_os_x_ld_bpl_dll.csh 2. Use libtbx/SConscript boost_adaptbx/SConscript as a starting point for your own SConscipt. Copy some supporting Python code from the libtbx tree as needed. Everything is also under CVS: http://cctbx.cvs.sourceforge.net/cctbx/ Ralf ____________________________________________________________________________________ Don't get soaked. Take a quick peek at the forecast with the Yahoo! Search weather shortcut. http://tools.search.yahoo.com/shortcuts/#loc_weather -------------- next part -------------- An HTML attachment was scrubbed... URL: From hypertextual at phonereader.com Tue Mar 13 08:46:07 2007 From: hypertextual at phonereader.com (hypertextual at phonereader.com) Date: Tue, 13 Mar 2007 08:46:07 +0100 Subject: [C++-sig] Rhetorics of Hypertext Message-ID: <1db8c01c76543$a8cab6a0$17e3c13e@wpc1193> Rhetorics of Hypertext March 13th 2007 POSTS of the week : Imagination radicale vs imaginaire social (imagination repr?sentation image) http://phonereader.com/m/R?mail20en-46744-470-35 : Sweeping change (changing transformation mobile) http://phonereader.com/m/R?mail20en-46744-471-35 : Penser l'Hypertexte ? l'?ge de l'Internet mobile (pens?e philosophie th?orie) http://phonereader.com/m/R?mail20en-46744-472-35 : Shifting boundaries (limit domain linking) http://phonereader.com/m/R?mail20en-46744-473-35 : Modalit? et virtualit? (possible r?alit? virtualit?) http://phonereader.com/m/R?mail20en-46744-474-35 : Books and Hypertexts (hypertext writing book) http://phonereader.com/m/R?mail20en-46744-475-35 : Elargissement de notre perception (ph?nom?nologie Benjamin art) http://phonereader.com/m/R?mail20en-46744-476-35 : Mouth that bites (fragment post text) http://phonereader.com/m/R?mail20en-46744-477-35 : L'objet textu? et son axiomatique (algorithme calcul parcours) http://phonereader.com/m/R?mail20en-46744-478-35 : Citability and hypertext (word sentence source) http://phonereader.com/m/R?mail20en-46744-479-35 : Possible et objectivit? (r?alit? virtualit? mod?le) http://phonereader.com/m/R?mail20en-46744-480-35 : Reading practises (writing reading text) http://phonereader.com/m/R?mail20en-46744-481-35 : Primary sources (linking thread origin) http://phonereader.com/m/R?mail20en-46744-482-35 : Structure du flux de la conscience (ph?nom?nologie dur?e temps) http://phonereader.com/m/R?mail20en-46744-483-35 Jean-Philippe Pastor : Politique : http://phonereader.com/m/R?mail20en-46744-351-35 ? Moonstone Order by fax : http://phonereader.com/m/R?mail20en-46744-74-35 Critics of the book on Fabula : http://phonereader.com/m/R?mail20en-46744-75-35 E-Books Principe et succession ? http://phonereader.com/m/R?mail20en-46744-71-35 Fuse of time ? http://phonereader.com/m/R?mail20en-46744-72-35 Anticipations ? http://phonereader.com/m/R?mail20en-46744-57-35 Le Possible et l'inattendu ? http://phonereader.com/m/R?mail20en-46744-58-35 Monde des possibles ? http://phonereader.com/m/R?mail20en-46744-59-35 Reprises ? http://phonereader.com/m/R?mail20en-46744-60-35 Jacques Derrida ? http://phonereader.com/m/R?mail20en-46744-61-35 Symbolique de la mise en page ? http://phonereader.com/m/R?mail20en-46744-62-35 Exclusive reading ? http://phonereader.com/m/R?mail20en-46744-63-35 Huit Hypoth?ses ? http://phonereader.com/m/R?mail20en-46744-64-35 Mythe et temporalit? ? http://phonereader.com/m/R?mail20en-46744-65-35 Individualisme ? http://phonereader.com/m/R?mail20en-46744-66-35 Derrida, la contrefacture, l'absence. ? http://phonereader.com/m/R?mail20en-46744-67-35 Machines textuelles ? http://phonereader.com/m/R?mail20en-46744-68-35 L'inconcevable ? http://phonereader.com/m/R?mail20en-46744-69-35 Contre-coup ? http://phonereader.com/m/R?mail20en-46744-70-35 plus ... / more ... http://phonereader.com/m/R?mail20en-46744-78-35 French Metablog http://phonereader.com/m/R?mail20en-46744-76-35 MetaBlog en anglais http://phonereader.com/m/R?mail20en-46744-77-35 French Theory http://phonereader.com/m/R?mail20en-46744-78-35 Metabole mobile http://phonereader.com/m/R?mail20en-46744-79-35 Enregistrez-vous sur Hypertextual http://phonereader.com/m/R?mail20en-46744-80-35 Tous les livres sur : http://phonereader.com/m/R?mail20en-46744-81-35 Google Print ou sur amazon.com avec Castoriadis ou la création des possibles http://phonereader.com/m/R?mail20en-46744-82-35 Hypertexte principal http://phonereader.com/m/R?mail20en-46744-83-35 Dialogues et entretiens sur metabole.net http://phonereader.com/m/R?mail20en-46744-84-35 E-books avec PHONEREADER 3G Mobility http://phonereader.com/m/R?mail20en-46744-85-35 Archives en fran?ais http://phonereader.com/m/R?mail20en-46744-86-35 Podcast of the day http://phonereader.com/m/R?mail20en-46744-327-35 References actualit? , http://phonereader.com/m/R?mail20en-46744-39-35 ?cran , http://phonereader.com/m/R?mail20en-46744-40-35 inattendu , http://phonereader.com/m/R?mail20en-46744-41-35 litt?rature , http://phonereader.com/m/R?mail20en-46744-42-35 livres , http://phonereader.com/m/R?mail20en-46744-43-35 m?tabole , http://phonereader.com/m/R?mail20en-46744-44-35 musique , http://phonereader.com/m/R?mail20en-46744-45-35 narration , http://phonereader.com/m/R?mail20en-46744-46-35 politique , http://phonereader.com/m/R?mail20en-46744-47-35 possible , http://phonereader.com/m/R?mail20en-46744-48-35 religion , http://phonereader.com/m/R?mail20en-46744-49-35 science , http://phonereader.com/m/R?mail20en-46744-50-35 temps , http://phonereader.com/m/R?mail20en-46744-51-35 texte , http://phonereader.com/m/R?mail20en-46744-52-35 voir vid?os , http://phonereader.com/m/R?mail20en-46744-53-35 voyages , http://phonereader.com/m/R?mail20en-46744-54-35 weblogs , http://phonereader.com/m/R?mail20en-46744-55-35 web/Tech . http://phonereader.com/m/R?mail20en-46744-56-35 March 13th 2007 [...] Within this newsletter we look specifically at the changing definition of a book. A book is no longer a tangible thing; a book is what a book does. It is an information architecture. We examine the various manifestations of electronic book readers and imminent technologies, such electronic ink, including a case study on the use of ebook reading devices by a lending library, and speculate about other uses of such devices We see the convergence of print and etext - manifestations of the same thing - electronically stored text, with the difference demonstrated only in their final rendering. We look at changes in print technologies and the shift in mindset necessary to accommodate emergent forms of digital text - as information services within a product-service system, the changing shape of digital design and changes in printing technologies from letterpress to the rise of digital printing... This newsletter was written by Jean-Philippe PASTOR ** M?tabole ** http://phonereader.com/m/R?mail20en-46744-19-35 To not receive any mail, clic here : http://phonereader.com/m/R?mail20en-46744-0-35 -------------- next part -------------- An HTML attachment was scrubbed... URL: From duranlef at iro.umontreal.ca Wed Mar 14 21:17:13 2007 From: duranlef at iro.umontreal.ca (=?ISO-8859-1?Q?Fran=E7ois_Duranleau?=) Date: Wed, 14 Mar 2007 16:17:13 -0400 (EDT) Subject: [C++-sig] problem with iterator ranges from base class In-Reply-To: <200703122231.06711.p.jaroszynski@gmail.com> References: <200703122231.06711.p.jaroszynski@gmail.com> Message-ID: Hi! I have a base class that serves as implementation detail (not a class to be exposed to Python). That class defines a pair of begin/end member functions to access some iterator (classic). When I want to expose the range either as __iter__ or some property, I get errors like this: Traceback (most recent call last): File "", line 1, in ? Boost.Python.ArgumentError: Python argument types in None.None(TheClass) did not match C++ signature: None(boost::python::back_reference) Here is the sample code illustrating the problem: #include #include using namespace ::boost::python ; struct Base // meant as implementation detail { private : // private to illustrate that the exact type is not available // outside the class typedef ::std::vector< int > int_seq ; public : typedef int_seq::const_iterator const_iterator ; int_seq seq ; const_iterator begin() const { return seq.begin() ; } const_iterator end() const { return seq.end() ; } } ; struct TheClass : Base { TheClass() { seq.resize( 1 , 1 ) ; // just to put something in for the example } } ; BOOST_PYTHON_MODULE( iter_from_base ) { class_< TheClass >( "TheClass" ) // Both have problems: .def( "__iter__" , range( & TheClass::begin , & TheClass::end ) ) .add_property( "seq" , range( & TheClass::begin , & TheClass::end ) ) ; } The only way I found to work this out is, aside from exposing the base class, to wrap the begin/end functions, e.g.: TheClass::const_iterator wrap_begin( const TheClass& c ) { return c.begin() ; } TheClass::const_iterator wrap_end( const TheClass& c ) { return c.begin() ; } BOOST_PYTHON_MODULE( iter_from_base ) { class_< TheClass >( "TheClass" ) // Both have problems: .def( "__iter__" , range( & wrap_begin , & wrap_end ) ) ; } Which is a little bit inconvenient. Would there be any way to fix this otherwise? -- Fran?ois Duranleau LIGUM, Universit? de Montr?al From dave at boost-consulting.com Thu Mar 15 05:30:20 2007 From: dave at boost-consulting.com (David Abrahams) Date: Thu, 15 Mar 2007 00:30:20 -0400 Subject: [C++-sig] [Attention] BBv2 Python Build Support Message-ID: <87ird36sdf.fsf@valverde.peloton> ******************************************************************** *** The Boost release has been waiting on this; I would *really* *** *** appreciate it if people would take a little time to test it. *** ******************************************************************** I've just checked into the RC_1_34_0 branch most of the support for building and testing Python extensions and Boost.Python in BBv2. It took me over a week, and it's almost completely revamped. In case you're wondering, yes, it was necessary. Support for Windows, Darwin, and Cygwin was essentially nonexistent or broken. If it was working for you on any of these platforms, you were probably getting "lucky." I would appreciate it if those who can test this would invoke bjam with --debug-configuration, and look at the output to make sure the python configuration stuff looks sensible. Here's the new comment on python.init: # Initializes the Python toolset. Note that all parameters are # optional. # # - version -- the version of Python to use. Should be in Major.Minor # format, for example 2.3. Do not include the subminor version. # # - cmd-or-prefix: Preferably, a command that invokes a Python # interpreter. Alternatively, the installation prefix for Python # libraries and includes. If empty, will be guessed from the # version, the platform's installation patterns, and the python # executables that can be found in PATH. # # - includes: the include path to Python headers. If empty, will be # guessed. # # - libraries: the path to Python library binaries. If empty, will be # guessed. On MacOS/Darwin, you can also pass the path of the # Python framework. # # - condition: if specified, should be a set of properties that are # matched against the build configuration when Boost.Build selects a # Python configuration to use. # # Example usage: # # using python : 2.3 ; # using python : 2.3 : /usr/local/bin/python ; # You can set up your user-config.jam so a bjam built under Windows will can build/test both windows and cygwin python extensions. Just pass cygwin in the "condition" parameter to "using python..." for the cygwin python installation. using python ; # windows installation using python : : /usr/bin/python2.5 : : : cygwin ; when you put target-os=cygwin in your build request, it should build with the cygwin version of python: bjam target-os=cygwin toolset=gcc This is supposed to work the other way, too (targeting windows python with a cygwin bjam) but it seems as though the code in the compiler toolsets for building that way is broken (i.e., it's not a python-specific problem). What's still broken =================== 1. When targeting Cygwin, PYTHONPATH setup information is not properly passed from Python extension targets to test targets, because the information is getting attached to an import library (one we don't even need to generate) instead of the shared library. This affects one test (builtin_converters) in the Boost.Python test suite. We're waiting on Vladimir Prus to fix that one. I hope he'll be able to do that in the next few days. 2. There's no support for debug-python builds (the ones that require the trailing "_d" in the executable and extension module names -- see http://boost.org/libs/python/doc/building.html#variants). I think I can implement that tomorrow. If you got this far in the message, thank you *very* much for your attention :) -- Dave Abrahams Boost Consulting www.boost-consulting.com From adrien-ml at wizzi.net Thu Mar 15 11:51:03 2007 From: adrien-ml at wizzi.net (Adrien Saladin) Date: Thu, 15 Mar 2007 11:51:03 +0100 Subject: [C++-sig] virtual method issue ? Message-ID: <200703151151.03499.adrien-ml@wizzi.net> Dear all, I'm exposing a C++ library to python using boost python. Almost everything works fine except this: I have a Minimizer class that needs a "function" object to minimize. These objects communicates with std::vector and I think the problem is around there... I had to (partially) expose std::vector to python because of a lvalue convertor problem. The function object is derived from an abstract class "Base". When I try to redefine the function in python, the std::vector doesn't seem to be correctly transmitted between objects. You will find bellow the minimal example I found to reproduce the undesired feature... Any advice would be really appreciated ! Thanks, Adrien ============ C++ CODE =============== #include #include #include #include #include #include #include #include #include #include typedef std::vector Vdouble; using namespace boost::python; struct Base { virtual ~Base(){}; virtual double Function(const Vdouble& X, Vdouble& Grad)=0; }; struct BaseWrap: Base, wrapper { double Function(const Vdouble& v, Vdouble& Grad) { return this->get_override("Function")(v,Grad); } }; struct Minimizer { Minimizer(Base& base):_base(base) { _vec.resize(5); _Grad.resize(5); for (uint i=0; i<5; i++) {_vec[i]=0.0; _Grad[i]=0.0;} }; void minimize(){ _base.Function(_vec, _Grad); std::cout << "_Grad[0] =" << _Grad[0] << std::endl; assert(_Grad[0]>=5.0); } //Data: Base& _base; Vdouble _vec; Vdouble _Grad; }; struct Derived: public Base { double Function(const Vdouble& v, Vdouble& grad) { grad[0]=12; return v[0]; } }; using namespace boost::python; BOOST_PYTHON_MODULE(minim) { class_ >("std_vector_double") .def(vector_indexing_suite >()) ; class_("Base") .def("Function",pure_virtual(&Base::Function)); class_ >("Derived") .def("Function",&Derived::Function ); class_("Minimizer", init< Base& >() ) .def("minimize", &Minimizer::minimize); } ========== END C++ CODE ======== compilation: g++ -o minim.so -shared minim.cpp -I/usr/include/python2.4 -lboost_python -lpython2.4 ========== PYTHON CODE: ========= from minim import * class PyDerived(Base): def Function(self, v, grad): grad[0]=45.0 return 0 pyderived=PyDerived() v=std_vector_double() grad=std_vector_double() for i in range(5): v.append(0.0) grad.append(0.0) derived=Derived() minim=Minimizer(derived) minim.minimize() #this one is OK assert grad[0]>5.0, "grad[0]>5.0" #this one is OK pyderived.Function(v,grad) print grad[0] assert grad[0]==45.0, "grad[0]==45.0" #this one is OK minim=Minimizer(pyderived) minim.minimize() # <--- Here I get a C++ assert error (not OK...) =========== END PYTHON CODE ================== From ndbecker2 at gmail.com Thu Mar 15 12:22:14 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 15 Mar 2007 07:22:14 -0400 Subject: [C++-sig] [Attention] BBv2 Python Build Support References: <87ird36sdf.fsf@valverde.peloton> Message-ID: David Abrahams wrote: > > I've just checked into the RC_1_34_0 branch most of the support for > building and testing Python extensions and Boost.Python in BBv2. Will this appear in the cvs head? From ndbecker2 at gmail.com Thu Mar 15 19:04:53 2007 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 15 Mar 2007 14:04:53 -0400 Subject: [C++-sig] [Attention] BBv2 Python Build Support References: <87ird36sdf.fsf@valverde.peloton> <874pom8l3j.fsf@valverde.peloton> Message-ID: David Abrahams wrote: > > on Thu Mar 15 2007, Neal Becker wrote: > >> David Abrahams wrote: >> >>> >>> I've just checked into the RC_1_34_0 branch most of the support for >>> building and testing Python extensions and Boost.Python in BBv2. >> >> Will this appear in the cvs head? > > Yeah, eventually. How soon do you need it? > Eventually, I guess. I must be lucky, I didn't even notice I needed it. Just thought I'd test it. From dave at boost-consulting.com Thu Mar 15 18:36:48 2007 From: dave at boost-consulting.com (David Abrahams) Date: Thu, 15 Mar 2007 13:36:48 -0400 Subject: [C++-sig] [Attention] BBv2 Python Build Support References: <87ird36sdf.fsf@valverde.peloton> Message-ID: <874pom8l3j.fsf@valverde.peloton> on Thu Mar 15 2007, Neal Becker wrote: > David Abrahams wrote: > >> >> I've just checked into the RC_1_34_0 branch most of the support for >> building and testing Python extensions and Boost.Python in BBv2. > > Will this appear in the cvs head? Yeah, eventually. How soon do you need it? -- Dave Abrahams Boost Consulting www.boost-consulting.com From roman.yakovenko at gmail.com Thu Mar 15 19:20:37 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 15 Mar 2007 20:20:37 +0200 Subject: [C++-sig] [boost] [Attention] BBv2 Python Build Support In-Reply-To: <874pom8l3j.fsf@valverde.peloton> References: <87ird36sdf.fsf@valverde.peloton> <874pom8l3j.fsf@valverde.peloton> Message-ID: <7465b6170703151120i78cfcf4frb0a2ebd1454f84e8@mail.gmail.com> On 3/15/07, David Abrahams wrote: > > on Thu Mar 15 2007, Neal Becker wrote: > > > David Abrahams wrote: > > > >> > >> I've just checked into the RC_1_34_0 branch most of the support for > >> building and testing Python extensions and Boost.Python in BBv2. > > > > Will this appear in the cvs head? > > Yeah, eventually. How soon do you need it? If you want to know problems sooner than later, than it is better to add it to cvs head. Python-Ogre project has new users, who join the project almost every day. The only way to use it on MAC and Linux is to build it from source code. Thus Boost.Python is built and tested on MAC, Linux and Windows. It is built against Python 2.4 and 2.5. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Thu Mar 15 21:10:54 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 15 Mar 2007 22:10:54 +0200 Subject: [C++-sig] virtual method issue ? In-Reply-To: <200703151151.03499.adrien-ml@wizzi.net> References: <200703151151.03499.adrien-ml@wizzi.net> Message-ID: <7465b6170703151310j59175a2es427eb9c0e2095307@mail.gmail.com> On 3/15/07, Adrien Saladin wrote: > > Dear all, > > I'm exposing a C++ library to python using boost python. > Almost everything works fine except this: > > I have a Minimizer class that needs a "function" object to minimize. These > objects communicates with std::vector and I think the problem is > around there... > > I had to (partially) expose std::vector to python because of a > lvalue > convertor problem. > > The function object is derived from an abstract class "Base". > When I try to redefine the function in python, the std::vector doesn't > seem to > be correctly transmitted between objects. > > You will find bellow the minimal example I found to reproduce the > undesired > feature... > Any advice would be really appreciated ! Your code contains one error and few places where better code could be created: 1. Minimizer(Base& base) - when you expose this constructor you'd better use call policies, otherwise you will have to keep base instance somewhere And the error: class PyDerived(Base): def Function(self, v, grad): grad[0]=45.0 return 0 You have to call __init__ method of Base class class PyDerived(Base): def __init__( self ): Base.__init__( self ) def Function(self, v, grad): grad[0]=45.0 return 0 This should fix the assert you get. P.S. This error comes too often. Is it possible to add an exception, which will give a hint to a user what should be done? Thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From p.jaroszynski at gmail.com Thu Mar 15 22:39:01 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Thu, 15 Mar 2007 22:39:01 +0100 Subject: [C++-sig] std::tr1::shared_ptr and C++ Singletons In-Reply-To: <200703122231.06711.p.jaroszynski@gmail.com> References: <200703122231.06711.p.jaroszynski@gmail.com> Message-ID: <200703152239.01959.p.jaroszynski@gmail.com> > 1) class_ > doesn't work=compile errors > while boost::shared_ptr does. In the code I am writing bindings for > std::tr1::shared_ptr is used. The fix is rather simple, one needs to declare the following get_pointer template before including boost/python.hpp. Maybe it's worth adding to the boost? namespace boost { template inline T * get_pointer(std::tr1::shared_ptr const & p){ return p.get(); } } -- Best Regards, Piotr Jaroszynski From adrien-ml at wizzi.net Thu Mar 15 23:05:12 2007 From: adrien-ml at wizzi.net (Adrien Saladin) Date: Thu, 15 Mar 2007 23:05:12 +0100 Subject: [C++-sig] virtual method issue ? In-Reply-To: <7465b6170703151310j59175a2es427eb9c0e2095307@mail.gmail.com> References: <200703151151.03499.adrien-ml@wizzi.net> <7465b6170703151310j59175a2es427eb9c0e2095307@mail.gmail.com> Message-ID: <200703152305.12798.adrien-ml@wizzi.net> Le jeudi 15 mars 2007 21:10, Roman Yakovenko a ?crit?: > > Your code contains one error and few places where better code could be > created: > > 1. Minimizer(Base& base) - when you expose this constructor you'd better > use call policies, otherwise > you will have to keep base instance somewhere > Hello, Thanks for the advice, I will probably have to modify my c++ code. Here the problem is the same than in C++ right ? If a C++ or python function returns a Minimizer instance and if the Base instance has been created "on the stack" in this function, it will reference a non-existing object. Which call policy is the more adequate in this case ? > And the error: > > class PyDerived(Base): > def Function(self, v, grad): > grad[0]=45.0 > return 0 > > > You have to call __init__ method of Base class > > class PyDerived(Base): > def __init__( self ): > Base.__init__( self ) > > def Function(self, v, grad): > grad[0]=45.0 > return 0 > > This should fix the assert you get. I changed the PyDerived class to this new one and I still get the C++ assert: _Grad[0] =0 python: minim.cpp:49: void Minimizer::minimize(): Assertion `_Grad[0]>=5.0' failed. Best regards, Adrien From seefeld at sympatico.ca Thu Mar 15 23:26:52 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 15 Mar 2007 18:26:52 -0400 Subject: [C++-sig] std::tr1::shared_ptr and C++ Singletons In-Reply-To: <200703152239.01959.p.jaroszynski@gmail.com> References: <200703122231.06711.p.jaroszynski@gmail.com> <200703152239.01959.p.jaroszynski@gmail.com> Message-ID: <45F9C82C.6090805@sympatico.ca> Piotr Jaroszynski wrote: >> 1) class_ > doesn't work=compile errors >> while boost::shared_ptr does. In the code I am writing bindings for >> std::tr1::shared_ptr is used. > The fix is rather simple, one needs to declare the following get_pointer > template before including boost/python.hpp. Maybe it's worth adding to the > boost? > > namespace boost { > template inline T * get_pointer(std::tr1::shared_ptr const & p){ > return p.get(); > } > } As I said on IRC: I don't think this is 'correct', as boost and tr1 are two disjunct sets of APIs. I don't know the details of how the converters work, but I'd guess just declaring the above get_pointer() function so it makes its way into the overload set seen when the compiler instantiates the conerter harness should be enough. If it isn't, a (simple, minimal) test case may be a good idea. :-) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From j.reid at mail.cryst.bbk.ac.uk Fri Mar 16 09:35:09 2007 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Fri, 16 Mar 2007 08:35:09 +0000 Subject: [C++-sig] Create sub-module Message-ID: Hi, Is it possible to create a sub-module inside a boost.python created module? At the moment I am using separate DLLs for this. I had a look at the documentation for scope and didn't find anything. I did find this old post: http://mail.python.org/pipermail/c++-sig/2002-June/001297.html Is the recommended way to use separate DLLs? John. From roman.yakovenko at gmail.com Fri Mar 16 11:24:41 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 16 Mar 2007 12:24:41 +0200 Subject: [C++-sig] virtual method issue ? In-Reply-To: <200703152305.12798.adrien-ml@wizzi.net> References: <200703151151.03499.adrien-ml@wizzi.net> <7465b6170703151310j59175a2es427eb9c0e2095307@mail.gmail.com> <200703152305.12798.adrien-ml@wizzi.net> Message-ID: <7465b6170703160324n56b0bdaax66bf04bdba529f5e@mail.gmail.com> On 3/16/07, Adrien Saladin wrote: > _Grad[0] =0 > python: minim.cpp:49: void Minimizer::minimize(): Assertion `_Grad[0]>=5.0' > failed. struct BaseWrap: Base, wrapper { double Function(const Vdouble& v, Vdouble& Grad) { return this->get_override("Function")(v,Grad); } }; This code should be changed to return this->get_override("Function")(boost::ref(v), boost::ref(Grad)); By default boost::python::object holds a copy of the instance. The code generator I created, Py++ is aware to this and generates right code. It has GUI, so you can create simple test case and see what code it will generate. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From p.jaroszynski at gmail.com Fri Mar 16 14:03:06 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Fri, 16 Mar 2007 14:03:06 +0100 Subject: [C++-sig] std::tr1::shared_ptr and C++ Singletons In-Reply-To: <45F9C82C.6090805@sympatico.ca> References: <200703122231.06711.p.jaroszynski@gmail.com> <200703152239.01959.p.jaroszynski@gmail.com> <45F9C82C.6090805@sympatico.ca> Message-ID: <200703161403.07170.p.jaroszynski@gmail.com> > As I said on IRC: I don't think this is 'correct', as boost and tr1 are two > disjunct sets of APIs. I don't know the details of how the converters work, > but I'd guess just declaring the above get_pointer() function so it makes > its way into the overload set seen when the compiler instantiates the > conerter harness should be enough. Seems you were at least partially right. I did some addition research and testcases follow. > If it isn't, a (simple, minimal) test case may be a good idea. :-) Here we go: testcase1 - [1] - couldn't be any simpler - compiles just fine testcase2 - [2] - putting foo in the blah namespace breaks - [3] testcase3 - [4] - workaround for testcase2, notice that including the boost/python.hpp before the template definition or not putting the template in the boost namespace breaks it. [1] - http://dev.gentooexperimental.org/~peper/boost/testcase1.cc [2] - http://dev.gentooexperimental.org/~peper/boost/testcase2.cc [3] - http://dev.gentooexperimental.org/~peper/boost/testcase2_error.txt [4] - http://dev.gentooexperimental.org/~peper/boost/testcase3.cc boost 1.33.1 and gcc 4.1.2. -- Best Regards, Piotr Jaroszynski From luca.sbardella at gmail.com Sat Mar 17 20:43:30 2007 From: luca.sbardella at gmail.com (Luca Sbardella) Date: Sat, 17 Mar 2007 20:43:30 +0100 Subject: [C++-sig] building boost_python with mingw in eclipse Message-ID: <8315652a0703171243p696f835csd4940b3b0fbfd2ca@mail.gmail.com> Hi all, I'm C++ programmer looking to port my libraries into python. After some time I decided to go with boost_python template wrapping approach. I need to build the application under windows and I would like to use eclipse for the job combines with the mingw compiler. Compilation works fine but it just won't link. Do anybody know what flags I need to use in the compiler or linker to make this work? I successfully complied boost_python using visual studio and bjam but I would like to get this working in eclipse with mingw. In eclipse I use for make the mingw32-make.exe, extra compiler flags BOOST_PYTHON_DYNAMIC_LIB and BOOST_PYTHON_SOURCE and the python library directory is included in the path (needless to say the boost and python include directories are also included). Any help would be appreciated. Luca -------------- next part -------------- An HTML attachment was scrubbed... URL: From adrien-ml at wizzi.net Sat Mar 17 23:19:31 2007 From: adrien-ml at wizzi.net (Adrien Saladin) Date: Sat, 17 Mar 2007 23:19:31 +0100 Subject: [C++-sig] virtual method issue ? In-Reply-To: <7465b6170703160324n56b0bdaax66bf04bdba529f5e@mail.gmail.com> References: <200703151151.03499.adrien-ml@wizzi.net> <200703152305.12798.adrien-ml@wizzi.net> <7465b6170703160324n56b0bdaax66bf04bdba529f5e@mail.gmail.com> Message-ID: <200703172319.31725.adrien-ml@wizzi.net> Le vendredi 16 mars 2007 11:24, Roman Yakovenko a ?crit?: > This code should be changed to > > return this->get_override("Function")(boost::ref(v), boost::ref(Grad)); > > By default boost::python::object holds a copy of the instance. It works perfectly now. Thanks ! > The code generator I created, Py++ is aware to this and generates right > code. It has GUI, so you can create simple test case and see what code it > will generate. I've installed Py++ and I think I will learn many things from it. Best regards, Adrien From kornerr at gmail.com Sun Mar 18 04:20:51 2007 From: kornerr at gmail.com (michael kapelko) Date: Sun, 18 Mar 2007 10:20:51 +0700 Subject: [C++-sig] How to do inheritance in boost.python? Message-ID: <45FCB013.2040004@gmail.com> I have this: #include #include using namespace boost::python; using namespace std; class Base { public: virtual ~Base() { } virtual int f() = 0; }; class BaseWrap: public Base, public wrapper { public: int f() { return this->get_override("f")(); } }; BOOST_PYTHON_MODULE(hello) { class_("Base") .def("f", pure_virtual(&Base::f)) ; } Module compiles ok into hello.so. But then I do in Python: >>> import hello >>> base = Base(); Traceback (most recent call last): File "", line 1, in ? NameError: name 'Base' is not defined >>> class Derived(Base): ... def f(self): ... return 42; ... Traceback (most recent call last): File "", line 1, in ? NameError: name 'Base' is not defined What's wrong with the code? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Also, here's a code with not pure virtual func: #include #include using namespace boost::python; using namespace std; class Base { public: virtual ~Base() { } virtual int f() { return 0; } }; class BaseWrap: public Base, public wrapper { public: int f() { if (override f = this->get_override("f")) return f(); return Base::f(); } int default_f() { return this->Base::f(); } }; BOOST_PYTHON_MODULE(hello) { class_("Base") .def("f", &Base::f, &BaseWrap::default_f) ; } And in Python I get the same error that Base isn't defined. Thanks. From luca.sbardella at gmail.com Sun Mar 18 11:04:30 2007 From: luca.sbardella at gmail.com (Luca Sbardella) Date: Sun, 18 Mar 2007 11:04:30 +0100 Subject: [C++-sig] building boost_python with mingw in eclipse Message-ID: <8315652a0703180304u12f3e11chf07acf3a92537808@mail.gmail.com> Basically, I get a lots of 'undefined reference to _imp__'. This seems a problem connected with the handling of __dllimport/__dllexport. Do I need to include an extra flag in the Preprocessor stage (for the debug build I currently use _DEBUG, BOOST_PYTHON_DYNAMIC_LIB and BOOST_PYTHON_SOURCE). Or anything else I need to check ? Best Regards Luca -------------- next part -------------- An HTML attachment was scrubbed... URL: From p.jaroszynski at gmail.com Sun Mar 18 12:48:11 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Sun, 18 Mar 2007 12:48:11 +0100 Subject: [C++-sig] How to do inheritance in boost.python? In-Reply-To: <45FCB013.2040004@gmail.com> References: <45FCB013.2040004@gmail.com> Message-ID: <200703181248.11300.p.jaroszynski@gmail.com> On Sunday 18 of March 2007 04:20:51 michael kapelko wrote: > >>> import hello > >>> base = Base(); base = hello.Base(); or from hello import * IMHO, you should get a little more familiar with python before playing with boost.python. -- Best Regards, Piotr Jaroszynski From roman.yakovenko at gmail.com Mon Mar 19 19:41:15 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 19 Mar 2007 20:41:15 +0200 Subject: [C++-sig] shared_ptr< const T> patch Message-ID: <7465b6170703191141g7eeb8126tcf70dbaee27cdeb3@mail.gmail.com> Good evening. Few month ago I created patch to boost::shared_ptr< const T> problem. You can find full thread here: http://mail.python.org/pipermail/c++-sig/2006-November/011626.html The patch was approved by David Abrahams, but for some reason it was not applied. Could it be applied now? Thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: pointer_holder.hpp.patch Type: text/x-patch Size: 3094 bytes Desc: not available URL: From roman.yakovenko at gmail.com Mon Mar 19 20:39:57 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 19 Mar 2007 21:39:57 +0200 Subject: [C++-sig] Indexing Suite V2 Message-ID: <7465b6170703191239t55d5aa89l15a00bf62464569a@mail.gmail.com> Good evening. Boost.Python library, out of the box, provides support for std::map and std::vector containers. 3 years ago Raoul Gough created new indexing suite, which supports almost all C++ containers. Few month ago I started to use it and found few bugs and missing functionality. The found bugs were fixed and functionality was added. Last year there were 2 discussion on this subject: http://mail.python.org/pipermail/c++-sig/2006-June/010830.html http://mail.python.org/pipermail/c++-sig/2006-June/010835.html >From my experience the indexes suites can peacefully coexist within a project. You can use only one of them, but still it is possible to compile both of them. The new indexing suite was built and tested on Windows( MSVC 7.1 ), Linux( gcc 4.1 ) and MAC. It was reported that it works on other gcc versions too. New indexing suite allowed me to create new and pretty useful call policy - return_range( http://language-binding.net/pyplusplus/documentation/functions/call_policies.html#return-range) I will be glad to contribute it to the library. Now the question: is it possible to add new indexing suite to the library? This step will simplify the installation process. Thanks. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From mark at image-engine.com Mon Mar 19 21:21:27 2007 From: mark at image-engine.com (Mark Williams) Date: Mon, 19 Mar 2007 12:21:27 -0800 Subject: [C++-sig] ownership of C++ object extended in Python Message-ID: <45FEF0C7.9000608@image-engine.com> In the Python FAQ Wiki at http://wiki.python.org/moin/boost.python/HowTo I see a heading titled "ownership of C++ object extended in Python" which discusses how to keep a PyObject* alive while the object it represents still exists in C++. It suggests writing a wrapper as follows: class MyClassWrap : public MyClass { MyClassWrap( PyObject* self_) : self(self_) { PyINCREF(self); } MyClassWrap( PyObject* self_, const MyClass& copy ) : MyClass(copy), self(self_) { PyINCREF(self); } ~MyClassWrap() { PyDECREF(self); } ... PyObject *self; }; I'm guessing (probably incorrectly) that this was written before the introduction of boost::python::wrapper<>. What I'd like to do is something like.. class MyClassWrap : public MyClass, wrapper { MyClassWrap() { Py_INCREF( detail::wrapper_base_::get_owner(*this) ) } ~MyClassWrap() { Py_DECREF( detail::wrapper_base_::get_owner(*this) ) } /* ... wrapped methods here ... */ } class_< MyClass, boost::intrusive_ptr, boost::noncopyable>("MyClass"); .. but of course get_owner returns 0 from within MyClassWrap() What is the correct way of incrementing/decrementing the reference count for the underlying PyObject* when using wrapper<>, please? Thanks in advance, Mark From roman.yakovenko at gmail.com Mon Mar 19 21:01:45 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 19 Mar 2007 22:01:45 +0200 Subject: [C++-sig] ownership of C++ object extended in Python In-Reply-To: <45FEF0C7.9000608@image-engine.com> References: <45FEF0C7.9000608@image-engine.com> Message-ID: <7465b6170703191301l67eae0cra0298d609bebe824@mail.gmail.com> On 3/19/07, Mark Williams wrote: > > In the Python FAQ Wiki at http://wiki.python.org/moin/boost.python/HowTo > I see a heading titled "ownership of C++ object extended in Python" > which discusses how to keep a PyObject* alive while the object it > represents still exists in C++. It suggests writing a wrapper as follows: > > class MyClassWrap : public MyClass > { > MyClassWrap( PyObject* self_) : self(self_) { PyINCREF(self); } > MyClassWrap( PyObject* self_, const MyClass& copy ) : MyClass(copy), > self(self_) { PyINCREF(self); } > ~MyClassWrap() { PyDECREF(self); } > ... > PyObject *self; > }; > > > I'm guessing (probably incorrectly) that this was written before the > introduction of boost::python::wrapper<>. What I'd like to do is > something like.. > > class MyClassWrap : public MyClass, wrapper > { > MyClassWrap() > { > Py_INCREF( detail::wrapper_base_::get_owner(*this) ) > } > > ~MyClassWrap() > { > Py_DECREF( detail::wrapper_base_::get_owner(*this) ) > } > > /* ... wrapped methods here ... */ > } > > class_< > MyClass, > boost::intrusive_ptr, > boost::noncopyable>("MyClass"); You wanted to write MyClassWrap, right? > .. but of course get_owner returns 0 from within MyClassWrap() > > What is the correct way of incrementing/decrementing the reference count > for the underlying PyObject* when using wrapper<>, please? This is because Boost.Python first creates instance of class MyClassWrap and only than it initializes "owner" - reference to "self" object. The only way to handle it right is inject "initialize on first use" code in every virtual function call. I even added this option to Py++. Take a look on transfer_ownership_tester.py ( http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/unittests/ ) You will find header file in "data" directory. I attached the generated code. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- A non-text attachment was scrubbed... Name: transfer_ownership.cpp Type: text/x-c++src Size: 4568 bytes Desc: not available URL: From mark at image-engine.com Mon Mar 19 23:21:48 2007 From: mark at image-engine.com (Mark Williams) Date: Mon, 19 Mar 2007 14:21:48 -0800 Subject: [C++-sig] ownership of C++ object extended in Python In-Reply-To: <7465b6170703191301l67eae0cra0298d609bebe824@mail.gmail.com> References: <45FEF0C7.9000608@image-engine.com> <7465b6170703191301l67eae0cra0298d609bebe824@mail.gmail.com> Message-ID: <45FF0CFC.9050506@image-engine.com> > virtual ~event_t_wrapper(){ > if (this->m_pyobj) { > //Py_DECREF(this->m_pyobj); > this->m_pyobj = 0; > } > } > Many thanks for the solution - it appears to be working just fine. However I'm wondering why the Py_DECREF line above is commented out? Thanks again, Mark From mark at image-engine.com Tue Mar 20 00:22:00 2007 From: mark at image-engine.com (Mark Williams) Date: Mon, 19 Mar 2007 15:22:00 -0800 Subject: [C++-sig] ownership of C++ object extended in Python In-Reply-To: <45FF0CFC.9050506@image-engine.com> References: <45FEF0C7.9000608@image-engine.com> <7465b6170703191301l67eae0cra0298d609bebe824@mail.gmail.com> <45FF0CFC.9050506@image-engine.com> Message-ID: <45FF1B18.8090007@image-engine.com> >> virtual ~event_t_wrapper(){ >> if (this->m_pyobj) { >> //Py_DECREF(this->m_pyobj); >> this->m_pyobj = 0; >> } >> } >> > > Many thanks for the solution - it appears to be working just fine. > However I'm wondering why the Py_DECREF line above is commented out? > One other thing - it would appear that if I add a __del__ method to my Python class then its destructor IS getting called when the instance goes out of scope, even though my C++ class is keeping a reference to its holder. When the C++ class eventually calls, via the wrapper, the virtual member function implemented in Python will this not lead to undefined behaviour, especially if this member function relies on any instance data? Why is it valid to call Py_INCREF only when the virtual member is called? Is there really no way of being able to do it from the wrapper's constructor? What I have essentially is this: class PythonDerived( CppBase ): def __init__(self): CppBase.__init__(self) def __del__(self): print("__del__") def func(self): #Override pure_virtual in CppBase print("PythonDerived.func") def test(): pd = PythonDerived() globalCppHolder.hold( pd ) # "pd" goes out of scope here > test() __del__ > globalCppHolder.callFuncInAllHeldObjects() PythonDerived.func So "PythonDerived.func" gets called after the destructor. Thanks again, Mark I'm wrapping CppBase as suggested in the example code you kindly sent. From shawn.mcgrath at gmail.com Tue Mar 20 06:11:01 2007 From: shawn.mcgrath at gmail.com (Shawn McGrath) Date: Tue, 20 Mar 2007 01:11:01 -0400 Subject: [C++-sig] Trouble exposing a class function that returns std::vector<> Message-ID: <8043863a0703192211u5ef94681x8a7725ab931fabda@mail.gmail.com> I have a type defined in my existing C++ code as follows: typedef std::vector RenderObjectList; and a class with a member function as follows: class GraphicObject { public: //... inline RenderObjectList &getRenderObjectList() { return render_object_list; } //... }; The vector type is exposed to python via: boost::python::class_("RenderObjectList") .def(boost::python::vector_indexing_suite()); How can I expose GraphicObject::getRenderObjectList() to python? I tried: .def("getRenderObjectList", &graphics::GraphicObject::getRenderObjectList) which gives error: error: no matching function for call to `boost::python::class_::def(const char[20], )' I tried .def("getRenderObjectList", &graphics::GraphicObject::getRenderObjectList, boost::python::return_internal_reference<>()) which gives error: error: no matching function for call to `boost::python::class_::def(const char[20], , boost::python::return_internal_reference<1u, boost::python::default_call_policies>)' I'm sure I just have to set the correct call policy, but I don't know the syntax, can anyone help? Thanks a lot, Shawn. From pk at cs.tut.fi Tue Mar 20 12:59:03 2007 From: pk at cs.tut.fi (=?ISO-8859-1?Q?Pertti_Kellom=E4ki?=) Date: Tue, 20 Mar 2007 13:59:03 +0200 Subject: [C++-sig] Problems with an instance of a Python class Message-ID: <45FFCC87.9040808@cs.tut.fi> I am having troubles using an instance of a Python class. The offending code is below. I can create the instance and call its method without problems, but when the scope ends, I get a segmentation fault with the stack trace #0 PyErr_Fetch #1 PyInstance_NewRaw #2 ~object_base #3 ~object If I comment out the instance creation and calling the method of the instance, there are no problems, so it seems that the problem is in instance's destructor. I'm assuming it is something simple that I am overlooking, which causes reference counting to go haywire. Any ideas? Pertti ------------------------------------------- { Py_Initialize(); // Run Python file defining PythonSchedulerClass FILE *fp = fopen(pythonFile_.c_str(), "r"); PyRun_SimpleFileEx(fp, pythonFile_.c_str(), 1); // Retrieve the main module object main_module(( handle<>(boost::python::borrowed( PyImport_AddModule("__main__"))))); // Get the class object passClass(main_module.attr("PythonSchedulerPass")); // Create an instance object instance(passClass()); // Call a method of the obejct needsTarget_ = call_method(instance.ptr(), "needsTarget"); Py_Finalize(); // Crash here in ~object() } -------------------------------------------- From roman.yakovenko at gmail.com Tue Mar 20 15:29:27 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 20 Mar 2007 16:29:27 +0200 Subject: [C++-sig] ownership of C++ object extended in Python In-Reply-To: <45FF0CFC.9050506@image-engine.com> References: <45FEF0C7.9000608@image-engine.com> <7465b6170703191301l67eae0cra0298d609bebe824@mail.gmail.com> <45FF0CFC.9050506@image-engine.com> Message-ID: <7465b6170703200729w1b3e021fx35dfe31ed2a826c7@mail.gmail.com> On 3/20/07, Mark Williams wrote: > > > virtual ~event_t_wrapper(){ > > if (this->m_pyobj) { > > //Py_DECREF(this->m_pyobj); > > this->m_pyobj = 0; > > } > > } > > > > Many thanks for the solution - it appears to be working just fine. > However I'm wondering why the Py_DECREF line above is commented out? I don't remember. I think if you uncomment it the object will be deleted twice. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From dave at boost-consulting.com Tue Mar 20 17:42:20 2007 From: dave at boost-consulting.com (David Abrahams) Date: Tue, 20 Mar 2007 12:42:20 -0400 Subject: [C++-sig] shared_ptr< const T> patch References: <7465b6170703191141g7eeb8126tcf70dbaee27cdeb3@mail.gmail.com> Message-ID: <87r6rjlvdf.fsf@valverde.peloton> on Mon Mar 19 2007, "Roman Yakovenko" wrote: > Good evening. Few month ago I created patch to boost::shared_ptr< const T> problem. > You can find full thread here: http://mail.python.org/pipermail/c++-sig/2006-November/ > 011626.html > > The patch was approved by David Abrahams, but for some reason it was not applied. > Could it be applied now? If your patch is added to the Boost Sourceforge patch tracker, it will get applied. Please assign it to me. -- Dave Abrahams Boost Consulting www.boost-consulting.com From mark at image-engine.com Tue Mar 20 19:12:14 2007 From: mark at image-engine.com (Mark Williams) Date: Tue, 20 Mar 2007 10:12:14 -0800 Subject: [C++-sig] ownership of C++ object extended in Python In-Reply-To: <7465b6170703200729w1b3e021fx35dfe31ed2a826c7@mail.gmail.com> References: <45FEF0C7.9000608@image-engine.com> <7465b6170703191301l67eae0cra0298d609bebe824@mail.gmail.com> <45FF0CFC.9050506@image-engine.com> <7465b6170703200729w1b3e021fx35dfe31ed2a826c7@mail.gmail.com> Message-ID: <460023FE.6060206@image-engine.com> Roman Yakovenko wrote: > On 3/20/07, Mark Williams wrote: >>> virtual ~event_t_wrapper(){ >>> if (this->m_pyobj) { >>> //Py_DECREF(this->m_pyobj); >>> this->m_pyobj = 0; >>> } >>> } >>> >> Many thanks for the solution - it appears to be working just fine. >> However I'm wondering why the Py_DECREF line above is commented out? > > I don't remember. I think if you uncomment it the object will be deleted twice. > OK. Unfortunately I'm finding that this solution does not work in the general case. If the Python derived class contains any member data then the program eventually segfaults - which is understandable because the instance has already been destructed at the time the virtual member is being called. I believe it may be the case that in many simple cases the solution posted works because the memory deallocated during destructing the class has not been overwritten. Currently the only workaround I see is to add a "transferOwnership" member function which does the same reference inc/dec as the posted example, but must be explicitly called from within Python. Unless, of course, there is a way of accessing the PyObject* from within the wrappers constructor or somehow receiving notification when boost::detail::initialize_wrapper() is called. The latter would be preferable but I do not (yet) see whether or not it is feasible. Thanks, Mark From roman.yakovenko at gmail.com Tue Mar 20 21:29:08 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 20 Mar 2007 22:29:08 +0200 Subject: [C++-sig] shared_ptr< const T> patch In-Reply-To: <87r6rjlvdf.fsf@valverde.peloton> References: <7465b6170703191141g7eeb8126tcf70dbaee27cdeb3@mail.gmail.com> <87r6rjlvdf.fsf@valverde.peloton> Message-ID: <7465b6170703201329i1f226497t5e6a2e0b8f864ce1@mail.gmail.com> On 3/20/07, David Abrahams wrote: > If your patch is added to the Boost Sourceforge patch tracker, it will > get applied. Please assign it to me. Okay, I just did it: http://sourceforge.net/tracker/index.php?func=detail&aid=1684708&group_id=7586&atid=307586 Would you mind to comment my other mail about "Indexing Suite v2"? Thank you. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From j.reid at mail.cryst.bbk.ac.uk Wed Mar 21 18:21:15 2007 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Wed, 21 Mar 2007 17:21:15 +0000 Subject: [C++-sig] Launch msvc debugger via bjam Message-ID: Hi, I have the following test in my Jamfile.v2 bpl-test gapped_pssm_test : test_gapped_pssm.py _gapped_pssms ; _gapped_pssms is the name of a python extension. I would like to launch python through my VC Express debugger. On the command line the following works: "C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\VCExpress" /debugexe python test_gapped_pssm.py How can I do this using bjam? The --debugger option and -sPYTHON_LAUNCH options that I have seen in various FAQs don't seem to work. I.e. the test is just run using python to launch it. Thanks, John. From pk at cs.tut.fi Thu Mar 22 14:13:13 2007 From: pk at cs.tut.fi (=?ISO-8859-1?Q?Pertti_Kellom=E4ki?=) Date: Thu, 22 Mar 2007 15:13:13 +0200 Subject: [C++-sig] Duplicate registration of to-Python converters Message-ID: <460280E9.2060603@cs.tut.fi> We are developing a system that allows user-written plugins. Some of these plugins can be written in Python, and they can access the internals of the system via bindings developed using Boost.Python. The plugins don't know anything about each other, so a plugin running Python calls Py_Initialize(), runs the Python code, and calls Py_Finalize(). The call to Py_Finalize() does not remove the registered to-Python converters, however, so if two plugins happen to import the same module produced by Boost.Python, the second importing causes RuntimeWarnings to be printed. Is there a way to turn these warnings off, remove the converters, or otherwise circumvent the problem? TIA. -- Pertti From roman.yakovenko at gmail.com Thu Mar 22 14:28:46 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 22 Mar 2007 15:28:46 +0200 Subject: [C++-sig] Duplicate registration of to-Python converters In-Reply-To: <460280E9.2060603@cs.tut.fi> References: <460280E9.2060603@cs.tut.fi> Message-ID: <7465b6170703220628v5b8ce5e3mceb4968e602f0700@mail.gmail.com> On 3/22/07, Pertti Kellom?ki wrote: > We are developing a system that allows user-written > plugins. Some of these plugins can be written in > Python, and they can access the internals of the > system via bindings developed using Boost.Python. > > The plugins don't know anything about each other, so > a plugin running Python calls Py_Initialize(), runs > the Python code, and calls Py_Finalize(). > > The call to Py_Finalize() does not remove the registered > to-Python converters, however, so if two plugins happen > to import the same module produced by Boost.Python, the > second importing causes RuntimeWarnings to be printed. > > Is there a way to turn these warnings off, remove the > converters, or otherwise circumvent the problem? even few 1. Find out the warning and remove it :-) 2. register only new classes: namespace bpl = boost::python; bpl::handle<> class_obj( bpl::objects::registered_class_object(bpl::type_id())); if( class_obj.get() == 0 ){ bpl::class_( ... ); } HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From pk at cs.tut.fi Thu Mar 22 14:42:23 2007 From: pk at cs.tut.fi (=?ISO-8859-1?Q?Pertti_Kellom=E4ki?=) Date: Thu, 22 Mar 2007 15:42:23 +0200 Subject: [C++-sig] Duplicate registration of to-Python converters In-Reply-To: <7465b6170703220628v5b8ce5e3mceb4968e602f0700@mail.gmail.com> References: <460280E9.2060603@cs.tut.fi> <7465b6170703220628v5b8ce5e3mceb4968e602f0700@mail.gmail.com> Message-ID: <460287BF.5010708@cs.tut.fi> Roman Yakovenko wrote: > 1. Find out the warning and remove it :-) Yes, that did cross my mind ;-) > 2. register only new classes: I'm not too familiar with the internals of Boost.Python. Can that be done within a BOOST_PYTHON_MODULE? That is, would it be possible to write a module in such a way that it would be re-importing safe in that sense? -- Pertti From roman.yakovenko at gmail.com Thu Mar 22 14:56:28 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 22 Mar 2007 15:56:28 +0200 Subject: [C++-sig] Duplicate registration of to-Python converters In-Reply-To: <460287BF.5010708@cs.tut.fi> References: <460280E9.2060603@cs.tut.fi> <7465b6170703220628v5b8ce5e3mceb4968e602f0700@mail.gmail.com> <460287BF.5010708@cs.tut.fi> Message-ID: <7465b6170703220656l4a8ac502j89a108e26e6b353a@mail.gmail.com> On 3/22/07, Pertti Kellom?ki wrote: > Roman Yakovenko wrote: > > 1. Find out the warning and remove it :-) > > Yes, that did cross my mind ;-) > > > 2. register only new classes: > > I'm not too familiar with the internals of Boost.Python. > Can that be done within a BOOST_PYTHON_MODULE? > That is, would it be possible to write a module in > such a way that it would be re-importing safe in > that sense? I am not sure I understand you, but it seems to me that the answer is "not" :-) You have add this code for every exposed class. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From pk at cs.tut.fi Thu Mar 22 15:32:31 2007 From: pk at cs.tut.fi (=?ISO-8859-1?Q?Pertti_Kellom=E4ki?=) Date: Thu, 22 Mar 2007 16:32:31 +0200 Subject: [C++-sig] Duplicate registration of to-Python converters In-Reply-To: <7465b6170703220628v5b8ce5e3mceb4968e602f0700@mail.gmail.com> References: <460280E9.2060603@cs.tut.fi> <7465b6170703220628v5b8ce5e3mceb4968e602f0700@mail.gmail.com> Message-ID: <4602937F.8060103@cs.tut.fi> Roman Yakovenko wrote: > 1. Find out the warning and remove it :-) Turns out there is even a sensible way to do it. The warning is issued using PyErr_Warn, and the Python 'warnings' module allows warnings to be selectively ignored, turned into errors etc. So thanks for the advice ;-) -- Pertti From dave at boost-consulting.com Thu Mar 22 15:45:28 2007 From: dave at boost-consulting.com (David Abrahams) Date: Thu, 22 Mar 2007 10:45:28 -0400 Subject: [C++-sig] Duplicate registration of to-Python converters References: <460280E9.2060603@cs.tut.fi> Message-ID: <877it9fibb.fsf@valverde.peloton> on Thu Mar 22 2007, Pertti Kellom?ki wrote: > We are developing a system that allows user-written > plugins. Some of these plugins can be written in > Python, and they can access the internals of the > system via bindings developed using Boost.Python. > > The plugins don't know anything about each other, so > a plugin running Python calls Py_Initialize(), runs > the Python code, and calls Py_Finalize(). Boost.Python doesn't support Py_Finalize() yet. -- Dave Abrahams Boost Consulting www.boost-consulting.com From dave at boost-consulting.com Thu Mar 22 15:47:14 2007 From: dave at boost-consulting.com (David Abrahams) Date: Thu, 22 Mar 2007 10:47:14 -0400 Subject: [C++-sig] Launch msvc debugger via bjam References: Message-ID: <871wjhfi8d.fsf@valverde.peloton> on Wed Mar 21 2007, John Reid wrote: > Hi, > > I have the following test in my Jamfile.v2 > > bpl-test gapped_pssm_test > : > test_gapped_pssm.py > _gapped_pssms > ; > > _gapped_pssms is the name of a python extension. > > > I would like to launch python through my VC Express debugger. On the > command line the following works: > "C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\VCExpress" > /debugexe python test_gapped_pssm.py > > How can I do this using bjam? The --debugger option and -sPYTHON_LAUNCH > options that I have seen in various FAQs don't seem to work. I.e. the > test is just run using python to launch it. To make it easy on yourself, stick C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\VCExpress" /debugexe %* in a .bat file, say dbg.bat then, bjam --debugger=dbg.bat ... HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com From pk at cs.tut.fi Thu Mar 22 16:13:46 2007 From: pk at cs.tut.fi (=?UTF-8?B?UGVydHRpIEtlbGxvbcOka2k=?=) Date: Thu, 22 Mar 2007 17:13:46 +0200 Subject: [C++-sig] Duplicate registration of to-Python converters In-Reply-To: <877it9fibb.fsf@valverde.peloton> References: <460280E9.2060603@cs.tut.fi> <877it9fibb.fsf@valverde.peloton> Message-ID: <46029D2A.7050206@cs.tut.fi> David Abrahams wrote: > Boost.Python doesn't support Py_Finalize() yet. Does this mean that it is not safe to follow the "Getting Started" outline at: If so, is there a workaround if one wants to repeatedly run Python programs in a fresh environment? I am able to run Python code with repeated sequences of Py_Initialize() PyRun_SimpleFile() Py_Finalize() so am I just being lucky that the ill effects have not surfaced? This is on Ubuntu, g++ 4.1.2, Boost 1.33.1 Thanks. -- Pertti From pk at cs.tut.fi Thu Mar 22 16:25:27 2007 From: pk at cs.tut.fi (=?ISO-8859-1?Q?Pertti_Kellom=E4ki?=) Date: Thu, 22 Mar 2007 17:25:27 +0200 Subject: [C++-sig] Duplicate registration of to-Python converters In-Reply-To: <46029D2A.7050206@cs.tut.fi> References: <460280E9.2060603@cs.tut.fi> <877it9fibb.fsf@valverde.peloton> <46029D2A.7050206@cs.tut.fi> Message-ID: <46029FE7.8000003@cs.tut.fi> Pertti Kellom?ki wrote: > so am I just being lucky that the ill effects have not > surfaced? Or maybe they in fact have. I got segmentation faults from inside the destructor of object, and I thought they were due to my limited understanding of how object is supposed to be used. I "fixed" the problem by manually calling Py_INCREF() for the problematic object, in the hope that some day I would see the light. But if the problem with Py_Finalize() has to do with reference counting, then maybe I'm out of luck for now. I am not very happy with the Py_INCREF() call, but if it really does solve the problem instead of merely masking it, I can live with it. Thanks for the great library, BTW! -- Pertti From j.reid at mail.cryst.bbk.ac.uk Thu Mar 22 16:29:00 2007 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Thu, 22 Mar 2007 15:29:00 +0000 Subject: [C++-sig] Launch msvc debugger via bjam In-Reply-To: <871wjhfi8d.fsf@valverde.peloton> References: <871wjhfi8d.fsf@valverde.peloton> Message-ID: David Abrahams wrote: > To make it easy on yourself, stick > > C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\VCExpress" /debugexe %* > > in a .bat file, say dbg.bat > > then, > > bjam --debugger=dbg.bat ... That is indeed a helpful idea as now I don't need to worry about escaping '\'s and quoting ' 's but my real problem is that boost.build just ignores the --debugger option. I get exactly the same output whether it is there or not. Is this only a v1 feature? What should I do in v2? John. From mark at image-engine.com Thu Mar 22 19:11:25 2007 From: mark at image-engine.com (Mark Williams) Date: Thu, 22 Mar 2007 10:11:25 -0800 Subject: [C++-sig] ownership of C++ object extended in Python In-Reply-To: <460023FE.6060206@image-engine.com> References: <45FEF0C7.9000608@image-engine.com> <7465b6170703191301l67eae0cra0298d609bebe824@mail.gmail.com> <45FF0CFC.9050506@image-engine.com> <7465b6170703200729w1b3e021fx35dfe31ed2a826c7@mail.gmail.com> <460023FE.6060206@image-engine.com> Message-ID: <4602C6CD.7060004@image-engine.com> Mark Williams wrote: > Roman Yakovenko wrote: >> On 3/20/07, Mark Williams wrote: >>>> virtual ~event_t_wrapper(){ >>>> if (this->m_pyobj) { >>>> //Py_DECREF(this->m_pyobj); >>>> this->m_pyobj = 0; >>>> } >>>> } >>>> >>> Many thanks for the solution - it appears to be working just fine. >>> However I'm wondering why the Py_DECREF line above is commented out? >> I don't remember. I think if you uncomment it the object will be deleted twice. >> > > OK. Unfortunately I'm finding that this solution does not work in the > general case. If the Python derived class contains any member data then > the program eventually segfaults - which is understandable because the > instance has already been destructed at the time the virtual member is > being called. I believe it may be the case that in many simple cases the > solution posted works because the memory deallocated during destructing > the class has not been overwritten. > I'm still trying to find a suitable workaround to this problem. One thing I've tried is modifying initialize_wrapper() in wrapper_base.hpp to increment the reference of the object being held. However, I have a feeling that it then becomes impossible to actually decrement the reference again, leading to a memory leak. I find it hard to believe that it's impossible to safely transfer ownership from Python to C++, but I don't find any clues in either the boost.python code or docs. Mark From roman.yakovenko at gmail.com Thu Mar 22 19:35:05 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 22 Mar 2007 20:35:05 +0200 Subject: [C++-sig] ownership of C++ object extended in Python In-Reply-To: <4602C6CD.7060004@image-engine.com> References: <45FEF0C7.9000608@image-engine.com> <7465b6170703191301l67eae0cra0298d609bebe824@mail.gmail.com> <45FF0CFC.9050506@image-engine.com> <7465b6170703200729w1b3e021fx35dfe31ed2a826c7@mail.gmail.com> <460023FE.6060206@image-engine.com> <4602C6CD.7060004@image-engine.com> Message-ID: <7465b6170703221135n1ceaffc0p9630f71d41f03f64@mail.gmail.com> On 3/22/07, Mark Williams wrote: > Mark Williams wrote: > > Roman Yakovenko wrote: > >> On 3/20/07, Mark Williams wrote: > >>>> virtual ~event_t_wrapper(){ > >>>> if (this->m_pyobj) { > >>>> //Py_DECREF(this->m_pyobj); > >>>> this->m_pyobj = 0; > >>>> } > >>>> } > >>>> > >>> Many thanks for the solution - it appears to be working just fine. > >>> However I'm wondering why the Py_DECREF line above is commented out? > >> I don't remember. I think if you uncomment it the object will be deleted twice. > >> > > > > OK. Unfortunately I'm finding that this solution does not work in the > > general case. If the Python derived class contains any member data then > > the program eventually segfaults - which is understandable because the > > instance has already been destructed at the time the virtual member is > > being called. I believe it may be the case that in many simple cases the > > solution posted works because the memory deallocated during destructing > > the class has not been overwritten. > > > > I'm still trying to find a suitable workaround to this problem. One > thing I've tried is modifying initialize_wrapper() in wrapper_base.hpp > to increment the reference of the object being held. However, I have a > feeling that it then becomes impossible to actually decrement the > reference again, leading to a memory leak. > > I find it hard to believe that it's impossible to safely transfer > ownership from Python to C++, but I don't find any clues in either the > boost.python code or docs. I am also looking for solution. I think I have something, but I am not sure. I think if you create small and complete test case, submit it may be more knowledgeable developers will be able to help you. If I will make some progress I will let you know -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From mark at image-engine.com Thu Mar 22 21:49:58 2007 From: mark at image-engine.com (Mark Williams) Date: Thu, 22 Mar 2007 12:49:58 -0800 Subject: [C++-sig] ownership of C++ object extended in Python In-Reply-To: <7465b6170703221135n1ceaffc0p9630f71d41f03f64@mail.gmail.com> References: <45FEF0C7.9000608@image-engine.com> <7465b6170703191301l67eae0cra0298d609bebe824@mail.gmail.com> <45FF0CFC.9050506@image-engine.com> <7465b6170703200729w1b3e021fx35dfe31ed2a826c7@mail.gmail.com> <460023FE.6060206@image-engine.com> <4602C6CD.7060004@image-engine.com> <7465b6170703221135n1ceaffc0p9630f71d41f03f64@mail.gmail.com> Message-ID: <4602EBF6.5090300@image-engine.com> > I am also looking for solution. I think I have something, but I am not sure. > I think if you create small and complete test case, submit it may be more > knowledgeable developers will be able to help you. > If I will make some progress I will let you know > Thanks again for your help. I've produced what I believe to be a fairly minimal test case, attached. When run it produces the output: Calling test funcion... Entering test()... Making PythonDerived object... Holding... Retrieving held PythonDerived object... Calling member function of held PythonDerived object... ** my string data ** PythonDerived object goes out of Python scope here... Leaving test()... PythonDerived.__del__ Retrieving held PythonDerived object... Calling member function of held PythonDerived object... ** my string data ** PythonDerived.__del__ Segmentation fault Mark -------------- next part -------------- A non-text attachment was scrubbed... Name: CppBase.cpp Type: text/x-c++src Size: 1814 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: PythonDerived.py Type: application/x-python Size: 897 bytes Desc: not available URL: From dave at boost-consulting.com Thu Mar 22 21:30:55 2007 From: dave at boost-consulting.com (David Abrahams) Date: Thu, 22 Mar 2007 16:30:55 -0400 Subject: [C++-sig] Duplicate registration of to-Python converters References: <460280E9.2060603@cs.tut.fi> <877it9fibb.fsf@valverde.peloton> <46029D2A.7050206@cs.tut.fi> Message-ID: <87bqiluikg.fsf@valverde.peloton> on Thu Mar 22 2007, Pertti Kellom?ki wrote: > David Abrahams wrote: >> Boost.Python doesn't support Py_Finalize() yet. > > Does this mean that it is not safe to follow the "Getting > Started" outline at: > Apparently not. I don't remember who wrote that part, but it's definitely wrong to suggest you can do it with Boost.Python. > If so, is there a workaround if one wants to repeatedly > run Python programs in a fresh environment? Not that I know of. > I am able to run Python code with repeated sequences of > > Py_Initialize() > PyRun_SimpleFile() > Py_Finalize() > > so am I just being lucky that the ill effects have not > surfaced? This is on Ubuntu, g++ 4.1.2, Boost 1.33.1 Probably, yes, you've gotten lucky. -- Dave Abrahams Boost Consulting www.boost-consulting.com From dave at boost-consulting.com Thu Mar 22 21:33:02 2007 From: dave at boost-consulting.com (David Abrahams) Date: Thu, 22 Mar 2007 16:33:02 -0400 Subject: [C++-sig] Launch msvc debugger via bjam References: <871wjhfi8d.fsf@valverde.peloton> Message-ID: <87648tuigx.fsf@valverde.peloton> on Thu Mar 22 2007, John Reid wrote: > David Abrahams wrote: >> To make it easy on yourself, stick >> >> C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\VCExpress" /debugexe %* >> >> in a .bat file, say dbg.bat >> >> then, >> >> bjam --debugger=dbg.bat ... > > That is indeed a helpful idea as now I don't need to worry about > escaping '\'s and quoting ' 's but my real problem is that boost.build > just ignores the --debugger option. I get exactly the same output > whether it is there or not. > > Is this only a v1 feature? What should I do in v2? In v2 it was helpfully renamed to "launcher". So bjam launcher=dbg.bat note it's now a feature and not an option, so there's no preceding "--". -- Dave Abrahams Boost Consulting www.boost-consulting.com From seefeld at sympatico.ca Thu Mar 22 21:39:38 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 22 Mar 2007 16:39:38 -0400 Subject: [C++-sig] Duplicate registration of to-Python converters In-Reply-To: <87bqiluikg.fsf@valverde.peloton> References: <460280E9.2060603@cs.tut.fi> <877it9fibb.fsf@valverde.peloton> <46029D2A.7050206@cs.tut.fi> <87bqiluikg.fsf@valverde.peloton> Message-ID: <4602E98A.3000300@sympatico.ca> David Abrahams wrote: > on Thu Mar 22 2007, Pertti Kellom?ki wrote: > >> David Abrahams wrote: >>> Boost.Python doesn't support Py_Finalize() yet. >> Does this mean that it is not safe to follow the "Getting >> Started" outline at: >> > > Apparently not. I don't remember who wrote that part, but it's > definitely wrong to suggest you can do it with Boost.Python. That page will be outdated with 1.34 anyway, now that we have bpl::exec() and bpl::exec_file(). Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From dave at boost-consulting.com Fri Mar 23 05:46:31 2007 From: dave at boost-consulting.com (David Abrahams) Date: Fri, 23 Mar 2007 00:46:31 -0400 Subject: [C++-sig] Duplicate registration of to-Python converters References: <460280E9.2060603@cs.tut.fi> <877it9fibb.fsf@valverde.peloton> <46029D2A.7050206@cs.tut.fi> <87bqiluikg.fsf@valverde.peloton> <4602E98A.3000300@sympatico.ca> Message-ID: <873b3wtvmg.fsf@valverde.peloton> on Thu Mar 22 2007, Stefan Seefeld wrote: > David Abrahams wrote: >> on Thu Mar 22 2007, Pertti Kellom?ki wrote: >> >>> David Abrahams wrote: >>>> Boost.Python doesn't support Py_Finalize() yet. >>> Does this mean that it is not safe to follow the "Getting >>> Started" outline at: >>> >> >> Apparently not. I don't remember who wrote that part, but it's >> definitely wrong to suggest you can do it with Boost.Python. > > That page will be outdated with 1.34 anyway, now that we have bpl::exec() > and bpl::exec_file(). Hi Stefan, Good point. Could I prevail upon you to update it and make it accurate? Thanks, -- Dave Abrahams Boost Consulting www.boost-consulting.com From pk at cs.tut.fi Fri Mar 23 08:12:12 2007 From: pk at cs.tut.fi (=?UTF-8?B?UGVydHRpIEtlbGxvbcOka2k=?=) Date: Fri, 23 Mar 2007 09:12:12 +0200 Subject: [C++-sig] Duplicate registration of to-Python converters In-Reply-To: <87bqiluikg.fsf@valverde.peloton> References: <460280E9.2060603@cs.tut.fi> <877it9fibb.fsf@valverde.peloton> <46029D2A.7050206@cs.tut.fi> <87bqiluikg.fsf@valverde.peloton> Message-ID: <46037DCC.8020105@cs.tut.fi> David Abrahams wrote: > on Thu Mar 22 2007, Pertti Kellom?ki wrote: >> I am able to run Python code with repeated sequences of >> >> Py_Initialize() >> PyRun_SimpleFile() >> Py_Finalize() >> >> so am I just being lucky that the ill effects have not >> surfaced? This is on Ubuntu, g++ 4.1.2, Boost 1.33.1 > > Probably, yes, you've gotten lucky. Googling for Py_Finalize() suggests that the above may not be advisable regardless of whether one uses Boost.Python or not. That's fine, I can easily live with one global interpreter whose state persists. So what is the correct way to use Boost.Python in an embedded setting? Is it safe to call Py_Initialize() once, then run scripts using PyRun_SimpleFile(), and at the end just let exit() reclaim the resources held by the Python interpreter? -- Pertti From j.reid at mail.cryst.bbk.ac.uk Fri Mar 23 10:54:58 2007 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Fri, 23 Mar 2007 09:54:58 +0000 Subject: [C++-sig] Launch msvc debugger via bjam In-Reply-To: <87648tuigx.fsf@valverde.peloton> References: <871wjhfi8d.fsf@valverde.peloton> <87648tuigx.fsf@valverde.peloton> Message-ID: David Abrahams wrote: > In v2 it was helpfully renamed to "launcher". So > > bjam launcher=dbg.bat > > note it's now a feature and not an option, so there's no preceding > "--". > Now running this: bjam debug launcher=dbg.bat gapped_pssm_test I get the following output: Using boost build at: C:\Dev\ThirdParty\boost\RC_1_34_0\boost/tools/build/v2 Using boost at: C:\Dev\ThirdParty\boost\RC_1_34_0\boost Using GSL at C:\Program Files\GnuWin32 Using python at C:\apps\Python25 Building Boost.Regex with the optional Unicode/ICU support disabled. Please refer to the Boost.Regex documentation for more information (don't panic: this is a strictly optional feature). C:/Dev/ThirdParty/boost/RC_1_34_0/boost/tools/build/v2/build\property.jam:282: in validate1 from module property error: Invalid property 'dbg.bat': unknown feature 'launcher'. C:/Dev/ThirdParty/boost/RC_1_34_0/boost/tools/build/v2/build\property.jam:290: in property.validate from module property C:/Dev/ThirdParty/boost/RC_1_34_0/boost/tools/build/v2/build\build-request.jam:185: in convert-command-line-element from module build-request C:/Dev/ThirdParty/boost/RC_1_34_0/boost/tools/build/v2/build\build-request.jam:143: in build-request.from-command-line from module build-request C:/Dev/ThirdParty/boost/RC_1_34_0/boost/tools/build/v2\build-system.jam:265: in load from module build-system C:\Dev\ThirdParty\boost\RC_1_34_0\boost\tools\build\v2/kernel\modules.jam:261: in import from module modules C:\Dev\ThirdParty\boost\RC_1_34_0\boost\tools\build\v2/kernel/bootstrap.jam:132: in boost-build from module C:\Dev\boost-build.jam:12: in module scope from module Any ideas? John. From dave at boost-consulting.com Fri Mar 23 14:32:12 2007 From: dave at boost-consulting.com (David Abrahams) Date: Fri, 23 Mar 2007 09:32:12 -0400 Subject: [C++-sig] Duplicate registration of to-Python converters References: <460280E9.2060603@cs.tut.fi> <877it9fibb.fsf@valverde.peloton> <46029D2A.7050206@cs.tut.fi> <87bqiluikg.fsf@valverde.peloton> <46037DCC.8020105@cs.tut.fi> Message-ID: <87y7loxezn.fsf@valverde.peloton> on Fri Mar 23 2007, Pertti Kellom?ki wrote: > So what is the correct way to use Boost.Python in an embedded > setting? Is it safe to call Py_Initialize() once, then run scripts > using PyRun_SimpleFile(), and at the end just let exit() reclaim > the resources held by the Python interpreter? On most OSes the process' resource are automatically reclaimed upon exit, and there, it's definitely safe. -- Dave Abrahams Boost Consulting www.boost-consulting.com From dave at boost-consulting.com Fri Mar 23 14:33:10 2007 From: dave at boost-consulting.com (David Abrahams) Date: Fri, 23 Mar 2007 09:33:10 -0400 Subject: [C++-sig] Launch msvc debugger via bjam References: <871wjhfi8d.fsf@valverde.peloton> <87648tuigx.fsf@valverde.peloton> Message-ID: <87slbwxey1.fsf@valverde.peloton> on Fri Mar 23 2007, John Reid wrote: > David Abrahams wrote: >> In v2 it was helpfully renamed to "launcher". So >> >> bjam launcher=dbg.bat >> >> note it's now a feature and not an option, so there's no preceding >> "--". >> > > Now running this: > bjam debug launcher=dbg.bat gapped_pssm_test sorry, bjam debug testing.launcher=dbg.bat gapped_pssm_test -- Dave Abrahams Boost Consulting www.boost-consulting.com From j.reid at mail.cryst.bbk.ac.uk Fri Mar 23 15:10:10 2007 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Fri, 23 Mar 2007 14:10:10 +0000 Subject: [C++-sig] Launch msvc debugger via bjam In-Reply-To: <87slbwxey1.fsf@valverde.peloton> References: <871wjhfi8d.fsf@valverde.peloton> <87648tuigx.fsf@valverde.peloton> <87slbwxey1.fsf@valverde.peloton> Message-ID: David Abrahams wrote: > sorry, > > bjam debug testing.launcher=dbg.bat gapped_pssm_test > That does not generate any errors but acts no differently from: bjam debug gapped_pssm_test whereas at a dos prompt: dbg.bat python does indeed launch VC Express. Any more ideas? John. From gabriel.becedillas at corest.com Fri Mar 23 20:53:55 2007 From: gabriel.becedillas at corest.com (Gabriel Becedillas) Date: Fri, 23 Mar 2007 16:53:55 -0300 Subject: [C++-sig] Release Python's lock before destructing pointer_holder Message-ID: <46043053.2070409@corest.com> I'm wrapping a class whose destructor does some work on sockets that might take some time to complete, so I need to release Python's lock before calling the destructor. This instances are held via boost::shared_ptr. One way I could manage to do so is to declare a destructor for pointer_holder<> like this: pointer_holder::~pointer_holder() { PyThreadState* prev = PyThreadState_Swap(NULL); PyEval_ReleaseLock(); m_p = Pointer(); PyEval_AcquireLock(); PyThreadState_Swap(prev); } There are 2 reasons why I don't like this solution. The first one is that I have to modify boost::python's code. The second is that I need this only for a certain class. Does anyone know a better/cleaner way to do this ? Thanks. -- Gabriel Becedillas Developer CORE SECURITY TECHNOLOGIES From dave at boost-consulting.com Fri Mar 23 22:12:44 2007 From: dave at boost-consulting.com (David Abrahams) Date: Fri, 23 Mar 2007 17:12:44 -0400 Subject: [C++-sig] Launch msvc debugger via bjam References: <871wjhfi8d.fsf@valverde.peloton> <87648tuigx.fsf@valverde.peloton> <87slbwxey1.fsf@valverde.peloton> Message-ID: <87vegr1x6b.fsf@valverde.peloton> on Fri Mar 23 2007, John Reid wrote: > David Abrahams wrote: >> sorry, >> >> bjam debug testing.launcher=dbg.bat gapped_pssm_test >> > That does not generate any errors but acts no differently from: > > bjam debug gapped_pssm_test > > whereas at a dos prompt: > dbg.bat python > > does indeed launch VC Express. > > Any more ideas? Ask Volodya on the Boost.Build list? I honestly am out of ideas. -- Dave Abrahams Boost Consulting www.boost-consulting.com From j.reid at mail.cryst.bbk.ac.uk Fri Mar 23 22:50:27 2007 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Fri, 23 Mar 2007 21:50:27 +0000 Subject: [C++-sig] Launch msvc debugger via bjam In-Reply-To: <87vegr1x6b.fsf@valverde.peloton> References: <871wjhfi8d.fsf@valverde.peloton> <87648tuigx.fsf@valverde.peloton> <87slbwxey1.fsf@valverde.peloton> <87vegr1x6b.fsf@valverde.peloton> Message-ID: David Abrahams wrote: > Ask Volodya on the Boost.Build list? I honestly am out of ideas. > Ok will do. Thx for the help. John. From roman.yakovenko at gmail.com Sat Mar 24 18:50:36 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sat, 24 Mar 2007 19:50:36 +0200 Subject: [C++-sig] Release Python's lock before destructing pointer_holder In-Reply-To: <46043053.2070409@corest.com> References: <46043053.2070409@corest.com> Message-ID: <7465b6170703241050m3cf9fa8ay391e6350cac6a9c1@mail.gmail.com> On 3/23/07, Gabriel Becedillas wrote: > I'm wrapping a class whose destructor does some work on sockets that > might take some time to complete, so I need to release Python's lock > before calling the destructor. > This instances are held via boost::shared_ptr. One way I could manage to > do so is to declare a destructor for pointer_holder<> like this: > > pointer_holder::~pointer_holder() > { > PyThreadState* prev = PyThreadState_Swap(NULL); > PyEval_ReleaseLock(); > > m_p = Pointer(); > > PyEval_AcquireLock(); > PyThreadState_Swap(prev); > } > > There are 2 reasons why I don't like this solution. The first one is > that I have to modify boost::python's code. The second is that I need > this only for a certain class. > Does anyone know a better/cleaner way to do this ? > Thanks. > boost::shared_ptr class takes custom deleter in constructor. You can use it and solution provided in "How can I find the existing PyObject that holds a C++ object?" Boost.Python FAQ to solve this issue. -- Roman Yakovenko C++ Pytdhon language binding http://www.language-binding.net/ From shawn.mcgrath at gmail.com Mon Mar 26 11:09:14 2007 From: shawn.mcgrath at gmail.com (Shawn McGrath) Date: Mon, 26 Mar 2007 05:09:14 -0400 Subject: [C++-sig] Transfering ownership from python to C++ Message-ID: <8043863a0703260209x4b0c4446m47cbdd3398f30680@mail.gmail.com> Hi guys, I've been working on this problem for at least a week now, and I can't find a solution. I'm pretty much ready to give up this project if I can't fix this. In C++ I have a base class called Entity with two pure virtual functions: update and process which are exposed to python. I subclass Entity in python, and pass instances of the subclassed entities to a C++ function called "addEntity" defined like this: void pyAddEntity(const std::string &name, PyObject *e) { pyEntity *ent = boost::python::extract(e); assert(ent != NULL); EntityID eid = Engine::Instance().addEntity(ent, name); try { ent->self = e; ent->added = true; Py_XINCREF(ent->self); } catch (...) { handleError(); LOG("ERROR PY_XINCREF"); } } To kill the memory, I call pyEntity::kill() which sets a done flag to true, and the Engine deletes it. I tried putting Py_XDECREF(self) inside pyEntity's dtor and it crashes with the stacktrace showing ntdll, (so meaningless). I tried delaying the call to Py_XDECREF until after the gc has run, still no good. There's nothing I can do to call Py_XDECREF(). The delayed delete stacktrace is a little more meaningful, it shows (unknown frames removed): #1 007C34B0 std::auto_ptr::~auto_ptr(this=0xabdeee8) (C:/MinGW/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/memory:258) #2 006E126B boost::python::objects::pointer_holder, rsblsb::python::pyEntity>::~pointer_holder(this=0xabdeee0) (C:/Game/svn/Platform/Framework/Python/Modules/PyModuleEntity.cc:127) #3 600CD32E boost_python-mgw-1_33_1!_ZN5boost6python15instance_holder10deallocateEP7_objectPv() (C:\Game\svn\Platform\Output\boost_python-mgw-1_33_1.dll:??) #4 1E0AB496 python24!PyType_GenericNew() (C:\WINDOWS\system32\python24.dll:??) #10 0053945B rsblsb::python::InterpreterClass::process(this=0x5356f44d) (C:/Game/svn/Platform/Framework/Python/PythonInterpreter.cc:92) #17 6011E19C boost_python-mgw-1_33_1!_ZNK5boost6python6detail12wrapper_base12get_overrideEPKcP11_typeobject() (C:\Game\svn\Platform\Output\boost_python-mgw-1_33_1.dll:??) I tried changing the addEntity() function like so: void pyAddEntity(const std::string &name, std::auto_ptr e); But that doesn't allow for virtual functions to be defined in Python, so that's not an option. Is there anything else I can try? I *REALLY* don't want to give up on this project, but I can't handle these memory leaks. Thanks a lot for the help -Shawn. From p.jaroszynski at gmail.com Tue Mar 27 18:23:11 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Tue, 27 Mar 2007 18:23:11 +0200 Subject: [C++-sig] Foo to FooWrapper Message-ID: <200703271823.12005.p.jaroszynski@gmail.com> Hello, I suppose the code will show my problem better then myself, so: http://dev.gentooexperimental.org/~peper/boost/test.cc $ g++ -fPIC -shared -lboost_python -I/usr/include/python2.4 test.cc -o test.so and in python: >>> import test >>> test.getFoo().blah() 0 >>> test.getFoo().blah1() Traceback (most recent call last): File "", line 1, in ? Boost.Python.ArgumentError: Python argument types in Foo.blah1(Foo) did not match C++ signature: blah1(FooWrapper {lvalue}) How can I fix that? Making a copy-ctor for Foo is a no option. I thought about using shared_ptr copy-ctor but how? -- Best Regards, Piotr Jaroszynski From roman.yakovenko at gmail.com Tue Mar 27 18:44:44 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 27 Mar 2007 18:44:44 +0200 Subject: [C++-sig] Foo to FooWrapper In-Reply-To: <200703271823.12005.p.jaroszynski@gmail.com> References: <200703271823.12005.p.jaroszynski@gmail.com> Message-ID: <7465b6170703270944x1b45cb0fraa8676c5bc37e1a7@mail.gmail.com> On 3/27/07, Piotr Jaroszynski wrote: > > Hello, > > I suppose the code will show my problem better then myself, so: > http://dev.gentooexperimental.org/~peper/boost/test.cc > $ g++ -fPIC -shared -lboost_python -I/usr/include/python2.4 test.cc -o > test.so > > and in python: > > >>> import test > >>> test.getFoo().blah() > 0 > >>> test.getFoo().blah1() > Traceback (most recent call last): > File "", line 1, in ? > Boost.Python.ArgumentError: Python argument types in > Foo.blah1(Foo) > did not match C++ signature: > blah1(FooWrapper {lvalue}) > > How can I fix that? Making a copy-ctor for Foo is a no option. I thought > about > using shared_ptr copy-ctor but how? > > Make blah1 to be static function, which take reference to Foo class. this should solve the problem -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Tue Mar 27 18:48:43 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Tue, 27 Mar 2007 12:48:43 -0400 Subject: [C++-sig] Foo to FooWrapper In-Reply-To: <200703271823.12005.p.jaroszynski@gmail.com> References: <200703271823.12005.p.jaroszynski@gmail.com> Message-ID: <46094AEB.8050906@sympatico.ca> Piotr Jaroszynski wrote: > Hello, > > I suppose the code will show my problem better then myself, so: FWIW, I think some prose would still be good. The code may show an error (or two), but it may not convey your intention. If you explain what you actually want to achieve we may be better able to suggest solutions. > http://dev.gentooexperimental.org/~peper/boost/test.cc Here are a number of errors that you may want to address first, if only to make it clear to the casual reader of the code (e.g. me) to understand what you want: * You write Foo doesn't have a copy-constructor. That's wrong. It gets one from the compiler. To really don't provide one add a declaration of a private copy-constructor (no body needed !). * You presumably provide FooWrapper in order to be able to override Foo methods in python. However, your Foo isn't even polymorphic, i.e. doesn't have a single virtual method. What are you really trying to achieve ? * I don't understand boost.python's object model well enough, but this whole Foo<->FooWrapper conversion looks suspicious to me. You expose FooWrapper to python, and then register a converter from shared_ptr to a python wrapper around FooWrapper objects. I can't even tell how this works, or rather, why it works. I wouldn't expect it to work at least, precisely because there is no (implicit) conversion from Foo to FooWrapper. David ? I also think that the various boost::python documentation (such as http://www.boost.org/libs/python/doc/v2/register_ptr_to_python.html and http://www.boost.org/libs/python/doc/v2/wrapper.html#examples) should be synched a bit better to better indicate the canonical way to do things. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From kanand at qualcomm.com Wed Mar 28 05:06:43 2007 From: kanand at qualcomm.com (Anand, Kumar) Date: Tue, 27 Mar 2007 20:06:43 -0700 Subject: [C++-sig] Py++ | Exposing Templates Message-ID: I am currently using version 0.8.2 of Py++. Is their support for exposing templates (basically template instantiations) automatically to python? I have the following code, and when I run it through Py++, compilation goes fine but I don't see the template instantiation being exposed. ---------- #include "ProtoA.h" //This defines a simple class called ProtoA #include "ProtocolStub.h" //This defines the template class:: template class ProtocolStub : public TYPE { ...} inline void instantiate(){ sizeof( ProtocolStub ); } typedef ProtocolStub < ProtoA > ProtoAStub; --------- Thanks Kumar -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Wed Mar 28 08:24:41 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 28 Mar 2007 08:24:41 +0200 Subject: [C++-sig] Py++ | Exposing Templates In-Reply-To: References: Message-ID: <7465b6170703272324v1775b5bkf42c3f7bad45c12@mail.gmail.com> On 3/28/07, Anand, Kumar wrote: > > I am currently using version 0.8.2 of Py++. Is their support for exposing > templates (basically template instantiations) automatically to python? > > I have the following code, and when I run it through Py++, compilation > goes fine but I don't see the template instantiation being exposed. > > ---------- > > #include "ProtoA.h" //This defines a simple class calledProtoA > > #include "ProtocolStub.h" //This defines the template class::template class ProtocolStub : public TYPE {? > } > > inline void instantiate(){ > > sizeof( ProtocolStub ); > > } > > typedef ProtocolStub < ProtoA > ProtoAStub; > Without seeing small and complete example it is impossible to answer your question :-( -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From p.jaroszynski at gmail.com Wed Mar 28 17:15:16 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Wed, 28 Mar 2007 17:15:16 +0200 Subject: [C++-sig] range with virtual functions Message-ID: <200703281715.16964.p.jaroszynski@gmail.com> Hello, I have yet another "problem" :] I want to expose a begin_foo and end_foo functions as an iterable object like: .add_property("foo", range(&Blah:begin_foo, &Blah::end_foo)) How can I do it when begin_foo and end_foo are virtual functions? I am interested in both cases, when they are pure virtual and virtual with default implementation. TIA for help! -- Best Regards, Piotr Jaroszynski From meine at informatik.uni-hamburg.de Wed Mar 28 18:07:06 2007 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Wed, 28 Mar 2007 18:07:06 +0200 Subject: [C++-sig] [python] Fix for boost::python::import In-Reply-To: <45E63B84.2040705@sympatico.ca> References: <45E63B84.2040705@sympatico.ca> Message-ID: <200703281807.06499.meine@informatik.uni-hamburg.de> Hi! Am Donnerstag, 01. M?rz 2007 03:33:40 schrieb Stefan Seefeld: > --- libs/python/src/import.cpp??27 Oct 2006 21:17:25 -0000??????1.2 > +++ libs/python/src/import.cpp??1 Mar 2007 02:00:47 -0000 > @@ -17,7 +17,7 @@ > ?{ > ? ?// should be 'char const *' but older python versions don't use 'const' > yet. char *n = python::extract(name); > - ?python::handle<> module(python::borrowed(PyImport_AddModule(n))); > + ?python::handle<> module(python::borrowed(PyImport_ImportModule(n))); > ? ?return python::object(module); > ?} I just saw your patch and looked at the docs.. isn't there a borrowed() too much now? In contrast to AddModule, ImportModule returns a new reference according to http://docs.python.org/api/importing.html -- Ciao, / / /--/ / / ANS From seefeld at sympatico.ca Wed Mar 28 18:39:54 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 28 Mar 2007 12:39:54 -0400 Subject: [C++-sig] [python] Fix for boost::python::import In-Reply-To: <200703281807.06499.meine@informatik.uni-hamburg.de> References: <45E63B84.2040705@sympatico.ca> <200703281807.06499.meine@informatik.uni-hamburg.de> Message-ID: <460A9A5A.7040801@sympatico.ca> Hans Meine wrote: > Hi! > > Am Donnerstag, 01. M?rz 2007 03:33:40 schrieb Stefan Seefeld: >> --- libs/python/src/import.cpp 27 Oct 2006 21:17:25 -0000 1.2 >> +++ libs/python/src/import.cpp 1 Mar 2007 02:00:47 -0000 >> @@ -17,7 +17,7 @@ >> { >> // should be 'char const *' but older python versions don't use 'const' >> yet. char *n = python::extract(name); >> - python::handle<> module(python::borrowed(PyImport_AddModule(n))); >> + python::handle<> module(python::borrowed(PyImport_ImportModule(n))); >> return python::object(module); >> } > > I just saw your patch and looked at the docs.. isn't there a borrowed() too > much now? In contrast to AddModule, ImportModule returns a new reference > according to http://docs.python.org/api/importing.html Oh, duh ! (Why isn't Python written in a nicer and more convenient language that didn't require users to think about ref counting on each call ?!) Dave, Thomas, may I remove that extra python::borrowed in HEAD as well as RC_1_34_0 ? Thanks, Stefan -- ...ich hab' noch einen Koffer in Berlin... From witt at acm.org Wed Mar 28 19:16:01 2007 From: witt at acm.org (Thomas Witt) Date: Wed, 28 Mar 2007 10:16:01 -0700 Subject: [C++-sig] [python] Fix for boost::python::import In-Reply-To: <460A9A5A.7040801@sympatico.ca> References: <45E63B84.2040705@sympatico.ca> <200703281807.06499.meine@informatik.uni-hamburg.de> <460A9A5A.7040801@sympatico.ca> Message-ID: <44DE8DA5-766D-42FC-A052-083DF1203188@acm.org> On Mar 28, 2007, at 9:39 AM, Stefan Seefeld wrote: > Hans Meine wrote: >> Hi! >> >> Am Donnerstag, 01. M?rz 2007 03:33:40 schrieb Stefan Seefeld: >>> --- libs/python/src/import.cpp 27 Oct 2006 21:17:25 -0000 1.2 >>> +++ libs/python/src/import.cpp 1 Mar 2007 02:00:47 -0000 >>> @@ -17,7 +17,7 @@ >>> { >>> // should be 'char const *' but older python versions don't >>> use 'const' >>> yet. char *n = python::extract(name); >>> - python::handle<> module(python::borrowed(PyImport_AddModule(n))); >>> + python::handle<> module(python::borrowed(PyImport_ImportModule >>> (n))); >>> return python::object(module); >>> } >> >> I just saw your patch and looked at the docs.. isn't there a >> borrowed() too >> much now? In contrast to AddModule, ImportModule returns a new >> reference >> according to http://docs.python.org/api/importing.html > > Oh, duh ! (Why isn't Python written in a nicer and more convenient > language > that didn't require users to think about ref counting on each call ?!) > > Dave, Thomas, > > may I remove that extra python::borrowed in HEAD as well as > RC_1_34_0 ? Unless Dave objects, please go ahead Thomas -- Thomas Witt witt at acm.org From seefeld at sympatico.ca Wed Mar 28 20:03:46 2007 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 28 Mar 2007 14:03:46 -0400 Subject: [C++-sig] [python] Fix for boost::python::import In-Reply-To: <87zm5x8dez.fsf@valverde.peloton> References: <45E63B84.2040705@sympatico.ca> <200703281807.06499.meine@informatik.uni-hamburg.de> <460A9A5A.7040801@sympatico.ca> <44DE8DA5-766D-42FC-A052-083DF1203188@acm.org> <87zm5x8dez.fsf@valverde.peloton> Message-ID: <460AAE02.8030408@sympatico.ca> David Abrahams wrote: > on Wed Mar 28 2007, Thomas Witt wrote: > >> On Mar 28, 2007, at 9:39 AM, Stefan Seefeld wrote: >>> may I remove that extra python::borrowed in HEAD as well as >>> RC_1_34_0 ? >> Unless Dave objects, please go ahead > > import is out of the tests for 1.34, so it's fine with me. I'm proposing a change to the implementation of boost::python::import(). Hopefully the change is correct, no matter whether we actually have a test for it or not. :-) Thanks, Stefan -- ...ich hab' noch einen Koffer in Berlin... From dave at boost-consulting.com Wed Mar 28 19:51:16 2007 From: dave at boost-consulting.com (David Abrahams) Date: Wed, 28 Mar 2007 13:51:16 -0400 Subject: [C++-sig] [python] Fix for boost::python::import In-Reply-To: <44DE8DA5-766D-42FC-A052-083DF1203188@acm.org> (Thomas Witt's message of "Wed\, 28 Mar 2007 10\:16\:01 -0700") References: <45E63B84.2040705@sympatico.ca> <200703281807.06499.meine@informatik.uni-hamburg.de> <460A9A5A.7040801@sympatico.ca> <44DE8DA5-766D-42FC-A052-083DF1203188@acm.org> Message-ID: <87zm5x8dez.fsf@valverde.peloton> on Wed Mar 28 2007, Thomas Witt wrote: > On Mar 28, 2007, at 9:39 AM, Stefan Seefeld wrote: > >> Hans Meine wrote: >>> Hi! >>> >>> Am Donnerstag, 01. M?rz 2007 03:33:40 schrieb Stefan Seefeld: >>>> --- libs/python/src/import.cpp 27 Oct 2006 21:17:25 -0000 1.2 >>>> +++ libs/python/src/import.cpp 1 Mar 2007 02:00:47 -0000 >>>> @@ -17,7 +17,7 @@ >>>> { >>>> // should be 'char const *' but older python versions don't >>>> use 'const' >>>> yet. char *n = python::extract(name); >>>> - python::handle<> module(python::borrowed(PyImport_AddModule(n))); >>>> + python::handle<> module(python::borrowed(PyImport_ImportModule >>>> (n))); >>>> return python::object(module); >>>> } >>> >>> I just saw your patch and looked at the docs.. isn't there a >>> borrowed() too >>> much now? In contrast to AddModule, ImportModule returns a new >>> reference >>> according to http://docs.python.org/api/importing.html >> >> Oh, duh ! (Why isn't Python written in a nicer and more convenient >> language >> that didn't require users to think about ref counting on each call ?!) >> >> Dave, Thomas, >> >> may I remove that extra python::borrowed in HEAD as well as >> RC_1_34_0 ? > > Unless Dave objects, please go ahead import is out of the tests for 1.34, so it's fine with me. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com From francis_moreau at hotmail.com Wed Mar 28 22:10:53 2007 From: francis_moreau at hotmail.com (Francis Moreau) Date: Wed, 28 Mar 2007 16:10:53 -0400 Subject: [C++-sig] std::string by reference from C++ to Python Message-ID: Hi, I would like to know what is the best way to pass a std::string by reference from C++ to Python. I know this should not work directly since string are immutable in Python. Let's say that I have this C++ base class: class State { virtual bool processEvent(const Event& in_event, std::string& out_strNextState); } And this C++ base class could be derived from Python, then Python would be able to decide what is the next state by setting out_strNextState function parameter. I know this could be much easier by changing the signature of the processEvent function (as retourning a std::string), but it's important for me to keep this signature. Thanks, Francis From roman.yakovenko at gmail.com Wed Mar 28 22:50:44 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 28 Mar 2007 22:50:44 +0200 Subject: [C++-sig] range with virtual functions In-Reply-To: <200703281715.16964.p.jaroszynski@gmail.com> References: <200703281715.16964.p.jaroszynski@gmail.com> Message-ID: <7465b6170703281350g2cfaa7cs6e5955aee7d0f201@mail.gmail.com> On 3/28/07, Piotr Jaroszynski wrote: > > Hello, > > I have yet another "problem" :] I want to expose a begin_foo and end_foo > functions as an iterable object like: > .add_property("foo", range(&Blah:begin_foo, &Blah::end_foo)) > > How can I do it when begin_foo and end_foo are virtual functions? I am > interested in both cases, when they are pure virtual and virtual with > default implementation. I am not sure whether range takes references to [pure]virtual functions, but you can use same trick as in your previous question: define 2 static functions, which take reference to your class instance and return relevant iterator. Than expose these functions. This should work. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Wed Mar 28 23:15:17 2007 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 28 Mar 2007 23:15:17 +0200 Subject: [C++-sig] std::string by reference from C++ to Python In-Reply-To: References: Message-ID: <7465b6170703281415r111e4470y325b604b325ac691@mail.gmail.com> On 3/28/07, Francis Moreau wrote: > > Hi, > > I would like to know what is the best way to pass a std::string by > reference > from C++ to Python. I know this should not work directly since string are > immutable in Python. > > Let's say that I have this C++ base class: > > class State { > virtual bool processEvent(const Event& in_event, std::string& > out_strNextState); > } > > And this C++ base class could be derived from Python, then Python would be > able to decide what is the next state by setting out_strNextState function > parameter. I know this could be much easier by changing the signature of > the > processEvent function (as retourning a std::string), but it's important > for > me to keep this signature. Well you cannot do this, but you can find acceptable solution - Py++ :-) In general the solution to have different interfaces for C++ and Python code. I attached few files that shows you how you can achieve this. All relevant source code is in Py++ SVN. Here is the source code you need to write if you use Py++: from pyplusplus.module_builder import module_builder_t from pyplusplus import function_transformers as ft mb = module_builder_t( ... ) mb.mem_fun( 'processEvent' ).add_transformation( ft.out( 'out_strNextState' ) ) mb.write_module( your file name ) That's all. Here you will find list of transformations Py++ supports: http://language-binding.net/pyplusplus/documentation/functions/transformation/built_in/built_in.html HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: function_transformations_to_be_exported.hpp Type: text/x-c++hdr Size: 562 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: function_transformations.cpp Type: text/x-c++src Size: 2285 bytes Desc: not available URL: From p.jaroszynski at gmail.com Thu Mar 29 01:36:28 2007 From: p.jaroszynski at gmail.com (Piotr Jaroszynski) Date: Thu, 29 Mar 2007 01:36:28 +0200 Subject: [C++-sig] range with virtual functions In-Reply-To: <7465b6170703281350g2cfaa7cs6e5955aee7d0f201@mail.gmail.com> References: <200703281715.16964.p.jaroszynski@gmail.com> <7465b6170703281350g2cfaa7cs6e5955aee7d0f201@mail.gmail.com> Message-ID: <200703290136.28720.p.jaroszynski@gmail.com> On Wednesday 28 of March 2007 22:50:44 Roman Yakovenko wrote: > I am not sure whether range takes references to [pure]virtual functions, > but you > can use same trick as in your previous question: define 2 static functions, > which take reference to your class instance and return relevant iterator. > Than expose > these functions. This should work. I basically get the idea, but I don't know how to implement these static functions to return the relevant iterator. When just wrapping non-pure virtuals I do: struct FooWrapper (...) iterator begin_foo() { if (override begin_foo = this->get_override("begin_foo")) return begin_foo(); return Foo::begin_foo(); } iterator default_begin_foo() { return this->Foo::begin_foo(); } and then .def("begin_foo", &Foo::begin_foo, &FooWrapper::defualt_begin_foo). So I suppose I need to somehow emulate what's def doing here in the static functions, but how? P.S. I always wondered why .def is not taking the &FooWrapper::begin_foo, but the default_begin_foo and the "original" begin_foo from Foo. -- Best Regards, Piotr Jaroszynski From dave at boost-consulting.com Fri Mar 30 23:44:44 2007 From: dave at boost-consulting.com (David Abrahams) Date: Fri, 30 Mar 2007 17:44:44 -0400 Subject: [C++-sig] Two Boost.Python talks at BoostCon'07 Message-ID: <87slbm2ypf.fsf@valverde.peloton> I'm giving a tutorial called "Hybrid Development with Boost.Python (and More!)" [http://www.boostcon.com/program/sessions#abrahams-hybrid] and Timothy Shead will be speaking on "Implementing out-of-the-ordinary object models using Boost.Python" [http://www.boostcon.com/program/sessions#shead-python-object-models] >From the site: "Promising to be the main face-to-face event for all things Boost, BoostCon'07 opens the door to your C++ future. From using the Boost libraries to writing and maintaining them, from evangelizing to deploying Boost within your organization, from infrastructure and process to vision and mission, and from TR1 to TR2, BoostCon brings together the sessions, the colleagues, and the inspiration to support your work with Boost for the next year." see http://www.boostcon.com -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com