From rwgk at yahoo.com Sat Apr 1 01:16:18 2006 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Fri, 31 Mar 2006 15:16:18 -0800 (PST) Subject: [C++-sig] Problem with Templated class and Boost.python In-Reply-To: Message-ID: <20060331231619.73542.qmail@web31507.mail.mud.yahoo.com> --- Arrakis Muadib wrote: > I'm trying to create a binding for a small c++ class. I decided to > created directly with Boost.Python and did'nt use Pyste cause it's not > supported on my distro and I cannot make gccxml working. > > Now I'm confronted with a class having a template parameter. > > The signature is something like this: > > template > class A : virtual public B > { > T methodX() {...} > ... > }; > > How can I map the template parameter to the boost.python class definition? If you have just one A to wrap, simply do it like this: class_ >("A_int") .def("method_a", &A::method_a) ; If you have many, write a helper struct like this: template struct A_wrapper { static void wrap(const char* python_name) { class_ >(python_name) .def("method_a", &A::method_a) // etc. ; } }; Later: A_wrapper::wrap("A_int"); A_wrapper::wrap("A_long"); // etc. Here is a complete example: http://cvs.sourceforge.net/viewcvs.py/cctbx/scitbx/include/scitbx/stl/vector_wrapper.h?view=markup http://cvs.sourceforge.net/viewcvs.py/cctbx/scitbx/include/scitbx/stl/vector_ext.cpp?view=markup Cheers, Ralf __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From DRandallTaylor at yahoo.com Sat Apr 1 19:31:49 2006 From: DRandallTaylor at yahoo.com (DRandallTaylor) Date: Sat, 1 Apr 2006 17:31:49 +0000 (UTC) Subject: [C++-sig] how to disambiguate 2 overloaded mem funs each with just 1 arg Message-ID: Hello, N00bie here. How do I disambiguate 2 member functions each having just 1 arg: struct C { int len(const char* const val) { return ::strlen(val); } int len(const std::string &val) { return val.size(); } }; BOOST_PYTHON_MODULE(pycore) { boost::python::class_("C") .def("len" , &C::len , boost::python::return_value_policy< python::return_by_value >() ); } There must be some way to tell boost::python that when I pass a python str object to C::len then convert the arg to a "const char* const" and call the first overload of C::len. (I told you this was going to be easy! But not for me because I'm n00b.). Cheers, DRandallTaylor From dave at boost-consulting.com Sat Apr 1 20:50:43 2006 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 01 Apr 2006 10:50:43 -0800 Subject: [C++-sig] how to disambiguate 2 overloaded mem funs each with just 1 arg References: Message-ID: DRandallTaylor writes: > Hello, > N00bie here. > How do I disambiguate 2 member functions each having just 1 arg: > > struct C > { > int len(const char* const val) { return ::strlen(val); } > int len(const std::string &val) { return val.size(); } > }; > BOOST_PYTHON_MODULE(pycore) > { > boost::python::class_("C") > .def("len" > , &C::len > , boost::python::return_value_policy< python::return_by_value >() > ); > } > > There must be some way to tell boost::python that when I pass a python str > object to C::len then convert the arg to a "const char* const" and call the > first overload of C::len. (I told you this was going to be easy! But not for > me because I'm n00b.). boost::python::class_("C") .def("len" , (int (C::*)(char const*))&C::len , boost::python::return_value_policy< python::return_by_value >() ); HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com From DRandallTaylor at yahoo.com Sat Apr 1 23:12:16 2006 From: DRandallTaylor at yahoo.com (DRandallTaylor) Date: Sat, 1 Apr 2006 21:12:16 +0000 (UTC) Subject: [C++-sig] python override of C++ virtual mem fun not exactly working yet Message-ID: Hello again. n00b here. Simple problem, but hard for me, cause I'm n00b. My Python class derives from a C++ class and overrides a virtual mem fun that has an implementation. In the derived Python class, I need "self" to be treated polymorphically with respect to the C++ base class. The boost::python Tutorial gives a sample but the derivation shown of BaseWrap is private, not public derivation: "struct BaseWrap : Base, wrapper". I know this is a simple problem... but having trouble with it... My error msg tells me that my 2nd arg is a InboxListener not a Callback. File "test.py", line 9, in __init__ self.inbox = sess.CreateInbox(self) ArgumentError: Python argument types in Session.CreateInbox(Session, InboxListener) did not match C++ signature: CreateInbox(class Session {lvalue}, class Callback {lvalue}) My C++ stuff: class CallbackWrap : public Callback, python::wrapper { public: void OnData() // iff public derivation used, then this is virtual; else non-virtual { if (python::override the_override = this->get_override("OnData")) the_override(); else Callback::OnData(); } void default_OnData() { this->Callback::OnData(); } }; python::class_ < CallbackWrap, boost::noncopyable > ("Callback") .def("OnData", &Callback::OnData, &CallbackWrap::default_OnData); python::class_("Session") .def("CreateInbox", &Session::CreateInbox, python::return_value_policy< python::return_by_value >()); And my Python stuff: class InboxListener(Callback): def __init__(self, session): self.inbox = session.CreateInbox(self) def OnData(): print "OnData called" return Seems like "self" passed to session.CreateInbox should be treated as a Callback type, since I've used public derivatation. Can you help me spot the error of my ways? And I thank you! DRandallTaylor From seefeld at sympatico.ca Sat Apr 1 23:29:26 2006 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Sat, 01 Apr 2006 16:29:26 -0500 Subject: [C++-sig] python override of C++ virtual mem fun not exactly working yet In-Reply-To: References: Message-ID: <442EF0B6.9000506@sympatico.ca> DRandallTaylor wrote: > Hello again. n00b here. > Simple problem, but hard for me, cause I'm n00b. > > My Python class derives from a C++ class and overrides a virtual mem fun that > has an implementation. In the derived Python class, I need "self" to be > treated polymorphically with respect to the C++ base class. The boost::python > Tutorial gives a sample but the derivation shown of BaseWrap is private, not > public derivation: "struct BaseWrap : Base, wrapper". I know this is a > simple problem... but having trouble with it... No it isn't. The default access-specifier for struct is public, not private. > > My error msg tells me that my 2nd arg is a InboxListener not a Callback. > > > File "test.py", line 9, in __init__ > self.inbox = sess.CreateInbox(self) > ArgumentError: Python argument types in > Session.CreateInbox(Session, InboxListener) > did not match C++ signature: > CreateInbox(class Session {lvalue}, class Callback {lvalue}) [...] > And my Python stuff: > > > class InboxListener(Callback): > def __init__(self, session): > self.inbox = session.CreateInbox(self) In python you have to invoke the base class constructor explicitely in order to initialize it. I believe this will fix your problem. HTH, Stefan From DRandallTaylor at yahoo.com Sat Apr 1 23:45:25 2006 From: DRandallTaylor at yahoo.com (DRandallTaylor) Date: Sat, 1 Apr 2006 21:45:25 +0000 (UTC) Subject: [C++-sig] python override of C++ virtual mem fun not exactly working yet References: <442EF0B6.9000506@sympatico.ca> Message-ID: I've added the invocation of the base class constructor (thanks Stefan): class InboxListener(Callback): def __init__(self, sess): Callback.__init__(self) self.inbox = sess.CreateInbox(self) # ArgumentError here def OnData(): print "OnData called" But I still get this: ArgumentError: Python argument types in VIAmSession.CreateInbox(Session, InboxListener) did not match C++ signature: CreateInbox(class Session {lvalue}, class Callback {lvalue}) Python does not yet know that my InboxListener is derived from Callback. How shall I tell her? Cheers. From DRandallTaylor at yahoo.com Sun Apr 2 02:44:27 2006 From: DRandallTaylor at yahoo.com (DRandallTaylor) Date: Sun, 2 Apr 2006 00:44:27 +0000 (UTC) Subject: [C++-sig] =?utf-8?q?python_override_of_C++_virtual_mem_fun_not_ex?= =?utf-8?q?actly=09working_yet?= References: <442EF0B6.9000506@sympatico.ca> Message-ID: DRandallTaylor yahoo.com> writes: > [...] May I rephrase? Is conversion from a Python class to the boost::python type that it is derived from automatic? I think it probably is, therefore I must be doing something wrong: class InboxListener(VIAmCallback): def __init__(self, sess): VIAmCallback.__init__(self) self.inbox = sess.CreateInbox(self) gives me: File "test.py", line 32, in run listener = InboxListener(sess) File "test.py", line 10, in __init__ self.inbox = sess.CreateInbox(self) ArgumentError: Python argument types in VIAmSession.CreateInbox(VIAmSession, InboxListener) did not match C++ signature: CreateInbox(class tt_stream_ns::VIAmSession {lvalue}, class tt_stream_ns::VIAmCallback {lvalue}) And my C++ looked like this: struct VIAmCallbackPublic : public tt_stream_ns::VIAmCallback { [...] }; struct VIAmCallbackWrap : public VIAmCallbackPublic, python::wrapper { [...] ] python::class_ ("VIAmCallback") .def("OnData", &VIAmCallbackPublic::OnData, &VIAmCallbackWrap::default_OnData); And I thought that the Python error message above tells me that python does NOT believe that InboxListener is actually a tt_stream_ns::VIAmCallback. So I experimented by added a python::bases<> template template arg like this: python::class_ < VIAmCallbackWrap , python::bases , boost::noncopyable > ("VIAmCallback") def("OnData", &VIAmCallbackPublic::OnData, &VIAmCallbackWrap::default_OnData); And then I get a worse error from Python: Traceback (most recent call last): File "test.py", line 1, in ? from pycore import * RuntimeError: extension class wrapper for base class struct VIAmCallbackPublic has not been created yet Am I completely clueless or simply overlooking something. Please tell me it is the latter. Sincerely, Your Faithful n00b. DRandallTaylor From roman.yakovenko at gmail.com Sun Apr 2 06:25:08 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 2 Apr 2006 06:25:08 +0200 Subject: [C++-sig] C++ compile error on .def from n00b. In-Reply-To: References: Message-ID: <7465b6170604012025j17de3ca5qbcbb8a422c732004@mail.gmail.com> On 3/31/06, Randy Taylor wrote: > I'm a n00b. > I'm trying to expose a C++ class that has a virtual mem fun with an > implementation that I wish to override in Python. Seems easy enough. > Compiler disagrees, saying she does not like my def. I must be doing something > wrong. What is it? Thanks kindly in advance. > Hi. It would be nice if you could provide less source that shows your problem. Also I think in this case ( noncopyable ) you did not setup class init method(s). In order to get more readable errors you can write something like this typedef boost::python::class_< ... > my_class_exposer_t; my_class_exposer_t exposer( ... ); exposer.def( ... ); At least you will be pointed by compiler to the right line. And an other thing: if you are just start with Boost.Python then try one of available code generators: pyplusplus ( has gui ) and/or Pyste. You will be able to save a lot of time. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From achim-bpl at mol-net.com Mon Apr 3 11:11:27 2006 From: achim-bpl at mol-net.com (Achim H.) Date: Mon, 3 Apr 2006 11:11:27 +0200 Subject: [C++-sig] =?iso-8859-1?q?How_to_deep-copy_and_delete_a_mixed_pyth?= =?iso-8859-1?q?on/C++_object=09from_C++?= In-Reply-To: <200603311757.k2VHvFgj015725@libra3.slac.stanford.edu> References: <200603311048.02242.achim-bpl@mol-net.com> <200603311757.k2VHvFgj015725@libra3.slac.stanford.edu> Message-ID: <200604031111.27888.achim-bpl@mol-net.com> Am Freitag, 31. M?rz 2006 19:57 schrieb Paul F. Kunz: > >>>>> On Fri, 31 Mar 2006 10:48:01 +0200, "Achim H." > >>>>> said: > > > > How can I achieve both correct copying and deletion semantics ? > > I'm not sure about deletion, but here is how I do the copying (with > a lot of help from Dave Abrahams)... > > > http://www.slac.stanford.edu/grp/ek/hippodraw/lib/FunctionWrap_8cxx-source.html#l00179 I came to a similar solution, in my case not refering directly to the owning python object, but looking up the class in the __main__ namespace (class name was present as a member for introspection reasons anyway). Your solution is more widely applicable (though at the moment only working in the case that there is no python override, as David Abrahams pointed out). I will therefore probably adopt your strategy. However, your way of cloning IMHO creates the same chicken-and-egg problem regarding ownership as my Wrapper did when I played around: the Wrap is owned by the python object and the python object is owned by the wrap. A delete on the Wrap will auto-destruct the python object, that will then try to delete the Wrap. If you try to delete it from the C++ side, you will get an error (e.g., SIGSEGV). I'm not totally sure whether Python at the end tries to delete all objects still being around. If python does not, you created a memory leak which probably is a minor concern to you if you don't have many instances of FunctionWrap around. I myself played around with an arbitrary Py_INCREF() which at first only postponed the error message to the program end. When I started to also employ boost::shared_ptr<> (class_ ...>), it vanished. Don't ask me why, it kind of works for me now. Still, I would like to understand the situation better and use a correct solution, not only one that works in most of the cases or on my current platform. Any how, thanks for your help, it at least provides me with a better implementation of the clone() method, though the ownership issues are still unsolved for me. Achim. From Barnaby.Dalton at brevanhoward.com Mon Apr 3 19:15:07 2006 From: Barnaby.Dalton at brevanhoward.com (Dalton, Barnaby) Date: Mon, 3 Apr 2006 18:15:07 +0100 Subject: [C++-sig] boost python how to handle a type of shared_ptr Message-ID: <383BF6A1AD220A4796FE90A9EC3FB406010DF782@bhmail1.rivagecapital.com> How do I use a shared_ptr type in boost python? I have the following test code: #include #include using namespace std; using namespace boost; // vector vector listTest1() { vector a; a.push_back(5); a.push_back(10); return a; } // shared pointer to vector shared_ptr > listTest2() { shared_ptr > a(new vector()); a->push_back(6); a->push_back(11); return a; } // const vector const vector listTest3() { vector a; a.push_back(7); a.push_back(12); return a; } // shared pointer to const vector shared_ptr > listTest4() { shared_ptr > a(new vector()); a->push_back(8); a->push_back(13); return a; } #include #include #include #include using namespace boost::python; BOOST_PYTHON_MODULE(hello) { class_ >("vec_int") .def("push_back", &vector::push_back) .def("__iter__", iterator >()) ; def("listTest1", listTest1); def("listTest2", listTest2); def("listTest3", listTest3); def("listTest4", listTest4); register_ptr_to_python< boost::shared_ptr > >(); //register_ptr_to_python< boost::shared_ptr > >(); } listTest 1->3 work fine. If I try and call listTest4 from python I get: Traceback (most recent call last): File "test.py", line 19, in ? a = [x for x in hello.listTest4()] TypeError: No to_python (by-value) converter found for C++ type: class boost::shared_ptr > const > If I uncomment 'register_ptr_to_python< boost::shared_ptr > >();' then I get compile errors: C:\Source\boost_1_33_1\boost\python\object\pointer_holder.hpp(138) : error C2664: 'boost::python::objects::find_dynamic_type' : cannot convert parameter 1 from 'const X *' to 'void *' Conversion loses qualifiers C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(152) : while compiling class-template member function 'void *boost::python::objects::pointer_holder::holds(boost::pyt hon::type_info,bool)' with [ Pointer=boost::shared_ptr>, Value=const X ] etc.... I'm new to boost python so please feel free to point to any documentation/examples I might have missed. Thanks Barney From me at elwedgo.de Tue Apr 4 12:07:01 2006 From: me at elwedgo.de (Peter Dinges) Date: Tue, 4 Apr 2006 10:07:01 +0000 (UTC) Subject: [C++-sig] Wrapper for exception translation References: <2040C0A1CA23D51181A30050BAAC9902011046D8@berexch.ber.haufemg.com> Message-ID: David Abrahams boost-consulting.com> writes: > > Colin Irwin hotmail.com> writes: > > > Anyway, I think I'm still keen to do whatever on this front to help > > with a wrapper to translate exceptions through into Python. > > Thanks; I'll keep it in mind. > Hello, just wanted to ask whether anyone found time yet to implement the exception wrapping template? Best regards, Peter Dinges From nail-mail at mail.ru Tue Apr 4 16:04:45 2006 From: nail-mail at mail.ru (Victor Nakoryakov) Date: Tue, 04 Apr 2006 18:04:45 +0400 Subject: [C++-sig] Renaming "" filename for PyRun_String Message-ID: Hi all, I use PyRun_String to run script stored in some file. I can't use PyRun_File because of different layouts of FILE struct in my project and in python lib. So once I get error I see something like: Traceback (most recent call last): File "", line 14, in ? NameError: ... Is it possible somehow to rename "" in this message to the real filename? -- Victor (aka nail) Nakoryakov nail-mailmailru Krasnoznamensk, Moscow, Russia From kelly at kkcsm.net Wed Apr 5 06:29:52 2006 From: kelly at kkcsm.net (Kelly Burkhart) Date: Tue, 4 Apr 2006 23:29:52 -0500 Subject: [C++-sig] Adding a method on the fly Message-ID: Is it possible to add a method to an object on the fly? Something like: // Add a method 'foo' to object void addMethod( object obj ) { call_method(obj.ptr(),"__setattr__", "foo", "lambda x:x+x" ); } The "lambda..." string would have to get evaluated by Python to return an appropriate object to bind to foo. Is there any way to do something like this? TIA, -K -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Wed Apr 5 07:55:42 2006 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 05 Apr 2006 01:55:42 -0400 Subject: [C++-sig] Adding a method on the fly In-Reply-To: References: Message-ID: <44335BDE.20101@sympatico.ca> Kelly Burkhart wrote: > Is it possible to add a method to an object on the fly? Something like: > > // Add a method 'foo' to object > void addMethod( object obj ) > { > call_method(obj.ptr(),"__setattr__", "foo", "lambda x:x+x" ); > } > > The "lambda..." string would have to get evaluated by Python to return > an appropriate object to bind to foo. Is there any way to do something > like this? Yes, the following code ought to do what you want: using namespace boost::python; int main(int, char **) { Py_Initialize(); try { // Retrieve the main module object main = import("__main__"); // Retrieve the main module's namespace object global(main.attr("__dict__")); // Define the derived class in Python. object result = exec( "class A: \n" " def hello(self): \n" " return 'Hello from Python!' \n" "def func(self, x): \n" " return x+x \n", global, global); object A = global["A"]; A.attr("method") = global["func"]; result = exec("a = A() \n" "print a.hello() \n" "print a.method(21) \n", global, global); } catch (error_already_set) { PyErr_Print(); } } Note that the first exec defines class 'A' as well as a function 'func' in python and stores them in 'global'. Then, from within C++, 'func' is attached to 'A' under the name 'method'. Finally, another chunk of python is executed, using the same namespace (making sure the modified 'A' class is exposed), where the newly created method is run through a new instance of 'A'. HTH, Stefan From star_1234 at sancharnet.in Wed Apr 5 10:13:07 2006 From: star_1234 at sancharnet.in (satish) Date: Wed, 05 Apr 2006 13:43:07 +0530 Subject: [C++-sig] Your confirmation is required to join the C++-sig mailing list References: Message-ID: <000e01c65888$c654a370$52b5583b@starsoft3ojkis> ----- Original Message ----- From: To: Sent: Friday, March 31, 2006 12:43 PM Subject: Your confirmation is required to join the C++-sig mailing list > Mailing list subscription confirmation notice for mailing list C++-sig > > We have received a request from 203.145.188.130 for subscription of > your email address, "star1234 at sancharnet.in", to the > c++-sig at python.org mailing list. To confirm that you want to be added > to this mailing list, simply reply to this message, keeping the > Subject: header intact. Or visit this web page: > > http://mail.python.org/mailman/confirm/c++-sig/39904eb3065e1b9624fdfd44ebcac837d019c81a > > > Or include the following line -- and only the following line -- in a > message to c++-sig-request at python.org: > > confirm 39904eb3065e1b9624fdfd44ebcac837d019c81a > > Note that simply sending a `reply' to this message should work from > most mail readers, since that usually leaves the Subject: line in the > right form (additional "Re:" text in the Subject: is okay). > > If you do not wish to be subscribed to this list, please simply > disregard this message. If you think you are being maliciously > subscribed to the list, or have any other questions, send them to > c++-sig-owner at python.org. From Naceur.Meskini at sophia.inria.fr Wed Apr 5 10:45:48 2006 From: Naceur.Meskini at sophia.inria.fr (Naceur Meskini) Date: Wed, 05 Apr 2006 10:45:48 +0200 Subject: [C++-sig] Your confirmation is required to join the C++-sig mailing list In-Reply-To: <000e01c65888$c654a370$52b5583b@starsoft3ojkis> References: <000e01c65888$c654a370$52b5583b@starsoft3ojkis> Message-ID: <443383BC.2000104@sophia.inria.fr> satish wrote: >----- Original Message ----- >From: >To: >Sent: Friday, March 31, 2006 12:43 PM >Subject: Your confirmation is required to join the C++-sig mailing list > > > > >>Mailing list subscription confirmation notice for mailing list C++-sig >> >>We have received a request from 203.145.188.130 for subscription of >>your email address, "star1234 at sancharnet.in", to the >>c++-sig at python.org mailing list. To confirm that you want to be added >>to this mailing list, simply reply to this message, keeping the >>Subject: header intact. Or visit this web page: >> >> >> >> >http://mail.python.org/mailman/confirm/c++-sig/39904eb3065e1b9624fdfd44ebcac837d019c81a > > >>Or include the following line -- and only the following line -- in a >>message to c++-sig-request at python.org: >> >> confirm 39904eb3065e1b9624fdfd44ebcac837d019c81a >> >>Note that simply sending a `reply' to this message should work from >>most mail readers, since that usually leaves the Subject: line in the >>right form (additional "Re:" text in the Subject: is okay). >> >>If you do not wish to be subscribed to this list, please simply >>disregard this message. If you think you are being maliciously >>subscribed to the list, or have any other questions, send them to >>c++-sig-owner at python.org. >> >> > >_______________________________________________ >C++-sig mailing list >C++-sig at python.org >http://mail.python.org/mailman/listinfo/c++-sig > > From kelly at kkcsm.net Wed Apr 5 15:17:28 2006 From: kelly at kkcsm.net (Kelly Burkhart) Date: Wed, 5 Apr 2006 08:17:28 -0500 Subject: [C++-sig] Adding a method on the fly In-Reply-To: <44335BDE.20101@sympatico.ca> References: <44335BDE.20101@sympatico.ca> Message-ID: On 4/5/06, Stefan Seefeld wrote: > > > Yes, the following code ought to do what you want: > > using namespace boost::python; > > int main(int, char **) > { > > Py_Initialize(); > try > { > // Retrieve the main module > object main = import("__main__"); > > // Retrieve the main module's namespace > object global(main.attr("__dict__")); > > // Define the derived class in Python. > object result = exec( > "class A: \n" > " def hello(self): \n" > " return 'Hello from Python!' \n" > "def func(self, x): \n" > " return x+x \n", > global, global); > object A = global["A"]; > A.attr("method") = global["func"]; > result = exec("a = A() \n" > "print a.hello() \n" > "print a.method(21) \n", > global, global); > } > catch (error_already_set) > { > PyErr_Print(); > } > } > > Note that the first exec defines class 'A' as well as a function 'func' > in python and stores them in 'global'. Then, from within C++, 'func' > is attached to 'A' under the name 'method'. Finally, another chunk > of python is executed, using the same namespace (making sure the > modified 'A' class is exposed), where the newly created method is run > through a new instance of 'A'. > Thank you Stefan, I can't find 'exec'. Is it in the boost::python namespace? What header do I need to include? -K -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Wed Apr 5 15:19:56 2006 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 05 Apr 2006 09:19:56 -0400 Subject: [C++-sig] Adding a method on the fly In-Reply-To: References: <44335BDE.20101@sympatico.ca> Message-ID: <4433C3FC.70807@sympatico.ca> Kelly Burkhart wrote: > I can't find 'exec'. Is it in the boost::python namespace? What header > do I need to include? It's in the development version, and will be part of the upcoming release. Sorry for not mentioning that. Regards, Stefan From kelly at kkcsm.net Wed Apr 5 18:17:33 2006 From: kelly at kkcsm.net (Kelly Burkhart) Date: Wed, 5 Apr 2006 11:17:33 -0500 Subject: [C++-sig] Adding a method on the fly In-Reply-To: <4433C3FC.70807@sympatico.ca> References: <44335BDE.20101@sympatico.ca> <4433C3FC.70807@sympatico.ca> Message-ID: On 4/5/06, Stefan Seefeld wrote: > > Kelly Burkhart wrote: > > > I can't find 'exec'. Is it in the boost::python namespace? What header > > do I need to include? > > It's in the development version, and will be part of the upcoming release. > Sorry for not mentioning that. > No problem. Will this development version be production soon? Is there a way to do what I'm looking for with v1.33.1? -K -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Wed Apr 5 18:21:31 2006 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 05 Apr 2006 12:21:31 -0400 Subject: [C++-sig] Adding a method on the fly In-Reply-To: References: <44335BDE.20101@sympatico.ca> <4433C3FC.70807@sympatico.ca> Message-ID: <4433EE8B.9020501@sympatico.ca> Kelly Burkhart wrote: > On 4/5/06, *Stefan Seefeld* > wrote: > > Kelly Burkhart wrote: > > > I can't find 'exec'. Is it in the boost::python namespace? What > header > > do I need to include? > > It's in the development version, and will be part of the upcoming > release. > Sorry for not mentioning that. > > > > No problem. Will this development version be production soon? As I said, it will be in the upcoming release, i.e. 1.34. > Is there a way to do what I'm looking for with v1.33.1? I'm not sure what bits you were particularly interested in. I showed the 'exec' function only as an example (because constructing and running a python object in pure C++ would be pretty pointless. :-) ). You were asking about binding a function to an object, which doesn't require 'exec'. Regards, Stefan From dominic.sacre at gmx.de Wed Apr 5 21:05:32 2006 From: dominic.sacre at gmx.de (Dominic =?iso-8859-1?q?Sacr=E9?=) Date: Wed, 5 Apr 2006 21:05:32 +0200 Subject: [C++-sig] Wrapping pointers Message-ID: <200604052105.33008.dominic.sacre@gmx.de> Hi, I'm having trouble exporting C++ pointers to Python. My code looks like this: class A { A *p; ... }; BOOST_PYTHON_MODULE(test) { class_("A", init<>()) .def_readonly("p", &A::p) ... ; } Now every time I try to access A::p from Python I get this error: TypeError: No to_python (by-value) converter found for C++ type: A* I searched the list archives and found a message about the same issue, where the only suggestion was to use .def_readonly("p", make_getter(&A::p, return_internal_reference<1>())) instead, but this doesn't help either. On the contrary, this causes a segfault every time I use A::p (although p points to a valid object, at least in C++). So what should I do to access this pointer from Python? Thanks, Dominic From kelly at kkcsm.net Wed Apr 5 21:58:48 2006 From: kelly at kkcsm.net (Kelly Burkhart) Date: Wed, 5 Apr 2006 14:58:48 -0500 Subject: [C++-sig] Adding a method on the fly In-Reply-To: <4433EE8B.9020501@sympatico.ca> References: <44335BDE.20101@sympatico.ca> <4433C3FC.70807@sympatico.ca> <4433EE8B.9020501@sympatico.ca> Message-ID: On 4/5/06, Stefan Seefeld wrote: > > Kelly Burkhart wrote: > > Is there a way to do what I'm looking for with v1.33.1? > > I'm not sure what bits you were particularly interested in. > I showed the 'exec' function only as an example (because constructing > and running a python object in pure C++ would be pretty pointless. :-) ). > > You were asking about binding a function to an object, which doesn't > require 'exec'. > I'm trying to programatically create a python function and bind it to a C++ object. Using your code above as an example, I belive it may be something like this: >From C++: using namespace boost; using namespace boost::python; class Foo {...}; void mkMethod( object self, const std::string &name ) { // Get Foo instance namespace object selfdict(self.attr("__dict__")); // Define class exec(str(format("def %s( arg ): print arg") % name.c_str), selfdict, selfdict); } ... // Wrap class Foo class_ cls("Foo"); // Wrap mkMethod function def("mkMethod", mkMethod); // Attach mkMethod to Foo cls.setattr("mkMethod",&mkMethod) >From Python: from foomod import Foo f = Foo() f.mkMethod('newmethod') # now a brand new method of f exists f.newmethod(123) -K -------------- next part -------------- An HTML attachment was scrubbed... URL: From mharidev at qualcomm.com Wed Apr 5 22:44:34 2006 From: mharidev at qualcomm.com (Haridev, Meghana) Date: Wed, 5 Apr 2006 13:44:34 -0700 Subject: [C++-sig] Problem running pygccxml example Message-ID: <5383A81D5EB8F14FBDB487D9B5FBEA88A23716@NAEX11.na.qualcomm.com> Hi Folks, I'm trying to run the pygccxml example (http://www.language-binding.net/pygccxml/pygccxml.html#i-d-like-to-see- an-example). (Also found under pygccxml-0.6.9/docs from the installation tarball) I modified example.py (http://www.language-binding.net/pygccxml/example.py ) to set the correct path for gccxml: config = pygccxml.parser.config_t( gccxml_path=r'/opt/buildtools/bin/gccxml' ) The source file 'core_class_hierarchy.hpp' has no changes: http://www.language-binding.net/pygccxml/core_class_hierarchy.hpp But I get the following error when I run example.py: Traceback (most recent call last): File "example.py", line 13, in ? namespaces = pygccxml.parser.parse( ['core_class_hierarchy.hpp'], config ) File "/opt/buildtools/lib/python2.4/site-packages/pygccxml/parser/__init__.py ", line 25, in parse answer = parser.read_files(files, compilation_mode) File "/opt/buildtools/lib/python2.4/site-packages/pygccxml/parser/project_rea der.py", line 61, in read_files return self.__parse_file_by_file(files) File "/opt/buildtools/lib/python2.4/site-packages/pygccxml/parser/project_rea der.py", line 82, in __parse_file_by_file decls = reader.read_file( header ) File "/opt/buildtools/lib/python2.4/site-packages/pygccxml/parser/source_read er.py", line 140, in read_file raise error KeyError: u'complex long double' I am using: GCC: 4.0.2 Python: 2.4.2 Gccxml: 0.7.0 Pygccxml: 0.6.9 Any pointers to what I'm doing wrong? Am I missing anything in the configuration? Thanks, -Meghana. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kelly at kkcsm.net Thu Apr 6 03:29:19 2006 From: kelly at kkcsm.net (Kelly Burkhart) Date: Wed, 5 Apr 2006 20:29:19 -0500 Subject: [C++-sig] Adding a method on the fly In-Reply-To: References: <44335BDE.20101@sympatico.ca> <4433C3FC.70807@sympatico.ca> <4433EE8B.9020501@sympatico.ca> Message-ID: Success! #include #include #include using namespace boost::python; struct Foo {}; void mkMethod( object self, const std::string &name, int val ) { // Get self instance namespace object selfdict( self.attr("__dict__") ); // Create function and bind to self.name std::string cmd( boost::str(boost::format("lambda x:x+%d") % val )); object result((handle<>( PyRun_String(cmd.c_str() , Py_eval_input , selfdict.ptr() , selfdict.ptr())) )); call_method(self.ptr(),"__setattr__", name.c_str(), result); } BOOST_PYTHON_MODULE(bptst) { class_< Foo > cls( "Foo", init<>() ); def("mkMethod", &mkMethod); cls.setattr("mkMethod", &mkMethod); } >From Python: >>> import bptst >>> f = bptst.Foo() >>> print f.__dict__ {} >>> f.mkMethod('bar', 123) >>> print f.__dict__ {'bar': at 0x402ed3ac>} >>> print f.bar(1) 124 >>> -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Thu Apr 6 07:33:53 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 6 Apr 2006 07:33:53 +0200 Subject: [C++-sig] Problem running pygccxml example In-Reply-To: <5383A81D5EB8F14FBDB487D9B5FBEA88A23716@NAEX11.na.qualcomm.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA88A23716@NAEX11.na.qualcomm.com> Message-ID: <7465b6170604052233s758fe662pf2879a9fedf4a5a0@mail.gmail.com> On 4/5/06, Haridev, Meghana wrote: > Hi Folks, > > > > > I am using: > > GCC: 4.0.2 > > Python: 2.4.2 > > Gccxml: 0.7.0 > > Pygccxml: 0.6.9 > > > > Any pointers to what I'm doing wrong? Am I missing anything in the > configuration? You just hited the bug found and fixed in cvs. Can you use CVS version? Today I am going to commit final version of pygccxml and pyplusplus for the next release. I also update the example. > > Thanks, > > -Meghana. > > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From lloyd at fusion.net.nz Thu Apr 6 11:04:01 2006 From: lloyd at fusion.net.nz (Lloyd Weehuizen) Date: Thu, 06 Apr 2006 21:04:01 +1200 Subject: [C++-sig] Problems with Python "Factories" In-Reply-To: <7465b6170604052233s758fe662pf2879a9fedf4a5a0@mail.gmail.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA88A23716@NAEX11.na.qualcomm.com> <7465b6170604052233s758fe662pf2879a9fedf4a5a0@mail.gmail.com> Message-ID: <4434D981.6070603@fusion.net.nz> Hi I've run into a problem wrapping a C++ base class thats extended in Python and contains a factory method. Here's a simplified example: class FactoryBaseType { }; class FactoryBase { virtual FactoryBaseType* InstanceType(); }; In python I override this with a concrete type: import mylib class ConcreatePythonType( mylib.FactoryBaseType ): pass class Factory( mylib.FactoryBase ) def InstanceType(): return ConcreatePythonType() I've managed to set up the wrappers for the types and everything works great up until I call the InstanceType method on a python based factory from C++. The object is created and returned correctly to C++ from python. The C++ wrapper then uses extract<> to pull out the FactoryBaseType and return it to the callee and thats where it all goes wrong. When the wrappers InstanceType completes the local boost::python::object that has its pointer extracted from goes out of scope and the extract'd pointer becomes a dangling pointer as nobody is referencing the constructed python object anymore. I'm not exactly sure how to solve this, do I have to hack around this by implementing some kind of temporary reference on the object? Or is there some way around this problem that I've missed? Thanks for your help Lloyd From roman.yakovenko at gmail.com Thu Apr 6 11:31:31 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 6 Apr 2006 11:31:31 +0200 Subject: [C++-sig] Problems with Python "Factories" In-Reply-To: <4434D981.6070603@fusion.net.nz> References: <5383A81D5EB8F14FBDB487D9B5FBEA88A23716@NAEX11.na.qualcomm.com> <7465b6170604052233s758fe662pf2879a9fedf4a5a0@mail.gmail.com> <4434D981.6070603@fusion.net.nz> Message-ID: <7465b6170604060231w5a2c2900sc6c581622057281d@mail.gmail.com> On 4/6/06, Lloyd Weehuizen wrote: > Hi > > I've run into a problem wrapping a C++ base class thats extended in > Python and contains a factory method. > > Here's a simplified example: > > class FactoryBaseType > { > }; > > class FactoryBase > { > virtual FactoryBaseType* InstanceType(); > }; > > In python I override this with a concrete type: > > import mylib > > class ConcreatePythonType( mylib.FactoryBaseType ): > pass you should override __init__ method. You have to call mylib.FactoryBaseType.__init__ > class Factory( mylib.FactoryBase ) same as above > def InstanceType(): > return ConcreatePythonType() > > I've managed to set up the wrappers for the types and everything works > great up until I call the InstanceType method on a python based factory > from C++. The object is created and returned correctly to C++ from > python. The C++ wrapper then uses extract<> to pull out the > FactoryBaseType and return it to the callee and thats where it all goes > wrong. When the wrappers InstanceType completes the local > boost::python::object that has its pointer extracted from goes out of > scope and the extract'd pointer becomes a dangling pointer as nobody is > referencing the constructed python object anymore. > > I'm not exactly sure how to solve this, do I have to hack around this by > implementing some kind of temporary reference on the object? Or is there > some way around this problem that I've missed? I am almost sure the code that you will add will solve your problem > Thanks for your help > Lloyd > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From achim-bpl at mol-net.com Thu Apr 6 11:37:08 2006 From: achim-bpl at mol-net.com (Achim H.) Date: Thu, 6 Apr 2006 11:37:08 +0200 Subject: [C++-sig] Problems with Python "Factories" In-Reply-To: <4434D981.6070603@fusion.net.nz> References: <5383A81D5EB8F14FBDB487D9B5FBEA88A23716@NAEX11.na.qualcomm.com> <7465b6170604052233s758fe662pf2879a9fedf4a5a0@mail.gmail.com> <4434D981.6070603@fusion.net.nz> Message-ID: <200604061137.08954.achim-bpl@mol-net.com> Am Donnerstag, 6. April 2006 11:04 schrieb Lloyd Weehuizen: > I've managed to set up the wrappers for the types and everything works > great up until I call the InstanceType method on a python based factory > from C++. The object is created and returned correctly to C++ from > python. The C++ wrapper then uses extract<> to pull out the > FactoryBaseType and return it to the callee and thats where it all goes > wrong. When the wrappers InstanceType completes the local > boost::python::object that has its pointer extracted from goes out of > scope and the extract'd pointer becomes a dangling pointer as nobody is > referencing the constructed python object anymore. > > I'm not exactly sure how to solve this, do I have to hack around this by > implementing some kind of temporary reference on the object? Or is there > some way around this problem that I've missed? I asked a similar question last week and there is no real solution. See the thread with these messages: http://article.gmane.org/gmane.comp.python.c++/9528 http://article.gmane.org/gmane.comp.python.c++/9546 Achim. P.S.: Somehow, I can only find my own mails from this thread on gmane, not the answers from other people. Does anybody know the reason why? From abhi at qualcomm.com Thu Apr 6 17:25:12 2006 From: abhi at qualcomm.com (Abhi) Date: Thu, 06 Apr 2006 08:25:12 -0700 Subject: [C++-sig] Problem running pygccxml example In-Reply-To: <7465b6170604052233s758fe662pf2879a9fedf4a5a0@mail.gmail.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA88A23716@NAEX11.na.qualcomm.com> <7465b6170604052233s758fe662pf2879a9fedf4a5a0@mail.gmail.com> Message-ID: This is due to the mismatch between the versions of gcc and pygccxml? or is it python and pygccxml? Just curious thanks - Abhi --On Thursday, April 06, 2006 7:33 AM +0200 Roman Yakovenko wrote: > On 4/5/06, Haridev, Meghana wrote: >> Hi Folks, >> >> >> >> >> I am using: >> >> GCC: 4.0.2 >> >> Python: 2.4.2 >> >> Gccxml: 0.7.0 >> >> Pygccxml: 0.6.9 >> >> >> >> Any pointers to what I'm doing wrong? Am I missing anything in the >> configuration? > > You just hited the bug found and fixed in cvs. Can you use CVS version? > > Today I am going to commit final version > of pygccxml and pyplusplus for the next release. I also update the > example. > >> >> Thanks, >> >> -Meghana. >> >> >> _______________________________________________ >> C++-sig mailing list >> C++-sig at python.org >> http://mail.python.org/mailman/listinfo/c++-sig >> >> >> > > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig From roman.yakovenko at gmail.com Thu Apr 6 17:35:16 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 6 Apr 2006 17:35:16 +0200 Subject: [C++-sig] Problem running pygccxml example In-Reply-To: References: <5383A81D5EB8F14FBDB487D9B5FBEA88A23716@NAEX11.na.qualcomm.com> <7465b6170604052233s758fe662pf2879a9fedf4a5a0@mail.gmail.com> Message-ID: <7465b6170604060835n59644cecu1dc06ff2dbbdab03@mail.gmail.com> On 4/6/06, Abhi wrote: > This is due to the mismatch between the versions of gcc and pygccxml? or is > it python and pygccxml? There is an other option: gccxml and pygccxml. Brad King, the author of gccxml, did not released gccxml for a long time. He added a lot of useful features to it. Thus he forces people to use cvs version. pygccxml ( 0.6.9 ) has been checked against gccxml latest release. cvs version of pygccxml uses almost latest version of gccxml. So this bug has been fixed. > Just curious > thanks > - Abhi -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From greg.landrum at gmail.com Thu Apr 6 20:21:09 2006 From: greg.landrum at gmail.com (Greg Landrum) Date: Thu, 6 Apr 2006 11:21:09 -0700 Subject: [C++-sig] operator definitions Message-ID: <60825b0f0604061121v2f4578fs2aa8ce73d759b943@mail.gmail.com> I just ran across this syntax for defining operators on exposed classes: class_("FilePos") .def(self + int()) // __add__ .def(int() + self) // __radd__ .def(self - self) // __sub__ .def(self - int()) // __sub__ .def(self += int()) // __iadd__ (example from: http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html) I don't know when this was added (or if it's been here all along and I have just never noticed), but this is amazingly clean and useful. Thanks! -greg From mharidev at qualcomm.com Fri Apr 7 00:53:28 2006 From: mharidev at qualcomm.com (Haridev, Meghana) Date: Thu, 6 Apr 2006 15:53:28 -0700 Subject: [C++-sig] Problem running pygccxml example Message-ID: <5383A81D5EB8F14FBDB487D9B5FBEA88C0DAD0@NAEX11.na.qualcomm.com> Thanks for the tip Roman - I am now able to run the pygccxml example after installing pygccxml from CVS. Now, moving to the pyplusplus example (pyplusplus also installed from CVS today), I'm trying to run the hello_world example at /pyplusplus/examples/tutorials. I made the appropriate changes to environment.py. I got this error when I ran simple.py: ------------------------------------------------------------------------ -- Traceback (most recent call last): File "simple.py", line 32, in ? extmodule = module_creator.create( decls=decls, module_name="hello_world",recursive=False) TypeError: create() got an unexpected keyword argument 'recursive' ------------------------------------------------------------------------ -- After looking at the __init__.py for module_creator which defines create() as: def create( decls, module_name ) I removed the extra "recursive" keyword from the argument list. But then I run into this error: ------------------------------------------------------------------------ -- Traceback (most recent call last): File "simple.py", line 32, in ? extmodule = module_creator.create( decls=decls, module_name="hello_world") File "/opt/data/install-pyplusplus/lib/python2.4/site-packages/pyplusplus/mod ule_creator/__init__.py", line 11, in create maker = creator_t(decls, module_name) File "/opt/data/install-pyplusplus/lib/python2.4/site-packages/pyplusplus/mod ule_creator/creator.py", line 84, in __init__ self.__decls = self._filter_decls( self._reorder_decls( self._prepare_decls( decls ) ) ) File "/opt/data/install-pyplusplus/lib/python2.4/site-packages/pyplusplus/mod ule_creator/creator.py", line 98, in _prepare_decls decls = filter( lambda x: not x.ignore, decls ) File "/opt/data/install-pyplusplus/lib/python2.4/site-packages/pyplusplus/mod ule_creator/creator.py", line 98, in decls = filter( lambda x: not x.ignore, decls ) AttributeError: 'enumeration_t' object has no attribute 'ignore' ------------------------------------------------------------------------ -- Any suggestions? Thanks, -Meghana. -----Original Message----- From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On Behalf Of Roman Yakovenko Sent: Wednesday, April 05, 2006 10:34 PM To: Development of Python/C++ integration Subject: Re: [C++-sig] Problem running pygccxml example On 4/5/06, Haridev, Meghana wrote: > Hi Folks, > > > > > I am using: > > GCC: 4.0.2 > > Python: 2.4.2 > > Gccxml: 0.7.0 > > Pygccxml: 0.6.9 > > > > Any pointers to what I'm doing wrong? Am I missing anything in the > configuration? You just hited the bug found and fixed in cvs. Can you use CVS version? Today I am going to commit final version of pygccxml and pyplusplus for the next release. I also update the example. > > Thanks, > > -Meghana. > > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig From roman.yakovenko at gmail.com Fri Apr 7 19:15:46 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 7 Apr 2006 20:15:46 +0300 Subject: [C++-sig] Problem running pygccxml example In-Reply-To: <5383A81D5EB8F14FBDB487D9B5FBEA88C0DAD0@NAEX11.na.qualcomm.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA88C0DAD0@NAEX11.na.qualcomm.com> Message-ID: <7465b6170604071015q60021a4gc855a9e3546926c3@mail.gmail.com> On 4/7/06, Haridev, Meghana wrote: > Thanks for the tip Roman - I am now able to run the pygccxml example > after installing pygccxml from CVS. > Any suggestions? I don't know why you have an old example. I attach new version of example and relevant file. This is actually a CVS version. So may be you can check out a cvs version one more time? The code in cvs version is frozen and will be release in a week or two. We are working on documentation. The example I provide should be enough to start with pyplusplus. > Thanks, > -Meghana. > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- A non-text attachment was scrubbed... Name: generate_code.py Type: text/x-python Size: 2155 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: environment.py Type: text/x-python Size: 1115 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: hello_world.hpp Type: text/x-c++hdr Size: 1359 bytes Desc: not available URL: From ndbecker2 at gmail.com Sat Apr 8 15:36:11 2006 From: ndbecker2 at gmail.com (Neal Becker) Date: Sat, 08 Apr 2006 09:36:11 -0400 Subject: [C++-sig] operator definitions References: <60825b0f0604061121v2f4578fs2aa8ce73d759b943@mail.gmail.com> Message-ID: Greg Landrum wrote: > I just ran across this syntax for defining operators on exposed classes: > class_("FilePos") > .def(self + int()) // __add__ > .def(int() + self) // __radd__ > .def(self - self) // __sub__ > .def(self - int()) // __sub__ > .def(self += int()) // __iadd__ > > (example from: > http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html) > > I don't know when this was added (or if it's been here all along and I > have just never noticed), but this is amazingly clean and useful. > Thanks! > It looks cool, but IMHO a bit too cute. AFAIK, it doesn't do anything different than the IMHO much more clear: .def("__iadd__", plusAssignOp, return_self<>()) Of course, beauty is in the eye and all that. From dave at boost-consulting.com Sat Apr 8 16:46:06 2006 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 08 Apr 2006 10:46:06 -0400 Subject: [C++-sig] operator definitions References: <60825b0f0604061121v2f4578fs2aa8ce73d759b943@mail.gmail.com> Message-ID: Neal Becker writes: >> >> I don't know when this was added (or if it's been here all along and I >> have just never noticed), but this is amazingly clean and useful. >> Thanks! >> > > It looks cool, but IMHO a bit too cute. AFAIK, it doesn't do anything > different than the IMHO much more clear: > .def("__iadd__", plusAssignOp, return_self<>()) How is that more clear? *I* certainly can't tell what it means. -- Dave Abrahams Boost Consulting www.boost-consulting.com From greg.landrum at gmail.com Sat Apr 8 17:02:41 2006 From: greg.landrum at gmail.com (Greg Landrum) Date: Sat, 8 Apr 2006 08:02:41 -0700 Subject: [C++-sig] operator definitions In-Reply-To: References: <60825b0f0604061121v2f4578fs2aa8ce73d759b943@mail.gmail.com> Message-ID: <60825b0f0604080802i44bd476fp5ee66c1c9db9d921@mail.gmail.com> On 4/8/06, David Abrahams wrote: > Neal Becker writes: > > > It looks cool, but IMHO a bit too cute. AFAIK, it doesn't do anything > > different than the IMHO much more clear: > > .def("__iadd__", plusAssignOp, return_self<>()) > > How is that more clear? *I* certainly can't tell what it means. I'm with David on this one, and the contrast is even more extreme when you define __add__ (the "clear" syntax for that stymied me, and it was the resulting web search that turned up the new-to-me syntax). -greg From ndbecker2 at gmail.com Sat Apr 8 17:18:40 2006 From: ndbecker2 at gmail.com (Neal Becker) Date: Sat, 08 Apr 2006 11:18:40 -0400 Subject: [C++-sig] operator definitions References: <60825b0f0604061121v2f4578fs2aa8ce73d759b943@mail.gmail.com> Message-ID: David Abrahams wrote: > Neal Becker writes: > >>> >>> I don't know when this was added (or if it's been here all along and I >>> have just never noticed), but this is amazingly clean and useful. >>> Thanks! >>> >> >> It looks cool, but IMHO a bit too cute. AFAIK, it doesn't do anything >> different than the IMHO much more clear: >> .def("__iadd__", plusAssignOp, return_self<>()) > > How is that more clear? *I* certainly can't tell what it means. > It's clear (to me) because it's consistent with the same method used to define any other function. In this case "clear" assumes the observer is already familiar with the usual useage of .def. From dave at boost-consulting.com Sun Apr 9 04:37:09 2006 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 08 Apr 2006 22:37:09 -0400 Subject: [C++-sig] operator definitions References: <60825b0f0604061121v2f4578fs2aa8ce73d759b943@mail.gmail.com> Message-ID: Neal Becker writes: > David Abrahams wrote: > >> Neal Becker writes: >> >>> It looks cool, but IMHO a bit too cute. AFAIK, it doesn't do anything >>> different than the IMHO much more clear: >>> .def("__iadd__", plusAssignOp, return_self<>()) >> >> How is that more clear? *I* certainly can't tell what it means. >> > > It's clear (to me) because it's consistent with the same method used to > define any other function. In this case "clear" assumes the observer is > already familiar with the usual useage of .def. And with the meaning of plusAssignOp and return_self<>() -- Dave Abrahams Boost Consulting www.boost-consulting.com From app26 at ast.cam.ac.uk Sun Apr 9 23:24:13 2006 From: app26 at ast.cam.ac.uk (Andrew Pontzen) Date: Sun, 9 Apr 2006 22:24:13 +0100 (BST) Subject: [C++-sig] Newcomer - constructors not working as I'd expect Message-ID: Hi - apologies in advance if this is a question covered elsewhere. I did as thorough a search as I could for information before posting. I am using boost.python (from boost package 1_33_1) to expose some C++ classes. I have a problem with constructors - here is a simple test case: class test1 { public: virtual string getText() const { return "Hello test1"; } virtual ~test1() { } ; }; class test2 : public test1 { public: virtual string getText() const { return "Hello test2"; } virtual ~test2() { } ; }; class test3 : public test2 { public: test3(const test1 &basedon) : ourobj(basedon) { }; virtual string getText() const { return ourobj.getText(); } const test1 & ourobj; virtual ~test3() { } ; }; void printMessage(test1 & obj) { cout << obj.getText() << endl; } BOOST_PYTHON_MODULE(testme) { def("pm", &printMessage); class_("test1",init<>()); class_ >("test2",init<>()); class_ >("test3",init()); } In Python, things happen as expected until constructing a test3 object: >>> a=testme.test1() >>> b=testme.test2() >>> testme.pm(a) Hello test1 >>> testme.pm(b) Hello test2 >>> c=testme.test3(a) >>> testme.pm(c) Segmentation fault Using gdb, it seems the C++ constructor for test3 is never called, so the getText() call accesses an invalid reference. I don't understand why this should be - clearly I've missed something! TIA for any help. Andrew -- Andrew Pontzen app26 at ast.cam.ac.uk Institute of Astronomy Madingley Road Cambridge CB3 0HA UK From dave at boost-consulting.com Mon Apr 10 02:17:22 2006 From: dave at boost-consulting.com (David Abrahams) Date: Sun, 09 Apr 2006 20:17:22 -0400 Subject: [C++-sig] Newcomer - constructors not working as I'd expect References: Message-ID: Andrew Pontzen writes: > I am using boost.python (from boost package 1_33_1) to expose some C++ > classes. I have a problem with constructors - here is a simple test case: I can't see anything wrong with your code. It's admittedly pretty simple, but could you try to reduce it further? It seems as though if it's broken as is, you could remove one level of inheritance at least, without changing anything. -- Dave Abrahams Boost Consulting www.boost-consulting.com From rwgk at yahoo.com Mon Apr 10 06:26:43 2006 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Sun, 9 Apr 2006 21:26:43 -0700 (PDT) Subject: [C++-sig] Newcomer - constructors not working as I'd expect In-Reply-To: Message-ID: <20060410042643.94482.qmail@web31501.mail.mud.yahoo.com> General remark: use valgrind you have memory problems: http://valgrind.org/ It is really easy, except that you need a suppression file for Python 2.4 or higher: Python-2.4*/Misc/valgrind-python.supp Then: valgrind --tool=memcheck --suppressions=valgrind-python.supp python script.py --- Andrew Pontzen wrote: > test3(const test1 &basedon) : ourobj(basedon) { }; > ... > class_ >("test3",init()); > } This doesn't match. I'd try: init() Even if this helps I'd be really worried. If you call methods of test3 after the test1 instance was destroyed you'll get a crash. I'd consider using boost::shared_ptr as a member of test3. If that's not an option a call policy may be the way to achieve safety, but I am not sure since I avoid this kind ... > const test1 & ourobj; of unsafe C++, except in very low-level code with no direct connection to the Python layer. Cheers, Ralf __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From app26 at ast.cam.ac.uk Mon Apr 10 10:56:40 2006 From: app26 at ast.cam.ac.uk (Andrew Pontzen) Date: Mon, 10 Apr 2006 09:56:40 +0100 (BST) Subject: [C++-sig] Newcomer - constructors not working as I'd expect In-Reply-To: <20060410042643.94482.qmail@web31501.mail.mud.yahoo.com> References: <20060410042643.94482.qmail@web31501.mail.mud.yahoo.com> Message-ID: On Sun, 9 Apr 2006, Ralf W. Grosse-Kunstleve wrote: > Then: valgrind --tool=memcheck --suppressions=valgrind-python.supp python > script.py Thanks for the tip > This doesn't match. I'd try: init() You're quite right - this fixes it. In retrospect, it is quite obvious that what I was attempting to do would cause undefined behaviour! (Although I'm suprised it didn't give me a compile-time error?) > Even if this helps I'd be really worried. If you call methods of test3 after > the test1 instance was destroyed you'll get a crash. I'd consider using > boost::shared_ptr as a member of test3. If that's not an option a call > policy may be the way to achieve safety, but I am not sure since I avoid this > kind ... > >> const test1 & ourobj; > > of unsafe C++, except in very low-level code with no direct connection to the > Python layer. I agree it's not ideal. However, in the case of my current project, I don't really see a way of avoiding keeping hold of the reference. Of course in the real code, I'll be using a policy! (Is there any particular reason why using boost::shared_ptr would be preferable to using a policy? I'd rather avoid adding support for shared_ptr through my existing code if possible, but am unsure if it might have any advantages.) Very grateful for the help, Andrew -- Andrew Pontzen app26 at ast.cam.ac.uk Institute of Astronomy Madingley Road Cambridge CB3 0HA UK From app26 at ast.cam.ac.uk Mon Apr 10 11:01:08 2006 From: app26 at ast.cam.ac.uk (Andrew Pontzen) Date: Mon, 10 Apr 2006 10:01:08 +0100 (BST) Subject: [C++-sig] Newcomer - constructors not working as I'd expect In-Reply-To: References: Message-ID: On Sun, 9 Apr 2006, David Abrahams wrote: > I can't see anything wrong with your code. It's admittedly pretty > simple, but could you try to reduce it further? It seems as though if > it's broken as is, you could remove one level of inheritance at least, > without changing anything. You're right, removing test2 and even disinheriting test3 didn't change anything. It was just the constructor with the wrong call pattern which mucked things up (see Ralf's post.) I originally constructed the example to explore another problem which I cleared up for myself, but should have simplified it! Thanks, Andrew -- Andrew Pontzen app26 at ast.cam.ac.uk Institute of Astronomy Madingley Road Cambridge CB3 0HA UK From vishant at ama-inc.com Mon Apr 10 16:11:52 2006 From: vishant at ama-inc.com (Vishant Shahnawaz) Date: Mon, 10 Apr 2006 10:11:52 -0400 Subject: [C++-sig] Unloading module/Freeing memory Message-ID: <200604101411.KAA18078@elephant.cnchost.com> Hi all, I have a C++ application in which I am importing a custom Python module and calling a function from that module that parses a file and writes out another file. After this the module does nothing. Hence I want to free up the memory which is very large. I have tried several things but none have worked. There are other modules that are imported that do other things and hence I cannot unload all the modules that are loaded, ONLY the custom parser module should be unloaded and the memory used while invoking the function should also be freed. Here is the C++ method body. I would sincerely appreciate any help I can get here as soon as possible. Vishant { PyObject *p_moduleName = (PyString_FromString("cppwriter")); if(!p_moduleName) { throw NPVException("Cannot make string\n" + getPythonError()); } // MEMORY USED HERE - 6-7 MB PyObject *p_module = (PyImport_Import(p_moduleName)); if(!p_module) { throw NPVException("Cannot find module cppwriter\n" + getPythonError()); } PyObject *p_script = (PyString_FromString(script.c_str())); if(!p_script) { throw NPVException("Cannot make string\n" + getPythonError()); } std::string implFile = outputDirectory + string(dllName + ".cpp"); std::string headerFile = outputDirectory + string("cppwriterheader.h"); PyObject *p_implFile = PyString_FromString(implFile.c_str()); PyObject *p_headerFile = PyString_FromString(headerFile.c_str()); PyObject *p_modelName = (PyString_FromString("CPPWriter")); PyObject *p_function = PyObject_GetAttrString(p_module, "runCPPWriterFromString"); if(!p_function) { throw NPVException("Cannot find function\n" + getPythonError()); } PyObject *p_pyTuple = PyTuple_New(6); PyTuple_SetItem(p_pyTuple, 0, p_script); PyTuple_SetItem(p_pyTuple, 1, p_implFile); PyTuple_SetItem(p_pyTuple, 2, p_headerFile); PyTuple_SetItem(p_pyTuple, 3, p_modelName); PyTuple_SetItem(p_pyTuple, 4, Py_None); PyTuple_SetItem(p_pyTuple, 5, Py_None); Py_INCREF(Py_None); Py_INCREF(Py_None); // MEMORY USED HERE - 20 MB PyObject *p_pyRetVal = (PyObject_CallObject(p_function, p_pyTuple)); std::string retString; try { retString = getPythonStdOut(); } catch (...) { retString = "Debug unavailable"; } std::string error = getPythonError(); // TRYING TO GET RID OF MEMORY HERE Py_DECREF(p_moduleName); Py_DECREF(p_module); //Py_DECREF(p_script); //Py_DECREF(p_implFile); //Py_DECREF(p_headerFile); //Py_DECREF(p_modelName); Py_DECREF(p_function); Py_DECREF(p_pyTuple); Py_DECREF(p_pyRetVal); PyObject *modules = PyImport_GetModuleDict(); _PyModule_Clear(p_function); _PyModule_Clear(p_module); PyDict_SetItemString(modules, "transformer2cpp", Py_None); int delsuccess = PyDict_DelItemString(modules, "transformer2cpp"); PyErr_Clear(); // END TRYING TO GET RID OF MEMORY if(error != "") throw NPVException(error); return retString; } -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at boost-consulting.com Mon Apr 10 17:51:24 2006 From: dave at boost-consulting.com (David Abrahams) Date: Mon, 10 Apr 2006 11:51:24 -0400 Subject: [C++-sig] Newcomer - constructors not working as I'd expect References: <20060410042643.94482.qmail@web31501.mail.mud.yahoo.com> Message-ID: Andrew Pontzen writes: >> This doesn't match. I'd try: init() > > You're quite right - this fixes it. In retrospect, it is quite obvious > that what I was attempting to do would cause undefined behaviour! > (Although I'm suprised it didn't give me a compile-time error?) Why should it, and how could it? A ctor taking a const reference to a temporary is perfectly legitimate, as long as the ctor doesn't attempt to store it. -- Dave Abrahams Boost Consulting www.boost-consulting.com From dave at boost-consulting.com Mon Apr 10 17:49:18 2006 From: dave at boost-consulting.com (David Abrahams) Date: Mon, 10 Apr 2006 11:49:18 -0400 Subject: [C++-sig] Newcomer - constructors not working as I'd expect References: <20060410042643.94482.qmail@web31501.mail.mud.yahoo.com> Message-ID: "Ralf W. Grosse-Kunstleve" writes: > General remark: use valgrind you have memory problems: http://valgrind.org/ > > It is really easy, except that you need a suppression file for Python 2.4 or > higher: Python-2.4*/Misc/valgrind-python.supp > > Then: valgrind --tool=memcheck --suppressions=valgrind-python.supp python > script.py > > --- Andrew Pontzen wrote: > >> test3(const test1 &basedon) : ourobj(basedon) { }; >> ... >> class_ >("test3",init()); >> } > > This doesn't match. I'd try: init() Ah, that explains everything. -- Dave Abrahams Boost Consulting www.boost-consulting.com From abhi at qualcomm.com Mon Apr 10 21:34:44 2006 From: abhi at qualcomm.com (Abhi) Date: Mon, 10 Apr 2006 12:34:44 -0700 Subject: [C++-sig] using the same module in different files Message-ID: <6CD9E7723ED55130B7FAEE2F@[10.30.5.17]> I have 2 files - A.h which I want to wrap in a file boost_A.cpp - and B.h which I want to wrap in a file boost_B.cpp >>> File A.h class A { public: void fooA(); }; >>> File B.h class B { public: void fooB(); }; File boost_A.cpp wraps class A, while file boost_B.cpp wraps class B. I want both these files to expose methods in the same module, ie, I want to "semantically" use BOOST_PYTHON_MODULE(common) for both these files, so that all the methods exposed from boost_A.cpp and boost_B.cpp get imported by doing >> import common >> a = common.A() >> b = common.B() Is this possible? thanks - Abhi From seefeld at sympatico.ca Mon Apr 10 22:07:50 2006 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 10 Apr 2006 16:07:50 -0400 Subject: [C++-sig] using the same module in different files In-Reply-To: <6CD9E7723ED55130B7FAEE2F@[10.30.5.17]> References: <6CD9E7723ED55130B7FAEE2F@[10.30.5.17]> Message-ID: <443ABB16.90600@sympatico.ca> Abhi wrote: > I want both these files to expose methods in the same module, ie, I want to > "semantically" use BOOST_PYTHON_MODULE(common) for both these files, so > that all the methods exposed from boost_A.cpp and boost_B.cpp get imported > by doing > >>> import common >>> a = common.A() >>> b = common.B() > > > Is this possible? Wouldn't it be simpler to integrate both from within python, using # inside common.py import A, B ? Regards, Stefan From abhi at qualcomm.com Mon Apr 10 22:57:27 2006 From: abhi at qualcomm.com (Abhi) Date: Mon, 10 Apr 2006 13:57:27 -0700 Subject: [C++-sig] using the same module in different files In-Reply-To: <443ABB16.90600@sympatico.ca> References: <6CD9E7723ED55130B7FAEE2F@[10.30.5.17]> <443ABB16.90600@sympatico.ca> Message-ID: Well the problem is that we have a large number of files that need to be wrapped by different people. The methods in these files are tightly coupled, ie, you will need to use methods from multiple files to execute code in python. Hence 1) It is not possible to put them in one boost wrapper file. 2) Also it is not good to have a long import line in python every time methods from these files need to be included. thanks - Abhi --On Monday, April 10, 2006 4:07 PM -0400 Stefan Seefeld wrote: > Abhi wrote: > >> I want both these files to expose methods in the same module, ie, I want >> to "semantically" use BOOST_PYTHON_MODULE(common) for both these files, >> so that all the methods exposed from boost_A.cpp and boost_B.cpp get >> imported by doing >> >>>> import common >>>> a = common.A() >>>> b = common.B() >> >> >> Is this possible? > > Wouldn't it be simpler to integrate both from within python, using > ># inside common.py > import A, B > > > ? > > Regards, > Stefan > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig From seefeld at sympatico.ca Mon Apr 10 23:16:47 2006 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 10 Apr 2006 17:16:47 -0400 Subject: [C++-sig] using the same module in different files In-Reply-To: References: <6CD9E7723ED55130B7FAEE2F@[10.30.5.17]> <443ABB16.90600@sympatico.ca> Message-ID: <443ACB3F.3030403@sympatico.ca> Abhi wrote: > Well the problem is that we have a large number of files that need to be > wrapped by different people. The methods in these files are tightly > coupled, ie, you will need to use methods from multiple files to execute > code in python. The types and methods you wrap may be coupled, but do the wrappers have to be coupled (at compile-time) ? In fact, I'v already split large wrapper files into smaller ones to cut down on compile- and link-time. You could have a set of python extension modules all using / wrapping from the same shared library underneath. > Hence > 1) It is not possible to put them in one boost wrapper file. > 2) Also it is not good to have a long import line in python every time > methods from these files need to be included. I'm not sure I understand what you mean by 'every time'. The common.py module I suggested would only be written once. Users would then only call import common and would see exactly the same thing as with your attempted solution. See http://boost.org/libs/python/doc/tutorial/doc/html/python/techniques.html#python.creating_packages for more on packaging extension modules. HTH, Stefan From rwgk at yahoo.com Tue Apr 11 01:54:19 2006 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 10 Apr 2006 16:54:19 -0700 (PDT) Subject: [C++-sig] Newcomer - constructors not working as I'd expect In-Reply-To: Message-ID: <20060410235419.94328.qmail@web31515.mail.mud.yahoo.com> --- Andrew Pontzen wrote: > I agree it's not ideal. However, in the case of my current project, I > don't really see a way of avoiding keeping hold of the reference. > Of course in the real code, I'll be using a policy! (Is there any > particular reason why using boost::shared_ptr would be preferable to using > a policy? I find using shared_ptr easier than figuring out which one is the right call policy. > I'd rather avoid adding support for shared_ptr through my > existing code if possible, but am unsure if it might have any advantages.) With shared_ptr your C++ code is also safe, not just the Python interface. Cheers, Ralf __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From rwgk at yahoo.com Tue Apr 11 02:13:53 2006 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 10 Apr 2006 17:13:53 -0700 (PDT) Subject: [C++-sig] using the same module in different files In-Reply-To: <6CD9E7723ED55130B7FAEE2F@[10.30.5.17]> Message-ID: <20060411001353.14642.qmail@web31510.mail.mud.yahoo.com> --- Abhi wrote: > I have 2 files > - A.h which I want to wrap in a file boost_A.cpp > - and B.h which I want to wrap in a file boost_B.cpp > > >>> File A.h > > class A > { > public: > void fooA(); > }; > > >>> File B.h > > class B > { > public: > void fooB(); > }; > > > File boost_A.cpp wraps class A, while file boost_B.cpp wraps class B. > > I want both these files to expose methods in the same module, ie, I want to > "semantically" use BOOST_PYTHON_MODULE(common) for both these files, so > that all the methods exposed from boost_A.cpp and boost_B.cpp get imported > by doing > > >> import common > >> a = common.A() > >> b = common.B() > > > Is this possible? IIUC, yes. Create a file boost_common.cpp, e.g.: void wrap_A(); void wrap_B(); BOOST_PYTHON_MODULE(common) { wrap_A(); wrap_B(); } boost_A.cpp would look like this: void wrap_A() { class_("A") // ... ; } And boost_B.cpp the same with A replaced by B. Compile the three files (boost_common.cpp, boost_A.cpp, boost_B.cpp) separately, but link like this: g++ -shared -o common.so boost_common.o boost_A.o boost_B.o -lboost_python HTH, Ralf __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From ljreeder77 at hotmail.com Tue Apr 11 07:20:19 2006 From: ljreeder77 at hotmail.com (Jason Reeder) Date: Tue, 11 Apr 2006 00:20:19 -0500 Subject: [C++-sig] (no subject) Message-ID: Dunno if this is still a valid email for you. Its Jason!! Reply if this is Ben Collar. -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Tue Apr 11 07:48:06 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 11 Apr 2006 07:48:06 +0200 Subject: [C++-sig] using the same module in different files In-Reply-To: <20060411001353.14642.qmail@web31510.mail.mud.yahoo.com> References: <6CD9E7723ED55130B7FAEE2F@10.30.5.17> <20060411001353.14642.qmail@web31510.mail.mud.yahoo.com> Message-ID: <7465b6170604102248v2ad59867lfe05bc1bb577e08f@mail.gmail.com> On 4/11/06, Ralf W. Grosse-Kunstleve wrote: > --- Abhi wrote: > > > I have 2 files > > - A.h which I want to wrap in a file boost_A.cpp > > - and B.h which I want to wrap in a file boost_B.cpp > > > > >>> File A.h > > > > class A > > { > > public: > > void fooA(); > > }; > > > > >>> File B.h > > > > class B > > { > > public: > > void fooB(); > > }; > > > > > > File boost_A.cpp wraps class A, while file boost_B.cpp wraps class B. > > > > I want both these files to expose methods in the same module, ie, I want to > > "semantically" use BOOST_PYTHON_MODULE(common) for both these files, so > > that all the methods exposed from boost_A.cpp and boost_B.cpp get imported > > by doing > > > > >> import common > > >> a = common.A() > > >> b = common.B() > > > > > > Is this possible? > > IIUC, yes. Create a file boost_common.cpp, e.g.: > > void wrap_A(); > void wrap_B(); > > BOOST_PYTHON_MODULE(common) > { > wrap_A(); > wrap_B(); > } > > > boost_A.cpp would look like this: > > void > wrap_A() > { > class_("A") > // ... > ; > } > > And boost_B.cpp the same with A replaced by B. Compile the three files > (boost_common.cpp, boost_A.cpp, boost_B.cpp) separately, but link like this: An other approach it to create common package in pure Python that will hide implementation/building blocks. > HTH, > Ralf -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From guillaume.desticourt at gmail.com Tue Apr 11 18:07:34 2006 From: guillaume.desticourt at gmail.com (Guillaume Desticourt) Date: Tue, 11 Apr 2006 17:07:34 +0100 Subject: [C++-sig] wrapping an abstract class with out parameters using boost.python Message-ID: <609a651c0604110907y13d1ecd5qe2135642956a8866@mail.gmail.com> hello, i try to wrap an abstract class so i can create class that inherit from it in python but at run time i got the following error message: RuntimeError: extension class wrapper for base class pimp::gfx2d::DisplayData has not been created yet my class is something like that: class DisplayData : public IdData, public LoadData, public Subject { public: virtual void accept(DisplayDataVisitor &visitor) = 0; virtual void getZone(Zone &zone, long time) const = 0; // ... } so i wrote a class: class DisplayDataWrap : public DisplayData, public wrapper; to encapsulate the DisplayData::accept, i ve defined: void DisplayDataWrap::accept(DisplayDataVisitor &visitor) { this->get_override("accept")(visitor); } and in the module definition i do: class_("PyDisplayData", no_init) .def("accept", pure_virtual(&DisplayData::accept)) ; the problem is that i have no idea how to encapsulate: the DisplaData::getZone method... i tried Zone getZone(void) const { Zone zone; this->get_override("getZone")(zone); return zone; } Zone (DisplayDataWrap::*getZone1)(void) const = &DisplayDataWrap::getZone; class_("PyDisplayData", no_init) .def("getZone", getZone1) ; and then i get the error messge RuntimeError: extension class wrapper for base class pimp::gfx2d::DisplayData has not been created yet and i am not even sure i ll be able to override the getZone in python... any help would be appreciated, thanks. -- Guillaume Desticourt -------------- next part -------------- An HTML attachment was scrubbed... URL: From ndbecker2 at gmail.com Wed Apr 12 15:59:13 2006 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 12 Apr 2006 09:59:13 -0400 Subject: [C++-sig] IMPORTANT 2.5 API changes for C Extension Modules and Embedders Message-ID: Hopefully you've seen this notice. I'm wondering if anyone has evaluated it's possible effect on boost::python? From sbarranx at modulonet.fr Wed Apr 12 15:59:47 2006 From: sbarranx at modulonet.fr (=?iso-8859-1?b?U3TpcGhhbmU=?= Barranx) Date: Wed, 12 Apr 2006 15:59:47 +0200 Subject: [C++-sig] Big error in GCCXML compilation :: segmentation fault termination Message-ID: <1144850387.443d07d3c3aab@webmail.modulonet.fr> Hi, I'm trying to wrap a C++ project for motion correction. Like in tutorials, I made the environment and py++ run scripts. But when I try to run the script, I got tons of error but obviously only two major problems. Here is the beginning of the error log: Traceback (most recent call last): File "D:\MotionCorrection\Foundation\wrap\pypp_Foundation.py", line 112, in ? export() File "D:\MotionCorrection\Foundation\wrap\pypp_Foundation.py", line 107, in export exporter.create() File "D:\MotionCorrection\Foundation\wrap\pypp_Foundation.py", line 98, in create decls = self.read_declarations() File "D:\MotionCorrection\Foundation\wrap\pypp_Foundation.py", line 65, in read_declarations return reader.read_files( self.__header_files ) File "C:\Program Files\Python24\Lib\site-packages\pygccxml\parser\project_reader.py", line 61, in read_files return self.__parse_file_by_file(files) File "C:\Program Files\Python24\Lib\site-packages\pygccxml\parser\project_reader.py", line 82, in __parse_file_by_file decls = reader.read_file( header ) File "C:\Program Files\Python24\Lib\site-packages\pygccxml\parser\source_reader.py", line 140, in read_file raise error gccxml_runtime_error_t: Error occured while running GCC-XML: In file included from D:/MotionCorrection/Foundation/VectorField.h:24, from D:/MotionCorrection/Foundation/Image.h:23: D:/MotionCorrection/Foundation/Matrix.h: In constructor ` Xform2DParams::Xform2DParams(int)': D:/MotionCorrection/Foundation/Matrix.h:40: error: `fill_n' undeclared in namespace `std' D:/MotionCorrection/Foundation/Matrix.h: In copy constructor ` Xform2DParams::Xform2DParams(const Xform2DParams&)': D:/MotionCorrection/Foundation/Matrix.h:46: error: `copy' undeclared in namespace `std' //** ?? 'fill_n' and 'copy' are actually members of the standard library **// In file included from D:/MotionCorrection/Foundation/VectorField.h:28, from D:/MotionCorrection/Foundation/Image.h:23: C:/Program Files/Microsoft Visual Studio .NET 2003/Vc7/Include/valarray: At global scope: C:/Program Files/Microsoft Visual Studio .NET 2003/Vc7/Include/valarray:30: error: ` size_t' was not declared in this scope C:/Program Files/Microsoft Visual Studio .NET 2003/Vc7/Include/valarray:30: error: template argument 1 is invalid //** I stop here but there is hundreds of errors next but only in 'valarray' **// So, can anybody help me? Here is my run script: import os, time from environment import settings from pygccxml import parser from pygccxml import declarations from pyplusplus import code_creators from pyplusplus import module_creator from pyplusplus import file_writers def full_path(path, file): return os.path.join( path, file ) def identify_call_policies( decl ): build_in_resolver = module_creator.build_in_resolver_t() policy = build_in_resolver( decl ) if policy: return policy if isinstance( decl, declarations.member_operator_t ) \ and decl.symbol == '()' \ and decl.parent.name == 'Foundation': return code_creators.return_internal_reference() return None class Foundation_exporter_t: def __init__(self): path = settings.Foundation_path self.__header_files = [] self.__header_files.append(full_path(path, 'Foundation.h')) self.__header_files.append(full_path(path, 'Image.h')) self.__header_files.append(full_path(path, 'LSPoly2Approx.h')) self.__header_files.append(full_path(path, 'Mask.h')) self.__header_files.append(full_path(path, 'Matrix.h')) self.__header_files.append(full_path(path, 'Polynomial1.h')) self.__header_files.append(full_path(path, 'Polynomial2.h')) self.__header_files.append(full_path(path, 'RecPar.h')) self.__header_files.append(full_path(path, 'SOUnwrapper.h')) self.__header_files.append(full_path(path, 'TUnwrapper.h')) self.__header_files.append(full_path(path, 'Vector2.h')) self.__header_files.append(full_path(path, 'VectorField.h')) self.__header_files.append(full_path(path, 'WLSPoly2Approx.h')) def read_declarations(self): config = parser.config_t( gccxml_path=settings.gccxml_path , working_directory=settings.MotionCorrection_path , include_paths=settings.vtk_paths) reader = parser.project_reader_t( config ) return reader.read_files( self.__header_files ) def filter_declarations(self, decls ): return declarations.filtering.by_location( decls, [settings.MotionCorrection_path] ) def create_extmodule(self, decls): creator = module_creator.creator_t( decls=decls , module_name=settings.Foundation.module_name , create_castinig_constructor=True , call_policies_resolver_=identify_call_policies) return creator.create() def customize_extmodule( self, extmodule ): global license extmodule.license = license extmodule.std_directories.append( settings.boost_path ) extmodule.std_directories.extend( settings.vtk_paths ) #butifying include code generation extmodule.user_defined_directories.append( settings.MotionCorrection_path ) extmodule.user_defined_directories.append( settings.Foundation_path ) extmodule.user_defined_directories.append( settings.working_dir ) extmodule.user_defined_directories.extend( config.include_paths ) extmodule.precompiled_header = 'boost/python.hpp' #Adding headers as first includes. It comes because of internal structure of Foundation extmodule.adopt_creator( code_creators.include_t( header=self.__header_files ), 2 ) #body_index = extmodule.creators.index( extmodule.body ) def write_files( self, extmodule ): file_writers.write_file(extmodule , os.path.join(settings.Foundation.generated_files_dir , settings.Foundation.module_name + '.cpp') ) def create(self): decls = self.read_declarations() self.filter_declarations(decls) extmodule = self.create_extmodule( decls ) self.customize_extmodule( extmodule ) self.write_files( extmodule ) def export(): start = time.clock() exporter = Foundation_exporter_t() exporter.create() finish = time.clock() - start print '%0.2f seconds' % finish if __name__ == '__main__': export() print 'done' //** end of the script **// Where is my mistake?? Did I forgot an inclusion? Why the GCCXML compiler doesn't recognize 'fill_n' and 'copy'? What's wrong with 'valarray' file? Thanks for helping From seefeld at sympatico.ca Wed Apr 12 16:08:43 2006 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 12 Apr 2006 10:08:43 -0400 Subject: [C++-sig] IMPORTANT 2.5 API changes for C Extension Modules and Embedders In-Reply-To: References: Message-ID: <443D09EB.7060703@sympatico.ca> Neal Becker wrote: > Hopefully you've seen this notice. Which notice ? Do you have a reference ? Thanks, Stefan From rwgk at yahoo.com Wed Apr 12 17:59:45 2006 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Wed, 12 Apr 2006 08:59:45 -0700 (PDT) Subject: [C++-sig] IMPORTANT 2.5 API changes for C Extension Modules and Embedders In-Reply-To: Message-ID: <20060412155945.68663.qmail@web31506.mail.mud.yahoo.com> --- Neal Becker wrote: > Hopefully you've seen this notice. I'm wondering if anyone has evaluated > it's possible effect on boost::python? I've successfully tested Python 2.5a1 with Boost.Python after locally making a few small changes related to http://www.python.org/dev/peps/pep-0353/ . I also used Fredrik Lundh's scanner (see PEP) to pin-point places in Boost.Python that need attention, but I don't have the time right now to go through systematically (I think it just needs a few int -> Py_ssize_t changes). Any help would be highly appreciated. Attached are my minimal changes. Cheers, Ralf __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -------------- next part -------------- A non-text attachment was scrubbed... Name: py25a1_bpl_minimal_patch Type: application/octet-stream Size: 1658 bytes Desc: 2207447082-py25a1_bpl_minimal_patch URL: From roman.yakovenko at gmail.com Wed Apr 12 20:48:13 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 12 Apr 2006 21:48:13 +0300 Subject: [C++-sig] Big error in GCCXML compilation :: segmentation fault termination In-Reply-To: <1144850387.443d07d3c3aab@webmail.modulonet.fr> References: <1144850387.443d07d3c3aab@webmail.modulonet.fr> Message-ID: <7465b6170604121148t5fc1ae03he188eedd7c1709a3@mail.gmail.com> On 4/12/06, St?phane Barranx wrote: > Hi, I'm trying to wrap a C++ project for motion correction. Like > in tutorials, I made the environment and py++ run scripts. But > when I try to run the script, I got tons of error but obviously > only two major problems. Hi. First of all you should treat GCC-XML as just an other compiler. So, basically before you can use pyplusplus/pygccxml GCC-XML should be able to compile your code. There are 2 ways you can fix the situation. The simple one is next. When GCC-XML compiles a code it defines new preprocessed definition __GCCXML__. So you can use it in order to fix your code. Now about your script. Nice work and you did it almost without documentation! But there is small problem with it. You use old version of pyplusplus. Few days ago I put at SourceForge next version of pyplusplus and pygccxml. It simplifies a lot the usage of pyplusplus. If you need help with porting your script to new version, I will be glad to help you. Anyway, there is a mailing list for pyplusplus and pygccxml: http://lists.sourceforge.net/lists/listinfo/pygccxml-development. Anonymous CVS access is not synchronized( SourceForge problems ), so I attach tutorials here. If you still need help, consider to use pyplusplus/pygccxml mailing list Thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- A non-text attachment was scrubbed... Name: hello_world.hpp Type: text/x-c++hdr Size: 1357 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: generate_code.py Type: text/x-python Size: 2325 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: module_builder.rest Type: application/octet-stream Size: 5579 bytes Desc: not available URL: From tkirke at gmail.com Thu Apr 13 22:16:18 2006 From: tkirke at gmail.com (Tony Kirke) Date: Thu, 13 Apr 2006 13:16:18 -0700 Subject: [C++-sig] Wrapping pointers In-Reply-To: <200604052105.33008.dominic.sacre@gmx.de> References: <200604052105.33008.dominic.sacre@gmx.de> Message-ID: <4dcfdcab0604131316q89ceaccgc2c3a17f6da2af08@mail.gmail.com> Did you get an answer to this? I am trying to figure out the same thing except my pointer is to an object of a different class instead of the same class On 4/5/06, Dominic Sacr? wrote: > > Hi, > > I'm having trouble exporting C++ pointers to Python. My code looks like > this: > > class A > { > A *p; > ... > }; > > BOOST_PYTHON_MODULE(test) > { > class_("A", init<>()) > .def_readonly("p", &A::p) > ... > ; > } > > Now every time I try to access A::p from Python I get this error: > > TypeError: No to_python (by-value) converter found for C++ type: A* > > I searched the list archives and found a message about the same issue, > where the only suggestion was to use > > .def_readonly("p", make_getter(&A::p, return_internal_reference<1>())) > > instead, but this doesn't help either. On the contrary, this causes a > segfault every time I use A::p (although p points to a valid object, at > least in C++). > So what should I do to access this pointer from Python? > > Thanks, > Dominic > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dominic.sacre at gmx.de Fri Apr 14 02:18:53 2006 From: dominic.sacre at gmx.de (Dominic =?iso-8859-1?q?Sacr=E9?=) Date: Fri, 14 Apr 2006 02:18:53 +0200 Subject: [C++-sig] Wrapping pointers In-Reply-To: <4dcfdcab0604131316q89ceaccgc2c3a17f6da2af08@mail.gmail.com> References: <200604052105.33008.dominic.sacre@gmx.de> <4dcfdcab0604131316q89ceaccgc2c3a17f6da2af08@mail.gmail.com> Message-ID: <200604140218.53896.dominic.sacre@gmx.de> On Thursday, 13. April 2006 22:16, Tony Kirke wrote: > Did you get an answer to this? > I am trying to figure out the same thing except my pointer is to an > object of a different class instead of the same class Nope, no reply. So I'm still looking for hints how to do this. > On 4/5/06, Dominic Sacr? wrote: > > Hi, > > > > I'm having trouble exporting C++ pointers to Python. My code looks > > like this: > > > > class A > > { > > A *p; > > ... > > }; > > > > BOOST_PYTHON_MODULE(test) > > { > > class_("A", init<>()) > > .def_readonly("p", &A::p) > > ... > > ; > > } > > > > Now every time I try to access A::p from Python I get this error: > > > > TypeError: No to_python (by-value) converter found for C++ type: A* From rwgk at yahoo.com Fri Apr 14 03:01:38 2006 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Thu, 13 Apr 2006 18:01:38 -0700 (PDT) Subject: [C++-sig] Wrapping pointers In-Reply-To: <200604140218.53896.dominic.sacre@gmx.de> Message-ID: <20060414010138.83299.qmail@web31506.mail.mud.yahoo.com> --- Dominic Sacr? wrote: > On Thursday, 13. April 2006 22:16, Tony Kirke wrote: > > Did you get an answer to this? > > I am trying to figure out the same thing except my pointer is to an > > object of a different class instead of the same class > > Nope, no reply. So I'm still looking for hints how to do this. CAVEAT: Raw pointers are intrinsically difficult to work with, terribly unsafe, and therefore not easily compatible with the safe Python layer. I avoid them completely (except in core code not exposed via public interfaces), i.e. I have no working experience related to your question. FWIW: You may want to try .add_property("p", make_getter(&A::p, xxx)); where xxx is a return value policy you have to find out about. > > On 4/5/06, Dominic Sacr? wrote: > > > Hi, > > > > > > I'm having trouble exporting C++ pointers to Python. My code looks > > > like this: > > > > > > class A > > > { > > > A *p; > > > ... > > > }; > > > > > > BOOST_PYTHON_MODULE(test) > > > { > > > class_("A", init<>()) > > > .def_readonly("p", &A::p) > > > ... > > > ; > > > } > > > > > > Now every time I try to access A::p from Python I get this error: > > > > > > TypeError: No to_python (by-value) converter found for C++ type: A* __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From faheem at email.unc.edu Sat Apr 15 04:26:53 2006 From: faheem at email.unc.edu (Faheem Mitha) Date: Fri, 14 Apr 2006 22:26:53 -0400 (EDT) Subject: [C++-sig] wrapping function objects Message-ID: Hi, Is there any way I can expose function objects to python, in such a way that I can call the associated operator() as a method in python? The only thing I could find that looked relavant was http://www.boost.org/libs/python/todo.html#wrapping-function-objects but I didn't understand how to use the method described in the linked message. Thanks in advance. Please cc me on any reply. Thanks. Faheem. From ludwig at fh-worms.de Sat Apr 15 13:03:59 2006 From: ludwig at fh-worms.de (Christoph Ludwig) Date: Sat, 15 Apr 2006 13:03:59 +0200 Subject: [C++-sig] GCC version compatibility In-Reply-To: References: <42CEF948.3010908@v.loewis.de> <20050709102010.GA3836@lap200.cdc.informatik.tu-darmstadt.de> <42D0D215.9000708@v.loewis.de> <20050710125458.GA3587@lap200.cdc.informatik.tu-darmstadt.de> <42D15DB2.3020300@v.loewis.de> <20050716101357.GC3607@lap200.cdc.informatik.tu-darmstadt.de> <20051012120917.GA11058@lap200.cdc.informatik.tu-darmstadt.de> <20051101161534.GC8657@lap200.cdc.informatik.tu-darmstadt.de> Message-ID: <20060415110358.GA25764@castellio.textgrid.it.fh-worms.de> Hi, On Tue, Nov 01, 2005 at 11:23:20AM -0500, David Abrahams wrote: > Christoph Ludwig writes: > > > On Tue, Nov 01, 2005 at 10:23:14AM -0500, David Abrahams wrote: > >> Christoph Ludwig writes: > >> > I finally had the spare time to look into this problem again and submitted > >> > patch #1324762. The proposed patch implements the following: > >> > >> > >> > >> Christoph, thanks so much for doing this! > > > > You are welcome. It was pretty straight forward once I realized how the C++ > > compiler is communicated from configure to distutils. But the patch has not > > been applied to Python's CVS yet; I don't know how the Python developers think > > about it. > > Hmm, maybe I should post encouraging someone to look at this. FYI, the patch got in. Regards Christoph -- FH Worms - University of Applied Sciences Fachbereich Informatik / Telekommunikation Erenburgerstr. 19, 67549 Worms, Germany -------------- next part -------------- An embedded message was scrubbed... From: "SourceForge.net" Subject: [ python-Patches-1324762 ] Compiling and linking main() with C++ compiler Date: Fri, 14 Apr 2006 07:37:09 -0700 Size: 9470 URL: From dave at boost-consulting.com Sat Apr 15 15:12:24 2006 From: dave at boost-consulting.com (David Abrahams) Date: Sat, 15 Apr 2006 09:12:24 -0400 Subject: [C++-sig] GCC version compatibility In-Reply-To: <20060415110358.GA25764@castellio.textgrid.it.fh-worms.de> (Christoph Ludwig's message of "Sat, 15 Apr 2006 13:03:59 +0200") References: <42CEF948.3010908@v.loewis.de> <20050709102010.GA3836@lap200.cdc.informatik.tu-darmstadt.de> <42D0D215.9000708@v.loewis.de> <20050710125458.GA3587@lap200.cdc.informatik.tu-darmstadt.de> <42D15DB2.3020300@v.loewis.de> <20050716101357.GC3607@lap200.cdc.informatik.tu-darmstadt.de> <20051012120917.GA11058@lap200.cdc.informatik.tu-darmstadt.de> <20051101161534.GC8657@lap200.cdc.informatik.tu-darmstadt.de> <20060415110358.GA25764@castellio.textgrid.it.fh-worms.de> Message-ID: Christoph Ludwig writes: > Hi, > > On Tue, Nov 01, 2005 at 11:23:20AM -0500, David Abrahams wrote: >> Christoph Ludwig writes: >> >> > On Tue, Nov 01, 2005 at 10:23:14AM -0500, David Abrahams wrote: >> >> Christoph Ludwig writes: >> >> > I finally had the spare time to look into this problem again and submitted >> >> > patch #1324762. The proposed patch implements the following: >> >> >> >> >> >> >> >> Christoph, thanks so much for doing this! >> > >> > You are welcome. It was pretty straight forward once I realized how the C++ >> > compiler is communicated from configure to distutils. But the patch has not >> > been applied to Python's CVS yet; I don't know how the Python developers think >> > about it. >> >> Hmm, maybe I should post encouraging someone to look at this. > > FYI, the patch got in. Great! IIRC, this had some practical consequences W.R.T. Boost.Python and/or debug builds of Python, but I can't quite remember what the upshot was. Can you? -- Dave Abrahams Boost Consulting www.boost-consulting.com From ludwig at fh-worms.de Sat Apr 15 18:48:09 2006 From: ludwig at fh-worms.de (Christoph Ludwig) Date: Sat, 15 Apr 2006 18:48:09 +0200 Subject: [C++-sig] GCC version compatibility In-Reply-To: References: <42D0D215.9000708@v.loewis.de> <20050710125458.GA3587@lap200.cdc.informatik.tu-darmstadt.de> <42D15DB2.3020300@v.loewis.de> <20050716101357.GC3607@lap200.cdc.informatik.tu-darmstadt.de> <20051012120917.GA11058@lap200.cdc.informatik.tu-darmstadt.de> <20051101161534.GC8657@lap200.cdc.informatik.tu-darmstadt.de> <20060415110358.GA25764@castellio.textgrid.it.fh-worms.de> Message-ID: <20060415164808.GA26536@castellio.textgrid.it.fh-worms.de> On Sat, Apr 15, 2006 at 09:12:24AM -0400, David Abrahams wrote: > Christoph Ludwig writes: > > On Tue, Nov 01, 2005 at 11:23:20AM -0500, David Abrahams wrote: > >> Christoph Ludwig writes: > >> > >> > On Tue, Nov 01, 2005 at 10:23:14AM -0500, David Abrahams wrote: > >> >> Christoph Ludwig writes: > >> >> > I finally had the spare time to look into this problem again and submitted > >> >> > patch #1324762. The proposed patch implements the following: > >> >> > >> >> > >> >> > >> >> Christoph, thanks so much for doing this! > >> > > >> > You are welcome. It was pretty straight forward once I realized how the C++ > >> > compiler is communicated from configure to distutils. But the patch has not > >> > been applied to Python's CVS yet; I don't know how the Python developers think > >> > about it. > >> > >> Hmm, maybe I should post encouraging someone to look at this. > > > > FYI, the patch got in. > > Great! > > IIRC, this had some practical consequences W.R.T. Boost.Python and/or > debug builds of Python, but I can't quite remember what the upshot > was. Can you? I do not see how the patch could affect Boost.Python (in the sense that you have to change anything in Boost.Python's code or its Jamfile). I only modified the configuration and build scripts of Python in such a way that Python's main() is compiled and linked with a C++ compiler only if the user explicitly asks for it. Therefore, the dependency of the Python executable on the C++ runtime library is removed. On many platforms (notably, Linux/ELF) you can load C++ extension modules anyway, but you are no longer forced to build your extension module with a particular C++ compiler version. (On platforms that support C++ extension modules only if main() was compiled and linked with a C++ compiler you need to build Python with the --with-cxx-main configure option. Of course, then you are no longer free to choose which compiler to build your C++ extensions with. I.e., then you have exactly the situation that used to be forced on all users.) The same holds for a Python debug build - I did not mess with the compiler option settings at all. If you want your Python executable built with debug info, you do it as before. Regards Christoph -- FH Worms - University of Applied Sciences Fachbereich Informatik / Telekommunikation Erenburgerstr. 19, 67549 Worms, Germany From stummer at gmail.com Sat Apr 15 20:13:36 2006 From: stummer at gmail.com (david stummer) Date: Sat, 15 Apr 2006 19:13:36 +0100 Subject: [C++-sig] boost python references Message-ID: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> hi guys, i am trying to use references with boost python. in python you can do: class person: name='david' a=person() b=a print a.name a.name='john' print b.name because b is a reference, it will always change as a changes. hence b.nameis also john. i want this functionality between boost and python. I have the C++ class: class_("Window") .def_readwrite("seat", &Window::seat) ; where seat is an object of class Seat. Seat contains an integer attribute named position. It's default value is 45. My python code is s=Seat() w=Window() print s.position # prints 45 w.seat.position=s.position s.position=66 print w.seat.position # prints 45 print s.position # prints 66 this shows that it isn't passed by reference as print w.seat.position needs to be 66 How can i used references? thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From stummer at gmail.com Sun Apr 16 12:44:46 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 11:44:46 +0100 Subject: [C++-sig] boost python references In-Reply-To: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> Message-ID: <4d0bf7320604160344y573655f1m496ebcdf925827d9@mail.gmail.com> s -------------- next part -------------- An HTML attachment was scrubbed... URL: From stummer at gmail.com Sun Apr 16 12:51:29 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 11:51:29 +0100 Subject: [C++-sig] boost python references In-Reply-To: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> Message-ID: <4d0bf7320604160351t5a12745evb33cae48c376397f@mail.gmail.com> Let me simplify my question. In python you can do something like class Seat: position="a" class Window: seat=None s=Seat() s2=Seat() w=Window() s2 = s w.seat=s print s.position print s2.position print w.seat.position s.position="b" print s.position print s2.position print w.seat.position and the output would be : a a a b b b indicating that s2 and w.seat reference s. This is what i want. However, when i try this with c++ classes it doesn't work, i.e. class Seat { public: std::string position; }; Seat::Seat() { position="david"; } class Window { public: Seat seat; }; class_("Seat") .def_readwrite("position", &Seat::position) ; class_("Window") .def_readwrite("seat", &Window::seat) ; when using similar python code above with these classes instead of the python-defined ones, i get the following output: a a a b b a The Seat object in the Window object isn't reference: it is a new object. Why is this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Sun Apr 16 12:53:36 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 16 Apr 2006 13:53:36 +0300 Subject: [C++-sig] boost python references In-Reply-To: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> Message-ID: <7465b6170604160353mf87237bm9389fae547ad19b9@mail.gmail.com> On 4/15/06, david stummer wrote: > hi guys, > > i am trying to use references with boost python. > > in python you can do: > > class person: > name='david' > > a=person() > b=a > print a.name > a.name='john' > print b.name > > because b is a reference, it will always change as a changes. hence b.name > is also john. > > i want this functionality between boost and python. > > I have the C++ class: > > class_("Window") > .def_readwrite("seat", &Window::seat) > ; > > where seat is an object of class Seat. Seat contains an integer attribute > named position. It's default value is 45. > > How can i used references? There is one important difference between Python code and C++. In Python you create value that belongs to class, while in C++ you create value that belongs to class instance. I think you should use add_static_property function. Also, I think you will need to use boost::python::make_function in order to construct getter/setter with desired call policies. http://www.boost.org/libs/python/doc/v2/class.html http://www.boost.org/libs/python/doc/v2/make_function.html#make_function-spec > thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From stummer at gmail.com Sun Apr 16 13:07:55 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 12:07:55 +0100 Subject: [C++-sig] boost python references In-Reply-To: <7465b6170604160353mf87237bm9389fae547ad19b9@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> <7465b6170604160353mf87237bm9389fae547ad19b9@mail.gmail.com> Message-ID: <4d0bf7320604160407oa2361dfm86438c20cd17c3da@mail.gmail.com> ok, i added a member function to Window which takes a Seat reference: void Window::AddSeat(Seat &s) { seat=s; } class_("Window") .def("FindWindow", make_function(&Window::AddSeat)) .def_readwrite("seat", &Window::seat) ; using make_function like you said, although this doesn't seem to have any effect. I thank you for your patience - i am a newbie -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Sun Apr 16 13:12:52 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 16 Apr 2006 14:12:52 +0300 Subject: [C++-sig] boost python references In-Reply-To: <4d0bf7320604160407oa2361dfm86438c20cd17c3da@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> <7465b6170604160353mf87237bm9389fae547ad19b9@mail.gmail.com> <4d0bf7320604160407oa2361dfm86438c20cd17c3da@mail.gmail.com> Message-ID: <7465b6170604160412q500d183dm82a20ed75f86de82@mail.gmail.com> On 4/16/06, david stummer wrote: > ok, i added a member function to Window which takes a Seat reference: > > void Window::AddSeat(Seat &s) > { > seat=s; > } > > class_("Window") > .def("FindWindow", make_function(&Window::AddSeat)) > > .def_readwrite("seat", &Window::seat) > ; > > using make_function like you said, although this doesn't seem to have any > effect. > > I thank you for your patience - i am a newbie :-). You missed important note: There is one important difference between Python code and C++. In Python you create value that belongs to class, while in C++ you create value that belongs to class instance. I think you should use add_static_property function. make_function you should use in order to setup call policies -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From stummer at gmail.com Sun Apr 16 13:30:43 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 12:30:43 +0100 Subject: [C++-sig] boost python references In-Reply-To: <7465b6170604160412q500d183dm82a20ed75f86de82@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> <7465b6170604160353mf87237bm9389fae547ad19b9@mail.gmail.com> <4d0bf7320604160407oa2361dfm86438c20cd17c3da@mail.gmail.com> <7465b6170604160412q500d183dm82a20ed75f86de82@mail.gmail.com> Message-ID: <4d0bf7320604160430k4153ca9buc0b92055c1fb9b30@mail.gmail.com> the trouble is i don't want it to be a static member.. -------------- next part -------------- An HTML attachment was scrubbed... URL: From stummer at gmail.com Sun Apr 16 13:36:26 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 12:36:26 +0100 Subject: [C++-sig] boost python references In-Reply-To: <4d0bf7320604160430k4153ca9buc0b92055c1fb9b30@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> <7465b6170604160353mf87237bm9389fae547ad19b9@mail.gmail.com> <4d0bf7320604160407oa2361dfm86438c20cd17c3da@mail.gmail.com> <7465b6170604160412q500d183dm82a20ed75f86de82@mail.gmail.com> <4d0bf7320604160430k4153ca9buc0b92055c1fb9b30@mail.gmail.com> Message-ID: <4d0bf7320604160436r7c92b6f0m7726e8ea4af4e7a3@mail.gmail.com> i was wondering if you could provide some simple code for me to follow - i'd really appreciate this as i am lost :( -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Sun Apr 16 13:38:11 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 16 Apr 2006 14:38:11 +0300 Subject: [C++-sig] boost python references In-Reply-To: <4d0bf7320604160430k4153ca9buc0b92055c1fb9b30@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> <7465b6170604160353mf87237bm9389fae547ad19b9@mail.gmail.com> <4d0bf7320604160407oa2361dfm86438c20cd17c3da@mail.gmail.com> <7465b6170604160412q500d183dm82a20ed75f86de82@mail.gmail.com> <4d0bf7320604160430k4153ca9buc0b92055c1fb9b30@mail.gmail.com> Message-ID: <7465b6170604160438y5ec1e203m7b6b4dcfca3b2fac@mail.gmail.com> On 4/16/06, david stummer wrote: > the trouble is i don't want it to be a static member.. Somewhere should be defined an unique instance of the class, you want to share across all instances. How you implement this, it is up to you. It is also possible that I don't understand you. So may be other people can help you? > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From stummer at gmail.com Sun Apr 16 13:46:21 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 12:46:21 +0100 Subject: [C++-sig] boost python references In-Reply-To: <7465b6170604160438y5ec1e203m7b6b4dcfca3b2fac@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> <7465b6170604160353mf87237bm9389fae547ad19b9@mail.gmail.com> <4d0bf7320604160407oa2361dfm86438c20cd17c3da@mail.gmail.com> <7465b6170604160412q500d183dm82a20ed75f86de82@mail.gmail.com> <4d0bf7320604160430k4153ca9buc0b92055c1fb9b30@mail.gmail.com> <7465b6170604160438y5ec1e203m7b6b4dcfca3b2fac@mail.gmail.com> Message-ID: <4d0bf7320604160446y5d803a2ftfa0c58a58dec049d@mail.gmail.com> >Somewhere should be defined an unique instance of the class, you want to share >across all instances. How you implement this, it is up to you. i don't know how to implement this - which is why i posted here in the first place.. as you can see i have tried references, but the object for some reason is being coied. -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Sun Apr 16 18:31:12 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 16 Apr 2006 19:31:12 +0300 Subject: [C++-sig] boost python references In-Reply-To: <4d0bf7320604160446y5d803a2ftfa0c58a58dec049d@mail.gmail.com> References: <4d0bf7320604151113w3e967cd5p9e3873f94ad4e18@mail.gmail.com> <7465b6170604160353mf87237bm9389fae547ad19b9@mail.gmail.com> <4d0bf7320604160407oa2361dfm86438c20cd17c3da@mail.gmail.com> <7465b6170604160412q500d183dm82a20ed75f86de82@mail.gmail.com> <4d0bf7320604160430k4153ca9buc0b92055c1fb9b30@mail.gmail.com> <7465b6170604160438y5ec1e203m7b6b4dcfca3b2fac@mail.gmail.com> <4d0bf7320604160446y5d803a2ftfa0c58a58dec049d@mail.gmail.com> Message-ID: <7465b6170604160931s262c5e8clebde71057eb74ac@mail.gmail.com> On 4/16/06, david stummer wrote: > >Somewhere should be defined an unique instance of the class, you want to > share > >across all instances. How you implement this, it is up to you. Somewhere should be some kind of static: 1. It could be module attribute, then all your get/set functions should work on it 2. Regular C++ static defined somewhere Try to implement some solution in C++, after that I will try to help you with boost.python -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From stummer at gmail.com Sun Apr 16 19:17:55 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 18:17:55 +0100 Subject: [C++-sig] call python from c++ (not in dll) Message-ID: <4d0bf7320604161017v5a273912me9a04468e3d21e4@mail.gmail.com> i have managed to put together dlls for python to call, but is it possible to call a python module from an exe file? if so where is this info as i cannot see it on boost.org? cheers -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Sun Apr 16 19:28:46 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 16 Apr 2006 20:28:46 +0300 Subject: [C++-sig] call python from c++ (not in dll) In-Reply-To: <4d0bf7320604161017v5a273912me9a04468e3d21e4@mail.gmail.com> References: <4d0bf7320604161017v5a273912me9a04468e3d21e4@mail.gmail.com> Message-ID: <7465b6170604161028j19a8f648j6e5a98d80b4c4e6@mail.gmail.com> On 4/16/06, david stummer wrote: > i have managed to put together dlls for python to call, but is it possible > to call a python module from an exe file? Why not, may be I am missing something, but it should work out of the box. Just link your exe with relevant libs and enjoy. > if so where is this info as i cannot see it on boost.org? > > cheers -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From stummer at gmail.com Sun Apr 16 19:55:24 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 18:55:24 +0100 Subject: [C++-sig] call python from c++ (not in dll) In-Reply-To: <7465b6170604161028j19a8f648j6e5a98d80b4c4e6@mail.gmail.com> References: <4d0bf7320604161017v5a273912me9a04468e3d21e4@mail.gmail.com> <7465b6170604161028j19a8f648j6e5a98d80b4c4e6@mail.gmail.com> Message-ID: <4d0bf7320604161055h33db71b5o509667272b3d7bc6@mail.gmail.com> i don't mean to be rude but your answer doesn't help me in the slightest i was loking for maybe a tutorial which i haven't found yet boost has absolutely nothing on calling python from an exe which i can see what is the point in using boost if relative newcommers like me cannot get anywhere -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Sun Apr 16 20:12:14 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 16 Apr 2006 21:12:14 +0300 Subject: [C++-sig] call python from c++ (not in dll) In-Reply-To: <4d0bf7320604161055h33db71b5o509667272b3d7bc6@mail.gmail.com> References: <4d0bf7320604161017v5a273912me9a04468e3d21e4@mail.gmail.com> <7465b6170604161028j19a8f648j6e5a98d80b4c4e6@mail.gmail.com> <4d0bf7320604161055h33db71b5o509667272b3d7bc6@mail.gmail.com> Message-ID: <7465b6170604161112mfa68ea2x4442d737bec94ff3@mail.gmail.com> On 4/16/06, david stummer wrote: > i don't mean to be rude but your answer doesn't help me in the slightest Sorry, I was lazy: http://boost.org/libs/python/doc/tutorial/doc/html/python/embedding.html > i was loking for maybe a tutorial which i haven't found yet Try google: boost python embedding exe tutorial. 3'rd link will point you to the url I provided > boost has absolutely nothing on calling python from an exe which i can see > > what is the point in using boost if relative newcommers like me cannot get > anywhere boost is relatively big project with some culture. It takes time to learn it and understand it's design. Almost all libraries have documentation, sometimes it even good. If you not satisfy with something you can: 1. ask( you get the answers, right? ) 2. search for answers( you are not the first newcomer! ) 3. try to write some document and post it on boost development mailing list. In that document you can specify what is wrong, on your opinion, and how it should be. 4. you can make it better( hint: contribute, contribute, contribute ) 5. you can complain and do nothing Enter your choice: ... :-) -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From stummer at gmail.com Sun Apr 16 20:44:14 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 19:44:14 +0100 Subject: [C++-sig] call python from c++ (not in dll) In-Reply-To: <7465b6170604161112mfa68ea2x4442d737bec94ff3@mail.gmail.com> References: <4d0bf7320604161017v5a273912me9a04468e3d21e4@mail.gmail.com> <7465b6170604161028j19a8f648j6e5a98d80b4c4e6@mail.gmail.com> <4d0bf7320604161055h33db71b5o509667272b3d7bc6@mail.gmail.com> <7465b6170604161112mfa68ea2x4442d737bec94ff3@mail.gmail.com> Message-ID: <4d0bf7320604161144l77c7ba93qe2fb0eee1d6c241d@mail.gmail.com> ok thanks i will take a look cheers -------------- next part -------------- An HTML attachment was scrubbed... URL: From stummer at gmail.com Sun Apr 16 20:48:01 2006 From: stummer at gmail.com (david stummer) Date: Sun, 16 Apr 2006 19:48:01 +0100 Subject: [C++-sig] c++ funciton slowing Message-ID: <4d0bf7320604161148ve0f87afq9528941431b17d39@mail.gmail.com> one of my c++ class functions which performs fine 'natively' slows right down (i.e. takes nearly 1 minute) when i call it from python. void Window::LoadImages() { int ids[3] = {IDB_BITMAP1,IDB_BITMAP2,IDB_BITMAP3}; for(int i=0;i<3;i++) { HANDLE hBm = ::LoadImage(::GetModuleHandle(NULL), MAKEINTRESOURCE(ids[i]), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR); BITMAP bm; ::GetObject(hBm, sizeof(BITMAP), &bm); int size = bm.bmHeight * bm.bmWidth * bm.bmBitsPixel / 8; pixels[i] = new BYTE[size]; ::GetBitmapBits((HBITMAP)hBm, size, pixels[i]); } } What it does is load resources from within visual studio. i have three individual resources. it stalls on this line: pixels[i] = new BYTE[size]; where pixels is declared as BYTE* pixels[4] any ideas? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at boost-consulting.com Mon Apr 17 00:09:23 2006 From: dave at boost-consulting.com (David Abrahams) Date: Sun, 16 Apr 2006 18:09:23 -0400 Subject: [C++-sig] wrapping an abstract class with out parameters using boost.python References: <609a651c0604110907y13d1ecd5qe2135642956a8866@mail.gmail.com> Message-ID: "Guillaume Desticourt" writes: > class DisplayDataWrap : > public DisplayData, > public wrapper; > > to encapsulate the DisplayData::accept, i ve defined: > > void DisplayDataWrap::accept(DisplayDataVisitor &visitor) > { > this->get_override("accept")(visitor); > } > > and in the module definition i do: > class_("PyDisplayData", no_init) > .def("accept", pure_virtual(&DisplayData::accept)) > ; > > the problem is that i have no idea how to encapsulate: > the DisplaData::getZone method... > i tried > Zone getZone(void) const > { > Zone zone; > this->get_override("getZone")(zone); > return zone; > } > > Zone (DisplayDataWrap::*getZone1)(void) const = &DisplayDataWrap::getZone; > > class_("PyDisplayData", no_init) > .def("getZone", getZone1) ; > > and then i get the error messge > RuntimeError: extension class wrapper for base class > pimp::gfx2d::DisplayData has not been created yet > and i am not even sure i ll be able to override the getZone in python... Please post a minimal, reproducible testcase that shows your problem. This should ideally be one C++ file (the extension module) and one Python file that I can invoke to exercise it and demonstrate the problem. -- Dave Abrahams Boost Consulting www.boost-consulting.com From nyamatongwe at gmail.com Mon Apr 17 00:54:03 2006 From: nyamatongwe at gmail.com (Neil Hodgson) Date: Mon, 17 Apr 2006 08:54:03 +1000 Subject: [C++-sig] c++ funciton slowing In-Reply-To: <4d0bf7320604161148ve0f87afq9528941431b17d39@mail.gmail.com> References: <4d0bf7320604161148ve0f87afq9528941431b17d39@mail.gmail.com> Message-ID: <50862ebd0604161554s71e902dby7efd275ee7bceba6@mail.gmail.com> david stummer: > one of my c++ class functions which performs fine 'natively' slows right > down (i.e. takes nearly 1 minute) when i call it from python. > ... > any ideas? Check the return codes of all the functions. You are most likely not loading resources from the file you think you are. Neil From roman.yakovenko at gmail.com Mon Apr 17 09:40:49 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 17 Apr 2006 10:40:49 +0300 Subject: [C++-sig] ANN: pyplusplus - v0.7.2 Message-ID: <7465b6170604170040j24b2d1dbue0cc07933f6243d5@mail.gmail.com> Hi. The next version of pyplusplus is available. Download page: http://www.language-binding.net/pyplusplus/download.html What is it? pyplusplus is an object-oriented framework for creating a code generator for boost.python library. Project home page: http://www.language-binding.net/pyplusplus/pyplusplus.html What is new? New user friendly API has been created. You can find tutorials here: http://www.language-binding.net/pyplusplus/tutorials/tutorials.html Logging functionality has been added to almost all operations Documentation has been improved a lot. Bug fixes Full list of changes you can find here: http://www.language-binding.net/pyplusplus/history/history.html Many thanks to Matthias Baas and Allen Bierbaum. They fixed bugs, wrote documentation, designed new user friendly API. Enjoy -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From stummer at gmail.com Mon Apr 17 11:06:01 2006 From: stummer at gmail.com (david stummer) Date: Mon, 17 Apr 2006 10:06:01 +0100 Subject: [C++-sig] c++ funciton slowing In-Reply-To: <50862ebd0604161554s71e902dby7efd275ee7bceba6@mail.gmail.com> References: <4d0bf7320604161148ve0f87afq9528941431b17d39@mail.gmail.com> <50862ebd0604161554s71e902dby7efd275ee7bceba6@mail.gmail.com> Message-ID: <4d0bf7320604170206n17b4ec8fxe2bf89e027a8d208@mail.gmail.com> ok my fault :( i forgot to switch to 'multi threaded dll' cheers -------------- next part -------------- An HTML attachment was scrubbed... URL: From stummer at gmail.com Mon Apr 17 13:38:24 2006 From: stummer at gmail.com (david stummer) Date: Mon, 17 Apr 2006 12:38:24 +0100 Subject: [C++-sig] sharing objects Message-ID: <4d0bf7320604170438j267af19g3b2057850f16e2db@mail.gmail.com> ok guys i was wondering if anyone had some examples of sharing class objects between c++ and python. i have made a few posts recently about sharing and referencing but nobody seems to have an answer so far. i understand it's quite difficult because of the differences the two languages have in referencing objects, storing them etc, cheers -------------- next part -------------- An HTML attachment was scrubbed... URL: From stummer at gmail.com Mon Apr 17 16:25:24 2006 From: stummer at gmail.com (david stummer) Date: Mon, 17 Apr 2006 15:25:24 +0100 Subject: [C++-sig] vector in class Message-ID: <4d0bf7320604170725w748fce67id6b2ebea7ed08e70@mail.gmail.com> i managed to get a vector working on it's own using typedef std::vector IntVec; class_("bankrolls") .def(vector_indexing_suite()) ; but now i would like to make a vector a member of a class. what steps would i take to make the vector a member of a c++ class and have it callable from python? -------------- next part -------------- An HTML attachment was scrubbed... URL: From mharidev at qualcomm.com Mon Apr 17 22:18:03 2006 From: mharidev at qualcomm.com (Haridev, Meghana) Date: Mon, 17 Apr 2006 13:18:03 -0700 Subject: [C++-sig] Generation of wrapper for arguments of type T*& Message-ID: <5383A81D5EB8F14FBDB487D9B5FBEA88C0E8C0@NAEX11.na.qualcomm.com> Hi Folks, I want to wrap the following piece of code using pyplusplus: ================================================= // Simulating an out using foo (T*& ptr) Class_Z { public: Class_X foo (Class_Y*& ptr) { Class_Y *newptr = new Class_Y(); ... ... ptr = newptr; return Class_X (); } } ================================================= Manually, I am wrapping it like this in Boost: ================================================= boost::python::tuple foo(Class_Z& self ) { Class_Y *ptr; Class_X objx = self.foo (ptr); return boost::python::make_tuple(objx, ptr); } class_("Class_Z") { .def( "foo", &::foo) } ================================================= I'm a newbie to boost/python/pyplusplus! Any suggestions much appreciated! Thanks, -Meghana. -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Mon Apr 17 22:35:07 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 17 Apr 2006 23:35:07 +0300 Subject: [C++-sig] Generation of wrapper for arguments of type T*& In-Reply-To: <5383A81D5EB8F14FBDB487D9B5FBEA88C0E8C0@NAEX11.na.qualcomm.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA88C0E8C0@NAEX11.na.qualcomm.com> Message-ID: <7465b6170604171335x5f7a581exf58a0d9ff6c5c721@mail.gmail.com> On 4/17/06, Haridev, Meghana wrote: > Hi Folks, > I want to wrap the following piece of code using pyplusplus: > I'm a newbie to boost/python/pyplusplus! Any suggestions much appreciated! I plan to add this feature to the next release of pyplusplus. Right now you have few ways to go: 1. If you have only few such functions, then you can wrap them manually. after this you can add .def( "for", &foo ) code to the class, using add_code method mb = module_builder_t( ... ) Class_Z = mb.class_( name="Class_Z" ) foo = Class_Z.member_function( name="foo", arg_types=[None] ) > Thanks, > > -Meghana. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Mon Apr 17 22:45:53 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 17 Apr 2006 23:45:53 +0300 Subject: [C++-sig] Generation of wrapper for arguments of type T*& In-Reply-To: <5383A81D5EB8F14FBDB487D9B5FBEA88C0E8C0@NAEX11.na.qualcomm.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA88C0E8C0@NAEX11.na.qualcomm.com> Message-ID: <7465b6170604171345p392d9ffav18c443705c8004e0@mail.gmail.com> On 4/17/06, Haridev, Meghana wrote: > Hi Folks, > I want to wrap the following piece of code using pyplusplus: > // Simulating an out using foo (T*& ptr) > Class_Z > { > public: > Class_X foo (Class_Y*& ptr) > { > > Class_Y *newptr = new Class_Y(); > ? > ptr = newptr; > return Class_X (); > } > } > Manually, I am wrapping it like this in Boost: > boost::python::tuple foo(Class_Z& self ) > > { > > Class_Y *ptr; > > Class_X objx = self.foo (ptr); > > return boost::python::make_tuple(objx, ptr); > > } > > > > class_("Class_Z") > > { > > .def( "foo", &::foo) > > } > > I'm a newbie to boost/python/pyplusplus! Any suggestions much appreciated! I plan to add this feature to the next release of pyplusplus. Right now you have few ways to go: 1. If you have only few such functions, then you can wrap them manually. after this you can add .def( "for", &foo ) code to the class, using add_code method #I assume that you already has module_builder_t class instance mb = module_builder_t( ... ) #Get declaration of Class_Z class Class_Z = mb.class_( name="Class_Z" ) #get declaration of function "foo", If "foo" function is overloaded, then #you can specify additional criteria: argument types foo = Class_Z.member_function( name="foo" ) foo.exclude() Class_Z.add_code( 'def( "foo", &foo )' ) This is a simple way to do this. Lets start with this one, if it will not help you, we will proceed to an other one. > Thanks, > > -Meghana. > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From mharidev at qualcomm.com Tue Apr 18 05:02:46 2006 From: mharidev at qualcomm.com (Haridev, Meghana) Date: Mon, 17 Apr 2006 20:02:46 -0700 Subject: [C++-sig] Generation of wrapper for arguments of type T*& Message-ID: <5383A81D5EB8F14FBDB487D9B5FBEA88C0E9A1@NAEX11.na.qualcomm.com> Thanks Roman. > I plan to add this feature to the next release of pyplusplus. > Right now you have few ways to go: > 1. If you have only few such functions, then you can wrap them manually. Can I make pyplusplus just insert the "manual wrappers" code in the generated file? Is there a simple example which illustrates a way in which we can just write/insert pieces of code to the generated file? Or do I have to create a different file where I put the code for the "manual wrappers" and then make pylusplus include that file in the generated file? > after this you can add > .def( "for", &foo ) > > code to the class, using add_code method > #I assume that you already has module_builder_t class instance > mb = module_builder_t( ... ) > #Get declaration of Class_Z class > Class_Z = mb.class_( name="Class_Z" ) > #get declaration of function "foo", If "foo" function is overloaded, then > > #you can specify additional criteria: argument types > foo = Class_Z.member_function( name="foo" ) > foo.exclude() > Class_Z.add_code( 'def( "foo", &foo )' ) This works for me....but how can I make pyplusplus insert this manual code to the generated code? :-) boost::python::tuple foo(Class_Z& self ) { Class_Y *ptr; Class_X objx = self.foo (ptr); return boost::python::make_tuple(objx, ptr); } > This is a simple way to do this. > Lets start with this one, if it will not help you, we will proceed to > other one. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rodlist at hotmail.com Tue Apr 18 05:29:55 2006 From: rodlist at hotmail.com (Rod Cloutier) Date: Mon, 17 Apr 2006 23:29:55 -0400 Subject: [C++-sig] Exposing Abstract Classes and factories Message-ID: Hi, I am trying to use pyplusplus to expose python binding to a library. The library currently uses a lot of abstract classes and generate concrete object through factories. I am currently unable to use, in python, any of the object created through the factories. Is it possible to expose abstract classes in python and call concrete object, not exposed in python, through the interface ? Here is a concise example of the problem: Many thanks for your help Rod ==================== Hello.h ============================= #include class Abstract { public: virtual void run() const = 0; }; std::auto_ptr create(); ==================== Hello.cpp ============================= #include #include "hello.h" class Concrete : public Abstract { virtual void run( ) const { std::cout << "Concrete ran!"; } }; std::auto_ptr create() { return std::auto_ptr( new Concrete() ); } ==================== PyPlusPlus Code ============================= from pyplusplus import module_builder from pyplusplus import code_creators mb = module_builder.module_builder_t( [r"hello.h"] , gccxml_path=r"C:/GCC_XML/bin" , working_directory=r"." , include_paths=['.'] , define_symbols=[] ) mb.build_code_creator( module_name='pyplusplusTest' ) mb.write_module( './bindings.cpp' ) ==================== Python Test Code ============================= import pyplusplusTest instance = pyplusplusTest.create() instance.run() --------------------- Error obtained ----------------------- Traceback (most recent call last): File "D:\dev\cplusplus\pyplusplusTest\Debug\test.py", line 5, in ? instance.run() Boost.Python.ArgumentError: Python argument types in Abstract.run(Abstract) did not match C++ signature: run(struct Abstract_wrapper {lvalue}) run(struct Abstract_wrapper {lvalue}) -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Tue Apr 18 07:11:38 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 18 Apr 2006 08:11:38 +0300 Subject: [C++-sig] Generation of wrapper for arguments of type T*& In-Reply-To: <5383A81D5EB8F14FBDB487D9B5FBEA88C0E9A1@NAEX11.na.qualcomm.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA88C0E9A1@NAEX11.na.qualcomm.com> Message-ID: <7465b6170604172211y6684daf4g737b7061b4de57d0@mail.gmail.com> On 4/18/06, Haridev, Meghana wrote: > Thanks Roman. > > Can I make pyplusplus just insert the "manual wrappers" code in the > generated file? Is there a simple example which illustrates a way in which > we can just write/insert pieces of code to the generated file? It is possible, but it still does not have good user interface. Anyway, please take a look on pyplusplus/file_writers package. It contains single_file.py and multiple_files.py modules. You can derive from them and implement custom algorithm for dumping code to files. > Or do I have to create a different file where I put the code for the "manual > wrappers" and then make pylusplus include that file in the generated file? You can do that. mb = module_builder_t(...) mb.build_code_creator( module_name="hw" ) my_include = code_creators.include_t( header=header_file ) mb.code_creator.adopt_include( my_include ) The price you pay is compilation time. This file will be included in every generated cpp file. > > > after this you can add > > > .def( "for", &foo ) > > code to the class, using add_code method > > #I assume that you already has module_builder_t class instance > > mb = module_builder_t( ... ) > > #Get declaration of Class_Z class > > Class_Z = mb.class_( name="Class_Z" ) > > #get declaration of function "foo", If "foo" function is overloaded, then > > #you can specify additional criteria: argument types > > foo = Class_Z.member_function( name="foo" ) > > foo.exclude() > > Class_Z.add_code( 'def( "foo", &foo )' ) > This works for me....but how can I make pyplusplus insert this manual code > to the generated code? :-) Yesterday, I did not have access to source code, so the answer was incomplete. Now I do have. Class_Z.add_wrapper_code( "your code here" ). That's all. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Tue Apr 18 07:18:30 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 18 Apr 2006 08:18:30 +0300 Subject: [C++-sig] Exposing Abstract Classes and factories In-Reply-To: References: Message-ID: <7465b6170604172218q28006b28h1d452b9029acba41@mail.gmail.com> On 4/18/06, Rod Cloutier wrote: > > Hi, > > I am trying to use pyplusplus to expose python binding to a library. The > library currently uses a lot of abstract classes and generate concrete > object through factories. I am currently unable to use, in python, any of > the object created through the factories. Is it possible to expose abstract > classes in python and call concrete object, not exposed in python, through > the interface ? > > Here is a concise example of the problem: > > Many thanks for your help I am sorry, but my machine crashed yesterday, so I can not give you the answer right now. I hope I will be able to check this issue this evening or tomorrow morning. > Rod -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From fatfatson at gmail.com Tue Apr 18 10:46:36 2006 From: fatfatson at gmail.com (=?GB2312?B?0rG+p7XL?=) Date: Tue, 18 Apr 2006 16:46:36 +0800 Subject: [C++-sig] how can i import a module without executing any code? Message-ID: <6d415ba30604180146q2601d128t2cdb70ccf07d4424@mail.gmail.com> when i use PyImport_Import() to import a module writen in Python, the global statement in which will by executed automatically, while i don't want so,i'd like the python-programmer to write their initial code in a "init()" function, and i will call it in C at the proper time instead, how can i make it so? thanks very much,especially for endureing my bad english:) -------------- next part -------------- An HTML attachment was scrubbed... URL: From fatfatson at gmail.com Tue Apr 18 10:49:14 2006 From: fatfatson at gmail.com (=?GB2312?B?0rG+p7XL?=) Date: Tue, 18 Apr 2006 16:49:14 +0800 Subject: [C++-sig] how can i import a module without executing any code? In-Reply-To: <6d415ba30604180146q2601d128t2cdb70ccf07d4424@mail.gmail.com> References: <6d415ba30604180146q2601d128t2cdb70ccf07d4424@mail.gmail.com> Message-ID: <6d415ba30604180149q6cda621bs16791cb0aa9ef185@mail.gmail.com> for additon: the exact meaning i say is : i expect a approch to ignore the global statements in the .py module. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hans_meine at gmx.net Tue Apr 18 18:22:41 2006 From: hans_meine at gmx.net (Hans Meine) Date: Tue, 18 Apr 2006 18:22:41 +0200 Subject: [C++-sig] how can i import a module without executing any code? In-Reply-To: <6d415ba30604180149q6cda621bs16791cb0aa9ef185@mail.gmail.com> References: <6d415ba30604180146q2601d128t2cdb70ccf07d4424@mail.gmail.com> <6d415ba30604180149q6cda621bs16791cb0aa9ef185@mail.gmail.com> Message-ID: <200604181822.41567.hans_meine@gmx.net> Hi! On Tuesday 18 April 2006 10:49, ??? wrote: > for additon: the exact meaning i say is : i expect a approch to ignore the > global statements in the .py module. Then, what should be the result of the import? def foo() pass is also a "global statement", as is the definition of classes. AFAICS, there is no such thing as what you're looking for. You might try postponing your import though. Ciao, / / /--/ / / ANS From roman.yakovenko at gmail.com Tue Apr 18 22:53:38 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 18 Apr 2006 23:53:38 +0300 Subject: [C++-sig] Exposing Abstract Classes and factories In-Reply-To: References: Message-ID: <7465b6170604181353j7782aa77k3c848220d15d2ee1@mail.gmail.com> On 4/18/06, Rod Cloutier wrote: > Hi, > > ==================== Hello.h ============================= > > #include > > class Abstract > { > public: > virtual void run() const = 0; > }; > > std::auto_ptr create(); > > > ==================== Hello.cpp ============================= > > #include > #include "hello.h" > > class Concrete : public Abstract > { > virtual void run( ) const > { > std::cout << "Concrete ran!"; > } > }; > > std::auto_ptr create() > { > return std::auto_ptr( new Concrete() ); > } > > You found bug in pyplusplus. Thanks. Description: I renamed your classes to "abstract" and "concrete", and put them into "factory" namespace namespace bp = boost::python; struct abstract_wrapper : factory::abstract, bp::wrapper< factory::abstract > { abstract_wrapper() : factory::abstract() , bp::wrapper< factory::abstract >() {} virtual int run( ) const { bp::override run = this->get_override( "run" ); return run( ); } }; BOOST_PYTHON_MODULE(factory){ if( true ){ typedef bp::class_< abstract_wrapper, boost::noncopyable > abstract_exposer_t; abstract_exposer_t abstract_exposer = abstract_exposer_t( "abstract" ); bp::scope abstract_scope( abstract_exposer ); abstract_exposer.def( "run" //, bp::pure_virtual( &::abstract_wrapper::run ) //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ , bp::pure_virtual( &::factory::abstract::run ) , bp::default_call_policies() ); bp::register_ptr_to_python< std::auto_ptr< factory::abstract > >(); } bp::def( "create" , &factory::create , bp::default_call_policies() ); } When generating "def" for pure virtual function "run", as argument I provided address to "run" function defined in wrapper class. This is just wrong. I need to provide reference to function defined in base class. Fix. I don't have access to CVS right now, so I attach file to the mail. Please put it in pyplusplus/code_creators directory. Also, I added this use case to unittests. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- A non-text attachment was scrubbed... Name: calldef.py Type: text/x-python Size: 40092 bytes Desc: not available URL: From dominic.sacre at gmx.de Wed Apr 19 04:20:30 2006 From: dominic.sacre at gmx.de (Dominic =?iso-8859-1?q?Sacr=E9?=) Date: Wed, 19 Apr 2006 04:20:30 +0200 Subject: [C++-sig] Wrapping pointers In-Reply-To: <20060414010138.83299.qmail@web31506.mail.mud.yahoo.com> References: <20060414010138.83299.qmail@web31506.mail.mud.yahoo.com> Message-ID: <200604190420.30241.dominic.sacre@gmx.de> On Friday, 14. April 2006 03:01, Ralf W. Grosse-Kunstleve wrote: > CAVEAT: Raw pointers are intrinsically difficult to work with, terribly > unsafe, and therefore not easily compatible with the safe Python layer. > I avoid them completely (except in core code not exposed via public > interfaces), i.e. I have no working experience related to your > question. Ok... Then what would you do? Does it make any difference whether I'm using pointers or references? I'm just wondering because obviously there's no such distinction in Python... > FWIW: You may want to try > > .add_property("p", make_getter(&A::p, xxx)); This doesn't help, I'm getting the same error as before. All I want is to be able to do something like this in Python: x = test.A() x.p.foo() The closest I could get was class A { ... A* get_p() { return p; } }; .def("get_p", &A::get_p, return_value_policy()) but I'd rather be able to access p as a data member rather than a method. Any ideas? Thanks, Dominic > > > On 4/5/06, Dominic Sacr? wrote: > > > > Hi, > > > > > > > > I'm having trouble exporting C++ pointers to Python. My code > > > > looks like this: > > > > > > > > class A > > > > { > > > > A *p; > > > > ... > > > > }; > > > > > > > > BOOST_PYTHON_MODULE(test) > > > > { > > > > class_("A", init<>()) > > > > .def_readonly("p", &A::p) > > > > ... > > > > ; > > > > } > > > > > > > > Now every time I try to access A::p from Python I get this error: > > > > > > > > TypeError: No to_python (by-value) converter found for C++ type: > > > > A* From guillaume.desticourt at gmail.com Wed Apr 19 10:56:33 2006 From: guillaume.desticourt at gmail.com (Guillaume Desticourt) Date: Wed, 19 Apr 2006 09:56:33 +0100 Subject: [C++-sig] wrapping an abstract class with out parameters using boost.python In-Reply-To: References: <609a651c0604110907y13d1ecd5qe2135642956a8866@mail.gmail.com> Message-ID: <609a651c0604190156x34745734k9f1d004f31e82619@mail.gmail.com> On 4/16/06, David Abrahams wrote: > > "Guillaume Desticourt" writes: [...] > Please post a minimal, reproducible testcase that shows your problem. > This should ideally be one C++ file (the extension module) and one > Python file that I can invoke to exercise it and demonstrate the problem. hello, i didnt manage to reproduce the problem in a short source sample, but i ve found that i ve forgotten a base<...> in one of my class_<...>. i am not sure of that because i ve tried and modified a lot of things, but this may have been the cause of the problem. regards, -- Guillaume Desticourt -------------- next part -------------- An HTML attachment was scrubbed... URL: From rodlist at hotmail.com Wed Apr 19 14:59:31 2006 From: rodlist at hotmail.com (Rod Cloutier) Date: Wed, 19 Apr 2006 12:59:31 +0000 Subject: [C++-sig] Exposing Abstract Classes and factories In-Reply-To: <7465b6170604181353j7782aa77k3c848220d15d2ee1@mail.gmail.com> Message-ID: Thank you Roman, it's working perfectly now! Rodrigue Cloutier From rodlist at hotmail.com Wed Apr 19 15:04:56 2006 From: rodlist at hotmail.com (Rod Cloutier) Date: Wed, 19 Apr 2006 13:04:56 +0000 Subject: [C++-sig] void pointers as arguments and return values ( using pyplusplus ) Message-ID: Hi, What is the status of void pointer handling in pyplusplus ? I have been trying to follow the multiple threads but I seem to get lost in all the different patches. So far, if I understand correctly, the cvs trunk has the patch. I should be able to get the latest version, compile boost.python and everything should work fine without any changes to pyplusplus. Am I right ? By the way, I did all those steps and it didn't work so I am trying to remove all known facts before looking for my problem... Thanks Rodrigue Cloutier From guillaume.desticourt at gmail.com Wed Apr 19 16:51:13 2006 From: guillaume.desticourt at gmail.com (Guillaume Desticourt) Date: Wed, 19 Apr 2006 15:51:13 +0100 Subject: [C++-sig] wrapping member functions with incomplete types parameters Message-ID: <609a651c0604190751u2fd3ccb6r1f0255268133d2be@mail.gmail.com> hello, i ve got a problem trying to wrap a constructor whose argument is a reference to an incomplete type: i have found this link that highlights the issue, but sadly doesn t come with a solution :) http://mail.python.org/pipermail/c++-sig/2003-January/003352.html i ve written some code which illustrates the problem: ultimatetrader% cat test.cc #include #include using namespace boost::python; typedef struct unknown problem_t; class Pouet { public: Pouet(problem_t& pb) : pb_(pb) { } private: problem_t& pb_; }; BOOST_PYTHON_MODULE(pyunknown) { class_("PyPouet", init()) ; } and the error message: test.cc:26: instantiated from here /usr/include/boost/python/type_id.hpp:79: error: invalid use of undefined type 'unknown' test.cc:8: error: forward declaration of 'unknown' thanks you for your help, -- Guillaume Desticourt -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Wed Apr 19 18:49:07 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 19 Apr 2006 19:49:07 +0300 Subject: [C++-sig] void pointers as arguments and return values ( using pyplusplus ) In-Reply-To: References: Message-ID: <7465b6170604190949r437f987an7c17388b009dbb1a@mail.gmail.com> On 4/19/06, Rod Cloutier wrote: > Hi, > > What is the status of void pointer handling in pyplusplus ? I have been > trying to follow the multiple threads but I seem to get lost in all the > different patches. As for me they just work. I run a tester ( call_policies_tester.py ) that checks this issue. > So far, if I understand correctly, the cvs trunk has the patch. I should be > able to get the latest version, compile boost.python and everything should > work fine without any changes to pyplusplus. Am I right ? Right > By the way, I did all those steps and it didn't work so I am trying to > remove all known facts before looking for my problem... Good luck. Also my advise is: take a look on boost.python tests and pyplusplus unit tests. ( Search for void* ptr and try to run relevant tests ) Also if you find some issues with pyplusplus, please report them to pygccxml-development list: http://lists.sourceforge.net/lists/listinfo/pygccxml-development > Thanks > > Rodrigue Cloutier Thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From dave at boost-consulting.com Thu Apr 20 01:25:26 2006 From: dave at boost-consulting.com (David Abrahams) Date: Wed, 19 Apr 2006 16:25:26 -0700 Subject: [C++-sig] none In-Reply-To: <223C228C-DC27-41BE-8E92-E439736F29CB@earthbrowser.com> (Matthew Giger's message of "Wed, 19 Apr 2006 12:12:36 -0700") References: <223C228C-DC27-41BE-8E92-E439736F29CB@earthbrowser.com> Message-ID: Hi Matthew, It's better to post these things to the C++-sig: http://www.boost.org/more/mailing_lists.htm#cplussig Matthew Giger writes: > Just thought I'd pass along a fix for a crashing bug to the > builtin_converters.cpp code: > > Change: > > std::wstring result(::PyObject_Length(intermediate), L' '); > int err = PyUnicode_AsWideChar( > (PyUnicodeObject *)intermediate > , result.size() ? &result[0] : 0 > , result.size()); > > if (err == -1) > throw_error_already_set(); > > > To: > > std::wstring result; > int len = ::PyObject_Length(intermediate); > if(len > 0) > { > result.resize(len, L' '); > int err = PyUnicode_AsWideChar( > (PyUnicodeObject *)intermediate > , result.size() ? &result[0] : 0 > , result.size()); > > if (err == -1) > throw_error_already_set(); > } > Can you explain why this is necessary and what causes the crash you're reporting? Also, can you submit a minimal failing testcase? Normally I don't make fixes withouut an example I can check in as a regression test so that the fix never gets undone. > Thanks for writing a great library. You're welcome. -- Dave Abrahams Boost Consulting www.boost-consulting.com From rwgk at yahoo.com Wed Apr 19 23:05:22 2006 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Wed, 19 Apr 2006 14:05:22 -0700 (PDT) Subject: [C++-sig] Wrapping pointers In-Reply-To: <200604190420.30241.dominic.sacre@gmx.de> Message-ID: <20060419210522.92608.qmail@web31512.mail.mud.yahoo.com> --- Dominic Sacr??? wrote: > .def("get_p", &A::get_p, return_value_policy()) > > but I'd rather be able to access p as a data member rather than a method. > Any ideas? I'd try: .add_property("p", make_function(&A::get_p, return_value_policy())) See also: http://www.boost.org/libs/python/doc/v2/make_function.html __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From dave at boost-consulting.com Thu Apr 20 03:18:16 2006 From: dave at boost-consulting.com (David Abrahams) Date: Wed, 19 Apr 2006 18:18:16 -0700 Subject: [C++-sig] none In-Reply-To: (Matthew Giger's message of "Wed, 19 Apr 2006 14:28:25 -0700") References: <223C228C-DC27-41BE-8E92-E439736F29CB@earthbrowser.com> Message-ID: Matthew Giger writes: > Sorry I didn't go through normal channels. Okay, but I would appreciate it if you'd do that now. > I don't have an easy way > to provide a regression test since I currently build the boost > libraries into my Mac Xcode project and not through the standard > build interface. How you build it isn't really important. The standard regression test is a C++ file containing an extension module and a Python file containing a driver for the test case. > The Python code that crashed it was like: > > uni_str = u'some string' > uni_len = len(uni_str) > uni_str = uni_str[uni_len:] So far I see no obvious crash, nor even any PYthon code that would cause Boost.Python to get involved. > The reasoning is fairly straightforward. If PyObject_Length() returns > 0, no memory is allocated for the wstring, thus passing in a pointer > to the first element if the wstring (never recommended, but we all do > it). I don't see any such pointer passing. result.size() ? &result[0] : 0 ensures that no such pointer is passed. > Logically, the result.size() being 0 should bypass the dereference Yes, and it does, unless your compiler is broken. And even if your compiler *is* broken, I don't know of an implementation where &result[0] would ever cause a crash. > and PyUnicode_AsWideChar should recognize that it > shouldn't do anything with a 0 length conversion. I've looked in the > latest python source and PyUnicode_AsWideChar looks ok, but on the > Mac we are using Python 2.3... And? Python 2.3 is broken, and dereferences the pointer even if the provided length is zero? That seems unlikely to me; have you confirmed it? > Once I made this fix, it no longer crashed. That doesn't mean much. Your code could be broken in other ways. That's why a minimal test case is so important. > It couldn't hurt to explicitly check the length for 0 rather than > relying on wstring and Python to be smart about it. I do explicitly check. I'm not sure what smartness you're referring to. > Again, sorry this isn't through normal channels. I will do that if > and when I have another bug I find. Thanks. -- Dave Abrahams Boost Consulting www.boost-consulting.com From dominic.sacre at gmx.de Thu Apr 20 00:44:41 2006 From: dominic.sacre at gmx.de (Dominic =?iso-8859-1?q?Sacr=E9?=) Date: Thu, 20 Apr 2006 00:44:41 +0200 Subject: [C++-sig] Wrapping pointers In-Reply-To: <20060419210522.92608.qmail@web31512.mail.mud.yahoo.com> References: <20060419210522.92608.qmail@web31512.mail.mud.yahoo.com> Message-ID: <200604200044.41722.dominic.sacre@gmx.de> On Wednesday, 19. April 2006 23:05, Ralf W. Grosse-Kunstleve wrote: > .add_property("p", make_function(&A::get_p, > return_value_policy())) Thanks, that did the trick! Dominic From fatfatson at gmail.com Thu Apr 20 08:25:14 2006 From: fatfatson at gmail.com (=?GB2312?B?0rG+p7XL?=) Date: Thu, 20 Apr 2006 14:25:14 +0800 Subject: [C++-sig] how to set a global object for python environment? Message-ID: <6d415ba30604192325h2fbf20d1s1bcf7b1c95372f93@mail.gmail.com> i have created some global object in C++, now i hope them could be visited from python, as in ASP,some one implemented the "Request","Response",then we use it in vbs. what i want exactly is that: expose a created object to python,but not a class definition, and when the object is modified in python, we can feel this in C++. is there a simple approach? -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Thu Apr 20 10:19:31 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 20 Apr 2006 11:19:31 +0300 Subject: [C++-sig] how to set a global object for python environment? In-Reply-To: <6d415ba30604192325h2fbf20d1s1bcf7b1c95372f93@mail.gmail.com> References: <6d415ba30604192325h2fbf20d1s1bcf7b1c95372f93@mail.gmail.com> Message-ID: <7465b6170604200119x5bbd94ebmab06daf7b3a8e7f2@mail.gmail.com> On 4/20/06, ??? wrote: > > i have created some global object in C++, > now i hope them could be visited from python, > as in ASP,some one implemented the "Request","Response",then we use it in > vbs. > > what i want exactly is that: expose a created object to python,but not a > class definition, and when the object is modified in python, we can feel > this in C++. > > is there a simple approach? yes and now: 1. You can expose class without exposing class definition 2. boost::python::scope().attr( "xyz" ) = ; -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Thu Apr 20 10:20:04 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 20 Apr 2006 11:20:04 +0300 Subject: [C++-sig] how to set a global object for python environment? In-Reply-To: <7465b6170604200119x5bbd94ebmab06daf7b3a8e7f2@mail.gmail.com> References: <6d415ba30604192325h2fbf20d1s1bcf7b1c95372f93@mail.gmail.com> <7465b6170604200119x5bbd94ebmab06daf7b3a8e7f2@mail.gmail.com> Message-ID: <7465b6170604200120k5dc3a201xd60152a741d154ea@mail.gmail.com> On 4/20/06, Roman Yakovenko wrote: > On 4/20/06, ??? wrote: > > > > i have created some global object in C++, > > now i hope them could be visited from python, > > as in ASP,some one implemented the "Request","Response",then we use it in > > vbs. > > > > what i want exactly is that: expose a created object to python,but not a > > class definition, and when the object is modified in python, we can feel > > this in C++. > > > > is there a simple approach? > > yes and now: ^^^^^^^^^^^^^ Sorry, I meant no > > 1. You can expose class without exposing class definition > 2. boost::python::scope().attr( "xyz" ) = ; > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From abhi at qualcomm.com Fri Apr 21 03:10:16 2006 From: abhi at qualcomm.com (Abhi) Date: Thu, 20 Apr 2006 18:10:16 -0700 Subject: [C++-sig] pyplusplus annoyance Message-ID: I noticed that whenever I run py++, the generated file always contains 2 #includes for the original c++ header files. #include "boost/python.hpp" #ifdef _MSC_VER #pragma hdrstop #endif //_MSC_VER #include "test.h" #include "test.h" Is it possible to suppress this (ie have only one #include generated for each different file) thanks - Abhi From abhi at qualcomm.com Fri Apr 21 03:21:17 2006 From: abhi at qualcomm.com (Abhi) Date: Thu, 20 Apr 2006 18:21:17 -0700 Subject: [C++-sig] Suppressing assignment operator in Boost.Python Message-ID: Consider a simplified example: // Example #include class A { public: int x; A() : x(5) {} private: const A& operator=(const A& a) { std::cout << "A::=" << std::endl; x = a.x; return *this; } A(const A& a); }; class B { public: A aObj; B() { aObj.x = 6; } }; Notice that the assignment operator is private, hence the aObj inside B needs to be manipulated in place. The boost.python binding for this looks like (and this is what py++ also generates out-of-the-box): #include "test.cpp" namespace bp = boost::python; BOOST_PYTHON_MODULE(Boost_YMI){ bp::class_< B, boost::noncopyable>( "B" ) .def( bp::init< >()[bp::default_call_policies()] ) .def_readwrite("aObj", &B::aObj) ; bp::class_< A, boost::noncopyable >( "A" ) .def( bp::init< >()[bp::default_call_policies()] ) .def_readwrite( "x", &A::x ); } But this does not compile since Boost.Python generates an assignment operator for the attribute aObj (which has a private assignment operator). Is this anyway to tell Boost.Python to not generate the assignment operator for aObj of B. One workaround (which is not at all elegant!) is for me to write the following hand-crafted methods: void set(B& self, A& a) { self.aObj.x = a.x; } const A& get(B& self) { return self.aObj; } and then not expose the aObj as an attribute but rather through these methods in python: bp::class_< B, boost::noncopyable>( "B" ) .def( bp::init< >()[bp::default_call_policies()] ) .def("set", &set) .def("get", &get, bp::return_internal_reference<>() ) thanks - Abhi From roman.yakovenko at gmail.com Fri Apr 21 10:10:23 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 21 Apr 2006 11:10:23 +0300 Subject: [C++-sig] pyplusplus annoyance In-Reply-To: References: Message-ID: <7465b6170604210110k65c0759dm97e9834d057a5f01@mail.gmail.com> On 4/21/06, Abhi wrote: > I noticed that whenever I run py++, the generated file always contains 2 > #includes for the original c++ header files. > > #include "boost/python.hpp" > #ifdef _MSC_VER > #pragma hdrstop > #endif //_MSC_VER > > #include "test.h" > > #include "test.h" > > > > Is it possible to suppress this (ie have only one #include generated for > each different file) I'll be glad to fix this issue. Can you send your script that use py++ and small header file, that will reproduce the problem. > thanks Also, can you post all issues regarding py++ to its mailing list: https://lists.sourceforge.net/lists/listinfo/pygccxml-development > - Abhi Thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From Nicolas.Rougier at loria.fr Fri Apr 21 10:58:24 2006 From: Nicolas.Rougier at loria.fr (Nicolas Rougier) Date: Fri, 21 Apr 2006 10:58:24 +0200 Subject: [C++-sig] Request for comment on "tutorial" Message-ID: <20060421105824.12238c92@dagda.loria.fr> Hi all, I've tried to gather what I've learned so far about boost python and I wrote a simple example to illustrate some mechanisms. Basically, I tried to illustrate: * - inheritance * - class having back references onto the PyObject that holds * the underlying C++ object * - vector indexing suite * (that gives access to C++ vector-based object "a la python" * - dotted notation for children * (child access via father.child or father[0]) * - simple deep clone mechanism for containers * - creation of python objects on c++ side * - docstrings Can you have a look at the code to check I did it the proper way (and if you might find it useful at all) ? Nicolas -------------- next part -------------- A non-text attachment was scrubbed... Name: core.cc Type: text/x-c++src Size: 5413 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: core.py Type: text/x-python Size: 1746 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Makefile Type: application/octet-stream Size: 100 bytes Desc: not available URL: From roman.yakovenko at gmail.com Fri Apr 21 13:07:02 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 21 Apr 2006 14:07:02 +0300 Subject: [C++-sig] Suppressing assignment operator in Boost.Python In-Reply-To: References: Message-ID: <7465b6170604210407u45666844scc3b962f7d7f6a3c@mail.gmail.com> On 4/21/06, Abhi wrote: > namespace bp = boost::python; > > > BOOST_PYTHON_MODULE(Boost_YMI){ > bp::class_< B, boost::noncopyable>( "B" ) > .def( bp::init< >()[bp::default_call_policies()] ) > .def_readwrite("aObj", &B::aObj) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This one has been fixed and unit test has been added. Now pyplusplus will generate def_readonly. You will not be able to assign new object to aObj, but you will be able to modify it's state. Quick work around is next: mb = module_builder_t( .... ) cls_b = mb.class_( 'B' ) cls_b.member_variable( 'aObj' ).exclude() cls_b.add_code( 'def_readonly( "aObj", &B::aObj )' ) > ; > > > bp::class_< A, boost::noncopyable >( "A" ) > .def( bp::init< >()[bp::default_call_policies()] ) > .def_readwrite( "x", &A::x ); > } > > > But this does not compile since Boost.Python generates an assignment > operator for the attribute aObj (which has a private assignment operator). > > Is this anyway to tell Boost.Python to not generate the assignment operator > for aObj of B. I am not sure that I fully understand you, but proposed solution should work for you. > One workaround (which is not at all elegant!) is for me to write the > following hand-crafted methods: > > void set(B& self, A& a) > { > self.aObj.x = a.x; > } > > const A& get(B& self) > { > return self.aObj; > } > > and then not expose the aObj as an attribute but rather through these > methods in python: > bp::class_< B, boost::noncopyable>( "B" ) > .def( bp::init< >()[bp::default_call_policies()] ) > .def("set", &set) > .def("get", &get, bp::return_internal_reference<>() ) I don't think you need this. > > thanks > - Abhi -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From abhi at qualcomm.com Fri Apr 21 16:36:01 2006 From: abhi at qualcomm.com (Abhi) Date: Fri, 21 Apr 2006 07:36:01 -0700 Subject: [C++-sig] Suppressing assignment operator in Boost.Python In-Reply-To: <7465b6170604210407u45666844scc3b962f7d7f6a3c@mail.gmail.com> References: <7465b6170604210407u45666844scc3b962f7d7f6a3c@mail.gmail.com> Message-ID: --On Friday, April 21, 2006 2:07 PM +0300 Roman Yakovenko wrote: > On 4/21/06, Abhi wrote: >> namespace bp = boost::python; >> >> >> BOOST_PYTHON_MODULE(Boost_YMI){ >> bp::class_< B, boost::noncopyable>( "B" ) >> .def( bp::init< >()[bp::default_call_policies()] ) >> .def_readwrite("aObj", &B::aObj) > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > This one has been fixed and unit test has been added. > Now pyplusplus will generate def_readonly. > You will not be able to assign new object to aObj, but you will be > able to modify > it's state. I want to be able to modify it in place. Like this: -----------------------------C++ // Example #include class A { public: int x; A() : x(5) {} private: const A& operator=(const A& a) { std::cout << "A::=" << std::endl; x = a.x; return *this; } A(const A& a); }; class B { public: A aObj; B() { aObj.x = 6; } }; ------------------------------- In python: ---------------.py b = B() a = A() # No assignment operator for A # so we cannot do # b.aObj = someNewA # but we can modify it in place b.aObj.x = 10; # and also read it in place y = b.aObj.x -------------------- How do I generate Boost.Python bindings in py++ for doing this? And also what does the .def look like (ie the Boost.Python binding look like). I could just do the code.wrapper in py++ for it. thanks - Abhi > > Quick work around is next: > mb = module_builder_t( .... ) > cls_b = mb.class_( 'B' ) > cls_b.member_variable( 'aObj' ).exclude() > cls_b.add_code( 'def_readonly( "aObj", &B::aObj )' ) > >> ; >> >> >> bp::class_< A, boost::noncopyable >( "A" ) >> .def( bp::init< >()[bp::default_call_policies()] ) >> .def_readwrite( "x", &A::x ); >> } >> >> >> But this does not compile since Boost.Python generates an assignment >> operator for the attribute aObj (which has a private assignment >> operator). >> >> Is this anyway to tell Boost.Python to not generate the assignment >> operator for aObj of B. > > I am not sure that I fully understand you, but proposed solution > should work for you. > >> One workaround (which is not at all elegant!) is for me to write the >> following hand-crafted methods: >> >> void set(B& self, A& a) >> { >> self.aObj.x = a.x; >> } >> >> const A& get(B& self) >> { >> return self.aObj; >> } >> >> and then not expose the aObj as an attribute but rather through these >> methods in python: >> bp::class_< B, boost::noncopyable>( "B" ) >> .def( bp::init< >()[bp::default_call_policies()] ) >> .def("set", &set) >> .def("get", &get, bp::return_internal_reference<>() ) > > I don't think you need this. > >> >> thanks >> - Abhi > > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ From roman.yakovenko at gmail.com Fri Apr 21 18:59:00 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 21 Apr 2006 19:59:00 +0300 Subject: [C++-sig] Suppressing assignment operator in Boost.Python In-Reply-To: References: <7465b6170604210407u45666844scc3b962f7d7f6a3c@mail.gmail.com> Message-ID: <7465b6170604210959r7701bd34vd3607ecef12b28da@mail.gmail.com> On 4/21/06, Abhi wrote: > > > --On Friday, April 21, 2006 2:07 PM +0300 Roman Yakovenko > wrote: > > > On 4/21/06, Abhi wrote: > >> namespace bp = boost::python; > >> > >> > >> BOOST_PYTHON_MODULE(Boost_YMI){ > >> bp::class_< B, boost::noncopyable>( "B" ) > >> .def( bp::init< >()[bp::default_call_policies()] ) > >> .def_readwrite("aObj", &B::aObj) > > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > > > This one has been fixed and unit test has been added. > > Now pyplusplus will generate def_readonly. > > You will not be able to assign new object to aObj, but you will be > > able to modify > > it's state. > > I want to be able to modify it in place. > Like this: > -----------------------------C++ > // Example > #include > class A > { > public: > int x; > A() : x(5) {} > > private: > const A& operator=(const A& a) > { > std::cout << "A::=" << std::endl; > x = a.x; > return *this; > } > A(const A& a); > > }; > > class B > { > public: > A aObj; > B() > { > aObj.x = 6; > } > }; > ------------------------------- > In python: > > ---------------.py > b = B() > a = A() > > # No assignment operator for A > # so we cannot do > # b.aObj = someNewA > # but we can modify it in place > b.aObj.x = 10; > # and also read it in place > y = b.aObj.x > -------------------- > > How do I generate Boost.Python bindings in py++ for doing this? > And also what does the .def look like (ie the Boost.Python binding look > like). I could just do the code.wrapper in py++ for it. Please read my answer to your mail. I explained what should you do in order to get desired result. > thanks > - Abhi -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From abhi at qualcomm.com Fri Apr 21 19:18:27 2006 From: abhi at qualcomm.com (Abhi) Date: Fri, 21 Apr 2006 10:18:27 -0700 Subject: [C++-sig] Suppressing assignment operator in Boost.Python In-Reply-To: <7465b6170604210959r7701bd34vd3607ecef12b28da@mail.gmail.com> References: <7465b6170604210407u45666844scc3b962f7d7f6a3c@mail.gmail.com> <7465b6170604210959r7701bd34vd3607ecef12b28da@mail.gmail.com> Message-ID: I realized that .def_readonly avoids the assignment of the attribute, but not its data members. Hence, it allows modification in place. Thanks Roman - Abhi --On Friday, April 21, 2006 7:59 PM +0300 Roman Yakovenko wrote: > On 4/21/06, Abhi wrote: >> >> >> --On Friday, April 21, 2006 2:07 PM +0300 Roman Yakovenko >> wrote: >> >> > On 4/21/06, Abhi wrote: >> >> namespace bp = boost::python; >> >> >> >> >> >> BOOST_PYTHON_MODULE(Boost_YMI){ >> >> bp::class_< B, boost::noncopyable>( "B" ) >> >> .def( bp::init< >()[bp::default_call_policies()] ) >> >> .def_readwrite("aObj", &B::aObj) >> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> > >> > This one has been fixed and unit test has been added. >> > Now pyplusplus will generate def_readonly. >> > You will not be able to assign new object to aObj, but you will be >> > able to modify >> > it's state. >> >> I want to be able to modify it in place. >> Like this: >> -----------------------------C++ >> // Example >> # include >> class A >> { >> public: >> int x; >> A() : x(5) {} >> >> private: >> const A& operator=(const A& a) >> { >> std::cout << "A::=" << std::endl; >> x = a.x; >> return *this; >> } >> A(const A& a); >> >> }; >> >> class B >> { >> public: >> A aObj; >> B() >> { >> aObj.x = 6; >> } >> }; >> ------------------------------- >> In python: >> >> ---------------.py >> b = B() >> a = A() >> >> # No assignment operator for A >> # so we cannot do >> # b.aObj = someNewA >> # but we can modify it in place >> b.aObj.x = 10; >> # and also read it in place >> y = b.aObj.x >> -------------------- >> >> How do I generate Boost.Python bindings in py++ for doing this? >> And also what does the .def look like (ie the Boost.Python binding look >> like). I could just do the code.wrapper in py++ for it. > > Please read my answer to your mail. I explained what should you do in > order to get > desired result. > >> thanks >> - Abhi > > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ From roman.yakovenko at gmail.com Sat Apr 22 12:05:16 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sat, 22 Apr 2006 13:05:16 +0300 Subject: [C++-sig] Wrapping pointers In-Reply-To: <200604200044.41722.dominic.sacre@gmx.de> References: <20060419210522.92608.qmail@web31512.mail.mud.yahoo.com> <200604200044.41722.dominic.sacre@gmx.de> Message-ID: <7465b6170604220305t3ed839b4wb6c3a7c0e60bd5e8@mail.gmail.com> On 4/20/06, Dominic Sacr? wrote: > On Wednesday, 19. April 2006 23:05, Ralf W. Grosse-Kunstleve wrote: > > .add_property("p", make_function(&A::get_p, > > return_value_policy())) > > Thanks, that did the trick! > > Dominic I am working on pyplusplus to generate right code for this situation. The case discussed here is already implemented ( not committed yet ). pyplusplus generates class wrapper and adds "get" method to it. I attached the file that demonstrates the use case. My question about "set" functionality. I am not sure what call policies I should set by default? default or with_custodian_and_ward_postcall< 0, 1 > I think, that first one is a mistake, while the second one is correct. I just want to be sure. Also, right now I don't see differences between regular member variable and static member variable as for the call policies. Am I right? Thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- A non-text attachment was scrubbed... Name: mvpointer.cpp Type: text/x-c++src Size: 3448 bytes Desc: not available URL: From capak at inputwish.com Mon Apr 24 10:34:00 2006 From: capak at inputwish.com (Libor Capak) Date: Mon, 24 Apr 2006 10:34:00 +0200 Subject: [C++-sig] singleton wrapping Message-ID: <20060424083400.GA28600@irma.inputwish.local> hello, i'm quite new to boost::python. i'd like to ask how you solve singleton wrapping. i have simple template: template class Singleton { static T* singleton; public: //.. blabla .. static T& instance() { mustBe(singleton); return *singleton; } // .. blabla .. }; and simple class, for example: class A : public Singleton { public: void foo(); }; and now to export into python module i'm using wrapper class: class A_wrap { static void foo() { A.instance().foo(); } }; BOOST_PYTHON_MODULE(test) { class_("A", no_init) .def("foo", &A_wrap::foo) .staticmethod("foo") ; } is there any trick how to skip writing class A_wrap and still use method A::foo() as staticmethod from python? Libor Capak From roman.yakovenko at gmail.com Mon Apr 24 10:17:45 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 24 Apr 2006 11:17:45 +0300 Subject: [C++-sig] singleton wrapping In-Reply-To: <20060424083400.GA28600@irma.inputwish.local> References: <20060424083400.GA28600@irma.inputwish.local> Message-ID: <7465b6170604240117r610948e4v25ca0782e5829570@mail.gmail.com> On 4/24/06, Libor Capak wrote: > hello, > ... > is there any trick how to skip writing class A_wrap and still > use method A::foo() as staticmethod from python? I think next link can help you: http://boost.org/libs/python/doc/v2/scope.html#scope-spec And next line can do the trick, but I am not sure scope().attr("A_singleton") = &A::instance(); > Libor Capak -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From juho.makinen at gmail.com Tue Apr 25 21:39:08 2006 From: juho.makinen at gmail.com (=?ISO-8859-1?Q?Juho_M=E4kinen?=) Date: Tue, 25 Apr 2006 22:39:08 +0300 Subject: [C++-sig] Howto expose exception classes using boost.python? Message-ID: <4a41ceba0604251239x340ee52am7aef5c86bc6c9cb2@mail.gmail.com> I have stumbled across this problem when I started looking the right way how to expose a bunch of C++ Exception classes to python. I have a "master" exception class, which is delivered from std::exception. All other exception classes in our project are base on this. As far as I know, Python requires that the exceptions must be delivered from PyExc_Exception, or a class which is delivered from PyExc_Exception, but I haven't found any way how to expose a C++ class which delivers from a python class. There's also PyErr_NewException() available within Python C API. This would be the ideal way how I'd like to use our exceptions. PyErr_SetObject(PyExc_MyException, InstanceOfMyException) -or in python- raise mymodule.MyException(arg1, arg2) I know that I can expose C++ exceptions with boost.python (the PodBayDoorException example) using boost::python::register_exception_translator, but I want to be able to distinguish my different C++ exceptions at the python side. (eg. It's not enough that all my exceptions are translated into PyExc_RuntimeError, or some other standard python exception) All ideas how to expose exception hierarchy from C++ to Python are appreciated. Thanks in advance, - Juho M?kinen From craigwharris at comcast.net Thu Apr 27 01:28:49 2006 From: craigwharris at comcast.net (craig harris) Date: Wed, 26 Apr 2006 16:28:49 -0700 Subject: [C++-sig] Best way to use python as an API inside a Qt/C++ app? Message-ID: <20060426232901.8DC8F1E4005@bag.python.org> I am using Qt as the GUI for a C++ app. I would also like to use python as an API for the application. For instance when a menu item is pressed, a stored python command is executed. Is this wise? And what is the best approach (tool) to implementing this? PyQt, boost.python, other? -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Thu Apr 27 02:20:50 2006 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 26 Apr 2006 20:20:50 -0400 Subject: [C++-sig] Best way to use python as an API inside a Qt/C++ app? In-Reply-To: <20060426232901.8DC8F1E4005@bag.python.org> References: <20060426232901.8DC8F1E4005@bag.python.org> Message-ID: <44500E62.5060309@sympatico.ca> craig harris wrote: > I am using Qt as the GUI for a C++ app. I would also like to use python > as an API for the application. For instance when a menu item is > pressed, a stored python command is executed. > > Is this wise? That sounds very reasonable to me. > And what is the best approach (tool) to implementing > this? PyQt, boost.python, other? That depends on what you mean by 'implement'. Executing some python code via a menu is trivial. What is much less trivial is to expose your application's state to python so you can access and modify it from these commands. As far as Qt is concerned, there already are a number of bindings (sip for Qt3, and boost.python for Qt4). How to reflect your own code into python is a different issue. Boost.python is definitely a good choice, though if you use a lot of Qt internally it would be best to stay with the tool used to generate the Qt bindings, and simply add your own classes. Regards, Stefan From roman.yakovenko at gmail.com Thu Apr 27 07:44:01 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 27 Apr 2006 08:44:01 +0300 Subject: [C++-sig] Howto expose exception classes using boost.python? In-Reply-To: <4a41ceba0604251239x340ee52am7aef5c86bc6c9cb2@mail.gmail.com> References: <4a41ceba0604251239x340ee52am7aef5c86bc6c9cb2@mail.gmail.com> Message-ID: <7465b6170604262244x5971cc89k89283af7c5f47837@mail.gmail.com> On 4/25/06, Juho M?kinen wrote: > I have stumbled across this problem when I started looking the right > way how to expose a bunch of C++ Exception classes to python. > > I have a "master" exception class, which is delivered from > std::exception. All other exception classes in our project are base on > this. > > As far as I know, Python requires that the exceptions must be > delivered from PyExc_Exception, or a class which is delivered from > PyExc_Exception, Are you sure? Where did you read it? > but I haven't found any way how to expose a C++ class > which delivers from a python class. I think such functionality does not exists in boost.python library. But I could be wrong. >There's also PyErr_NewException() available within Python C API. > > This would be the ideal way how I'd like to use our exceptions. > > PyErr_SetObject(PyExc_MyException, InstanceOfMyException) > -or in python- > raise mymodule.MyException(arg1, arg2) Here is relevant piece of Python documentation: void PyErr_SetObject( PyObject *type, PyObject *value) This function is similar to PyErr_SetString() but lets you specify an arbitrary Python object for the ``value'' of the exception. I think that "an arbitrary" is the key to your solution. In the past I created custom exception translator. ( I can not find code, so I will just write an algorithm ) Register all your exception classes as usual. Write next function: template void translate_it( const MyCppException& err ){ boost::python::object bp_err( err ); extract PyObjectType and PyObject from py_err this exact code I don't remember, but it is not to difficult to find out. pay attention to reference count ( borrow or handle ) PyErr_SetObject( py_type, py_error ); } For every exception register it's translation using translate_it. >From now your C++/Python code can throw/raise exceptions as usual. They will be translated to Python. The only (very) bad thing is that the code that treats exceptions now should treat 2 exception kinds. > > I know that I can expose C++ exceptions with boost.python (the > PodBayDoorException example) using > boost::python::register_exception_translator, but I want to be able to > distinguish my different C++ exceptions at the python side. (eg. It's > not enough that all my exceptions are translated into > PyExc_RuntimeError, or some other standard python exception) > > All ideas how to expose exception hierarchy from C++ to Python are appreciated. > > Thanks in advance, I hope this will help you, also I am sure other people do know better solution. In any case if you find a solution can you post it? I will add it to pyplusplus ( http://www.language-binding.net/pyplusplus/pyplusplus.html ) > - Juho M?kinen -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Thu Apr 27 12:50:20 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 27 Apr 2006 13:50:20 +0300 Subject: [C++-sig] Fwd: Virtual Functions with Default Implementations In-Reply-To: <7465b6170604270349o39af72bev199f5f6b0fa5d23@mail.gmail.com> References: <7465b6170604270349o39af72bev199f5f6b0fa5d23@mail.gmail.com> Message-ID: <7465b6170604270350q7d65c78j5872235307c5933e@mail.gmail.com> Hi. It seems to me that I miss something very important ( or very obvious ) Here is a relevant piece of code from tutorials( http://tinyurl.com/hw5hb ) struct Base { virtual ~Base() {} virtual int f() { return 0; } }; struct BaseWrap : Base, wrapper { int f() { if (override f = this->get_override("f")) return f(); // *note* return Base::f(); } int default_f() { return this->Base::f(); } }; class_("Base") .def("f", &Base::f, &BaseWrap::default_f) ; Why boost.python requests to register BaseWrap::default_f? It seems to me that BaseWrap::f implementation deals with this. So what did I miss? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From iluetkeb at gmail.com Thu Apr 27 14:15:26 2006 From: iluetkeb at gmail.com (=?ISO-8859-1?Q?Ingo_L=FCtkebohle?=) Date: Thu, 27 Apr 2006 14:15:26 +0200 Subject: [C++-sig] status of signatures? Message-ID: Hi, what is the status of the boost-signatures patch (originally by Ralf IIRC)? Is it being taken forwarded? Is it planned for inclusion into boost::python sometime soon? I think more complete documentation generation like implemented by that patch is one of the most important things still missing! Having read through the list I recognize there are awkward cases with unwieldy signatures but are they a showstopper? I figure that most easy cases might be covered already. Best Regards Ingo From roman.yakovenko at gmail.com Thu Apr 27 14:34:42 2006 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 27 Apr 2006 15:34:42 +0300 Subject: [C++-sig] status of signatures? In-Reply-To: References: Message-ID: <7465b6170604270534w1cf51004ide38ac4d15721e69@mail.gmail.com> On 4/27/06, Ingo L?tkebohle wrote: > Hi, > > what is the status of the boost-signatures patch (originally by Ralf > IIRC)? Is it being taken forwarded? Is it planned for inclusion into > boost::python sometime soon? I could be wrong, but it is already in CVS. Take a look on cvs ( boost.python ) news.html file. > Best Regards > > Ingo -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From amohr at pixar.com Thu Apr 27 18:34:05 2006 From: amohr at pixar.com (Alex Mohr) Date: Thu, 27 Apr 2006 09:34:05 -0700 Subject: [C++-sig] Fwd: Virtual Functions with Default Implementations In-Reply-To: <7465b6170604270350q7d65c78j5872235307c5933e@mail.gmail.com> References: <7465b6170604270349o39af72bev199f5f6b0fa5d23@mail.gmail.com> <7465b6170604270350q7d65c78j5872235307c5933e@mail.gmail.com> Message-ID: <4450F27D.3030003@pixar.com> > Why boost.python requests to register BaseWrap::default_f? > It seems to me that BaseWrap::f implementation deals with this. > > So what did I miss? I think it's so you can call Base's f() from your derived, overridden f without an infinite loop. Something like this: class Dervied(Base): def f(self): return Base.f(self) * 2 Here I don't want my Base.f(self) call to dispatch back to me, as this would cause an infinite recursion. Alex From padilla.henry at gmail.com Fri Apr 28 19:21:28 2006 From: padilla.henry at gmail.com (Henry Padilla) Date: Fri, 28 Apr 2006 13:21:28 -0400 Subject: [C++-sig] VC 2005 support in boost.python Message-ID: I play Civilization 4, that uses Boost. We can't compile the SDK because there's some incompatibility issues with VC 2005. I was wondering if anyone still had incompatibility issues with VC 2005 or if the macro BOOST_WORKAROUND was still needed. Thanks for the help. Tom P. -------------- next part -------------- An HTML attachment was scrubbed... URL: From Paul_Kunz at slac.stanford.edu Fri Apr 28 19:43:30 2006 From: Paul_Kunz at slac.stanford.edu (Paul F. Kunz) Date: Fri, 28 Apr 2006 10:43:30 -0700 Subject: [C++-sig] VC 2005 support in boost.python In-Reply-To: (padilla.henry@gmail.com) References: Message-ID: <200604281743.k3SHhUuj017163@noric04.slac.stanford.edu> >>>>> On Fri, 28 Apr 2006 13:21:28 -0400, "Henry Padilla" said: > I play Civilization 4, that uses Boost. We can't compile the SDK > because there's some incompatibility issues with VC 2005. > I was wondering if anyone still had incompatibility issues with VC > 2005 or if the macro BOOST_WORKAROUND was still needed. > Thanks for the help. Tom P. At least for me, the Boost.Python libraries work with VC 2005. From padilla.henry at gmail.com Fri Apr 28 19:52:51 2006 From: padilla.henry at gmail.com (Henry Padilla) Date: Fri, 28 Apr 2006 13:52:51 -0400 Subject: [C++-sig] VC 2005 support in boost.python In-Reply-To: <200604281743.k3SHhUuj017163@noric04.slac.stanford.edu> References: <200604281743.k3SHhUuj017163@noric04.slac.stanford.edu> Message-ID: The specific error we get is "error C2665: 'boost::**python::detail::make_getter' : none of the 3 overloads could convert all the argument types". Don't know if this is fixed and compatible with the Civ4 SDK. Tom P. On 4/28/06, Paul F. Kunz wrote: > > >>>>> On Fri, 28 Apr 2006 13:21:28 -0400, "Henry Padilla" < > padilla.henry at gmail.com> said: > > > I play Civilization 4, that uses Boost. We can't compile the SDK > > because there's some incompatibility issues with VC 2005. > > > I was wondering if anyone still had incompatibility issues with VC > > 2005 or if the macro BOOST_WORKAROUND was still needed. > > > Thanks for the help. Tom P. > > At least for me, the Boost.Python libraries work with VC 2005. > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From Nicolas.Rougier at loria.fr Sat Apr 29 17:18:47 2006 From: Nicolas.Rougier at loria.fr (Nicolas P. Rougier) Date: Sat, 29 Apr 2006 17:18:47 +0200 Subject: [C++-sig] Simple boost example available Message-ID: <1146323927.16912.2.camel@localhost.localdomain> Hi all, I put on http://www.loria.fr/~rougier/software/boost/ a simple example showing some boost python features in action. Hope this can help someone. Nicolas Rougier. From dave at boost-consulting.com Sun Apr 30 06:38:40 2006 From: dave at boost-consulting.com (David Abrahams) Date: Sun, 30 Apr 2006 00:38:40 -0400 Subject: [C++-sig] Howto expose exception classes using boost.python? References: <4a41ceba0604251239x340ee52am7aef5c86bc6c9cb2@mail.gmail.com> <7465b6170604262244x5971cc89k89283af7c5f47837@mail.gmail.com> Message-ID: "Roman Yakovenko" writes: >> As far as I know, Python requires that the exceptions must be >> delivered from PyExc_Exception, or a class which is delivered from >> PyExc_Exception, > > Are you sure? Where did you read it? Officially, it does require that exceptions be derived from PyExc_Exception (I don't remember where that's documented), but unofficially, you can throw anything :) -- Dave Abrahams Boost Consulting www.boost-consulting.com