From hapopen at gmail.com Sat Dec 1 08:14:07 2012 From: hapopen at gmail.com (simon zhang) Date: Sat, 1 Dec 2012 15:14:07 +0800 Subject: [C++-sig] Do the boost.python support member data pointer?? Message-ID: Do the boost.python support member data pointer?? -------------- next part -------------- An HTML attachment was scrubbed... URL: From jaedyn.cppsig at jaedyn.co Thu Dec 6 01:17:54 2012 From: jaedyn.cppsig at jaedyn.co (Jaedyn K. Draper) Date: Wed, 5 Dec 2012 18:17:54 -0600 Subject: [C++-sig] Boost.Python - Passing boost::python::object as argument to python function? Message-ID: <50BFE2EA.1000105@jaedyn.co> An HTML attachment was scrubbed... URL: From giuseppe.corbelli at copanitalia.com Thu Dec 6 09:18:34 2012 From: giuseppe.corbelli at copanitalia.com (Giuseppe Corbelli) Date: Thu, 06 Dec 2012 09:18:34 +0100 Subject: [C++-sig] Boost.Python - Passing boost::python::object as argumentto python function? In-Reply-To: <50BFE2EA.1000105@jaedyn.co> References: <50BFE2EA.1000105@jaedyn.co> Message-ID: <50C054DA.8010002@copanitalia.com> On 06/12/2012 01:17, Jaedyn K. Draper wrote: > Hello everyone! > > So I'm working on a little project in which I'm using Python as an embedded > scripting engine. So far I've not had much trouble with it using boost.python, > but there's something I'd like to do with it if it's possible. > > Basically, Python can be used to extend my C++ classes by adding functions and > even data values to the class. I'd like to be able to have these persist in > the C++ side, so one python function can add data members to a class, and then > later the same instance passed to a different function will still have them. > The goal here being to write a generic core engine in C++, and let users > extend it in Python in any way they need without ever having to touch the C++. > > So what I thought would work was that I would store a |boost::python::object| > in the C++ class as a value |self|, and when calling the python from the C++, > I'd send that python object through |boost::python::ptr()|, so that > modifications on the python side would persist back to the C++ class. > Unfortunately when I try this, I get the following error: > > |TypeError: No to_python (by-value) converter found for C++ type: > boost::python::api::object| > > Is there any way of passing an object directly to a python function like that, > or any other way I can go about this to achieve my desired result? Please post some example code. Is it an option to aggregate a std::map into the C++ class? It would serve somehow like a "__dict__" on the C++ side. -- Giuseppe Corbelli WASP Software Engineer, Copan Italia S.p.A Phone: +390303666318 Fax: +390302659932 E-mail: giuseppe.corbelli at copanitalia.com From jaedyn.cppsig at jaedyn.co Thu Dec 6 11:12:15 2012 From: jaedyn.cppsig at jaedyn.co (Jaedyn K. Draper) Date: Thu, 6 Dec 2012 04:12:15 -0600 Subject: [C++-sig] Boost.Python - Passing boost::python::object as argumentto python function? In-Reply-To: <50C054DA.8010002@copanitalia.com> References: <50BFE2EA.1000105@jaedyn.co> <50C054DA.8010002@copanitalia.com> Message-ID: <50C06E2A.7020004@jaedyn.co> An HTML attachment was scrubbed... URL: From legordian at gmx.net Fri Dec 7 14:22:00 2012 From: legordian at gmx.net (Charly Bicker) Date: Fri, 07 Dec 2012 14:22:00 +0100 Subject: [C++-sig] Extending classes Message-ID: <20121207132200.139420@gmx.net> Dear all, currently, I am revisiting an problem I had with my project some time ago: due to several external constraints, I need to do some work if certain member functions of my classes are called from python. Initially, I put the additional code in the wrappers which were exposed with boost::python. Unfortunately, I also have cases were objects are instantiated in C++ parts of the code and then handed to python-space, which results in problems when the member functions mentioned above are called. I described the problem already earlier, cf. http://mail.python.org/pipermail//cplusplus-sig/2012-September/016753.html. Unfortunately, I never found a solution to this problem. Can I somehow execute additional code when member functions are called from python, even if the object was instantiated in C++ (i.e. is not a wrapper class). Is there a way to use a global function for this? I cannot touch the C++ code too much, although a "friend" declaration might be possible. Thanks in advance for your help! Best regards, Karl Bicker From brandsmeier at gmx.de Fri Dec 7 14:30:16 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Fri, 7 Dec 2012 14:30:16 +0100 Subject: [C++-sig] Extending classes In-Reply-To: <20121207132200.139420@gmx.net> References: <20121207132200.139420@gmx.net> Message-ID: Dear Karl, I have one idea that may help you. Say you have a class `MyClass` with a method `func` where you want extra code to be excecuted when that function is called from python. I would add a function `MyClass_func` which is a function outside a class that has the same return code as `func` and takes the same arguments, excepts that it additionally has `MyClass& self` as its first argument. The type `MyClass&` can also be varied, e.g. `boost::shared_ptr`. Inside `MyClass_func` you can do whatever you want, and finally you call `self.func(...)`. In boost::python you simply export `&MyClass_func` instead of `&MyClass::func`. This worked for some of my applications, but might not be suitable in some inheritance cases. I would need more information on your application to see if this would be applicable. -Holger On Fri, Dec 7, 2012 at 2:22 PM, Charly Bicker wrote: > Dear all, > > currently, I am revisiting an problem I had with my project some time ago: due to several external constraints, I need to do some work if certain member functions of my classes are called from python. Initially, I put the additional code in the wrappers which were exposed with boost::python. Unfortunately, I also have cases were objects are instantiated in C++ parts of the code and then handed to python-space, which results in problems when the member functions mentioned above are called. I described the problem already earlier, cf. http://mail.python.org/pipermail//cplusplus-sig/2012-September/016753.html. > > Unfortunately, I never found a solution to this problem. Can I somehow execute additional code when member functions are called from python, even if the object was instantiated in C++ (i.e. is not a wrapper class). Is there a way to use a global function for this? I cannot touch the C++ code too much, although a "friend" declaration might be possible. > > Thanks in advance for your help! > > Best regards, > Karl Bicker > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From legordian at gmx.net Fri Dec 7 16:11:43 2012 From: legordian at gmx.net (Charly Bicker) Date: Fri, 07 Dec 2012 16:11:43 +0100 Subject: [C++-sig] Extending classes Message-ID: <20121207151143.166270@gmx.net> Dear Holger, that sounds like it could solve most of my problems! Thank you very much! I will try to implement it over the next few days. Best regards, Karl ----- Original Message ----- From: "Holger Brandsmeier" To: "Development of Python/C++ integration" Sent: Friday, December 7, 2012 2:30:16 PM Subject: Re: [C++-sig] Extending classes Dear Karl, I have one idea that may help you. Say you have a class `MyClass` with a method `func` where you want extra code to be excecuted when that function is called from python. I would add a function `MyClass_func` which is a function outside a class that has the same return code as `func` and takes the same arguments, excepts that it additionally has `MyClass& self` as its first argument. The type `MyClass&` can also be varied, e.g. `boost::shared_ptr`. Inside `MyClass_func` you can do whatever you want, and finally you call `self.func(...)`. In boost::python you simply export `&MyClass_func` instead of `&MyClass::func`. This worked for some of my applications, but might not be suitable in some inheritance cases. I would need more information on your application to see if this would be applicable. -Holger On Fri, Dec 7, 2012 at 2:22 PM, Charly Bicker wrote: > Dear all, > > currently, I am revisiting an problem I had with my project some time ago: due to several external constraints, I need to do some work if certain member functions of my classes are called from python. Initially, I put the additional code in the wrappers which were exposed with boost::python. Unfortunately, I also have cases were objects are instantiated in C++ parts of the code and then handed to python-space, which results in problems when the member functions mentioned above are called. I described the problem already earlier, cf. http://mail.python.org/pipermail//cplusplus-sig/2012-September/016753.html. > > Unfortunately, I never found a solution to this problem. Can I somehow execute additional code when member functions are called from python, even if the object was instantiated in C++ (i.e. is not a wrapper class). Is there a way to use a global function for this? I cannot touch the C++ code too much, although a "friend" declaration might be possible. > > Thanks in advance for your help! > > Best regards, > Karl Bicker > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig From HarrievA at gmx.de Tue Dec 11 19:14:59 2012 From: HarrievA at gmx.de (Hendrik van Arragon) Date: Tue, 11 Dec 2012 19:14:59 +0100 Subject: [C++-sig] Let pyhton deside which data it recieves... Message-ID: <20121211181459.51910@gmx.net> Hi, I've made a little class for serial communication, which i want to export from c++ to python. My problem is, that this class does not know what data it's receiving. The layer above the protocol knows how to handle the data. Here is an example: class Packet{ public: uint8_t id; uint8_t data[256]; template void copy_to(T *pointer){ if(sizeof(T) > sizeof(data)){ cout << "size of object you want to copy data to is bigger then a Packtet. Do you know what you are doing?" << endl; } memcpy(pointer,&data,sizeof(T)); } void copy_to_void(void *p, uint8_t size){ memcpy(p,&data,size); } }; struct example_data{ uint8_t ad_value; int16_t acc_x; int16_t acc_y; }; I use pyplusplus to export everything. With the line template void Packet::copy_to(example_data*) it works. But how do i do that, without knowing exemple_data at compile time... I know ctypes has some pointer and dataalignmentstuff, but my understanding of pyplusplus, boost::python and ctype's is not good enough. Maybe it is possible to get a ctype-pointer and use the copy_to_void()-Funktion. But i don't know how???? Can somebody help me? Regards, Hendrik From roman.yakovenko at gmail.com Thu Dec 13 17:59:05 2012 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 13 Dec 2012 18:59:05 +0200 Subject: [C++-sig] Let pyhton deside which data it recieves... In-Reply-To: <20121211181459.51910@gmx.net> References: <20121211181459.51910@gmx.net> Message-ID: On Tue, Dec 11, 2012 at 8:14 PM, Hendrik van Arragon wrote: > Hi, > > I've made a little class for serial communication, which i want to export from c++ to python. My problem is, that this class does not know what data it's receiving. The layer above the protocol knows how to handle the data. Here is an example: > ... > Maybe it is possible to get a ctype-pointer and use the copy_to_void()-Funktion. But i don't know how???? Take a look on py++ documentation, unittests and examples. They contain information how to integrate ctypes & boost.python in a single module or even class. HTH, Roman From rwgk at google.com Sat Dec 15 02:28:34 2012 From: rwgk at google.com (Ralf Grosse-Kunstleve) Date: Fri, 14 Dec 2012 17:28:34 -0800 Subject: [C++-sig] outdated info in the "projects using boost.python" docs In-Reply-To: References: Message-ID: Thanks, I've updated the web page. I simply took this from the RDkit page: A collection of cheminformatics and machine-learning software written in C++ and Python. On Fri, Nov 23, 2012 at 2:26 AM, Greg Landrum wrote: > Hi, > > A colleague pointed out to me that the "Project using Boost.Python" > document ( > http://www.boost.org/doc/libs/1_52_0/libs/python/doc/projects.html) > includes a link to Rational Discovery LLC, a company that no longer > exists (we shut it down in 2006). That link, which now re-directs to > some completely different organization) should probably be removed. > > The software we developed that uses boost.python to expose chemistry > functionality in C++ to Python has been open-sourced as the rdkit > (www.rdkit.org). If there's any interest in new additions to the > "projects using boost.python" page, I would be happy to provide a > blurb. > > Thanks, > -greg > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jzwinck at gmail.com Sun Dec 16 21:37:40 2012 From: jzwinck at gmail.com (John Zwinck) Date: Sun, 16 Dec 2012 20:37:40 +0000 Subject: [C++-sig] RAII for the GIL in Boost.Python? Message-ID: I posted this to the Boost list, where it was suggested this list as a next step. If you saw it there, the rest of this message is the same. There are many questions and example classes online for dealing with Python's Global Interpreter Lock (GIL) using RAII. I think Boost.Python should provide this functionality, rather than having users who build hybrid C++/Python applications copy-pasting code from various places. I was one of those users recently, and the code I developed to deal with it is now public: https://github.com/jzwinck/pccl/blob/master/InterpreterLockGuard.hpp https://github.com/jzwinck/pccl/blob/master/InterpreterLockGuard.cpp https://github.com/jzwinck/pccl/blob/master/test/InterpreterLockGuard.test.cpp Comments in the header explain how to use it in the context of Boost.Python, and contain a link to a wiki from which some of the code was originally copied. Why add this to Boost? It's clearly code that a bunch of people need, often people who are already using Boost.Python. It has a permissive license, and I can re-license it if need be. And it depends on Boost (mostly for thread-specific storage--I hope this dependency won't be seen as a liability here, though it could be removed if needed). This particular implementation was used successfully in a few programs in a commercial setting on Fedora and RHEL systems. It has some extra checks that we found helpful compared to some of the versions you'll find posted online. The code also builds and passes its tests with GCC on Mac OS X. I welcome any feedback regarding the suitability for inclusion in Boost of this particular code and/or the concepts it implements. John Zwinck From adam.preble at gmail.com Sun Dec 16 23:58:02 2012 From: adam.preble at gmail.com (Adam Preble) Date: Sun, 16 Dec 2012 16:58:02 -0600 Subject: [C++-sig] RAII for the GIL in Boost.Python? In-Reply-To: References: Message-ID: Is the particular focus of your code on getting the GIL released during longer method invocations? I don't have any sway with Boost or anything, so I'm just asking out of my own personal curiosities. When I first saw the message, without seeing the code, I was wondering if you might have coincidentally created a scope-lock kind of thing for acquiring the GIL in the first place. On Sun, Dec 16, 2012 at 2:37 PM, John Zwinck wrote: > I posted this to the Boost list, where it was suggested this list as a > next step. If you saw it there, the rest of this message is the same. > > There are many questions and example classes online for dealing with > Python's Global Interpreter Lock (GIL) using RAII. I think Boost.Python > should provide this functionality, rather than having users who build > hybrid C++/Python applications copy-pasting code from various places. I > was one of those users recently, and the code I developed to deal with it > is now public: > > https://github.com/jzwinck/**pccl/blob/master/**InterpreterLockGuard.hpp > https://github.com/jzwinck/**pccl/blob/master/**InterpreterLockGuard.cpp > https://github.com/jzwinck/**pccl/blob/master/test/** > InterpreterLockGuard.test.cpp > > Comments in the header explain how to use it in the context of > Boost.Python, and contain a link to a wiki from which some of the code was > originally copied. > > Why add this to Boost? It's clearly code that a bunch of people need, > often people who are already using Boost.Python. It has a permissive > license, and I can re-license it if need be. And it depends on Boost > (mostly for thread-specific storage--I hope this dependency won't be seen > as a liability here, though it could be removed if needed). > > This particular implementation was used successfully in a few programs in > a commercial setting on Fedora and RHEL systems. It has some extra checks > that we found helpful compared to some of the versions you'll find posted > online. The code also builds and passes its tests with GCC on Mac OS X. > > I welcome any feedback regarding the suitability for inclusion in Boost of > this particular code and/or the concepts it implements. > > John Zwinck > -------------- next part -------------- An HTML attachment was scrubbed... URL: From s_sourceforge at nedprod.com Mon Dec 17 04:25:59 2012 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Sun, 16 Dec 2012 22:25:59 -0500 Subject: [C++-sig] RAII for the GIL in Boost.Python? In-Reply-To: References: Message-ID: <50CE90C7.2332.3DFE2850@s_sourceforge.nedprod.com> On 16 Dec 2012 at 20:37, John Zwinck wrote: > There are many questions and example classes online for dealing with > Python's Global Interpreter Lock (GIL) using RAII. I think Boost.Python > should provide this functionality, rather than having users who build > hybrid C++/Python applications copy-pasting code from various places. I > was one of those users recently, and the code I developed to deal with > it is now public: > > https://github.com/jzwinck/pccl/blob/master/InterpreterLockGuard.hpp > https://github.com/jzwinck/pccl/blob/master/InterpreterLockGuard.cpp > https://github.com/jzwinck/pccl/blob/master/test/InterpreterLockGuard.test.cpp > [snip] > I welcome any feedback regarding the suitability for inclusion in Boost > of this particular code and/or the concepts it implements. Firstly, thanks for taking the time to write and submit this. So please don't take what I'm about to say too negatively. GIL (un)locking isn't just a case of a quick RAII holder and you're done as your code has implemented. If that were the case, one of us would have submitted such a thing years ago. There are very, very good reasons why we have deliberately foresworn a GIL solution, and that's because getting it "right" probably means a refactoring of much of the BPL design. I won't go into too much detail here (search this list's archives), but in short there isn't just GIL management but also interpreter management, and on top of that both of those have to work right as exception throws happen plus policies must be instituted for things like container iterators (e.g. do you release GIL each iteration, every 10 iterations etc) and policies for multiple interpreter interactions (e.g. do we timeslice interpreters, or wait for one to go to i/o sleep first?). In short, the code is complex and error prone. I know, as I wrote the only full BPL GIL management implementation I know of and no one has seen fit to attempt to port it to Boost yet, and I would assume no one ever will. Now, I've had some discussion with Dave about the future of BPL and how it relates to some related infrastructure I'm currently prototyping, but I have to admit I don't think it likely that this infrastructure will get greenlit (we'll know before Spring 2013) and as a result, someone else will have to acquire the resources to get something done for a more permanent solution. Even if greenlit, we're talking 2014 easily before any open source release could be possible. So, if you can't wait before then, feel free to dive in and implement a full solution. Start here: https://github.com/ned14/tnfox/blob/master/Python/FXPython.h https://github.com/ned14/tnfox/blob/master/Python/FXPython.cxx And you'll need to modernise these patches to Boost: https://github.com/ned14/tnfox/blob/master/Python/BoostPatches.zip You'll see a similar, but much more complex, RAII system in there. Good luck! Niall -- Any opinions or advice expressed here do NOT reflect those of my employer Research In Motion Inc. Work Portfolio: http://careers.stackoverflow.com/nialldouglas/ -------------- next part -------------- A non-text attachment was scrubbed... Name: SMime.p7s Type: application/x-pkcs7-signature Size: 6061 bytes Desc: not available URL: From greg.landrum at gmail.com Mon Dec 17 06:27:43 2012 From: greg.landrum at gmail.com (Greg Landrum) Date: Mon, 17 Dec 2012 06:27:43 +0100 Subject: [C++-sig] outdated info in the "projects using boost.python" docs In-Reply-To: References: Message-ID: Thanks Ralf. On Sat, Dec 15, 2012 at 2:28 AM, Ralf Grosse-Kunstleve wrote: > Thanks, I've updated the web page. I simply took this from the RDkit page: > > A collection of cheminformatics and machine-learning software written in > C++ and Python. > > > On Fri, Nov 23, 2012 at 2:26 AM, Greg Landrum wrote: > >> Hi, >> >> A colleague pointed out to me that the "Project using Boost.Python" >> document ( >> http://www.boost.org/doc/libs/1_52_0/libs/python/doc/projects.html) >> includes a link to Rational Discovery LLC, a company that no longer >> exists (we shut it down in 2006). That link, which now re-directs to >> some completely different organization) should probably be removed. >> >> The software we developed that uses boost.python to expose chemistry >> functionality in C++ to Python has been open-sourced as the rdkit >> (www.rdkit.org). If there's any interest in new additions to the >> "projects using boost.python" page, I would be happy to provide a >> blurb. >> >> Thanks, >> -greg >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From diepen at astron.nl Mon Dec 17 08:27:02 2012 From: diepen at astron.nl (Ger van Diepen) Date: Mon, 17 Dec 2012 08:27:02 +0100 Subject: [C++-sig] outdated info in the "projects using boost.python" docs In-Reply-To: References: Message-ID: <50CED756020000A90000E55C@smtp.nfra.nl> Hello Ralf, Could you add pyrap (pyrap.googlecode.com) to the list under Scientific Programming? Pyrap is the python interface to the Radio-Astronomical Package casacore (casacore.googlecode.com). Astronomers love pyrap because it makes it easily possible to get their data (observed with radio-astronomical telescopes like LOFAR, ASKAP, and eVLA) in numpy arrays and do basic data inspection and manipulation using the many python packages that are available. Boost.Python made it quite easily possible to create converters for the various data types, also for numpy arrays and individual elements of a numpy array. It's nice they work fully recursively. Mapping C++ functions to Python was straightforward. Cheers, Ger van Diepen >>> Ralf Grosse-Kunstleve 12/15/2012 2:28 AM >>> Thanks, I've updated the web page. I simply took this from the RDkit page: A collection of cheminformatics and machine-learning software written in C++ and Python. On Fri, Nov 23, 2012 at 2:26 AM, Greg Landrum wrote: Hi A colleague pointed out to me that the "Project using Boost.Python" document (http://www.boost.org/doc/libs/1_52_0/libs/python/doc/projects.html) includes a link to Rational Discovery LLC, a company that no longer exists (we shut it down in 2006). That link, which now re-directs to some completely different organization) should probably be removed. The software we developed that uses boost.python to expose chemistry functionality in C++ to Python has been open-sourced as the rdkit (www.rdkit.org). If there's any interest in new additions to the "projects using boost.python" page, I would be happy to provide a blurb. Thanks, -greg _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Tue Dec 18 19:02:03 2012 From: hapopen at gmail.com (simon zhang) Date: Wed, 19 Dec 2012 02:02:03 +0800 Subject: [C++-sig] How to find an object from tuple Message-ID: I wanted to know a way to find an object from tuple.The tuple maybe set in python.The flowing is sample. ####################################################################### #include using namespace boost::python; class A { public: A() { M=make_tule("ab","cd","1234"); } bool find(std::string ss) { //How to find an object from tuple "M" and return "true" or "false",. The function "find" would work in c++. } tuple M; } BOOST_PYTHON_MODULE(ctopy) { class_ ("A",init<>()) .add_property("M",&A::M) ; } ####################################################################### -------------- next part -------------- An HTML attachment was scrubbed... URL: From hesam.ilati2 at gmail.com Thu Dec 20 05:47:11 2012 From: hesam.ilati2 at gmail.com (Hesam) Date: Wed, 19 Dec 2012 23:47:11 -0500 Subject: [C++-sig] Boost-Python Linking Problem with Hello World Example Message-ID: Hello, I am trying to compile and link boost-python hello world example and I have some linking problems. OS: Ubuntu g++ -fPIC -w Test2.cpp -I ../../../Libs/Python/Python-2.7.3/Include -I ../../../Libs/Python/Python-2.7.3 -I ../../../Libs/Boost/boost_1_52_0 -Wl,-rpath,../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -L -L../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -lssl -lcrypto -lpthread -lm -lutil -lpython2.7 -Wl,-rpath, -L../../../Libs/Boost/boost_1_52_0/lib -L../../../Libs/Boost/boost_1_52_0/stage/lib -lboost_python I get the following *Error* ../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_AsWideChar' ../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_FromEncodedObject' collect2: ld returned 1 exit status make: *** [Test2] Error 1 I heard that it maybe due to incompatible version of python and the python library used by Boost-python. But whatever I tried I couldn't solve the problem. It is taking several days and no success. Appreciate any comment. Thanks Hesam -------------- next part -------------- An HTML attachment was scrubbed... URL: From jennolsen84 at gmail.com Thu Dec 20 05:54:05 2012 From: jennolsen84 at gmail.com (Jennifer Olsen) Date: Wed, 19 Dec 2012 22:54:05 -0600 Subject: [C++-sig] Unwrapping a boost::function from boost::python::object with extract Message-ID: How can I call C++ module functions from C++ but select the function from python? See example below I know I can setup a map of strings manually, and select the function I want to run, but I'd like a cleaner python solution. I would also like it to be very efficient, hopefully by basically unwrapping the boost::python::object and extracting the boost::function<> out of it. Here is the python file ----------------------- import mymodule #this is a C++ module I wrote t = mymodule .TestClass() t.Reference() # time invoking a boost::function 100k times t.CallSomeClassMemberFunction(t.a) # instead of choosing a function directly, i want to pass in which function I want ----------------------- C++ module code (compiles, but does not run.. you can see my intent): #include #include #include #include struct TestClass { TestClass() { } int a() const { return rand()%4; } int b() const { return rand()%64; } template void BenchMark(T & aUnknownFunction) { timeval begin, end; long long sum(0); gettimeofday(&begin, NULL); __asm__ __volatile__ ("cpuid"); for (int x(0); x!=100000; ++x) sum += aUnknownFunction(); __asm__ __volatile__ ("cpuid"); gettimeofday(&end, NULL); std::cout << "Time: " << 1000000 * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) << std::endl; srand(sum); } void CallSomeClassMemberFunction(boost::python::object &aTest) { boost::function unknownFunction = boost::python::extract >(aTest) ; // i don't think this works as I want BenchMark( unknownFunction ); } void Reference() { boost::function unknownFunction = boost::bind(&TestClass::a, this); BenchMark( unknownFunction ); } }; BOOST_PYTHON_MODULE(mymodule) { boost::python::class_("TestClass") .def("a",&TestClass::a) .def("b",&TestClass::b) .def("CallSomeClassMemberFunction",&TestClass::CallSomeClassMemberFunction) .def("Reference",&TestClass::Reference) ; } ---- The idea is to do this efficiently. I have been able to successfully repeatedly evaluate the boost::python::object, but since I am doing it a lot, i'd like to avoid paying the overhead every time. I also posted this on stackoverflow, if you are interested in replying there: http://stackoverflow.com/questions/13961486/unwrapping-a-boostfunction-from-boostpythonobject-with-extract Thanks in advance From anders.e.e.wallin at gmail.com Thu Dec 20 06:58:27 2012 From: anders.e.e.wallin at gmail.com (Anders Wallin) Date: Thu, 20 Dec 2012 07:58:27 +0200 Subject: [C++-sig] Boost-Python Linking Problem with Hello World Example In-Reply-To: References: Message-ID: It's hard to get the includes/libs right by hand on the command line. Better to use a build-tool like bjam, cmake, or something. Can you try this cmake build: https://github.com/aewallin/sandbox/tree/master/boost_python_helloworld cmake . and then make if you want to see the actual command-line cmake produces I think you can see it with make VERBOSE=1. Ubuntu 12.10 has boost 1.49.0 by default, so it looks like you are using a DIY-build of boost? That may be the cause also. Anders On Thu, Dec 20, 2012 at 6:47 AM, Hesam wrote: > Hello, > > I am trying to compile and link boost-python hello world example and I > have some linking problems. > > OS: Ubuntu > > g++ -fPIC -w Test2.cpp -I ../../../Libs/Python/Python-2.7.3/Include -I ../../../Libs/Python/Python-2.7.3 -I ../../../Libs/Boost/boost_1_52_0 -Wl,-rpath,../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -L -L../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -lssl -lcrypto -lpthread -lm -lutil -lpython2.7 -Wl,-rpath, -L../../../Libs/Boost/boost_1_52_0/lib -L../../../Libs/Boost/boost_1_52_0/stage/lib -lboost_python > > I get the following *Error* > > ../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_AsWideChar' > ../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_FromEncodedObject' > collect2: ld returned 1 exit status > make: *** [Test2] Error 1 > > I heard that it maybe due to incompatible version of python and the python > library used by Boost-python. But whatever I tried I couldn't solve the > problem. It is taking several days and no success. Appreciate any comment. > > Thanks > > Hesam > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From coutinhotiago at gmail.com Thu Dec 20 11:53:03 2012 From: coutinhotiago at gmail.com (Tiago Coutinho) Date: Thu, 20 Dec 2012 11:53:03 +0100 Subject: [C++-sig] ownership of C++ object extended in Python Message-ID: Hello all, I want to export a class 'A' from an external library to python. In python I need to extend the class and I need to tell the library that it will 'own' the object. I have tried the example in: http://wiki.python.org/moin/boost.python/HowTo (chapter "ownership of C++ object") And executing the code I send in attachment I get: $ python test1.py Traceback (most recent call last): File "test1.py", line 19, in main() File "test1.py", line 15, in main print(a.get_int()) Boost.Python.ArgumentError: Python argument types in A.get_int(B) did not match C++ signature: get_int(A {lvalue}) Can you tell me what I am missing in the code? Thank you in advance PS: Sorry quality of the Makefile. Regards Tiago -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Makefile Type: application/octet-stream Size: 484 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: test1.cpp Type: text/x-c++src Size: 915 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: test1.py Type: application/octet-stream Size: 192 bytes Desc: not available URL: From jaedyn.cppsig at jaedyn.co Thu Dec 20 12:45:29 2012 From: jaedyn.cppsig at jaedyn.co (Jaedyn K. Draper) Date: Thu, 20 Dec 2012 05:45:29 -0600 Subject: [C++-sig] Boost-Python Linking Problem with Hello World Example In-Reply-To: References: Message-ID: <50D2F926.90807@jaedyn.co> An HTML attachment was scrubbed... URL: From hapopen at gmail.com Fri Dec 21 12:52:28 2012 From: hapopen at gmail.com (simon zhang) Date: Fri, 21 Dec 2012 19:52:28 +0800 Subject: [C++-sig] How to converter std::string* in boost.python? Message-ID: How to converter std::string* in boost.python?I have to handle some data of c++ in python.The data may be big.So I return a pointer to python. But there are some errors. c++ #include #include class A {public: A() { data="342342fgsf"; ss=&data; } std::string *ss; std::string data;}; BOOST_PYTHON_MODULE(ctopy){ using namespace boost::python; class_ ("A",init<>()) .add_property(ss,&A::ss) ;} python import ctopy t1=ctopy.A() print t1.ss #error.TypeError:No to_python (by-value) converter found for c++ type:std::string* -------------- next part -------------- An HTML attachment was scrubbed... URL: From talljimbo at gmail.com Fri Dec 21 19:50:59 2012 From: talljimbo at gmail.com (Jim Bosch) Date: Fri, 21 Dec 2012 10:50:59 -0800 Subject: [C++-sig] How to converter std::string* in boost.python? In-Reply-To: References: Message-ID: <50D4AF93.8030308@gmail.com> On 12/21/2012 03:52 AM, simon zhang wrote: > How to converter std::string* in boost.python?I have to handle some data > of c++ in python.The data may be big.So I return a pointer to python. > But there are some errors. > If the data is big, and you really want to avoid a deep copy, the only way to use it is if you manually allocate the memory as a Python str object using the Python C API or return it as something else (like a NumPy array or buffer object, or a custom Boost.Python-wrapped class) that can hold C++-allocated memory. A Python str object always owns its own memory, and so does std::string, so you can't convert from one to the other without a deep copy. Jim From christophe.tornieri at cm-labs.com Fri Dec 21 22:40:40 2012 From: christophe.tornieri at cm-labs.com (Christophe Tornieri) Date: Fri, 21 Dec 2012 16:40:40 -0500 Subject: [C++-sig] Exposing function at run-time Message-ID: Hi, class Base { } -- Christophe Tornieri Tech Lead, Software Architect Sub-sea division christophe.tornieri at cm-labs.com http://www.vxsim.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From christophe.tornieri at cm-labs.com Fri Dec 21 23:06:00 2012 From: christophe.tornieri at cm-labs.com (Christophe Tornieri) Date: Fri, 21 Dec 2012 17:06:00 -0500 Subject: [C++-sig] Adding method at run-time to class instances Message-ID: Hi, Is-it possible to add methods to a class instance exposed in Python ? I mean at the object level. I have a class that is wrapped through another class. When an instance of this wrapper class is build at run time (through a Python call), I would like to expose some new methods. In my case, the wrapped class contains a list of properties that is filled at construction. For each of this property, I wanted to expose a new property on the wrapper class in python. Christophe -- Christophe Tornieri Tech Lead, Software Architect Sub-sea division christophe.tornieri at cm-labs.com http://www.vxsim.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From christophe.tornieri at cm-labs.com Fri Dec 21 23:16:04 2012 From: christophe.tornieri at cm-labs.com (Christophe Tornieri) Date: Fri, 21 Dec 2012 17:16:04 -0500 Subject: [C++-sig] Adding method at run-time to class instances Message-ID: Here is some code. The main idea is that I don't want to expose each derived module classes but only the interface IModule that contains all the necessary information at run-time. The following code is actually working but produces the following warning: to-Python converter for class ModuleConfiguration already registered; second conversion method ignored This sounds normal as the class is exposed each time a new instance of MyModule or MySecondModule is created. // Dummy class for test class IModule : public Vx::VxInputOutputInterface { public: IModule() {} }; class ModuleConfiguration { public: ModuleConfiguration(IModule& iProxy); IModule& mProxy; }; template struct GetParameterFunctor; template<> struct GetParameterFunctor { GetParameterFunctor(const std::string& iName) : mName(iName) { } int operator()(ModuleConfiguration* iProxy) { return iProxy->mProxy.getParameter(mName).getFieldBase().toInteger(); } std::string mName; }; template struct SetParameterFunctor { SetParameterFunctor(const std::string& iName) : mName(iName) { } int operator()(ModuleConfiguration* iProxy, T iValue) { return iProxy->mProxy.getParameter(mName).setValue(iValue); } std::string mName; }; namespace boostVx { namespace python { namespace detail { boostVx::mpl::vector get_signature(boostVx::function, ModuleConfiguration*) { return boostVx::mpl::vector(); } boostVx::mpl::vector get_signature(boostVx::function, ModuleConfiguration*) { return boostVx::mpl::vector(); } } } } class CreateGetParameterFieldVisitor : public VxData::FieldVisitor { public: CreateGetParameterFieldVisitor(boostVx::python::class_& iPythonConfigurationWrapper) : mPythonConfigurationWrapper(iPythonConfigurationWrapper) {} virtual void visit(VxData::Field& i_field) { GetParameterFunctor getP(i_field.getID().asString()); SetParameterFunctor setP(i_field.getID().asString()); mPythonConfigurationWrapper.add_property(i_field.getID().asString(), boostVx::function(getP), boostVx::function(setP)); } private: boostVx::python::class_& mPythonConfigurationWrapper; }; ModuleConfiguration::ModuleConfiguration(IModule& iProxy) : mProxy(iProxy) { boostVx::python::class_ pythonConfigurationWrapper("ModuleConfiguration", boostVx::python::no_init); CreateGetParameterFieldVisitor visitor(pythonConfigurationWrapper); const_cast(mProxy.getParameterContainer()).accept(visitor); } class ModuleWrapperInterface { public: ModuleWrapperInterface(IModule* iProxy) : mProxy(iProxy) , mConfiguration(*iProxy) { } ModuleConfiguration& getModuleConfiguration() { return mConfiguration; } private: IModule* mProxy; ModuleConfiguration mConfiguration; }; class MyModule : public IModule { public: MyModule() : mThreshold(5, "threshold", this) , mCeil(10, "ceil", this) { } const char* getClassName() const { return "MyModule"; } private: Vx::VxParameter mThreshold; Vx::VxParameter mCeil; }; class MySecondModule : public IModule { public: MySecondModule() : mTorque(120, "torque", this) { } const char* getClassName() const { return "MySecondModule"; } private: Vx::VxParameter mTorque; }; boostVx::shared_ptr createModule(const std::string& iName) { if( iName == "first" ) return boostVx::shared_ptr(new ModuleWrapperInterface(new MyModule()), NullDeleter()); else if( iName == "second" ) return boostVx::shared_ptr(new ModuleWrapperInterface(new MySecondModule()), NullDeleter()); else { std::cerr << "No module with name " << iName << " found, returning MyModule." << std::endl; return boostVx::shared_ptr(new ModuleWrapperInterface(new MyModule()), NullDeleter()); } } BOOST_PYTHON_MODULE(Modules) { boostVx::python::class_, boostVx::noncopyable>("ModuleInterface", boostVx::python::no_init) .def("getParameters", &ModuleWrapperInterface::getModuleConfiguration, boostVx::python::return_internal_reference<>() ); boostVx::python::def("createModule", &createModule); } -- Christophe Tornieri Tech Lead, Software Architect Sub-sea division christophe.tornieri at cm-labs.com http://www.vxsim.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sat Dec 22 23:45:31 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 23 Dec 2012 06:45:31 +0800 Subject: [C++-sig] Trouble destructor, call python's object with pointer in boost.python Message-ID: when I call python's function with pointer as an argument in boost.python, there are some troubles in destructor. The following is a sample code #include #include #include #include #include using namespace boost::python; class A {public: A() { std::cout<< "A start"< ("A",init<>()) ; class_ ("B",init<>()) .def("run",&B::run) ;} python: import ctopyclass tc: def fun1(self,tt): print "fun1" def fun2(self,tt): print "fun2" bb=ctopy.D() bb.run(tc) this result: A start fun1 A end fun2 A end A end note: The "A end" has been printed three.I try it in "valgrind"?there are some errors.I just want to run the destructor once.How to do? -------------- next part -------------- An HTML attachment was scrubbed... URL: From jaedyn.cppsig at jaedyn.co Sat Dec 22 23:49:04 2012 From: jaedyn.cppsig at jaedyn.co (Jaedyn K. Draper) Date: Sat, 22 Dec 2012 16:49:04 -0600 Subject: [C++-sig] Trouble destructor, call python's object with pointer in boost.python In-Reply-To: References: Message-ID: <50D638E0.9040600@jaedyn.co> If you pass it to a function like that, it passes it by value even if it's a pointer. So it's calling the copy constructor (which you haven't defined so it isn't printing "A start"), rather than the regular constructor, and then destructing it. To pass an actual pointer rather than sending it by value, you need to do it like this: | B() { aa=new A; } ~B() { delete aa; } void run(object ct) { _obj=ct(); //creat a python object _obj.attr("fun1")(ptr(aa)); //call a function named "fun1" with a pointer arg _obj.attr("fun2")(ptr(aa)); //call a function named "fun2" with a pointer arg } A*aa; object _obj;| Note that both "aa" objects are wrapped by a call to "ptr()" Just be careful with the python when doing this, as it can cause a crash if python tries to access that object after its C++ lifetime has ended. So always make sure the python object's lifetime ends before the C++ object's does. On 12/22/2012 4:45 PM, simon zhang wrote: > when I call python's function with pointer as an argument in > boost.python, there are some troubles in destructor. The following is > a sample code > > |#include > #include > #include > #include > #include > using namespace boost::python; > > class A{ > public: > A() { > std::cout<< "A start"< } > ~A() {std::cout<< "A end" < } > class B{ > public: > B() { aa=new A > ; } > ~B() { delete > aa; } > void run(object ct) { > _obj=ct(); //creat a python object > _obj.attr("fun1")(aa); //call a function named "fun1" with a pointer arg > _obj.attr("fun2")(aa); //call a function named "fun2" with a pointer arg > } > A*aa; > object _obj; > } > > BOOST_PYTHON_MODULE(ctopy) > { > class_ ("A",init<>()) > ; > class_ ("B",init<>()) > .def("run",&B::run) > ; > }| > > python: > |import ctopy > class tc: > def fun1(self,tt): > print"fun1" > def fun2(self,tt): > print"fun2" > bb=ctopy.D() > bb.run(tc)| > this result: > |A start > fun1 > A end > fun2 > A end > A end| > > > note: > > The "A end" has been printed three.I try it in "valgrind",there are > some errors.I just want to run the destructor once.How to do? > > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sun Dec 23 00:08:08 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 23 Dec 2012 07:08:08 +0800 Subject: [C++-sig] Trouble destructor, call python's object with pointer in boost.python In-Reply-To: <50D638E0.9040600@jaedyn.co> References: <50D638E0.9040600@jaedyn.co> Message-ID: You are right.Thank you. 2012/12/23 Jaedyn K. Draper > If you pass it to a function like that, it passes it by value even if > it's a pointer. So it's calling the copy constructor (which you haven't > defined so it isn't printing "A start"), rather than the regular > constructor, and then destructing it. > > To pass an actual pointer rather than sending it by value, you need to do > it like this: > > B() { aa=new A; } > ~B() { delete > > aa; } > void run(object ct) { > _obj=ct(); //creat a python object > > _obj.attr("fun1")(ptr(aa)); //call a function named "fun1" with a pointer arg > _obj.attr("fun2")(ptr(aa)); //call a function named "fun2" with a pointer arg > } > A *aa; > object _obj; > > > Note that both "aa" objects are wrapped by a call to "ptr()" > > Just be careful with the python when doing this, as it can cause a crash > if python tries to access that object after its C++ lifetime has ended. So > always make sure the python object's lifetime ends before the C++ object's > does. > > > On 12/22/2012 4:45 PM, simon zhang wrote: > > when I call python's function with pointer as an argument in boost.python, > there are some troubles in destructor. The following is a sample code > > #include #include #include #include #include using namespace boost::python; > class A {public: > A() { > std::cout<< "A start"< } > ~A() {std::cout<< "A end" < B() { aa=new A > > ; } > ~B() { delete > > aa; } > void run(object ct) { > _obj=ct(); //creat a python object > _obj.attr("fun1")(aa); //call a function named "fun1" with a pointer arg > _obj.attr("fun2")(aa); //call a function named "fun2" with a pointer arg > } > A *aa; > object _obj;} > > BOOST_PYTHON_MODULE(ctopy){ > class_ ("A",init<>()) > ; > class_ ("B",init<>()) > .def("run",&B::run) > ;} > > > python: > > import ctopyclass tc: > def fun1(self,tt): > print "fun1" > def fun2(self,tt): > print "fun2" > bb=ctopy.D() > bb.run(tc) > > this result: > > A start > fun1 > A end > fun2 > A end > A end > > note: > > The "A end" has been printed three.I try it in "valgrind"?there are some > errors.I just want to run the destructor once.How to do? > > > > _______________________________________________ > Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jaedyn.cppsig at jaedyn.co Sun Dec 23 00:12:08 2012 From: jaedyn.cppsig at jaedyn.co (Jaedyn K. Draper) Date: Sat, 22 Dec 2012 17:12:08 -0600 Subject: [C++-sig] Trouble destructor, call python's object with pointer in boost.python In-Reply-To: References: <50D638E0.9040600@jaedyn.co> Message-ID: <50D63E48.7060100@jaedyn.co> No problem! On 12/22/2012 5:08 PM, simon zhang wrote: > You are right.Thank you. > > 2012/12/23 Jaedyn K. Draper > > > If you pass it to a function like that, it passes it by value even > if it's a pointer. So it's calling the copy constructor (which you > haven't defined so it isn't printing "A start"), rather than the > regular constructor, and then destructing it. > > To pass an actual pointer rather than sending it by value, you > need to do it like this: > > | > B(){aa=newA;}~B(){deleteaa;}voidrun(object ct){_obj=ct();//creat a > python object > > > _obj.attr("fun1")(ptr(aa)); //call a function named "fun1" with a pointer arg > _obj.attr("fun2")(ptr(aa)); //call a function named "fun2" with a pointer arg > } > A*aa; > object _obj;| > > > Note that both "aa" objects are wrapped by a call to "ptr()" > > Just be careful with the python when doing this, as it can cause a > crash if python tries to access that object after its C++ lifetime > has ended. So always make sure the python object's lifetime ends > before the C++ object's does. > > > On 12/22/2012 4:45 PM, simon zhang wrote: >> when I call python's function with pointer as an argument in >> boost.python, there are some troubles in destructor. The >> following is a sample code >> >> |#include >> #include >> #include >> #include >> #include >> using namespace boost::python; >> >> class A{ >> public: >> A() { >> std::cout<< "A start"<> } >> ~A() {std::cout<< "A end" <> } >> class B{ >> public: >> B() { aa=new A >> >> ; } >> ~B() { delete >> >> aa; } >> void run(object ct) { >> _obj=ct(); //creat a python object >> _obj.attr("fun1")(aa); //call a function named "fun1" with a pointer arg >> _obj.attr("fun2")(aa); //call a function named "fun2" with a pointer arg >> } >> A*aa; >> object _obj; >> } >> >> BOOST_PYTHON_MODULE(ctopy) >> { >> class_ ("A",init<>()) >> ; >> class_ ("B",init<>()) >> .def("run",&B::run) >> ; >> }| >> >> python: >> |import ctopy >> class tc: >> def fun1(self,tt): >> print"fun1" >> def fun2(self,tt): >> print"fun2" >> bb=ctopy.D() >> bb.run(tc)| >> this result: >> |A start >> fun1 >> A end >> fun2 >> A end >> A end| >> >> >> note: >> >> The "A end" has been printed three.I try it in "valgrind",there >> are some errors.I just want to run the destructor once.How to do? >> >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sun Dec 23 08:44:17 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 23 Dec 2012 15:44:17 +0800 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop Message-ID: I have make a boost.python module with an endless loop.But I can't kill the process by ctrl-c.The following is an example. C++ #include #include #include #include usring namespace boost::python; void foo() { int it=0; while (true) { //endless loop ++it; std::cout<< it < From jaedyn.cppsig at jaedyn.co Sun Dec 23 08:48:53 2012 From: jaedyn.cppsig at jaedyn.co (Jaedyn K. Draper) Date: Sun, 23 Dec 2012 01:48:53 -0600 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: References: Message-ID: <50D6B765.5020104@jaedyn.co> Instead of Py_Initialize() (wherever it is you call it), try calling Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal handlers (including SIGINT) to send python exceptions instead of killing the process. This may be what's hitting you. On 12/23/2012 1:44 AM, simon zhang wrote: > I have make a boost.python module with an endless loop.But I can't > kill the process by ctrl-c.The following is an example. > > C++ > > |#include > #include > #include > #include > usringnamespace boost::python; > > void foo() { > int it=0; > while (true) { //endless loop > ++it; > std::cout<< it< sleep(3); > } > } > > BOOST_PYTHON_MODULE(ctopy) > { > def("foo",foo); > }| > > python: > > |import ctopy > ctopy.foo()| > > result: > > |1 > 2 > 3 > 4 > .....................| > > I can't kill the foreground process by Ctrl-c.why the module don't > accept signal "SIGINT" that was sent by Ctrl-c.How to make it work. > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sun Dec 23 08:59:07 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 23 Dec 2012 15:59:07 +0800 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: <50D6B765.5020104@jaedyn.co> References: <50D6B765.5020104@jaedyn.co> Message-ID: But I don't call Py_Initialize().I call C++ code in Python.Don't embed the Python to C++... 2012/12/23 Jaedyn K. Draper > Instead of Py_Initialize() (wherever it is you call it), try calling > Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal > handlers (including SIGINT) to send python exceptions instead of killing > the process. This may be what's hitting you. > > > On 12/23/2012 1:44 AM, simon zhang wrote: > > I have make a boost.python module with an endless loop.But I can't kill > the process by ctrl-c.The following is an example. > > C++ > > #include #include module.hpp>#include def.hpp>#include > usring namespace boost::python; > void foo() { > int it=0; > while (true) { //endless loop > ++it; > std::cout<< it < sleep(3); > }} > > BOOST_PYTHON_MODULE(ctopy){ > def("foo",foo);} > > python: > > import ctopy > ctopy.foo() > > result: > > 1234..................... > > I can't kill the foreground process by Ctrl-c.why the module don't accept > signal "SIGINT" that was sent by Ctrl-c.How to make it work. > > > _______________________________________________ > Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jaedyn.cppsig at jaedyn.co Sun Dec 23 08:58:21 2012 From: jaedyn.cppsig at jaedyn.co (Jaedyn K. Draper) Date: Sun, 23 Dec 2012 01:58:21 -0600 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: References: <50D6B765.5020104@jaedyn.co> Message-ID: <50D6B99D.5020808@jaedyn.co> Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe someone else can help. :( On 12/23/2012 1:59 AM, simon zhang wrote: > But I don't call Py_Initialize().I call C++ code in > Python.Don't embed the Python to C++... > > 2012/12/23 Jaedyn K. Draper > > > Instead of Py_Initialize() (wherever it is you call it), try > calling Py_InitializeEx(0). Py_Initialize() (or > Py_InitializeEx(1)) binds signal handlers (including SIGINT) to > send python exceptions instead of killing the process. This may be > what's hitting you. > > > On 12/23/2012 1:44 AM, simon zhang wrote: >> I have make a boost.python module with an endless loop.But I >> can't kill the process by ctrl-c.The following is an example. >> >> C++ >> >> |#include >> #include > module.hpp> >> #include > def.hpp> >> #include >> usringnamespace boost::python; >> >> void foo() { >> int it=0; >> while (true) { //endless loop >> ++it; >> std::cout<< it<> sleep(3); >> } >> } >> >> BOOST_PYTHON_MODULE(ctopy) >> { >> def("foo",foo); >> }| >> >> python: >> >> |import ctopy >> ctopy.foo()| >> >> result: >> >> |1 >> 2 >> 3 >> 4 >> .....................| >> >> I can't kill the foreground process by Ctrl-c.why the module >> don't accept signal "SIGINT" that was sent by Ctrl-c.How to make >> it work. >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sun Dec 23 09:06:16 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 23 Dec 2012 16:06:16 +0800 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: <50D6B99D.5020808@jaedyn.co> References: <50D6B765.5020104@jaedyn.co> <50D6B99D.5020808@jaedyn.co> Message-ID: I do embedded and extended.Anyway, thank you for your reply. :) This boost.python document is so complex. :( 2012/12/23 Jaedyn K. Draper > Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe > someone else can help. :( > > > On 12/23/2012 1:59 AM, simon zhang wrote: > > But I don't call Py_Initialize().I call C++ code in Python.Don't embed > the Python to C++... > > 2012/12/23 Jaedyn K. Draper > >> Instead of Py_Initialize() (wherever it is you call it), try calling >> Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal >> handlers (including SIGINT) to send python exceptions instead of killing >> the process. This may be what's hitting you. >> >> >> On 12/23/2012 1:44 AM, simon zhang wrote: >> >> I have make a boost.python module with an endless loop.But I can't kill >> the process by ctrl-c.The following is an example. >> >> C++ >> >> #include #include > module.hpp>#include > def.hpp>#include >> usring namespace boost::python; >> void foo() { >> int it=0; >> while (true) { //endless loop >> ++it; >> std::cout<< it <> sleep(3); >> }} >> >> BOOST_PYTHON_MODULE(ctopy){ >> def("foo",foo);} >> >> python: >> >> import ctopy >> ctopy.foo() >> >> result: >> >> 1234..................... >> >> I can't kill the foreground process by Ctrl-c.why the module don't >> accept signal "SIGINT" that was sent by Ctrl-c.How to make it work. >> >> >> _______________________________________________ >> Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> > > > > _______________________________________________ > Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sun Dec 23 09:16:20 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 23 Dec 2012 16:16:20 +0800 Subject: [C++-sig] How to converter std::string* in boost.python? In-Reply-To: <50D4AF93.8030308@gmail.com> References: <50D4AF93.8030308@gmail.com> Message-ID: ..so,It seems I can only convert std::string and make a deep copy.I can only avoid it as much as possible.Thank you. 2012/12/22 Jim Bosch > On 12/21/2012 03:52 AM, simon zhang wrote: > >> How to converter std::string* in boost.python?I have to handle some data >> of c++ in python.The data may be big.So I return a pointer to python. >> But there are some errors. >> >> > If the data is big, and you really want to avoid a deep copy, the only way > to use it is if you manually allocate the memory as a Python str object > using the Python C API or return it as something else (like a NumPy array > or buffer object, or a custom Boost.Python-wrapped class) that can hold > C++-allocated memory. A Python str object always owns its own memory, and > so does std::string, so you can't convert from one to the other without a > deep copy. > > Jim > > ______________________________**_________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/**mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sun Dec 23 10:02:51 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 23 Dec 2012 17:02:51 +0800 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: <50D6B99D.5020808@jaedyn.co> References: <50D6B765.5020104@jaedyn.co> <50D6B99D.5020808@jaedyn.co> Message-ID: This is the answer from the stackoverflow. while (true) { //endless loop ++it; std::cout<< it < > Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe > someone else can help. :( > > > On 12/23/2012 1:59 AM, simon zhang wrote: > > But I don't call Py_Initialize().I call C++ code in Python.Don't embed > the Python to C++... > > 2012/12/23 Jaedyn K. Draper > >> Instead of Py_Initialize() (wherever it is you call it), try calling >> Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal >> handlers (including SIGINT) to send python exceptions instead of killing >> the process. This may be what's hitting you. >> >> >> On 12/23/2012 1:44 AM, simon zhang wrote: >> >> I have make a boost.python module with an endless loop.But I can't kill >> the process by ctrl-c.The following is an example. >> >> C++ >> >> #include #include > module.hpp>#include > def.hpp>#include >> usring namespace boost::python; >> void foo() { >> int it=0; >> while (true) { //endless loop >> ++it; >> std::cout<< it <> sleep(3); >> }} >> >> BOOST_PYTHON_MODULE(ctopy){ >> def("foo",foo);} >> >> python: >> >> import ctopy >> ctopy.foo() >> >> result: >> >> 1234..................... >> >> I can't kill the foreground process by Ctrl-c.why the module don't >> accept signal "SIGINT" that was sent by Ctrl-c.How to make it work. >> >> >> _______________________________________________ >> Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> > > > > _______________________________________________ > Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jaedyn.cppsig at jaedyn.co Sun Dec 23 10:21:12 2012 From: jaedyn.cppsig at jaedyn.co (Jaedyn K. Draper) Date: Sun, 23 Dec 2012 03:21:12 -0600 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: References: <50D6B765.5020104@jaedyn.co> <50D6B99D.5020808@jaedyn.co> Message-ID: <50D6CD08.9060404@jaedyn.co> I was at least half right, then. Python was eating the signals. :) As an alternative, to prevent yourself from having to do this in every loop in your code, you might try this on the python side: import signal signal.signal(signal.SIGINT, signal.SIG_DFL) That'll stop Python from catching SIGINT globally and will always send the SIGINT down to the C++ and terminate. Of course, that may or may not be an option depending on your specific needs. On 12/23/2012 3:02 AM, simon zhang wrote: > This is the answer from the stackoverflow. > > | while (true) { //endless loop > ++it; > std::cout<< it< sleep(3); > if(PyErr_CheckSignals() == -1) { > exit(1); > } > }| > > 2012/12/23 Jaedyn K. Draper > > > Oh, my mistake. Not sure then, I've only embedded, never extended. > Maybe someone else can help. :( > > > On 12/23/2012 1:59 AM, simon zhang wrote: >> But I don't call Py_Initialize().I call C++ code in >> Python.Don't embed the Python to C++... >> >> 2012/12/23 Jaedyn K. Draper > > >> >> Instead of Py_Initialize() (wherever it is you call it), try >> calling Py_InitializeEx(0). Py_Initialize() (or >> Py_InitializeEx(1)) binds signal handlers (including SIGINT) >> to send python exceptions instead of killing the process. >> This may be what's hitting you. >> >> >> On 12/23/2012 1:44 AM, simon zhang wrote: >>> I have make a boost.python module with an endless loop.But I >>> can't kill the process by ctrl-c.The following is an example. >>> >>> C++ >>> >>> |#include >>> #include >> module.hpp> >>> #include >> def.hpp> >>> #include >>> usringnamespace boost::python; >>> >>> void foo() { >>> int it=0; >>> while (true) { //endless loop >>> ++it; >>> std::cout<< it<>> sleep(3); >>> } >>> } >>> >>> BOOST_PYTHON_MODULE(ctopy) >>> { >>> def("foo",foo); >>> }| >>> >>> python: >>> >>> |import ctopy >>> ctopy.foo()| >>> >>> result: >>> >>> |1 >>> 2 >>> 3 >>> 4 >>> .....................| >>> >>> I can't kill the foreground process by Ctrl-c.why the module >>> don't accept signal "SIGINT" that was sent by Ctrl-c.How to >>> make it work. >>> >>> >>> >>> _______________________________________________ >>> Cplusplus-sig mailing list >>> Cplusplus-sig at python.org >>> http://mail.python.org/mailman/listinfo/cplusplus-sig >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Mon Dec 24 06:41:35 2012 From: hapopen at gmail.com (simon zhang) Date: Mon, 24 Dec 2012 13:41:35 +0800 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: <50D6CD08.9060404@jaedyn.co> References: <50D6B765.5020104@jaedyn.co> <50D6B99D.5020808@jaedyn.co> <50D6CD08.9060404@jaedyn.co> Message-ID: Jaedyn. I would like to discuss another issue, what is a good way to avoid deep copy.There may be a few big data.From c++ to python and from python to c++.Similar from c + + return std::string* to python and python pass the std::string* parameter to c + + function.Maybe it should be similar to smart pointers.Do you have any good suggestions in there? 2012/12/23 Jaedyn K. Draper > I was at least half right, then. Python was eating the signals. :) > > As an alternative, to prevent yourself from having to do this in every > loop in your code, you might try this on the python side: > > import signal > signal.signal(signal.SIGINT, signal.SIG_DFL) > > That'll stop Python from catching SIGINT globally and will always send the > SIGINT down to the C++ and terminate. > > Of course, that may or may not be an option depending on your specific > needs. > > > On 12/23/2012 3:02 AM, simon zhang wrote: > > This is the answer from the stackoverflow. > > while (true) { //endless loop > ++it; > std::cout<< it < sleep(3); > if(PyErr_CheckSignals() == -1) { > exit(1); > } > } > > > 2012/12/23 Jaedyn K. Draper > >> Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe >> someone else can help. :( >> >> >> On 12/23/2012 1:59 AM, simon zhang wrote: >> >> But I don't call Py_Initialize().I call C++ code in Python.Don't embed >> the Python to C++... >> >> 2012/12/23 Jaedyn K. Draper >> >>> Instead of Py_Initialize() (wherever it is you call it), try calling >>> Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal >>> handlers (including SIGINT) to send python exceptions instead of killing >>> the process. This may be what's hitting you. >>> >>> >>> On 12/23/2012 1:44 AM, simon zhang wrote: >>> >>> I have make a boost.python module with an endless loop.But I can't >>> kill the process by ctrl-c.The following is an example. >>> >>> C++ >>> >>> #include #include >> module.hpp>#include >> def.hpp>#include >>> usring namespace boost::python; >>> void foo() { >>> int it=0; >>> while (true) { //endless loop >>> ++it; >>> std::cout<< it <>> sleep(3); >>> }} >>> >>> BOOST_PYTHON_MODULE(ctopy){ >>> def("foo",foo);} >>> >>> python: >>> >>> import ctopy >>> ctopy.foo() >>> >>> result: >>> >>> 1234..................... >>> >>> I can't kill the foreground process by Ctrl-c.why the module don't >>> accept signal "SIGINT" that was sent by Ctrl-c.How to make it work. >>> >>> >>> _______________________________________________ >>> Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig >>> >>> >>> >>> _______________________________________________ >>> Cplusplus-sig mailing list >>> Cplusplus-sig at python.org >>> http://mail.python.org/mailman/listinfo/cplusplus-sig >>> >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> > > > > _______________________________________________ > Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jaedyn.cppsig at jaedyn.co Mon Dec 24 06:56:55 2012 From: jaedyn.cppsig at jaedyn.co (Jaedyn K. Draper) Date: Sun, 23 Dec 2012 23:56:55 -0600 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: References: <50D6B765.5020104@jaedyn.co> <50D6B99D.5020808@jaedyn.co> <50D6CD08.9060404@jaedyn.co> Message-ID: <50D7EEA7.8010107@jaedyn.co> Yeah, I saw your other question about it. There's nothing in boost that I know of to achieve that. The only thing I'd know to suggest is what others have already suggested - encapsulate it in another class and pass that in. But it wouldn't be converted to a python string that way. My question is whether or not you may be trying to optimize prematurely. If your performance needs are strict enough that you need to pass your string as a pointer, might it be better to write your application in C++ entirely, or prototype in python and then convert to C++? You might try passing it as a reference instead of a pointer. I don't know if that'll work or not. But, try having a regular std::string instead of a std::string* and send it using ref(str) (the same way that I suggested you send things using ptr() in your earlier question). Again, I don't know if it'll work, but it might be worth a try. If it's a return value, returning a ref() would cause a local variable to go out of scope. You could try passing the desired string as a reference parameter, rather than trying to return it. Again, I'm not sure if this will work or not, as I've never tried it. If neither of those work, I'd have to play around with it some to see if I could come up with anything else. On 12/23/2012 11:41 PM, simon zhang wrote: > Jaedyn. > > I would like to discuss another issue, what is a good way to avoid > deep copy.There may be a few big data.From c++ to python and from > python to c++.Similar from c + + return std::string* to python and > python pass the std::string* parameter to c + + function.Maybe it > should be similar to smart pointers.Do you have any good suggestions > in there? > > > 2012/12/23 Jaedyn K. Draper > > > I was at least half right, then. Python was eating the signals. :) > > As an alternative, to prevent yourself from having to do this in > every loop in your code, you might try this on the python side: > > import signal > signal.signal(signal.SIGINT, signal.SIG_DFL) > > That'll stop Python from catching SIGINT globally and will always > send the SIGINT down to the C++ and terminate. > > Of course, that may or may not be an option depending on your > specific needs. > > > On 12/23/2012 3:02 AM, simon zhang wrote: >> This is the answer from the stackoverflow. >> >> | while (true) { //endless loop >> ++it; >> std::cout<< it<> sleep(3); >> if(PyErr_CheckSignals() == -1) { >> exit(1); >> } >> }| >> >> 2012/12/23 Jaedyn K. Draper > > >> >> Oh, my mistake. Not sure then, I've only embedded, never >> extended. Maybe someone else can help. :( >> >> >> On 12/23/2012 1:59 AM, simon zhang wrote: >>> But I don't call Py_Initialize().I call C++ code in >>> Python.Don't embed the Python to C++... >>> >>> 2012/12/23 Jaedyn K. Draper >> > >>> >>> Instead of Py_Initialize() (wherever it is you call it), >>> try calling Py_InitializeEx(0). Py_Initialize() (or >>> Py_InitializeEx(1)) binds signal handlers (including >>> SIGINT) to send python exceptions instead of killing the >>> process. This may be what's hitting you. >>> >>> >>> On 12/23/2012 1:44 AM, simon zhang wrote: >>>> I have make a boost.python module with an endless >>>> loop.But I can't kill the process by ctrl-c.The >>>> following is an example. >>>> >>>> C++ >>>> >>>> |#include >>>> #include >>> module.hpp> >>>> #include >>> def.hpp> >>>> #include >>>> usringnamespace boost::python; >>>> >>>> void foo() { >>>> int it=0; >>>> while (true) { //endless loop >>>> ++it; >>>> std::cout<< it<>>> sleep(3); >>>> } >>>> } >>>> >>>> BOOST_PYTHON_MODULE(ctopy) >>>> { >>>> def("foo",foo); >>>> }| >>>> >>>> python: >>>> >>>> |import ctopy >>>> ctopy.foo()| >>>> >>>> result: >>>> >>>> |1 >>>> 2 >>>> 3 >>>> 4 >>>> .....................| >>>> >>>> I can't kill the foreground process by Ctrl-c.why the >>>> module don't accept signal "SIGINT" that was sent by >>>> Ctrl-c.How to make it work. >>>> >>>> >>>> >>>> _______________________________________________ >>>> Cplusplus-sig mailing list >>>> Cplusplus-sig at python.org >>>> http://mail.python.org/mailman/listinfo/cplusplus-sig >>> >>> >>> _______________________________________________ >>> Cplusplus-sig mailing list >>> Cplusplus-sig at python.org >>> http://mail.python.org/mailman/listinfo/cplusplus-sig >>> >>> >>> >>> >>> _______________________________________________ >>> Cplusplus-sig mailing list >>> Cplusplus-sig at python.org >>> http://mail.python.org/mailman/listinfo/cplusplus-sig >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Mon Dec 24 08:27:57 2012 From: hapopen at gmail.com (simon zhang) Date: Mon, 24 Dec 2012 15:27:57 +0800 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: <50D7EEA7.8010107@jaedyn.co> References: <50D6B765.5020104@jaedyn.co> <50D6B99D.5020808@jaedyn.co> <50D6CD08.9060404@jaedyn.co> <50D7EEA7.8010107@jaedyn.co> Message-ID: Because string's memory allocated is different in python and c++.General method should not suited. I have reviewed the project.Maybe you're right, it is premature to optimize the program.At least for now, it should not be a problem. The program will have a c + + interface and a python interface, so I'm doing some complicated. I will use a simple method.The data will be copied. In the future, I will be write a wrapped class. When the c++ interface, it store the data by std :: string.When the python interface,it store the data by the Python str object. And perhaps this is a good way. What do you think? 2012/12/24 Jaedyn K. Draper > Yeah, I saw your other question about it. There's nothing in boost that I > know of to achieve that. The only thing I'd know to suggest is what others > have already suggested - encapsulate it in another class and pass that in. > But it wouldn't be converted to a python string that way. > > My question is whether or not you may be trying to optimize prematurely. > If your performance needs are strict enough that you need to pass your > string as a pointer, might it be better to write your application in C++ > entirely, or prototype in python and then convert to C++? > > You might try passing it as a reference instead of a pointer. I don't know > if that'll work or not. But, try having a regular std::string instead of a > std::string* and send it using ref(str) (the same way that I suggested you > send things using ptr() in your earlier question). Again, I don't know if > it'll work, but it might be worth a try. > > If it's a return value, returning a ref() would cause a local variable to > go out of scope. You could try passing the desired string as a reference > parameter, rather than trying to return it. Again, I'm not sure if this > will work or not, as I've never tried it. > > If neither of those work, I'd have to play around with it some to see if I > could come up with anything else. > > > On 12/23/2012 11:41 PM, simon zhang wrote: > > Jaedyn. > > I would like to discuss another issue, what is a good way to avoid deep > copy.There may be a few big data.From c++ to python and from python to > c++.Similar from c + + return std::string* to python and python pass the > std::string* parameter to c + + function.Maybe it should be similar to > smart pointers.Do you have any good suggestions in there? > > > 2012/12/23 Jaedyn K. Draper > >> I was at least half right, then. Python was eating the signals. :) >> >> As an alternative, to prevent yourself from having to do this in every >> loop in your code, you might try this on the python side: >> >> import signal >> signal.signal(signal.SIGINT, signal.SIG_DFL) >> >> That'll stop Python from catching SIGINT globally and will always send >> the SIGINT down to the C++ and terminate. >> >> Of course, that may or may not be an option depending on your specific >> needs. >> >> >> On 12/23/2012 3:02 AM, simon zhang wrote: >> >> This is the answer from the stackoverflow. >> >> while (true) { //endless loop >> ++it; >> std::cout<< it <> sleep(3); >> if(PyErr_CheckSignals() == -1) { >> exit(1); >> } >> } >> >> >> 2012/12/23 Jaedyn K. Draper >> >>> Oh, my mistake. Not sure then, I've only embedded, never extended. >>> Maybe someone else can help. :( >>> >>> >>> On 12/23/2012 1:59 AM, simon zhang wrote: >>> >>> But I don't call Py_Initialize().I call C++ code in Python.Don't embed >>> the Python to C++... >>> >>> 2012/12/23 Jaedyn K. Draper >>> >>>> Instead of Py_Initialize() (wherever it is you call it), try calling >>>> Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal >>>> handlers (including SIGINT) to send python exceptions instead of killing >>>> the process. This may be what's hitting you. >>>> >>>> >>>> On 12/23/2012 1:44 AM, simon zhang wrote: >>>> >>>> I have make a boost.python module with an endless loop.But I can't >>>> kill the process by ctrl-c.The following is an example. >>>> >>>> C++ >>>> >>>> #include #include >>> module.hpp>#include >>> def.hpp>#include >>>> usring namespace boost::python; >>>> void foo() { >>>> int it=0; >>>> while (true) { //endless loop >>>> ++it; >>>> std::cout<< it <>>> sleep(3); >>>> }} >>>> >>>> BOOST_PYTHON_MODULE(ctopy){ >>>> def("foo",foo);} >>>> >>>> python: >>>> >>>> import ctopy >>>> ctopy.foo() >>>> >>>> result: >>>> >>>> 1234..................... >>>> >>>> I can't kill the foreground process by Ctrl-c.why the module don't >>>> accept signal "SIGINT" that was sent by Ctrl-c.How to make it work. >>>> >>>> >>>> _______________________________________________ >>>> Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig >>>> >>>> >>>> >>>> _______________________________________________ >>>> Cplusplus-sig mailing list >>>> Cplusplus-sig at python.org >>>> http://mail.python.org/mailman/listinfo/cplusplus-sig >>>> >>> >>> >>> >>> _______________________________________________ >>> Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig >>> >>> >>> >>> _______________________________________________ >>> Cplusplus-sig mailing list >>> Cplusplus-sig at python.org >>> http://mail.python.org/mailman/listinfo/cplusplus-sig >>> >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> > > > > _______________________________________________ > Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From s_sourceforge at nedprod.com Tue Dec 25 00:10:06 2012 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Mon, 24 Dec 2012 18:10:06 -0500 Subject: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop In-Reply-To: References: , <50D6B99D.5020808@jaedyn.co>, Message-ID: <50D8E0CE.6552.232E0060@s_sourceforge.nedprod.com> In your C++ you simply need to ensure that Python gets a regular chance to execute in any native loop you execute so it can do its housekeeping. One way is PyErr_CheckSignals(), but that won't do for long running loops, only short ones, because it doesn't do much housekeeping. In my own code I simply called a small function every 1024 loop cycles which instantly returned e.g. if(!(idx++ & 1023)) call_my_null_python_function(); def my_null_python_function: pass This works very well indeed. Niall On 23 Dec 2012 at 17:02, simon zhang wrote: > This is the answer from the stackoverflow. > > while (true) { //endless loop > ++it; > std::cout<< it < sleep(3); > if(PyErr_CheckSignals() == -1) { > exit(1); > } > } > > > 2012/12/23 Jaedyn K. Draper > > > Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe > > someone else can help. :( > > > > > > On 12/23/2012 1:59 AM, simon zhang wrote: > > > > But I don't call Py_Initialize().I call C++ code in Python.Don't embed > > the Python to C++... > > > > 2012/12/23 Jaedyn K. Draper > > > >> Instead of Py_Initialize() (wherever it is you call it), try calling > >> Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal > >> handlers (including SIGINT) to send python exceptions instead of killing > >> the process. This may be what's hitting you. > >> > >> > >> On 12/23/2012 1:44 AM, simon zhang wrote: > >> > >> I have make a boost.python module with an endless loop.But I can't kill > >> the process by ctrl-c.The following is an example. > >> > >> C++ > >> > >> #include #include >> module.hpp>#include >> def.hpp>#include > >> usring namespace boost::python; > >> void foo() { > >> int it=0; > >> while (true) { //endless loop > >> ++it; > >> std::cout<< it < >> sleep(3); > >> }} > >> > >> BOOST_PYTHON_MODULE(ctopy){ > >> def("foo",foo);} > >> > >> python: > >> > >> import ctopy > >> ctopy.foo() > >> > >> result: > >> > >> 1234..................... > >> > >> I can't kill the foreground process by Ctrl-c.why the module don't > >> accept signal "SIGINT" that was sent by Ctrl-c.How to make it work. > >> > >> > >> _______________________________________________ > >> Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig > >> > >> > >> > >> _______________________________________________ > >> Cplusplus-sig mailing list > >> Cplusplus-sig at python.org > >> http://mail.python.org/mailman/listinfo/cplusplus-sig > >> > > > > > > > > _______________________________________________ > > Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig > > > > > > > > _______________________________________________ > > Cplusplus-sig mailing list > > Cplusplus-sig at python.org > > http://mail.python.org/mailman/listinfo/cplusplus-sig > > > -- Any opinions or advice expressed here do NOT reflect those of my employer Research In Motion Inc. Work Portfolio: http://careers.stackoverflow.com/nialldouglas/ -------------- next part -------------- A non-text attachment was scrubbed... Name: SMime.p7s Type: application/x-pkcs7-signature Size: 6061 bytes Desc: not available URL: From s_sourceforge at nedprod.com Tue Dec 25 00:05:55 2012 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Mon, 24 Dec 2012 18:05:55 -0500 Subject: [C++-sig] How to converter std::string* in boost.python? In-Reply-To: References: , <50D4AF93.8030308@gmail.com>, Message-ID: <50D8DFD3.30358.232A2A42@s_sourceforge.nedprod.com> Traditionally, the proper solution to avoid deep copies is to wrap a std::shared_ptr instead of a std::string directly. If your strings are short of course, it may well be faster to leave it as is. std::shared_ptr is not lightweight and may use atomic instructions, the bandwidth for which in a system is always limited. Niall On 23 Dec 2012 at 16:16, simon zhang wrote: > ..so,It seems I can only convert std::string and make a deep copy.I can > only avoid it as much as possible.Thank you. > > > 2012/12/22 Jim Bosch > > > On 12/21/2012 03:52 AM, simon zhang wrote: > > > >> How to converter std::string* in boost.python?I have to handle some data > >> of c++ in python.The data may be big.So I return a pointer to python. > >> But there are some errors. > >> > >> > > If the data is big, and you really want to avoid a deep copy, the only way > > to use it is if you manually allocate the memory as a Python str object > > using the Python C API or return it as something else (like a NumPy array > > or buffer object, or a custom Boost.Python-wrapped class) that can hold > > C++-allocated memory. A Python str object always owns its own memory, and > > so does std::string, so you can't convert from one to the other without a > > deep copy. > > > > Jim > > > > ______________________________**_________________ > > Cplusplus-sig mailing list > > Cplusplus-sig at python.org > > http://mail.python.org/**mailman/listinfo/cplusplus-sig > > > -- Any opinions or advice expressed here do NOT reflect those of my employer Research In Motion Inc. Work Portfolio: http://careers.stackoverflow.com/nialldouglas/ -------------- next part -------------- A non-text attachment was scrubbed... Name: SMime.p7s Type: application/x-pkcs7-signature Size: 6061 bytes Desc: not available URL: From hapopen at gmail.com Tue Dec 25 07:22:11 2012 From: hapopen at gmail.com (simon zhang) Date: Tue, 25 Dec 2012 14:22:11 +0800 Subject: [C++-sig] How to converter std::string* in boost.python? In-Reply-To: <50D8DFD3.30358.232A2A42@s_sourceforge.nedprod.com> References: <50D4AF93.8030308@gmail.com> <50D8DFD3.30358.232A2A42@s_sourceforge.nedprod.com> Message-ID: If Jim is right,string's memory allocated is different in python and c++.However,before python handle some strings from c++,it must convert string from c++'s memory to python's memory. So smart pointers have not changed anything.It must keep a separate copy of a string in c++ memory and python memory. My idea.In the future, I will be write a wrapped class. When the c++ interface, it store the data by std :: string.When the python interface,it store the data by the Python str object. And perhaps this is a good way. 2012/12/25 Niall Douglas > Traditionally, the proper solution to avoid deep copies is to wrap a > std::shared_ptr instead of a std::string directly. > > If your strings are short of course, it may well be faster to leave > it as is. std::shared_ptr is not lightweight and may use atomic > instructions, the bandwidth for which in a system is always limited. > > Niall > > On 23 Dec 2012 at 16:16, simon zhang wrote: > > > ..so,It seems I can only convert std::string and make a deep copy.I can > > only avoid it as much as possible.Thank you. > > > > > > 2012/12/22 Jim Bosch > > > > > On 12/21/2012 03:52 AM, simon zhang wrote: > > > > > >> How to converter std::string* in boost.python?I have to handle some > data > > >> of c++ in python.The data may be big.So I return a pointer to python. > > >> But there are some errors. > > >> > > >> > > > If the data is big, and you really want to avoid a deep copy, the only > way > > > to use it is if you manually allocate the memory as a Python str object > > > using the Python C API or return it as something else (like a NumPy > array > > > or buffer object, or a custom Boost.Python-wrapped class) that can hold > > > C++-allocated memory. A Python str object always owns its own memory, > and > > > so does std::string, so you can't convert from one to the other > without a > > > deep copy. > > > > > > Jim > > > > > > ______________________________**_________________ > > > Cplusplus-sig mailing list > > > Cplusplus-sig at python.org > > > http://mail.python.org/**mailman/listinfo/cplusplus-sig< > http://mail.python.org/mailman/listinfo/cplusplus-sig> > > > > > > > > -- > Any opinions or advice expressed here do NOT reflect those > of my employer Research In Motion Inc. > Work Portfolio: http://careers.stackoverflow.com/nialldouglas/ > > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jzwinck at gmail.com Tue Dec 25 12:52:26 2012 From: jzwinck at gmail.com (John Zwinck) Date: Tue, 25 Dec 2012 11:52:26 +0000 Subject: [C++-sig] RAII for the GIL in Boost.Python? In-Reply-To: References: Message-ID: On 16/12/2012 22:58, Adam Preble wrote: > Is the particular focus of your code on getting the GIL released during > longer method invocations? I don't have any sway with Boost or > anything, so I'm just asking out of my own personal curiosities. When I > first saw the message, without seeing the code, I was wondering if you > might have coincidentally created a scope-lock kind of thing for > acquiring the GIL in the first place. Yes, the main point is to provide a reasonably safe, RAII-style mechanism for releasing the GIL around long-running C++ functions. But in addition to a class for releasing (and later reacquiring, hence RAII), I made a class for acquiring the GIL (if it is not held) and releasing it at the end. This allows nesting: complex C++ computations can be done with the GIL released, yet some bits of C++ can call back into Python by using the second class to acquire the GIL when needed. You needn't have sway with Boost to help here--simply reviewing the code and letting us know if it would help you would be great. From jzwinck at gmail.com Tue Dec 25 13:14:18 2012 From: jzwinck at gmail.com (John Zwinck) Date: Tue, 25 Dec 2012 12:14:18 +0000 Subject: [C++-sig] RAII for the GIL in Boost.Python? In-Reply-To: <50CE90C7.2332.3DFE2850@s_sourceforge.nedprod.com> References: <50CE90C7.2332.3DFE2850@s_sourceforge.nedprod.com> Message-ID: On 17/12/2012 03:25, Niall Douglas wrote: > I won't go into too much detail here (search this list's archives), > but in short there isn't just GIL management but also interpreter > management, and on top of that both of those have to work right as > exception throws happen plus policies must be instituted for things > like container iterators (e.g. do you release GIL each iteration, > every 10 iterations etc) and policies for multiple interpreter > interactions (e.g. do we timeslice interpreters, or wait for one to > go to i/o sleep first?). The code I published is agnostic about iterators and iteration--the user must explicitly decide when the GIL is to be released and reacquired. I'm sure there could be fancier ways of doing it, e.g. with tags given to BPL's class_::def() for long-running methods. But I didn't worry about that level of complexity--I think the simple solution offers benefits today, and can be expanded upon later. Having multiple Python interpreters in one process seems fraught anyway, so I haven't really considered whether my code can (or should) support that. I hope you'll agree it is not the common case. From s_sourceforge at nedprod.com Tue Dec 25 23:18:39 2012 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Tue, 25 Dec 2012 17:18:39 -0500 Subject: [C++-sig] RAII for the GIL in Boost.Python? In-Reply-To: References: , <50CE90C7.2332.3DFE2850@s_sourceforge.nedprod.com>, Message-ID: <50DA263F.10317.2825415B@s_sourceforge.nedprod.com> On 25 Dec 2012 at 12:14, John Zwinck wrote: > The code I published is agnostic about iterators and iteration--the user > must explicitly decide when the GIL is to be released and reacquired. > I'm sure there could be fancier ways of doing it, e.g. with tags given > to BPL's class_::def() for long-running methods. But I didn't worry > about that level of complexity--I think the simple solution offers > benefits today, and can be expanded upon later. > > Having multiple Python interpreters in one process seems fraught anyway, > so I haven't really considered whether my code can (or should) support > that. I hope you'll agree it is not the common case. Solving multiple interpreters is absolutely the same problem as solving GIL management correctly. You get multiple interpreters "for free". I also didn't mention the problem of multiple copies of BPL, which is a related issue. Right now, if python loads two BPL based modules, two separate copies of the BPL runtime are loaded. The problem is that there are now two BPL registries, so you start encountering "Unknown type" exceptions when the wrong registry gets looked up. The traditional solution is to hack the flags passed to dlopen() by python to use RTLD_GLOBAL. Now you get one single type registry, and things start to work again. But now you have the problem of symbol collision due to the ODR rule, so if extension A defines type Foo and extension B defines a different type Foo, they collide. Basically, you get a runtime warning when you try to load the second extension, and it segfaults or memory corrupts during usage. What BPL needs is to do execution stack aware type registry lookups, so you need to examine from which extension the lookup is coming from and search the type registry appropriately. Thankfully, metaprogramming makes this uber easy (just slip in a pointer to any local function, and have a O(log N) routine map it to its containing module, then cache that so lookup henceforth is instant). As I mentioned earlier, BPL basically needs to be refactored and rewritten pretty much from scratch. The wider solution I'm currently prototyping - if it gets greenlit - solves all these problems, and a ton load more I haven't mentioned here (e.g. what happens if the two BPLs are different versions, and are slightly binary incompatible?). Chances are though it will be rejected sadly. And even if greenlit, we're talking 2014 at the earliest. So, for now, you may be right that submitting a toy solution now in the hope its limitations spur people to get it fixed properly may be the better approach. Certainly our collective hesitation so far hasn't achieved much. So sure, press on. Niall -- Any opinions or advice expressed here do NOT reflect those of my employer Research In Motion Inc. Work Portfolio: http://careers.stackoverflow.com/nialldouglas/ -------------- next part -------------- A non-text attachment was scrubbed... Name: SMime.p7s Type: application/x-pkcs7-signature Size: 6061 bytes Desc: not available URL: From s_sourceforge at nedprod.com Tue Dec 25 23:01:59 2012 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Tue, 25 Dec 2012 17:01:59 -0500 Subject: [C++-sig] How to converter std::string* in boost.python? In-Reply-To: References: , <50D8DFD3.30358.232A2A42@s_sourceforge.nedprod.com>, Message-ID: <50DA2257.674.28160082@s_sourceforge.nedprod.com> For a BPL wrapped object to be a python string, it simply needs to provide the same API as a Python string. Replicate the magic underscore methods of python's string object and your wrapped std::string becomes a python string. It used to be the case that inheriting your BPL object from python's string was problematic. I'd assume recent Pythons have fixed that. Niall On 25 Dec 2012 at 14:22, simon zhang wrote: > If Jim is right,string's memory allocated is different in python and > c++.However,before python > handle some strings from c++,it must convert string from c++'s memory to > python's memory. > > So smart pointers have not changed anything.It must keep a separate copy of > a string in c++ memory and python memory. > > My idea.In the future, I will be write a wrapped class. When the c++ > interface, it store the data by std :: string.When the python > interface,it store the data by the Python str object. And perhaps this is > a good way. > > > 2012/12/25 Niall Douglas > > > Traditionally, the proper solution to avoid deep copies is to wrap a > > std::shared_ptr instead of a std::string directly. > > > > If your strings are short of course, it may well be faster to leave > > it as is. std::shared_ptr is not lightweight and may use atomic > > instructions, the bandwidth for which in a system is always limited. > > > > Niall > > > > On 23 Dec 2012 at 16:16, simon zhang wrote: > > > > > ..so,It seems I can only convert std::string and make a deep copy.I can > > > only avoid it as much as possible.Thank you. > > > > > > > > > 2012/12/22 Jim Bosch > > > > > > > On 12/21/2012 03:52 AM, simon zhang wrote: > > > > > > > >> How to converter std::string* in boost.python?I have to handle some > > data > > > >> of c++ in python.The data may be big.So I return a pointer to python. > > > >> But there are some errors. > > > >> > > > >> > > > > If the data is big, and you really want to avoid a deep copy, the only > > way > > > > to use it is if you manually allocate the memory as a Python str object > > > > using the Python C API or return it as something else (like a NumPy > > array > > > > or buffer object, or a custom Boost.Python-wrapped class) that can hold > > > > C++-allocated memory. A Python str object always owns its own memory, > > and > > > > so does std::string, so you can't convert from one to the other > > without a > > > > deep copy. > > > > > > > > Jim > > > > > > > > ______________________________**_________________ > > > > Cplusplus-sig mailing list > > > > Cplusplus-sig at python.org > > > > http://mail.python.org/**mailman/listinfo/cplusplus-sig< > > http://mail.python.org/mailman/listinfo/cplusplus-sig> > > > > > > > > > > > > > -- > > Any opinions or advice expressed here do NOT reflect those > > of my employer Research In Motion Inc. > > Work Portfolio: http://careers.stackoverflow.com/nialldouglas/ > > > > > > > > > > _______________________________________________ > > Cplusplus-sig mailing list > > Cplusplus-sig at python.org > > http://mail.python.org/mailman/listinfo/cplusplus-sig > > > -- Any opinions or advice expressed here do NOT reflect those of my employer Research In Motion Inc. Work Portfolio: http://careers.stackoverflow.com/nialldouglas/ -------------- next part -------------- A non-text attachment was scrubbed... Name: SMime.p7s Type: application/x-pkcs7-signature Size: 6061 bytes Desc: not available URL: From adam.preble at gmail.com Wed Dec 26 18:49:27 2012 From: adam.preble at gmail.com (Adam Preble) Date: Wed, 26 Dec 2012 11:49:27 -0600 Subject: [C++-sig] RAII for the GIL in Boost.Python? In-Reply-To: References: Message-ID: On Tue, Dec 25, 2012 at 5:52 AM, John Zwinck wrote: > > Yes, the main point is to provide a reasonably safe, RAII-style mechanism > for releasing the GIL around long-running C++ functions. But in addition > to a class for releasing (and later reacquiring, hence RAII), I made a > class for acquiring the GIL (if it is not held) and releasing it at the > end. This allows nesting: complex C++ computations can be done with the > GIL released, yet some bits of C++ can call back into Python by using the > second class to acquire the GIL when needed. > > You needn't have sway with Boost to help here--simply reviewing the code > and letting us know if it would help you would be great. > > It might be a solution to a problem I always bring up now when I try to get Python to interoperate with another language. The situation goes something like this: 1. Define an interface in the other language. 2. Create some background thread agent in the other language that takes instances of that interface and calls some prescribe method in them. 3. Throw a Python implementation of that interface into an instance of that background agent object. 4. Start the background agent and step back. There may be blood. Assuming that survives: 5. Create some other class in the other language that also calls back to a Python agent. It doesn't need it's own thread. 6. Chain some more Python code up as a callback to it. 7. Set up the prior test so now the other/Python interaction looks like: other->Python callback->other->Python callback->other . . . #7 there showed that I walked into the whole thing very naively. I was trying to use the GIL manipulation code in Python's C headers for working with the GIL, I'd double lock, and die. Since I was dealing with multiple threads, I was really asking for the worst kind of asynchronous, non-deterministic bugs. Eventually I was using Stackless Python and taking explicit control over when the interpreter does its thing at all. There would be a queue on the side for calls from C++ threads which would muck with objects created from the Python side. Each would get their turn. At that point I didn't need any GIL management at all since all the relevant code executed on the thread that started the Python interpreter. The GIL stopped acting up then, but I don't know how inefficient the whole thing could be. Consider with multiple other/Python interactions going on over and over, it would mean constantly putting something on a queue somewhere. At least having some kind of recurse lock on the GIL would have probably been simpler. I don't know if it would be any more or less efficient. Also if you're curious, I tried that above experiment with .NET code using IronPython and Python.NET. IronPython took it in stride, and Python.NET died at #3; it couldn't create a Python implementation of a .NET interface because it would choke somewhere in the module the moment I'd finish defining the Python implementation. I think it was while filling in the __new__ operator for the just-defined object. So my own problems have been somewhat different--I haven't even been thinking about releasing the lock on longer operations. However, having some smarts around the GIL would be nice. In my own head I thought of it like the interpreter state having a special mutex, from which one could use all the Boost locks as if it were any other mutex. I never thought about what that would be like to implement. -------------- next part -------------- An HTML attachment was scrubbed... URL: From s_sourceforge at nedprod.com Thu Dec 27 22:20:53 2012 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Thu, 27 Dec 2012 16:20:53 -0500 Subject: [C++-sig] RAII for the GIL in Boost.Python? In-Reply-To: References: , , Message-ID: <50DCBBB5.7703.323D1560@s_sourceforge.nedprod.com> On 26 Dec 2012 at 11:49, Adam Preble wrote: > Also if you're curious, I tried that above experiment with .NET code using > IronPython and Python.NET. IronPython took it in stride, and Python.NET > died at #3; it couldn't create a Python implementation of a .NET interface > because it would choke somewhere in the module the moment I'd finish > defining the Python implementation. I think it was while filling in the > __new__ operator for the just-defined object. > > So my own problems have been somewhat different--I haven't even been > thinking about releasing the lock on longer operations. However, having > some smarts around the GIL would be nice. In my own head I thought of it > like the interpreter state having a special mutex, from which one could use > all the Boost locks as if it were any other mutex. I never thought about > what that would be like to implement. Thing is, .NET has a full IDL interop with COM shim and a well specified threading model. In other words, it provides lots of support for mixing up lots of different languages, so getting multiple language interop working on .NET (or even COM) is vastly easier than anywhere else. Niall -- Any opinions or advice expressed here do NOT reflect those of my employer Research In Motion Inc. Work Portfolio: http://careers.stackoverflow.com/nialldouglas/ -------------- next part -------------- A non-text attachment was scrubbed... Name: SMime.p7s Type: application/x-pkcs7-signature Size: 6061 bytes Desc: not available URL: From hapopen at gmail.com Sun Dec 30 13:27:12 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 30 Dec 2012 20:27:12 +0800 Subject: [C++-sig] compile some code with boost.python by mingw in win7-64bit Message-ID: I decided to make my program compatible with windows environment.But I have very little programming experience on windows.There are some errors need help. Environment: - os: win7-64bit, - ide: codeblocks12.11, - python: Python 2.7.3 Windows X86-64 Installer (Windows AMD64 / Intel 64 / X86-64 binary [1] -- does not include source) - compiler: mingw that come from codeblocks installation package. - boost: boost1.52 I only copy and test this "hello" code that come from ".\boost_1_52_0\libs\python\example\tutorial" Code: #include #include #include char const* greet(){ return "hello, world"; } BOOST_PYTHON_MODULE(hello_ext){ using namespace boost::python; def("greet", greet);} using namespace std;int main(){ cout << "Hello world!" << endl; return 0;} Error info: mingw32-g++.exe -Wall -fexceptions -g -ID:\boost\include\boost-1_52 -IC:\Python27\include -c E:\project\snail-MW\test1\main.cpp -o obj\Debug\main.o mingw32-g++.exe -LD:\boost\lib\ -LC:\Python27\libs -o bin\Debug\test1.exe obj\Debug\main.o -static obj\Debug\main.o: In function `inithello_ext': E:/project/snail-MW/test1/main.cpp:11: undefined reference to `boost::python::detail::init_module(char const*, void (*)())' obj\Debug\main.o: In function `ZNK5boost6python9type_info4nameEv': D:/boost/include/boost-1_52/boost/python/type_id.hpp:165: undefined reference to `boost::python::detail::gcc_demangle(char const*)' obj\Debug\main.o: In function `ZNK5boost6python15to_python_valueIRKPKcEclES5_': D:/boost/include/boost-1_52/boost/python/converter/builtin_converters.hpp:161: undefined reference to `boost::python::converter::do_return_to_python(char const*)' ................ It have also do some errors that only compile by the command of "bjam toolset=gcc variant=release " in the station ".\boost_1_52_0\libs\python\example\tutorial". -------------- next part -------------- An HTML attachment was scrubbed... URL: From blairdhall at gmail.com Sun Dec 30 21:10:18 2012 From: blairdhall at gmail.com (Blair Hall) Date: Mon, 31 Dec 2012 09:10:18 +1300 Subject: [C++-sig] compile some code with boost.python by mingw in win7-64bit In-Reply-To: References: Message-ID: The comments here may be helpful http://ctrl-dev.com/2012/02/compiling-boost-python-with-mingw I would try to get the example working first without the IDE (ie follow the boost.python documentation), because boost needs to build libraries (unless you downloaded them pre-built). The IDE just makes an already complicated problem more complicated. NOTE 1: that the boost examples do not seem to work 100% as described in the documentation. I have found that, with mingw, the necessary libraries do get built, but you need to make sure they are available when running the program (e.g., the boost build process for the examples tries to test things after they have bgeen built, but this fails because the libraries cannot be located at run time). On the contrary, using msvc9 things seem to work much better. 2: the boost embedding example does not seem to work (either with mingw or msvc). There are references to this problem already from other people. On Mon, Dec 31, 2012 at 1:27 AM, simon zhang wrote: > > I decided to make my program compatible with windows environment.But I > have very little programming experience on windows.There are some errors > need help. > > Environment: > > os: win7-64bit, > ide: codeblocks12.11, > python: Python 2.7.3 Windows X86-64 Installer (Windows AMD64 / Intel 64 / > X86-64 binary [1] -- does not include source) > compiler: mingw that come from codeblocks installation package. > boost: boost1.52 > > I only copy and test this "hello" code that come from > ".\boost_1_52_0\libs\python\example\tutorial" > > Code: > > #include > #include > #include > > char const* greet() > { return "hello, world"; } > > BOOST_PYTHON_MODULE(hello_ext) > { > using namespace boost::python; > def("greet", greet); > } > > using namespace std; > int main() > { > cout << "Hello world!" << endl; > return 0; > } > > Error info: > > mingw32-g++.exe -Wall -fexceptions -g -ID:\boost\include\boost-1_52 > -IC:\Python27\include -c E:\project\snail-MW\test1\main.cpp -o > obj\Debug\main.o > mingw32-g++.exe -LD:\boost\lib\ -LC:\Python27\libs -o bin\Debug\test1.exe > obj\Debug\main.o -static > obj\Debug\main.o: In function `inithello_ext': > E:/project/snail-MW/test1/main.cpp:11: undefined reference to > `boost::python::detail::init_module(char const*, void (*)())' > obj\Debug\main.o: In function `ZNK5boost6python9type_info4nameEv': > D:/boost/include/boost-1_52/boost/python/type_id.hpp:165: undefined > reference to `boost::python::detail::gcc_demangle(char const*)' > obj\Debug\main.o: In function > `ZNK5boost6python15to_python_valueIRKPKcEclES5_': > > D:/boost/include/boost-1_52/boost/python/converter/builtin_converters.hpp:161: > undefined reference to `boost::python::converter::do_return_to_python(char > const*)' > ................ > > It have also do some errors that only compile by the command of "bjam > toolset=gcc variant=release " in the station > ".\boost_1_52_0\libs\python\example\tutorial". > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig