From gjcarneiro at gmail.com Sat Nov 1 00:06:07 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Fri, 31 Oct 2008 23:06:07 +0000 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <490B8530.5040702@sympatico.ca> References: <179747.31109.qm@web58302.mail.re3.yahoo.com> <490B8530.5040702@sympatico.ca> Message-ID: 2008/10/31 Stefan Seefeld > Alan Baljeu wrote: > > Question: which python-calling-C++ tool should I try? >> >> > > Wow, quiet group. I guess I'll try pybindgen first. >> >> > > That may be because the question is not quite clear. > First of all, you should realize that, despite the generic name of this > list, this is mostly about boost.python. I beg to disagree. If this list is about boost.python, change the name of the list. Until then, I recommend anyone out there with questions related to pybindgen to use this mailing list. Regards, -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Sat Nov 1 00:19:36 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 31 Oct 2008 19:19:36 -0400 Subject: [C++-sig] new to python; old to C++ In-Reply-To: References: <179747.31109.qm@web58302.mail.re3.yahoo.com> <490B8530.5040702@sympatico.ca> Message-ID: <490B9288.8010405@sympatico.ca> Gustavo Carneiro wrote: > > > 2008/10/31 Stefan Seefeld > > > That may be because the question is not quite clear. > First of all, you should realize that, despite the generic name of > this list, this is mostly about boost.python. > > > I beg to disagree. If this list is about boost.python, change the > name of the list. Until then, I recommend anyone out there with > questions related to pybindgen to use this mailing list. This list is mostly about boost.python, as a matter of fact, whether we like it or not. I would never suggest non-boost python-C++ questions should be sent elsewhere. Quite on the contrary, I fully agree with you, and suggested the same in the past. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From alanbaljeu at yahoo.com Sat Nov 1 02:23:30 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Fri, 31 Oct 2008 18:23:30 -0700 (PDT) Subject: [C++-sig] new to python; old to C++ Message-ID: <372051.35566.qm@web58307.mail.re3.yahoo.com> ----- Original Message ---- From: Stefan Seefeld To: Development of Python/C++ integration Sent: Friday, October 31, 2008 6:22:40 PM Subject: Re: [C++-sig] new to python; old to C++ Alan Baljeu wrote: >> Question: which python-calling-C++ tool should I try? >> >> Wow, quiet group. I guess I'll try pybindgen first. >> >That may be because the question is not quite clear. >First of all, you should realize that, despite the generic name of this >list, this is mostly about boost.python. Second, it isn't clear what you >mean by 'tool', i.e., a language binding library ? A code generator ? I think my question should be quite clear. I really don't care what kind of tool, so much as I'm looking for certain qualities of the solution. As I said, ease of use, simplicity, less C++ are priorities. Wrapping large libraries, generality are less important. >As you are on a mostly-boost.python list, I guess the suggestion to use >boost.python doesn't come as a surprise. That's the most important part. >As far as code-generation is concerned, I have never bothered to use a >tool. I write my binding code manually. Your milage may vary... >HTH, > Stefan Surprisingly, I haven't seen a boost recommendation. That's probably just as well since I don't care much for template syntax. Manually... do you wrap many functions or classes, or only occasional ones? Alan __________________________________________________________________ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/ From alanbaljeu at yahoo.com Sat Nov 1 02:32:01 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Fri, 31 Oct 2008 18:32:01 -0700 (PDT) Subject: [C++-sig] new to python; old to C++ Message-ID: <482228.19356.qm@web58308.mail.re3.yahoo.com> My first impression of Py++ is that it generates stuff for Boost.Python to use to connect Python to C++. To get it going I also need to introduce GCC, and probably Cygwin. That seems like a lot of layers. Does this not make for a complicated system, with a lot to learn? I'm looking for small, simple tools. Feel free to persuade me on Py++, if you still think it's the way to go. Alan Baljeu ----- Original Message ---- From: Renato Araujo To: Development of Python/C++ integration Sent: Friday, October 31, 2008 6:15:27 PM Subject: Re: [C++-sig] new to python; old to C++ Hi Allan I think you can try Py++, to generate yours bindings then use the code to create a toolset to mke your work more easy an fast. BR Renato On Fri, Oct 31, 2008 at 7:04 PM, Alan Baljeu wrote: > Wow, quiet group. I guess I'll try pybindgen first. > > Alan Baljeu > > > ----- Original Message ---- > From: Alan Baljeu > To: cplusplus-sig at python.org > Sent: Friday, October 31, 2008 12:54:39 AM > Subject: [C++-sig] new to python; old to C++ > > Hi all. > > Question: which python-calling-C++ tool should I try? > > Background: > I need to use Visual Studio 2005 native C++. I know some tools use > gcc. That's fine provided it's for something besides compiling my > application. > > I'm looking at Python as a way to accelerate development of a C++ application. This application uses classes enough, but is largely not object oriented. My thought is I would identify a bunch of C++ functions to wrap, wrap them, and then use Python to pull together new functionality. The key to success is to make the C++ aspect of this wrapping as easy as possible. I don't mean "less work" as much as I mean "obvious work that I'm liable to do right because it's simple". If this means restrictions on what interfaces I can expose, I can live with that. > > Simple means reducing the toolset I need to understand, keeping the input simple, and keeping the output simple. > > Above all, "Not crashing" is a priority for me. > > Alan Baljeu > > > __________________________________________________________________ > Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at > http://ca.toolbar.yahoo.com. > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > __________________________________________________________________ > Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at > http://ca.toolbar.yahoo.com. > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Renato Araujo Oliveira Filho _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com. From seefeld at sympatico.ca Sat Nov 1 03:50:10 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 31 Oct 2008 22:50:10 -0400 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <372051.35566.qm@web58307.mail.re3.yahoo.com> References: <372051.35566.qm@web58307.mail.re3.yahoo.com> Message-ID: <490BC3E2.7020007@sympatico.ca> Alan Baljeu wrote: > Surprisingly, I haven't seen a boost recommendation. That's probably just > as well since I don't care much for template syntax. > boost.python is a C++ - Python language binding library. It allows a very concise definition of what you want to export, and how (with what policies). > Manually... do you wrap many functions or classes, or only occasional ones? > I'm not sure I understand the question. You need to wrap all functions and types that you wish to use from Python. Only you know what that specifically means. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From alanbaljeu at yahoo.com Sat Nov 1 04:48:08 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Fri, 31 Oct 2008 20:48:08 -0700 (PDT) Subject: [C++-sig] new to python; old to C++ Message-ID: <855200.53735.qm@web58305.mail.re3.yahoo.com> >> Manually... do you wrap many functions or classes, or only occasional ones? >> > >I'm not sure I understand the question. You need to wrap all functions >and types that you wish to use from Python. Only you know what that >specifically means. > >Regards, > Stefan It was a personal question. Do you specifically do much wrapping stuff or not? I'm trying to gauge your approach compared to how much you do it. I mean, if I had to wrap one C++ function I would of course use the Python API. If I had to grab 2000 classes, I would look for something else to help. What's the breaking point? How quickly does it go for you? __________________________________________________________________ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/ From roman.yakovenko at gmail.com Sat Nov 1 07:58:01 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sat, 1 Nov 2008 08:58:01 +0200 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <482228.19356.qm@web58308.mail.re3.yahoo.com> References: <482228.19356.qm@web58308.mail.re3.yahoo.com> Message-ID: <7465b6170810312358h27a072e0s3177e1d43ac35e04@mail.gmail.com> On Sat, Nov 1, 2008 at 3:32 AM, Alan Baljeu wrote: > My first impression of Py++ is that it generates stuff for Boost.Python to use to connect Python to C++. To get it going I also need to introduce GCC, and probably Cygwin. That seems like a lot of layers. Does this not make for a complicated system, with a lot to learn? Where did you read this. Py++ has dependency on GCC-XML, not on GCC, neither on cygwin. The following URL explains how to install it: http://gccxml.org/HTML/Install.html > I'm looking for small, simple tools. Feel free to persuade me on Py++, if you still think it's the way to go. It is up to you. Py++ is not simple and it takes day or two to learn to operate it. But you don't have. If you just start with Boost.Python you still can use it: http://language-binding.net/pyplusplus/documentation/tutorials/pyplusplus_gui.html and save a lot of time. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From greg.landrum at gmail.com Sat Nov 1 08:46:33 2008 From: greg.landrum at gmail.com (Greg Landrum) Date: Sat, 1 Nov 2008 08:46:33 +0100 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <855200.53735.qm@web58305.mail.re3.yahoo.com> References: <855200.53735.qm@web58305.mail.re3.yahoo.com> Message-ID: <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> On Sat, Nov 1, 2008 at 4:48 AM, Alan Baljeu wrote: > > It was a personal question. Do you specifically do much wrapping stuff or not? I'm > trying to gauge your approach compared to how much you do it. I mean, if I had > to wrap one C++ function I would of course use the Python API. If I had to grab 2000 > classes, I would look for something else to help. What's the breaking point? How > quickly does it go for you? People are obviously going to have different pain points. I use straight boost.python to wrap C++ classes and functions, not any of the wrapper generators like SWIG or Py++. I find that doing things by hand helps me end up with a more "pythonic" wrapper. If I had to deal with a large existing class library, I'd probably go a different route and use a generator. There's an interesting question about whether it's better to use boost.python or SWIG. I've been using boost.python for years, so I have a lot invested in it, but if I were starting from scratch, I might consider using SWIG because it gives you the flexibility to generate wrappers for languages other than Python. an aside: you do not need gcc or cygwin to use boost.python by itself. It works fine with visual studio. -greg From gjcarneiro at gmail.com Sat Nov 1 12:40:09 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Sat, 1 Nov 2008 11:40:09 +0000 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> Message-ID: 2008/11/1 Greg Landrum > On Sat, Nov 1, 2008 at 4:48 AM, Alan Baljeu wrote: > > > > It was a personal question. Do you specifically do much wrapping stuff > or not? I'm > > trying to gauge your approach compared to how much you do it. I mean, if > I had > > to wrap one C++ function I would of course use the Python API. If I had > to grab 2000 > > classes, I would look for something else to help. What's the breaking > point? How > > quickly does it go for you? > > People are obviously going to have different pain points. I use > straight boost.python to wrap C++ classes and functions, not any of > the wrapper generators like SWIG or Py++. I find that doing things by > hand helps me end up with a more "pythonic" wrapper. If I had to deal > with a large existing class library, I'd probably go a different route > and use a generator. My opinion is biased, of course, but I use PyBindGen, usually with the help of pygccxml for automatic scanning. I recommend PyBindGen for people that dislike the kind of C++ template abuse that boost.python does. Of course I also recommend anyone starting with pybindgen to be aware of its limitations, namely lack of support for multiple inheritance and C++ exceptions. > There's an interesting question about whether it's better to use > boost.python or SWIG. I've been using boost.python for years, so I > have a lot invested in it, but if I were starting from scratch, I > might consider using SWIG because it gives you the flexibility to > generate wrappers for languages other than Python. On the other hand, SWIG generates ugly and inneficient code, at least for the Python case. The C code generated by SWIG is Python builtin module providing a bunch of _functions_, not classes and methods. Then another module is generated on top, Python code, which "adapts" the functional C module to make it look more object oriented. > > > an aside: you do not need gcc or cygwin to use boost.python by itself. > It works fine with visual studio. pybindgen generated code also compiles fine with visual studio (or at least the unit test suite does). You only need (py)gccxml for scanning code, not for generating. And generated code does not require any library to compile. And you can even skip (py)gccxml if you want API definitions "by hand", and this way depend only on pybindgen and python for code generation. Finally, pybindgen is a small pure python module of which a copy can easily be included in the project itself ;-) http://pybindgen.googlecode.com/svn/trunk/apidocs/index.html -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From paul at pecm.nl Sat Nov 1 12:48:14 2008 From: paul at pecm.nl (Paul Melis) Date: Sat, 01 Nov 2008 12:48:14 +0100 Subject: [C++-sig] new to python; old to C++ In-Reply-To: References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> Message-ID: <490C41FE.5000100@pecm.nl> Gustavo Carneiro wrote: > > There's an interesting question about whether it's better to use > boost.python or SWIG. I've been using boost.python for years, so I > have a lot invested in it, but if I were starting from scratch, I > might consider using SWIG because it gives you the flexibility to > generate wrappers for languages other than Python. > > > On the other hand, SWIG generates ugly and inneficient code, at least > for the Python case. I'd like to see proof of the claim that SWIG's wrapper code is inefficient. In my experience it is not more inefficient than what, for example, boost.python via Py++ provides. Paul From gjcarneiro at gmail.com Sat Nov 1 19:51:26 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Sat, 1 Nov 2008 18:51:26 +0000 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <490C41FE.5000100@pecm.nl> References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> Message-ID: 2008/11/1 Paul Melis > Gustavo Carneiro wrote: > > > > There's an interesting question about whether it's better to use > > boost.python or SWIG. I've been using boost.python for years, so I > > have a lot invested in it, but if I were starting from scratch, I > > might consider using SWIG because it gives you the flexibility to > > generate wrappers for languages other than Python. > > > > > > On the other hand, SWIG generates ugly and inneficient code, at least > > for the Python case. > I'd like to see proof of the claim that SWIG's wrapper code is > inefficient. In my experience it is not more inefficient than what, for > example, boost.python via Py++ provides. I don't know how boost.python works, but I know that, in manually wrapped code, when calling a method Python does exactly one dictionary lookup method_name -> method_object, while with SWIG wrappers there are always two lookups method_name -> method_object, then functiona_name -> function_object. In addition, at least one python opcode has to be interpreted between method and function invocation. "inefficient" is a subjective term and I should not have used it, but I can state with almost certainty (not having measured it) that manually wrapped code (or pybindgen generated code, for that matter, but I don't mean boost.python because i don't know it well enough) is faster than SWIG generated code. I am not going to argue whether the difference is substantial for practical uses, I don't know, I just argue that the difference exists. Regards, -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From alanbaljeu at yahoo.com Sat Nov 1 21:42:52 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Sat, 1 Nov 2008 13:42:52 -0700 (PDT) Subject: [C++-sig] new to python; old to C++ Message-ID: <438829.34745.qm@web58307.mail.re3.yahoo.com> On Sat, Nov 1, 2008 at 3:32 AM, Alan Baljeu wrote: >> My first impression of Py++ is that it generates stuff for Boost.Python to use to connect >>Python to C++. To get it going I also need to introduce GCC, and probably >>Cygwin. That seems like a lot of layers. Does this not make for a >> complicated system, with a lot to learn? > Where did you read this. Py++ has dependency on GCC-XML, not on GCC, > neither on cygwin. http://www.language-binding.net/pyplusplus/pyplusplus.html says: """ Py++ does not reinvent the wheel. It uses GCC C++ compiler to parse C++ source files. To be more precise, the tool chain looks like this: 1. source code is passed to GCC-XML 2. GCC-XML passes it to GCC C++ compiler 3. GCC-XML generates an XML description of a C++ program from GCC's internal representation. 4. Py++ uses pygccxml package to read GCC-XML generated file.""" __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com. From alanbaljeu at yahoo.com Sat Nov 1 21:54:09 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Sat, 1 Nov 2008 13:54:09 -0700 (PDT) Subject: [C++-sig] new to python; old to C++ Message-ID: <905142.90926.qm@web58301.mail.re3.yahoo.com> 2008/11/1 Greg Landrum <<< My opinion is biased, of course, but I use PyBindGen, usually with the help of pygccxml for automatic scanning. I recommend PyBindGen for people that dislike the kind of C++ template abuse that boost.python does. Of course I also recommend anyone starting with pybindgen to be aware of its limitations, namely lack of support for multiple inheritance and C++ exceptions. >>> This is what attracts me to PyBindGen. I dislike "template abuse", and my C++ doesn't use multiple inheritance or exceptions. Any other limitations I should know? <<< pybindgen generated code also compiles fine with visual studio (or at least the unit test suite does). You only need (py)gccxml for scanning code, not for generating. And generated code does not require any library to compile. And you can even skip (py)gccxml if you want API definitions "by hand", and this way depend only on pybindgen and python for code generation. Finally, pybindgen is a small pure python module of which a copy can easily be included in the project itself ;-) http://pybindgen.googlecode.com/svn/trunk/apidocs/index.html >>> So you use gccxml as well. Does it make a big diff? __________________________________________________________________ Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail. Click on Options in Mail and switch to New Mail today or register for free at http://mail.yahoo.ca From gjcarneiro at gmail.com Sat Nov 1 22:15:38 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Sat, 1 Nov 2008 21:15:38 +0000 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <905142.90926.qm@web58301.mail.re3.yahoo.com> References: <905142.90926.qm@web58301.mail.re3.yahoo.com> Message-ID: 2008/11/1 Alan Baljeu > 2008/11/1 Greg Landrum > <<< > My opinion is biased, of course, but I use PyBindGen, usually with the help > of pygccxml for automatic scanning. I recommend PyBindGen for people that > dislike the kind of C++ template abuse that boost.python does. Of course I > also recommend anyone starting with pybindgen to be aware of its > limitations, namely lack of support for multiple inheritance and C++ > exceptions. > >>> > > This is what attracts me to PyBindGen. I dislike "template abuse", and my > C++ doesn't use multiple inheritance or exceptions. Any other limitations I > should know? See http://code.google.com/p/pybindgen/wiki/Features And also the open bug reports: https://bugs.launchpad.net/pybindgen/+bugs But even I don't know all the pybindgen limitations :P > > > <<< > pybindgen generated code also compiles fine with visual studio (or at least > the unit test suite does). You only need (py)gccxml for scanning code, not > for generating. And generated code does not require any library to compile. > And you can even skip (py)gccxml if you want API definitions "by hand", and > this way depend only on pybindgen and python for code generation. Finally, > pybindgen is a small pure python module of which a copy can easily be > included in the project itself ;-) > > http://pybindgen.googlecode.com/svn/trunk/apidocs/index.html > >>> > So you use gccxml as well. Does it make a big diff? pybindgen has _one_ layer that uses gccxml for scanning, but it is optional; use it if you like, or don't. I think gccxml scanning is important for large APIs which have a tendency to change over time. If you have a small and mostly static API then it should be OK to just manually write the API definition by hand (in a Python-based IDL). Otherwise it becomes tedious to do it by hand. -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Sun Nov 2 08:39:56 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 2 Nov 2008 09:39:56 +0200 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <438829.34745.qm@web58307.mail.re3.yahoo.com> References: <438829.34745.qm@web58307.mail.re3.yahoo.com> Message-ID: <7465b6170811020039o1ef0636dpc5b2867b81ef2b40@mail.gmail.com> On Sat, Nov 1, 2008 at 10:42 PM, Alan Baljeu wrote: > On Sat, Nov 1, 2008 at 3:32 AM, Alan Baljeu wrote: >>> My first impression of Py++ is that it generates stuff for Boost.Python to use to connect >>>Python to C++. To get it going I also need to introduce GCC, and probably >>>Cygwin. That seems like a lot of layers. Does this not make for a >>> complicated system, with a lot to learn? > >> Where did you read this. Py++ has dependency on GCC-XML, not on GCC, >> neither on cygwin. > > http://www.language-binding.net/pyplusplus/pyplusplus.html says: > """ > > Py++ does not reinvent the wheel. It uses GCC C++ compiler to parse C++ > source files. To be more precise, the tool chain looks like this: > 1. source code is passed to GCC-XML > 2. GCC-XML passes it to GCC C++ compiler > 3. GCC-XML generates an XML description of a C++ program from GCC's internal > representation. GCCXML indeed used GCC to parse C++ code. GCCXML "embeds" GCC. The only GCCXML requirement is a C\C++ compiler, so you can build it from source code. You don't have to install cygwin. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From dave at boostpro.com Sun Nov 2 14:11:48 2008 From: dave at boostpro.com (David Abrahams) Date: Sun, 02 Nov 2008 08:11:48 -0500 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <490C41FE.5000100@pecm.nl> (Paul Melis's message of "Sat, 01 Nov 2008 12:48:14 +0100") References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> Message-ID: <87mygie0h7.fsf@mcbain.luannocracy.com> on Sat Nov 01 2008, Paul Melis wrote: > Gustavo Carneiro wrote: >> >> There's an interesting question about whether it's better to use >> boost.python or SWIG. I've been using boost.python for years, so I >> have a lot invested in it, but if I were starting from scratch, I >> might consider using SWIG because it gives you the flexibility to >> generate wrappers for languages other than Python. >> >> >> On the other hand, SWIG generates ugly and inneficient code, at least >> for the Python case. > I'd like to see proof of the claim that SWIG's wrapper code is > inefficient. In my experience it is not more inefficient than what, for > example, boost.python via Py++ provides. I doubt that SWIG generates less efficient code. However, I would be surprised if SWIG's is as careful about dealing with lifetime and ownership issues that are crucial to writing Pythonic and un-crashable bindings as Boost.Python is. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From merlin66b at gmail.com Sun Nov 2 15:12:12 2008 From: merlin66b at gmail.com (Thomas Berg) Date: Sun, 2 Nov 2008 15:12:12 +0100 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <87mygie0h7.fsf@mcbain.luannocracy.com> References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> <87mygie0h7.fsf@mcbain.luannocracy.com> Message-ID: On Sun, Nov 2, 2008 at 2:11 PM, David Abrahams wrote: > > on Sat Nov 01 2008, Paul Melis wrote: > >> Gustavo Carneiro wrote: >>> >>> There's an interesting question about whether it's better to use >>> boost.python or SWIG. I've been using boost.python for years, so I >>> have a lot invested in it, but if I were starting from scratch, I >>> might consider using SWIG because it gives you the flexibility to >>> generate wrappers for languages other than Python. >>> >>> >>> On the other hand, SWIG generates ugly and inneficient code, at least >>> for the Python case. >> I'd like to see proof of the claim that SWIG's wrapper code is >> inefficient. In my experience it is not more inefficient than what, for >> example, boost.python via Py++ provides. > > I doubt that SWIG generates less efficient code. However, I would be > surprised if SWIG's is as careful about dealing with lifetime and > ownership issues that are crucial to writing Pythonic and un-crashable > bindings as Boost.Python is. > > -- > Dave Abrahams > BoostPro Computing > http://www.boostpro.com > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > At the Europython 2004 conference, there was a talk with the topic "Python Wrapper Tools: A Performance Study". I'm unable to find the pdf online now, it used to be located here: http://people.web.psi.ch/geus/talks/europython2004_geus.pdf It contains results from benchmarking wrapped calls with the different tools. Things may have changed now, of course, but SWIG was found to have larger overhead than the other alternatives. Also, SIP was found to be faster than boost python. I still have the pdf if anyone is interested. Thomas From mathieu.malaterre at gmail.com Sun Nov 2 15:51:59 2008 From: mathieu.malaterre at gmail.com (Mathieu Malaterre) Date: Sun, 2 Nov 2008 15:51:59 +0100 Subject: [C++-sig] new to python; old to C++ In-Reply-To: References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> <87mygie0h7.fsf@mcbain.luannocracy.com> Message-ID: On Sun, Nov 2, 2008 at 3:12 PM, Thomas Berg wrote: > On Sun, Nov 2, 2008 at 2:11 PM, David Abrahams wrote: >> >> on Sat Nov 01 2008, Paul Melis wrote: >> >>> Gustavo Carneiro wrote: >>>> >>>> There's an interesting question about whether it's better to use >>>> boost.python or SWIG. I've been using boost.python for years, so I >>>> have a lot invested in it, but if I were starting from scratch, I >>>> might consider using SWIG because it gives you the flexibility to >>>> generate wrappers for languages other than Python. >>>> >>>> >>>> On the other hand, SWIG generates ugly and inneficient code, at least >>>> for the Python case. >>> I'd like to see proof of the claim that SWIG's wrapper code is >>> inefficient. In my experience it is not more inefficient than what, for >>> example, boost.python via Py++ provides. >> >> I doubt that SWIG generates less efficient code. However, I would be >> surprised if SWIG's is as careful about dealing with lifetime and >> ownership issues that are crucial to writing Pythonic and un-crashable >> bindings as Boost.Python is. >> >> -- >> Dave Abrahams >> BoostPro Computing >> http://www.boostpro.com >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> > > > At the Europython 2004 conference, there was a talk with the topic > "Python Wrapper Tools: A Performance Study". I'm unable to find the > pdf online now, it used to be located here: > http://people.web.psi.ch/geus/talks/europython2004_geus.pdf > > It contains results from benchmarking wrapped calls with the different > tools. Things may have changed now, of course, but SWIG was found to > have larger overhead than the other alternatives. Also, SIP was found > to be faster than boost python. I still have the pdf if anyone is > interested. > it's available on web.archive.org http://web.archive.org/web/20070703071726/http://people.web.psi.ch/geus/talks/europython2004_geus.pdf 2cts -- Mathieu From mathieu.malaterre at gmail.com Sun Nov 2 15:57:35 2008 From: mathieu.malaterre at gmail.com (Mathieu Malaterre) Date: Sun, 2 Nov 2008 15:57:35 +0100 Subject: [C++-sig] new to python; old to C++ In-Reply-To: References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> <87mygie0h7.fsf@mcbain.luannocracy.com> Message-ID: On Sun, Nov 2, 2008 at 3:51 PM, Mathieu Malaterre wrote: > On Sun, Nov 2, 2008 at 3:12 PM, Thomas Berg wrote: >> On Sun, Nov 2, 2008 at 2:11 PM, David Abrahams wrote: >>> >>> on Sat Nov 01 2008, Paul Melis wrote: >>> >>>> Gustavo Carneiro wrote: >>>>> >>>>> There's an interesting question about whether it's better to use >>>>> boost.python or SWIG. I've been using boost.python for years, so I >>>>> have a lot invested in it, but if I were starting from scratch, I >>>>> might consider using SWIG because it gives you the flexibility to >>>>> generate wrappers for languages other than Python. >>>>> >>>>> >>>>> On the other hand, SWIG generates ugly and inneficient code, at least >>>>> for the Python case. >>>> I'd like to see proof of the claim that SWIG's wrapper code is >>>> inefficient. In my experience it is not more inefficient than what, for >>>> example, boost.python via Py++ provides. >>> >>> I doubt that SWIG generates less efficient code. However, I would be >>> surprised if SWIG's is as careful about dealing with lifetime and >>> ownership issues that are crucial to writing Pythonic and un-crashable >>> bindings as Boost.Python is. >>> >>> -- >>> Dave Abrahams >>> BoostPro Computing >>> http://www.boostpro.com >>> _______________________________________________ >>> Cplusplus-sig mailing list >>> Cplusplus-sig at python.org >>> http://mail.python.org/mailman/listinfo/cplusplus-sig >>> >> >> >> At the Europython 2004 conference, there was a talk with the topic >> "Python Wrapper Tools: A Performance Study". I'm unable to find the >> pdf online now, it used to be located here: >> http://people.web.psi.ch/geus/talks/europython2004_geus.pdf >> >> It contains results from benchmarking wrapped calls with the different >> tools. Things may have changed now, of course, but SWIG was found to >> have larger overhead than the other alternatives. Also, SIP was found >> to be faster than boost python. I still have the pdf if anyone is >> interested. >> > > it's available on web.archive.org > > http://web.archive.org/web/20070703071726/http://people.web.psi.ch/geus/talks/europython2004_geus.pdf > http://europython-develop.zope.nl/conferences/epc2004/slides/science/geus_wrap_perf $ gunzip -c geus_wrap_perf > out.pdf 2cts From greg.landrum at gmail.com Sun Nov 2 17:05:39 2008 From: greg.landrum at gmail.com (Greg Landrum) Date: Sun, 2 Nov 2008 17:05:39 +0100 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <490C41FE.5000100@pecm.nl> References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> Message-ID: <60825b0f0811020805n67378a9bib4eb3f262177e265@mail.gmail.com> On Sat, Nov 1, 2008 at 12:48 PM, Paul Melis wrote: > I'd like to see proof of the claim that SWIG's wrapper code is > inefficient. In my experience it is not more inefficient than what, for > example, boost.python via Py++ provides. I was curious about the claims of SWIG's inefficiency, so I did a quick little experiment. As I said in an earlier post, I normally work with boost.python, but I have a partial, proof-of-concept SWIG wrapper for my project as well (done to explore using the project from Java). I quickly built a python wrapper from that and then wrote a simple test : ------- t1 = time.time() for i in range(50000): for j in range(m.getNumAtoms()): # <- m is a wrapped object a = m.getAtomWithIdx(j) # <- returns a reference to an existing wrapped object t2 = time.time() print 'Part 1: %.2f'%(t2-t1) t1 = time.time() for i in range(50000): m = RDKFuncs.MolFromSmiles('C') # <- returns a new wrapped object t2 = time.time() print 'Part 2: %.2f'%(t2-t1) ------- m.getNumAtoms() returns 9, so the inner loop in Part 1 is called 450K times. I tested this on an ubuntu linux machine using the boost.python from boost v1.35, g++ 4.2.4, and swig 1.3.34 Approximate run time for Part 1: boost : 2.1 seconds SWIG : 2.4 seconds Approximate run time for Part 2: boost : 4.7 seconds SWIG : 3.9 seconds Expanding Part 1 to see what happens when I return an int from a wrapped function: ------- t1 = time.time() for i in range(50000): for j in range(m.getNumAtoms()): # <- m is a wrapped object a = m.getAtomWithIdx(j) # <- returns a reference to an existing wrapped object n = a.getAtomicNum() # <- returns an integer t2 = time.time() print 'Part 1a: %.2f'%(t2-t1) ------- Approximate run time for Part 1a: boost : 3.5 seconds SWIG : 3.9 seconds I don't know if this is even remotely generalizable, but based on this simple test, I don't see a runtime penalty associated with using SWIG; in fact, at least in this very simple test, the wrapper generated by SWIG is somewhat faster than that generated using my boost.python wrapper. This isn't very artificial test isn't enough to make me want to change wrapping strategies to SWIG from boost.python. If anything is going to do that it's going to be the ability to support languages other than Python. Probably not worth two cents, but I thought I'd toss them in anyway. -greg From david at ar.media.kyoto-u.ac.jp Sun Nov 2 16:53:48 2008 From: david at ar.media.kyoto-u.ac.jp (David Cournapeau) Date: Mon, 03 Nov 2008 00:53:48 +0900 Subject: [C++-sig] new to python; old to C++ In-Reply-To: <60825b0f0811020805n67378a9bib4eb3f262177e265@mail.gmail.com> References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> <60825b0f0811020805n67378a9bib4eb3f262177e265@mail.gmail.com> Message-ID: <490DCD0C.8090700@ar.media.kyoto-u.ac.jp> Greg Landrum wrote: > On Sat, Nov 1, 2008 at 12:48 PM, Paul Melis wrote: > >> I'd like to see proof of the claim that SWIG's wrapper code is >> inefficient. In my experience it is not more inefficient than what, for >> example, boost.python via Py++ provides. >> > > I was curious about the claims of SWIG's inefficiency, so I did a > quick little experiment. > The claim of SWIG inefficiency is when compared to SIP or hand wrapping. python.boost is not particularly efficient either. That's the cost for having safer wrapper, I guess cheers, David From rwgk at yahoo.com Mon Nov 3 02:58:08 2008 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Sun, 2 Nov 2008 17:58:08 -0800 (PST) Subject: [C++-sig] new to python; old to C++ References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> <87mygie0h7.fsf@mcbain.luannocracy.com> Message-ID: <574792.26426.qm@web31406.mail.mud.yahoo.com> Intersting discussion, but if the time spent crossing the language boundary becomes rate limiting (irrespective of the wrapping tool used), the design of the system is questionable IMO. We use C++ as a way to accelerate calculations that are rate-limiting if implemented in Python, i.e. to implement something with a significant runtime. In all practical situations I encountered, this means the time spent crossing the language boundary is insignificant in comparison to the time spent doing the actual calculation. If that's not the case, the algorithm is probably simple and a pure Python solution is better anyway. BTW: I write all my Boost.Python wrapper code manually. Typically it adds only about 1% to the development time of new algorithms. Probably, the biggest problem is getting used to the tools. But for someone old to C++, this should be a minor hurdle to take. It is more a problem for (Python) people not used to the gotchas of C++. Ralf From renatox at gmail.com Mon Nov 3 16:48:45 2008 From: renatox at gmail.com (Renato Araujo) Date: Mon, 3 Nov 2008 12:48:45 -0300 Subject: [C++-sig] Contructor with "with_custodian_and_ward_postcall" Message-ID: <95291a80811030748q541d8e88x5750688894c34173@mail.gmail.com> Hi all, Im trying create a binding for the flowing example: C++ ======== struct object { object(object *parent); }; when the object parent is destroyed then all children will be destroyed too. How I can solve this with boost::python? I'm trying in this way: object_wrapper* _ojbect_constructor(object_wrapper* parent) { return new object_wrapper(parent); } qclass_("MyObject", python::no_init) .def("__init__", python::make_constructor(_ojbect_wrapper_constructor, python::with_custodian_and_ward_postcall<0, 1>())) But this not compile, because the "with_custodian_and_ward" need a function with 2 args like (self, parent) how can I solve this problem? -- Renato Araujo Oliveira Filho From alanbaljeu at yahoo.com Mon Nov 3 19:07:01 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Mon, 3 Nov 2008 10:07:01 -0800 (PST) Subject: [C++-sig] Unit testing python/C++ code Message-ID: <404343.45765.qm@web58303.mail.re3.yahoo.com> I'm going with unittest by default. I just thought I'd ask if doing C++ interop might motivate a different choice of tool. Anybody? Alan Baljeu __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com. From renatox at gmail.com Mon Nov 3 20:09:12 2008 From: renatox at gmail.com (Renato Araujo) Date: Mon, 3 Nov 2008 16:09:12 -0300 Subject: [C++-sig] Contructor with "with_custodian_and_ward_postcall" In-Reply-To: <95291a80811030748q541d8e88x5750688894c34173@mail.gmail.com> References: <95291a80811030748q541d8e88x5750688894c34173@mail.gmail.com> Message-ID: <95291a80811031109u43a5be6cne2c260f5f74722e7@mail.gmail.com> I will explain this more. First I have a c++ class like this: struct object { object(object *parent=0) // if parent !=NULL append this at parent children list ~object() //Destroy all children }; Is possible implement this using "python::with_custodian_and_ward_postcall"? I tried some like that: qclass_ ("object", python::init<> ()) //here i put self will be the ward and arg 2 (parent) will be the custodian or .def(python::init()[python::with_custodian_and_ward_postcall<2,1>()]) On Mon, Nov 3, 2008 at 12:48 PM, Renato Araujo wrote: > Hi all, > > Im trying create a binding for the flowing example: > > C++ > ======== > > struct object > { > object(object *parent); > }; > > when the object parent is destroyed then all children will be destroyed too. > > > How I can solve this with boost::python? > > I'm trying in this way: > > object_wrapper* _ojbect_constructor(object_wrapper* parent) > { > return new object_wrapper(parent); > } > > qclass_("MyObject", > python::no_init) .def("__init__", > python::make_constructor(_ojbect_wrapper_constructor, > python::with_custodian_and_ward_postcall<0, 1>())) > > > But this not compile, because the "with_custodian_and_ward" need a > function with 2 args like (self, parent) how can I solve this problem? > > -- Renato Araujo Oliveira Filho From renatox at gmail.com Mon Nov 3 20:15:16 2008 From: renatox at gmail.com (Renato Araujo) Date: Mon, 3 Nov 2008 16:15:16 -0300 Subject: [C++-sig] Fwd: Contructor with "with_custodian_and_ward_postcall" In-Reply-To: <95291a80811031109u43a5be6cne2c260f5f74722e7@mail.gmail.com> References: <95291a80811030748q541d8e88x5750688894c34173@mail.gmail.com> <95291a80811031109u43a5be6cne2c260f5f74722e7@mail.gmail.com> Message-ID: <95291a80811031115t1c6a7af3w8f6e65fe558d2b7b@mail.gmail.com> Sorry my last message I sent incomplete :) I will try again. First I have a c++ class like this: struct object { object(object *parent=0) // if parent !=NULL append this at parent children list ~object() //Destroy all children }; Is possible implement this using "python::with_custodian_and_ward_postcall"? I tried some like that: qclass_ ("object", python::init<> ()) //here i put self will be the ward and arg 2 (parent) will be the custodian //the lifetime of the self will be dependent on the lifetime of the parent .def(python::init()[python::with_custodian_and_ward_postcall<2,1>()]) But in my python code I tried some like that: o = object() o2 = object(o) del o and this produce this exception during the del operation: *** glibc detected *** python: free(): invalid pointer: 0x09c89970 *** From alanbaljeu at yahoo.com Tue Nov 4 00:04:24 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Mon, 3 Nov 2008 15:04:24 -0800 (PST) Subject: [C++-sig] [pybindgen] references? Message-ID: <814302.4271.qm@web58304.mail.re3.yahoo.com> I just read the tutorial page on pybindgen, but it doesn't talk about reference types. 99% of my C++ code involves passing around things like foo&, so this is significant to me. How is it done? __________________________________________________________________ Instant Messaging, free SMS, sharing photos and more... Try the new Yahoo! Canada Messenger at http://ca.beta.messenger.yahoo.com/ From gjcarneiro at gmail.com Tue Nov 4 00:49:22 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Mon, 3 Nov 2008 23:49:22 +0000 Subject: [C++-sig] [pybindgen] references? In-Reply-To: <814302.4271.qm@web58304.mail.re3.yahoo.com> References: <814302.4271.qm@web58304.mail.re3.yahoo.com> Message-ID: 2008/11/3 Alan Baljeu > I just read the tutorial page on pybindgen, but it doesn't talk about > reference types. 99% of my C++ code involves passing around things like > foo&, so this is significant to me. How is it done? e.g.: MyClass.add_method("foobar", retval('void'), param("something &", "paramName", direction=Parameter.DIRECTION_INOUT) See http://pybindgen.googlecode.com/svn/trunk/apidocs/pybindgen.typehandlers.base.Parameter-class.html#__init__ > > > > __________________________________________________________________ > Instant Messaging, free SMS, sharing photos and more... Try the new Yahoo! > Canada Messenger at http://ca.beta.messenger.yahoo.com/ > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Tue Nov 4 00:57:54 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 03 Nov 2008 18:57:54 -0500 Subject: [C++-sig] [pybindgen] references? In-Reply-To: <814302.4271.qm@web58304.mail.re3.yahoo.com> References: <814302.4271.qm@web58304.mail.re3.yahoo.com> Message-ID: <490F9002.5080202@sympatico.ca> Alan Baljeu wrote: > I just read the tutorial page on pybindgen, but it doesn't talk about reference types. 99% of my C++ code involves passing around things like foo&, so this is significant to me. How is it done? > If you need fine-grained control about argument passing policies (as soon as you need reference-semantics, you have to be careful about lifetime management, for example), you are better off writing boost.python bindings manually, I would suggest. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From alanbaljeu at yahoo.com Tue Nov 4 01:18:47 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Mon, 3 Nov 2008 16:18:47 -0800 (PST) Subject: [C++-sig] [pybindgen] references? Message-ID: <335150.97037.qm@web58305.mail.re3.yahoo.com> From: Stefan Seefeld >If you need fine-grained control about argument passing policies (as >soon as you need reference-semantics, you have to be careful about >lifetime management, for example), you are better off writing >boost.python bindings manually, I would suggest. > >Regards, > Stefan void baz(foo &x); If I have a foo, I call baz with it, foo's contents may change but it's still the same object. I don't see an issue here, unless foo is a smart pointer type. And the only smart pointers I receive are weak references, and I'm assuming/planning that the referenced object doesn't get destroyed. So I'm feeling safe to not concern myself. Alan __________________________________________________________________ Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail. Click on Options in Mail and switch to New Mail today or register for free at http://mail.yahoo.ca From meine at informatik.uni-hamburg.de Tue Nov 4 10:19:16 2008 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Tue, 4 Nov 2008 10:19:16 +0100 Subject: [C++-sig] [pybindgen] references? In-Reply-To: <335150.97037.qm@web58305.mail.re3.yahoo.com> References: <335150.97037.qm@web58305.mail.re3.yahoo.com> Message-ID: <200811041019.17142.meine@informatik.uni-hamburg.de> On Tuesday 04 November 2008 01:18:47 Alan Baljeu wrote: > void baz(foo &x); > > If I have a foo, I call baz with it, foo's contents may change but it's > still the same object. I don't see an issue here, unless foo is a smart > pointer type. Sometimes, baz might store the reference (admittedly, that's not very good design, except for some register_xxx functions, but I'd use pointers then anyway), and when the foo is destroyed, you have a dangling reference. In boost::python, this can be prevented, by binding the lifetime of foo to some other objects (where baz stores the reference). Greetings, Hans From gjcarneiro at gmail.com Tue Nov 4 12:05:49 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Tue, 4 Nov 2008 11:05:49 +0000 Subject: [C++-sig] [pybindgen] references? In-Reply-To: <490F9002.5080202@sympatico.ca> References: <814302.4271.qm@web58304.mail.re3.yahoo.com> <490F9002.5080202@sympatico.ca> Message-ID: 2008/11/3 Stefan Seefeld > Alan Baljeu wrote: > >> I just read the tutorial page on pybindgen, but it doesn't talk about >> reference types. 99% of my C++ code involves passing around things like >> foo&, so this is significant to me. How is it done? >> >> > > If you need fine-grained control about argument passing policies (as soon > as you need reference-semantics, you have to be careful about lifetime > management, for example), you are better off writing boost.python bindings > manually, I would suggest. I know that you're thinking about the custodian_and_ward policy of boost.python, but pybindgen also supports it. For example: class SomeObject { [...] Foobar* get_foobar_with_self_as_custodian (); Foobar* get_foobar_with_other_as_custodian (const SomeObject *other); void set_foobar_with_self_as_custodian (Foobar *foobar); }; Is wrapped by pybindgen code: SomeObject = mod.add_class('SomeObject', allow_subclassing=True) [...] SomeObject.add_method('get_foobar_with_self_as_custodian', retval('Foobar*', custodian=0), []) SomeObject.add_method('get_foobar_with_other_as_custodian', retval('Foobar*', custodian=1), [param('SomeObject*', 'other', transfer_ownership=False)]) SomeObject.add_method('set_foobar_with_self_as_custodian', retval('void'), [param('Foobar*', 'foobar', custodian=0)]) [excerpt from the pybindgen unit tests] Although I personally dislike this sort of practice; I would hate working with APIs this complicated even in C++. -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From alanbaljeu at yahoo.com Tue Nov 4 14:24:03 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Tue, 4 Nov 2008 05:24:03 -0800 (PST) Subject: [C++-sig] [pybindgen] references? Message-ID: <871378.36910.qm@web58303.mail.re3.yahoo.com> Glad to read this here. I agree with you about the dislike thing. I can't stand auto_ptr, and if I had to give an object control over the life of another object I would generally choose a shared_ptr protocol. I mean, the object existed fine before passing it in. Why can't it survive independent of its new owner? On the binding side of things, it's good to know pybindgen supports this. I really think the documentation needs an overhaul. I find reference manuals too hard to use for learning an API. The front page of the reference (which I nearly overlooked) has a good tutorial but it's quite incomplete. In fact, if I wasn't aesthetically predisposed to choosing this solution, or if you didn't answer my questions all timely-like, I would have given up. Yet it's looking more and more like it's a nearly comprehensive (and elegant) solution so I'm glad to stick with it. ---------------------- I know that you're thinking about the custodian_and_ward policy of boost.python, but pybindgen also supports it. For example: class SomeObject { [...] Foobar* get_foobar_with_self_as_custodian (); Foobar* get_foobar_with_other_as_custodian (const SomeObject *other); void set_foobar_with_self_as_custodian (Foobar *foobar); }; Is wrapped by pybindgen code: SomeObject = mod.add_class('SomeObject', allow_subclassing=True) [...] SomeObject.add_method('get_foobar_with_self_as_custodian', retval('Foobar*', custodian=0), []) SomeObject.add_method('get_foobar_with_other_as_custodian', retval('Foobar*', custodian=1), [param('SomeObject*', 'other', transfer_ownership=False)]) SomeObject.add_method('set_foobar_with_self_as_custodian', retval('void'), [param('Foobar*', 'foobar', custodian=0)]) [excerpt from the pybindgen unit tests] Although I personally dislike this sort of practice; I would hate working with APIs this complicated even in C++. -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com. -------------- next part -------------- An HTML attachment was scrubbed... URL: From gjcarneiro at gmail.com Tue Nov 4 15:18:52 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Tue, 4 Nov 2008 14:18:52 +0000 Subject: [C++-sig] [pybindgen] references? In-Reply-To: <871378.36910.qm@web58303.mail.re3.yahoo.com> References: <871378.36910.qm@web58303.mail.re3.yahoo.com> Message-ID: 2008/11/4 Alan Baljeu > Glad to read this here. I agree with you about the dislike thing. I can't > stand auto_ptr, and if I had to give an object control over the life of > another object I would generally choose a shared_ptr protocol. I mean, the > object existed fine before passing it in. Why can't it survive independent > of its new owner? > > On the binding side of things, it's good to know pybindgen supports this. > I really think the documentation needs an overhaul. I find reference > manuals too hard to use for learning an API. The front page of the > reference (which I nearly overlooked) has a good tutorial but it's quite > incomplete. In fact, if I wasn't aesthetically predisposed to choosing this > solution, or if you didn't answer my questions all timely-like, I would have > given up. Yet it's looking more and more like it's a nearly comprehensive > (and elegant) solution so I'm glad to stick with it. > Well, I'm sure you'll find a pybindgen limitation sooner or later :-) As you would with any other tool... But I have developed pybindgen mostly oriented to the features I needed to bind NS-3[1]. Regarding documentation, you are right. I would never condemn anyone for not using pybindgen due to lack of documentation, but I'm afraid I don't have time or predisposition for writing good documentation. The little tutorial documentation there is not even written by me, Mathieu Lacage wrote it (or most of it). Otherwise, pybindgen can still be useful for programmers with enough patience to work with incomplete documentation, and that's why I make it available anyway. If you find it useful, good for you, if not, I understand. [1] http://www.nsnam.org/ > > > ---------------------- > > I know that you're thinking about the custodian_and_ward policy of > boost.python, but pybindgen also supports it. For example: > > class SomeObject > { > [...] > Foobar* get_foobar_with_self_as_custodian (); > Foobar* get_foobar_with_other_as_custodian (const SomeObject *other); > void set_foobar_with_self_as_custodian (Foobar *foobar); > }; > > > Is wrapped by pybindgen code: > > SomeObject = mod.add_class('SomeObject', allow_subclassing=True) > [...] > SomeObject.add_method('get_foobar_with_self_as_custodian', > retval('Foobar*', custodian=0), []) > SomeObject.add_method('get_foobar_with_other_as_custodian', > retval('Foobar*', custodian=1), > [param('SomeObject*', 'other', > transfer_ownership=False)]) > SomeObject.add_method('set_foobar_with_self_as_custodian', > retval('void'), > [param('Foobar*', 'foobar', custodian=0)]) > > [excerpt from the pybindgen unit tests] > > Although I personally dislike this sort of practice; I would hate working > with APIs this complicated even in C++. > > -- > Gustavo J. A. M. Carneiro > INESC Porto, Telecommunications and Multimedia Unit > "The universe is always one step beyond logic." -- Frank Herbert > > ------------------------------ > Instant message from any web browser! Try the new * Yahoo! Canada > Messenger for the Web BETA* > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From Matthew.Scouten at tradingtechnologies.com Tue Nov 4 17:29:34 2008 From: Matthew.Scouten at tradingtechnologies.com (Matthew Scouten (TT)) Date: Tue, 4 Nov 2008 10:29:34 -0600 Subject: [C++-sig] mutable object has bogus __hash__ Message-ID: <32490DFF7774554A85D65D23A9F0F93809636CCD@chiex01> I have a bunch of classes in c++ with operator== defined, and I exported it as __eq__. They apparently inherited object's __hash__ . All was well until my users started to put these objects in sets and use them as dict keys. Objects that have the compare equal do not have the same hash. This causes weirdness and errors. I would like to have the __hash__ not exist. These objects are mutable and should NOT be used as keys. Is there a way to hide it? If I have them throw NotImplemented will python do something sensible with that? I suppose the other thing I could do is come up with some bull---- hash function, but I would rather not. The objects really do not belong in sets or dict keys. -------------- next part -------------- An HTML attachment was scrubbed... URL: From renatox at gmail.com Tue Nov 4 18:13:13 2008 From: renatox at gmail.com (Renato Araujo) Date: Tue, 4 Nov 2008 14:13:13 -0300 Subject: [C++-sig] Contructor with "with_custodian_and_ward_postcall" In-Reply-To: <95291a80811031115t1c6a7af3w8f6e65fe558d2b7b@mail.gmail.com> References: <95291a80811030748q541d8e88x5750688894c34173@mail.gmail.com> <95291a80811031109u43a5be6cne2c260f5f74722e7@mail.gmail.com> <95291a80811031115t1c6a7af3w8f6e65fe558d2b7b@mail.gmail.com> Message-ID: <95291a80811040913h206f9e83jb061b8be6e16bad@mail.gmail.com> Hi To solve my previous problem I'm trying implement a class witch store the PyObject pointer. using this solution: http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/faq.html#xref then during the destructor of my wrapper class I want destroy the PyObjects of my children with: object_wrapper::~object_wrapper() { foreach(object child, this->children()) python::detail::dealloc(child->py_self;); } but after my function I get a core dump in this boost call: * boost::python::objects::find_instance_impl(_object*, boost::python::type_info, bool) (in /usr/lib/libboost_python-py25.so.1.35.0) I think there is a way to communicate to boost to remove my objet form your object list. How I can do this? This is the correct way?? Have a best boost way to do this?? BR -- Renato Araujo Oliveira Filho On Mon, Nov 3, 2008 at 4:15 PM, Renato Araujo wrote: > Sorry my last message I sent incomplete :) > > > I will try again. > > First I have a c++ class like this: > > struct object > { > object(object *parent=0) // if parent !=NULL append this at > parent children list > ~object() //Destroy all children > }; > > > Is possible implement this using "python::with_custodian_and_ward_postcall"? > I tried some like that: > > qclass_ ("object", python::init<> ()) > //here i put self will be the ward and arg 2 (parent) will be the > custodian > //the lifetime of the self will be dependent on the lifetime of the parent > .def(python::init()[python::with_custodian_and_ward_postcall<2,1>()]) > > > But in my python code I tried some like that: > > o = object() > o2 = object(o) > > del o > > and this produce this exception during the del operation: > *** glibc detected *** python: free(): invalid pointer: 0x09c89970 *** > -- Renato Araujo Oliveira Filho From amohr at pixar.com Tue Nov 4 18:18:45 2008 From: amohr at pixar.com (Alex Mohr) Date: Tue, 04 Nov 2008 09:18:45 -0800 Subject: [C++-sig] mutable object has bogus __hash__ In-Reply-To: <32490DFF7774554A85D65D23A9F0F93809636CCD@chiex01> References: <32490DFF7774554A85D65D23A9F0F93809636CCD@chiex01> Message-ID: <491083F5.9000506@pixar.com> > I would like to have the __hash__ not exist. These objects are mutable > and should NOT be used as keys. Is there a way to hide it? If I have > them throw NotImplemented will python do something sensible with that? Python raises a TypeError for unhashable things: >>> [1,2,3].__hash__() Traceback (most recent call last): File "", line 1, in TypeError: list objects are unhashable (You get the same with hash([1,2,3]) of course.) Alex From Matthew.Scouten at tradingtechnologies.com Tue Nov 4 20:18:59 2008 From: Matthew.Scouten at tradingtechnologies.com (Matthew Scouten (TT)) Date: Tue, 4 Nov 2008 13:18:59 -0600 Subject: [C++-sig] mutable object has bogus __hash__ In-Reply-To: <491083F5.9000506@pixar.com> References: <32490DFF7774554A85D65D23A9F0F93809636CCD@chiex01> <491083F5.9000506@pixar.com> Message-ID: <32490DFF7774554A85D65D23A9F0F93809636E4B@chiex01> I looks at what it would take to write proper hash functions. These classes have ridiculously complicated equality operators. I am going to throw a type error and any users who don't like it can bite me. Thanks guys. -----Original Message----- From: cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org [mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at py thon.org] On Behalf Of Alex Mohr Sent: Tuesday, November 04, 2008 11:19 AM To: Development of Python/C++ integration Subject: Re: [C++-sig] mutable object has bogus __hash__ > I would like to have the __hash__ not exist. These objects are mutable > and should NOT be used as keys. Is there a way to hide it? If I have > them throw NotImplemented will python do something sensible with that? Python raises a TypeError for unhashable things: >>> [1,2,3].__hash__() Traceback (most recent call last): File "", line 1, in TypeError: list objects are unhashable (You get the same with hash([1,2,3]) of course.) Alex _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig From lucianomw at gmail.com Tue Nov 4 22:05:38 2008 From: lucianomw at gmail.com (Luciano M. Wolf) Date: Tue, 4 Nov 2008 18:05:38 -0300 Subject: [C++-sig] Using destructors in the right way Message-ID: Hi all, I'm using boost::python to make bindings and now I got in trouble. I'm trying to avoid Python deleting my C++ class before other classes but I didn't find any clue on how to do this. Is there a way to tell boost that my object should be the last one to be deleted? Thanks in advance! Regards, Luciano From renatox at gmail.com Thu Nov 6 23:38:14 2008 From: renatox at gmail.com (Renato Araujo) Date: Thu, 6 Nov 2008 19:38:14 -0300 Subject: [C++-sig] Using destructors in the right way In-Reply-To: References: Message-ID: <95291a80811061438j2b487cd8if74aa2c9132f1374@mail.gmail.com> Hi Luciano You can use in your class definition something like that: class_ .... but I don't know why this not work when you have custom constructors like that: class_ ("MyClass", python::no_init) .def("__init__", python::make_constructor(my_constructor)) .... BR Renato On Tue, Nov 4, 2008 at 6:05 PM, Luciano M. Wolf wrote: > Hi all, > > I'm using boost::python to make bindings and now I got in trouble. I'm > trying to avoid Python deleting my C++ class before other classes but > I didn't find any clue on how to do this. Is there a way to tell boost > that my object should be the last one to be deleted? Thanks in > advance! > > Regards, > Luciano > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > From rogeeff at gmail.com Mon Nov 10 08:56:14 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Mon, 10 Nov 2008 07:56:14 +0000 (UTC) Subject: [C++-sig] can these exports be avoided Message-ID: Simple scenario: class IObject {}; class Base : public IObject { public: virtual void foo() {} }; class Derived : public Base { virtual void foo() {} }; Both IObject and Base are exported into Python: bp::class_( "IObject", bp::no_init ); bp::class_,noncopyable>( "Base", bp::no_init ) .def( "foo", &Base::foo ); Also imagine I've got function like this: IObjectPtr create( std::string const& class_name ) { ... if(class_name=="Derived") return IObjectPtr(new Derived); } and this function is also exported into Python. Now in Python I've got code like this: o = mymodule.create( "Derived" ); print type(o) As it stands above print statement produces "mymodule.IObject". What would be preferable if it can produce mymodule.Base, so I can invoke method foo. It can be resolved with additional export for class Derived: bp::class_,noncopyable>( "Derived", bp::no_init ); In which case above print statement starts to show mymodule.Derived. But I would really prefer to avoid these exports for derived classes. After all RTTI information should shoe Boost.Python whole hierarchy, isn't it? Why can't it figure out that pointer produced by create actually convertible to Base*, even though it knows nothing about Derived? Does any one have any workarounds? Thanks, Gennadiy From partoa at gmail.com Mon Nov 10 10:15:24 2008 From: partoa at gmail.com (Patrick Atambo) Date: Mon, 10 Nov 2008 12:15:24 +0300 Subject: [C++-sig] can these exports be avoided In-Reply-To: References: Message-ID: I'd say you have 2 options depending.... 1. Containment. This would require an API class that you export to Python with methods that call your implementation and it's my favorite since from here you can control what is used when calling your implementation methods/objects. 2. Use Private methods and Friend them to the methods that need to call them. But again as I said, all this depends on your objective. Kind Regards, Patrick Atambo. On Mon, Nov 10, 2008 at 10:56 AM, Gennadiy Rozental wrote: > Simple scenario: > > class IObject {}; > > class Base : public IObject { > public: > virtual void foo() {} > }; > > class Derived : public Base { > virtual void foo() {} > }; > > Both IObject and Base are exported into Python: > > bp::class_( "IObject", bp::no_init ); > > bp::class_,noncopyable>( "Base", bp::no_init ) > .def( "foo", &Base::foo ); > > Also imagine I've got function like this: > > IObjectPtr > create( std::string const& class_name ) > { > ... > if(class_name=="Derived") > return IObjectPtr(new Derived); > } > > and this function is also exported into Python. > > Now in Python I've got code like this: > > o = mymodule.create( "Derived" ); > print type(o) > > As it stands above print statement produces "mymodule.IObject". What would > be > preferable if it can produce mymodule.Base, so I can invoke method foo. > > It can be resolved with additional export for class Derived: > > bp::class_,noncopyable>( "Derived", bp::no_init ); > > In which case above print statement starts to show mymodule.Derived. But I > would > really prefer to avoid these exports for derived classes. After all RTTI > information should shoe Boost.Python whole hierarchy, isn't it? Why can't > it > figure out that pointer produced by create actually convertible to Base*, > even > though it knows nothing about Derived? > > Does any one have any workarounds? > > Thanks, > > Gennadiy > > > _______________________________________________ > 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 meine at informatik.uni-hamburg.de Mon Nov 10 10:28:48 2008 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Mon, 10 Nov 2008 10:28:48 +0100 Subject: [C++-sig] can these exports be avoided In-Reply-To: References: Message-ID: <200811101028.49094.meine@informatik.uni-hamburg.de> On Monday 10 November 2008 08:56:14 Gennadiy Rozental wrote: > It can be resolved with additional export for class Derived: > > bp::class_,noncopyable>( "Derived", bp::no_init ); > > In which case above print statement starts to show mymodule.Derived. AFAICS that's the proper solution. > But I > would really prefer to avoid these exports for derived classes. After all > RTTI information should shoe Boost.Python whole hierarchy, isn't it? I don't know much about RTTI, but it was my impression that boost::python has no (portable) way of automatically deriving inheritance trees. You might want to try py++ if you don't want to write the export code yourself. HTH, Hans From paul at pecm.nl Mon Nov 10 19:59:02 2008 From: paul at pecm.nl (Paul Melis) Date: Mon, 10 Nov 2008 19:59:02 +0100 Subject: [C++-sig] Manage new object return policy (was Re: new to python; old to C++) In-Reply-To: <87mygie0h7.fsf@mcbain.luannocracy.com> References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> <87mygie0h7.fsf@mcbain.luannocracy.com> Message-ID: <49188476.6060409@pecm.nl> David Abrahams wrote: > on Sat Nov 01 2008, Paul Melis wrote: > > >> Gustavo Carneiro wrote: >> >>> There's an interesting question about whether it's better to use >>> boost.python or SWIG. I've been using boost.python for years, so I >>> have a lot invested in it, but if I were starting from scratch, I >>> might consider using SWIG because it gives you the flexibility to >>> generate wrappers for languages other than Python. >>> >>> >>> On the other hand, SWIG generates ugly and inneficient code, at least >>> for the Python case. >>> >> I'd like to see proof of the claim that SWIG's wrapper code is >> inefficient. In my experience it is not more inefficient than what, for >> example, boost.python via Py++ provides. >> > > I doubt that SWIG generates less efficient code. However, I would be > surprised if SWIG's is as careful about dealing with lifetime and > ownership issues that are crucial to writing Pythonic and un-crashable > bindings as Boost.Python is In an attempt to compare SWIG and Boost on a simple synthetic benchmark I've become stuck on the following: I have a method that returns a new instance of class Value and returns a pointer to it. The caller is then responsible for freeing of that instance at a suitable moment later on. I specify the manage_new_object return value policy for this method (and another one which basically returns its result) and this compiles fine. But when I run a small test script I get CallbackWrap::operator() Have override for __call__, calling it Traceback (most recent call last): File "t_boost.py", line 25, in r = obj.go(v1, v2) ReferenceError: Attempt to return dangling pointer to object of type: Value The FAQ entry on this doesn't really help, so perhaps I'm not using the return policy correctly or missing something else. The full test code (which is actually quite small) is attached. Thanks in advance for any help, Paul -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: classes.h URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: t_boost.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: boost.cpp URL: From rogeeff at gmail.com Mon Nov 10 21:16:20 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Mon, 10 Nov 2008 20:16:20 +0000 (UTC) Subject: [C++-sig] can these exports be avoided References: Message-ID: Patrick Atambo gmail.com> writes: > > > I'd say you have 2 options depending.... > > Containment. This would require an API class that you export to Python with methods that call your implementation and it's my favorite since from here you can control what is used when calling your implementation methods/objects. > Use Private methods and Friend them to the methods that need to call them. > > But again as I said, all this depends on your objective.Kind Regards,Patrick Atambo. Umm. don't really follow what you ar etrying to say here and how it's related to my problem. Genandiy From rogeeff at gmail.com Mon Nov 10 21:20:19 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Mon, 10 Nov 2008 20:20:19 +0000 (UTC) Subject: [C++-sig] can these exports be avoided References: <200811101028.49094.meine@informatik.uni-hamburg.de> Message-ID: Hans Meine informatik.uni-hamburg.de> writes: > > On Monday 10 November 2008 08:56:14 Gennadiy Rozental wrote: > > It can be resolved with additional export for class Derived: > > > > bp::class_,noncopyable> > > ( "Derived", bp::no_init ); > > > > In which case above print statement starts > > to show mymodule.Derived. > > AFAICS that's the proper solution. My current workaround looks like this: Base* as_base( IObject* obj ) { return dynamic_cast( obj ); } Next I export this function and use if in Python in all point of foo() invocation: mymodule.as_base( object_derived_from_base ).foo() Gennadiy From dave at boostpro.com Mon Nov 10 23:05:19 2008 From: dave at boostpro.com (David Abrahams) Date: Mon, 10 Nov 2008 17:05:19 -0500 Subject: [C++-sig] Manage new object return policy (was Re: new to python; old to C++) In-Reply-To: <49188476.6060409@pecm.nl> (Paul Melis's message of "Mon, 10 Nov 2008 19:59:02 +0100") References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> <87mygie0h7.fsf@mcbain.luannocracy.com> <49188476.6060409@pecm.nl> Message-ID: <874p2f44ps.fsf@mcbain.luannocracy.com> on Mon Nov 10 2008, Paul Melis wrote: > The FAQ entry on this doesn't really help, so perhaps I'm not using the return policy > correctly or missing something else. The full test code (which is actually quite > small) is attached. Please reduce it to its absolute minimum. Remove every single thing you don't need in order to produce the result. If the problem isn't glaringly obvious to you then, I will help. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From dave at boostpro.com Mon Nov 10 23:43:13 2008 From: dave at boostpro.com (David Abrahams) Date: Mon, 10 Nov 2008 17:43:13 -0500 Subject: [C++-sig] can these exports be avoided In-Reply-To: (Gennadiy Rozental's message of "Mon, 10 Nov 2008 07:56:14 +0000 (UTC)") References: Message-ID: <87bpwn2oe6.fsf@mcbain.luannocracy.com> on Mon Nov 10 2008, Gennadiy Rozental wrote: > Simple scenario: > > class IObject {}; > > class Base : public IObject { > public: > virtual void foo() {} > }; > > class Derived : public Base { > virtual void foo() {} > }; > > Both IObject and Base are exported into Python: > > bp::class_( "IObject", bp::no_init ); > > bp::class_,noncopyable>( "Base", bp::no_init ) > .def( "foo", &Base::foo ); > > Also imagine I've got function like this: > > IObjectPtr > create( std::string const& class_name ) > { > ... > if(class_name=="Derived") > return IObjectPtr(new Derived); > } > > and this function is also exported into Python. > > Now in Python I've got code like this: > > o = mymodule.create( "Derived" ); > print type(o) > > As it stands above print statement produces "mymodule.IObject". What would be > preferable if it can produce mymodule.Base, so I can invoke method foo. > > It can be resolved with additional export for class Derived: > > bp::class_,noncopyable>( "Derived", bp::no_init ); > > In which case above print statement starts to show mymodule.Derived. But I would > really prefer to avoid these exports for derived classes. After all RTTI > information should shoe Boost.Python whole hierarchy, isn't it? Why can't it > figure out that pointer produced by create actually convertible to Base*, even > though it knows nothing about Derived? It could, if we were willing to search the DAG of known classes rooted at Base. I always thought that would be terrible, but maybe not... after all, you can do it one level at a time. If you enter a ticket, I'll look into it. Please attach a minimal example. > Does any one have any workarounds? You found the only one I know of: register Derived. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From alanbaljeu at yahoo.com Tue Nov 11 17:37:19 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Tue, 11 Nov 2008 08:37:19 -0800 (PST) Subject: [C++-sig] PyEval_EvalCode Message-ID: <430343.28356.qm@web58303.mail.re3.yahoo.com> I was reading some legacy code we have here, and discovered an unexpected idiom. Starting from C++, we create a dictionary, store a few named constants in there, and then call PyEval_EvalCode passing in the dictionary. The code it calls is a bunch of python files generated from a CAD model, with no function definitions. It works of course. Question is, what do you think of this approach? What is a more typical idiom? Alan Baljeu __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com. From seefeld at sympatico.ca Tue Nov 11 22:25:00 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Tue, 11 Nov 2008 16:25:00 -0500 Subject: [C++-sig] PyEval_EvalCode In-Reply-To: <430343.28356.qm@web58303.mail.re3.yahoo.com> References: <430343.28356.qm@web58303.mail.re3.yahoo.com> Message-ID: <4919F82C.1010809@sympatico.ca> Alan Baljeu wrote: > I was reading some legacy code we have here, and discovered an unexpected idiom. Starting from C++, we create a dictionary, store a few named constants in there, and then call PyEval_EvalCode passing in the dictionary. The code it calls is a bunch of python files generated from a CAD model, with no function definitions. It works of course. Question is, what do you think of this approach? What is a more typical idiom? > I don't quite understand the question. What I think of this approach depends a lot on what you use it for. Typical for what ? Having a C++ application run some python script that has access to some of the application state (i.e., whatever you expose through the dictionary) is certainly a fine way to make your application scriptable. (FWIW, boost.python offers 'exec()' and 'eval()' for this.) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From alanbaljeu at yahoo.com Wed Nov 12 15:29:37 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Wed, 12 Nov 2008 06:29:37 -0800 (PST) Subject: [C++-sig] PyEval_EvalCode References: <430343.28356.qm@web58303.mail.re3.yahoo.com> <4919F82C.1010809@sympatico.ca> Message-ID: <81129.83155.qm@web58306.mail.re3.yahoo.com> My question probably derives from ignorance of the Python way. I kind of assumed people would advocate defining classes and methods, and then calling those methods. You apparently see things otherwise. One thing I'm considering is to make one layer of abstraction, and define a Python function which takes a dictionary and a script file name. I think this level would make the code more instrumentable than with all the stuff in C++. Alan Baljeu ----- Original Message ---- From: Stefan Seefeld To: Development of Python/C++ integration Sent: Tuesday, November 11, 2008 4:25:00 PM Subject: Re: [C++-sig] PyEval_EvalCode Alan Baljeu wrote: > I was reading some legacy code we have here, and discovered an unexpected idiom. Starting from C++, we create a dictionary, store a few named constants in there, and then call PyEval_EvalCode passing in the dictionary. The code it calls is a bunch of python files generated from a CAD model, with no function definitions. It works of course. Question is, what do you think of this approach? What is a more typical idiom? > I don't quite understand the question. What I think of this approach depends a lot on what you use it for. Typical for what ? Having a C++ application run some python script that has access to some of the application state (i.e., whatever you expose through the dictionary) is certainly a fine way to make your application scriptable. (FWIW, boost.python offers 'exec()' and 'eval()' for this.) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig __________________________________________________________________ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/ From seefeld at sympatico.ca Wed Nov 12 15:47:29 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 12 Nov 2008 09:47:29 -0500 Subject: [C++-sig] PyEval_EvalCode In-Reply-To: <81129.83155.qm@web58306.mail.re3.yahoo.com> References: <430343.28356.qm@web58303.mail.re3.yahoo.com> <4919F82C.1010809@sympatico.ca> <81129.83155.qm@web58306.mail.re3.yahoo.com> Message-ID: <491AEC81.1020303@sympatico.ca> Alan Baljeu wrote: > My question probably derives from ignorance of the Python way. I kind of assumed people would advocate defining classes and methods, and then calling those methods. You apparently see things otherwise. Not really. I just see it as an orthogonal problem to the question you asked in your previous mail. > One thing I'm considering is to make one layer of abstraction, and define a Python function which takes a dictionary and a script file name. I think this level would make the code more instrumentable than with all the stuff in C++. > Again, without at last a little bit of context (what is your application doing, and what part of that would you like to control via python scripts) it's very hard to have a meaningful discussion. Also, I'm not sure whether your question is really specific to 'eval', or whether you also include 'exec' into this discussion (eval evaluates an expression and returns the result, while exec runs code, not necessarily returning anything). It seems your question is all about the boundary / interface between the (C++) application and the script that gets invoked: What should the content of the dictionary be, and what is the script expected to do with it ? In the most trivial case such a script could simply store named values (numbers, strings) into the dictionary, which then get evaluated by the application. The script thus becomes a mere configuration file. Alternatively, you can inject functions or even objects into the dictionary, which the script is expected to interact with. How much 'Object-Orientedness' makes sense really depends on your needs, which you haven't talked about yet. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From alanbaljeu at yahoo.com Wed Nov 12 16:40:56 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Wed, 12 Nov 2008 07:40:56 -0800 (PST) Subject: [C++-sig] PyEval_EvalCode References: <430343.28356.qm@web58303.mail.re3.yahoo.com> <4919F82C.1010809@sympatico.ca> <81129.83155.qm@web58306.mail.re3.yahoo.com> <491AEC81.1020303@sympatico.ca> Message-ID: <948060.15054.qm@web58306.mail.re3.yahoo.com> >It seems your question is all about the boundary / interface between the (C++) application and the script that gets invoked: What should the content of the dictionary be, and what is the script expected to do with it ? >In the most trivial case such a script could simply store named values (numbers, strings) into the dictionary, which then get evaluated >by the application. The script thus becomes a mere configuration file. >Alternatively, you can inject functions or even objects into the dictionary, which the script is expected to interact with. How much 'Object-Orientedness' makes sense really depends on your needs, which you haven't >talked about yet. > >Regards, > Stefan Okay I'll try. I'm growing a CAD plug-in DLL. I have decided that the edit/compile/run/test cycle is too slow both because C++ is a bear, and because both compile and run steps are extremely slow in my environment. To solve this, I intend to write as much Python as possible, to drive C++ objects, so I won't have to recompile, or restart my environment. (Previous use of python had a slightly different goal: use Python minimally for a dynamic modeling problem) So Python code will consist of interactive and non-interactive scripts. These scripts receive objects, work with them, and return other objects. There are no persistent Python objects. There are probably Python classes designed as temporary wrappers of C++ classes. Steps 1) Embed Python in this C++ plug-in. [Done.] 2) Expose C++ objects (functions and/or data) defined by the plugin to Python. 3) Write code in Python to create and manipulate those objects. (exec, not eval) 4) Write code in C++, but only when required to make (2) easy. (Abundant existing code may hinder my goals, and force more C++) 5) Develop a library of unittest methods. __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com. From seefeld at sympatico.ca Wed Nov 12 16:48:41 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 12 Nov 2008 10:48:41 -0500 Subject: [C++-sig] PyEval_EvalCode In-Reply-To: <948060.15054.qm@web58306.mail.re3.yahoo.com> References: <430343.28356.qm@web58303.mail.re3.yahoo.com> <4919F82C.1010809@sympatico.ca> <81129.83155.qm@web58306.mail.re3.yahoo.com> <491AEC81.1020303@sympatico.ca> <948060.15054.qm@web58306.mail.re3.yahoo.com> Message-ID: <491AFAD9.3020203@sympatico.ca> Alan Baljeu wrote: > To solve this, I intend to write as much Python as possible, to drive > C++ objects, so I won't have to recompile, or restart my environment. > (Previous use of python had a slightly different goal: use Python > minimally for a dynamic modeling problem) So Python code will consist > of interactive and non-interactive scripts. These scripts receive > objects, work with them, and return other objects. There are no > persistent Python objects. There are probably Python classes designed > as temporary wrappers of C++ classes. > Steps > 1) Embed Python in this C++ plug-in. [Done.] > 2) Expose C++ objects (functions and/or data) defined by the plugin to Python. > 3) Write code in Python to create and manipulate those objects. (exec, not eval) > 4) Write code in C++, but only when required to make (2) easy. (Abundant existing code may hinder my goals, and force more C++) > 5) Develop a library of unittest methods. > > This sounds perfectly reasonable to me, FWIW. An interesting question then is how you embed your interactive Python shell into the application's main event loop. But that's mainly an implementation detail. :-) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From alanbaljeu at yahoo.com Wed Nov 12 17:09:18 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Wed, 12 Nov 2008 08:09:18 -0800 (PST) Subject: [C++-sig] PyEval_EvalCode References: <430343.28356.qm@web58303.mail.re3.yahoo.com> <4919F82C.1010809@sympatico.ca> <81129.83155.qm@web58306.mail.re3.yahoo.com> <491AEC81.1020303@sympatico.ca> <948060.15054.qm@web58306.mail.re3.yahoo.com> <491AFAD9.3020203@sympatico.ca> Message-ID: <806070.28447.qm@web58306.mail.re3.yahoo.com> >This sounds perfectly reasonable to me, FWIW. >An interesting question then is how you embed your interactive Python shell into the application's main event loop. But that's mainly an implementation detail. :-) > >Regards, > Stefan Well, rendering is on a separate thread, so no issue there. My plan is: 1) create a console 2) type text into the console 3) ask you guys how to get the interactive Python shell to respond to that text. If there's another option besides a console window, I'm interested. __________________________________________________________________ Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail. Click on Options in Mail and switch to New Mail today or register for free at http://mail.yahoo.ca From dave at boostpro.com Wed Nov 12 17:31:21 2008 From: dave at boostpro.com (David Abrahams) Date: Wed, 12 Nov 2008 11:31:21 -0500 Subject: [C++-sig] PyEval_EvalCode In-Reply-To: <806070.28447.qm@web58306.mail.re3.yahoo.com> (Alan Baljeu's message of "Wed, 12 Nov 2008 08:09:18 -0800 (PST)") References: <430343.28356.qm@web58303.mail.re3.yahoo.com> <4919F82C.1010809@sympatico.ca> <81129.83155.qm@web58306.mail.re3.yahoo.com> <491AEC81.1020303@sympatico.ca> <948060.15054.qm@web58306.mail.re3.yahoo.com> <491AFAD9.3020203@sympatico.ca> <806070.28447.qm@web58306.mail.re3.yahoo.com> Message-ID: <87skpwucrq.fsf@mcbain.luannocracy.com> on Wed Nov 12 2008, Alan Baljeu wrote: >>This sounds perfectly reasonable to me, FWIW. >>An interesting question then is how you embed your interactive Python shell into the > application's main event loop. But that's mainly an implementation detail. :-) >> >>Regards, > > Stefan > > Well, rendering is on a separate thread, so no issue there. My plan is: > 1) create a console > 2) type text into the console > 3) ask you guys how to get the interactive Python shell to respond to that text. That looks like a question someone on the regular Python (non-C++) list would likely be better at answering. > If there's another option besides a console window, I'm interested. And that one too. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From renatox at gmail.com Wed Nov 12 17:58:28 2008 From: renatox at gmail.com (Renato Araujo) Date: Wed, 12 Nov 2008 13:58:28 -0300 Subject: [C++-sig] c++ object deleted Message-ID: <95291a80811120858l78a385c3lf23dff87ae0cbc36@mail.gmail.com> Hi all, Is possible in boost::python create a exception when the c++ object is deleted internal, and you try use the python object. Because here I got a core when I try do this. BR -- Renato Araujo Oliveira Filho From seefeld at sympatico.ca Wed Nov 12 18:14:16 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 12 Nov 2008 12:14:16 -0500 Subject: [C++-sig] c++ object deleted In-Reply-To: <95291a80811120858l78a385c3lf23dff87ae0cbc36@mail.gmail.com> References: <95291a80811120858l78a385c3lf23dff87ae0cbc36@mail.gmail.com> Message-ID: <491B0EE8.3070405@sympatico.ca> Renato Araujo wrote: > Hi all, > > Is possible in boost::python create a exception when the c++ object is > deleted internal, and you try use the python object. Because here I > got a core when I try do this. > I'm not sure whether it's technically possible, but I do believe this is a bad idea: Exceptions are just the wrong tool to deal with programming errors. Instead, you need to figure out what the expected lifetime of your object is and why it got deleted before. Once you understand this, modify your boost.python wrappers to use the appropriate return-value (or call) policies so this doesn't happen again. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From paul at pecm.nl Wed Nov 12 19:22:24 2008 From: paul at pecm.nl (Paul Melis) Date: Wed, 12 Nov 2008 19:22:24 +0100 Subject: [C++-sig] Manage new object return policy (was Re: new to python; old to C++) In-Reply-To: <874p2f44ps.fsf@mcbain.luannocracy.com> References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> <87mygie0h7.fsf@mcbain.luannocracy.com> <49188476.6060409@pecm.nl> <874p2f44ps.fsf@mcbain.luannocracy.com> Message-ID: <491B1EE0.7070500@pecm.nl> David Abrahams wrote: > on Mon Nov 10 2008, Paul Melis wrote: > > >> The FAQ entry on this doesn't really help, so perhaps I'm not using the return policy >> correctly or missing something else. The full test code (which is actually quite >> small) is attached. >> > > Please reduce it to its absolute minimum. Remove every single thing you > don't need in order to produce the result. If the problem isn't > glaringly obvious to you then, I will help. > Well, I tried reducing in several different ways. The bottom line is that I can't seem to produce this without using the wrapper class (or perhaps I missed the right combination of return policies). Simplest test case (see below for the rest of the files): #!/usr/bin/env python from boost import * class MyCallback(Callback): def __init__(self): Callback.__init__(self) def __call__(self): return Value() cb = MyCallback() r = go(cb) Storing a reference to the Value instance returned in __call__ keeps the instance alive, so the dangling pointer is caused by the Value instance that is returned getting destructed. But this only happens when the instance is passed through go() as its return value. When calling cb directly the Value is not destructed (as I would expect). What I don't understand is why it is destructed when it *is* passed through go(), shouldn't the manage_new_object policy keep it alive? Regards, Paul // classes.h #ifndef CLASSES_H #define CLASSES_H #include class Value { public: Value() { printf("Value::Value()\n"); } Value(const Value& v) { printf("Value::Value(const Value& v)\n"); } ~Value() { printf("Value::~Value()\n"); } }; class Callback { public: Callback() {} Callback(const Callback& cb) {} virtual ~Callback() {} // Returns new Value instance virtual Value* operator()() { return new Value(); } }; Value *go(Callback *cb) { return (*cb)(); }; #endif // boost.cpp // g++ -shared -fPIC -O3 -o boost.so -I/usr/include/python2.5 boost.cpp -lboost_python -lpython2.5 #include #include "classes.h" namespace bp = boost::python; struct CallbackWrap : Callback, bp::wrapper { virtual Value *operator()() { printf("CallbackWrap::operator()\n"); if (bp::override call = this->get_override("__call__")) { printf("Have override for __call__, calling it\n"); return call(); } return Callback::operator()(); } Value *default_call() { printf("CallbackWrap::default_call()\n"); return this->Callback::operator()(); } }; BOOST_PYTHON_MODULE(boost) { bp::class_< Value >("Value"); bp::class_< CallbackWrap, boost::noncopyable >("Callback") .def("__call__", &Callback::operator(), &CallbackWrap::default_call, bp::return_value_policy()); bp::def("go", &go, bp::return_value_policy()); } From renatox at gmail.com Wed Nov 12 19:09:57 2008 From: renatox at gmail.com (Renato Araujo) Date: Wed, 12 Nov 2008 15:09:57 -0300 Subject: [C++-sig] c++ object deleted In-Reply-To: <491B0EE8.3070405@sympatico.ca> References: <95291a80811120858l78a385c3lf23dff87ae0cbc36@mail.gmail.com> <491B0EE8.3070405@sympatico.ca> Message-ID: <95291a80811121009u4aee4747m24b678faa860b9ce@mail.gmail.com> The main problem here is my parent control. My object life is controlled by the parent life time: somethin like that: struct object { object(object *parent) { parent.add_child(this); } ~object() } On Wed, Nov 12, 2008 at 2:14 PM, Stefan Seefeld wrote: > Renato Araujo wrote: >> >> Hi all, >> >> Is possible in boost::python create a exception when the c++ object is >> deleted internal, and you try use the python object. Because here I >> got a core when I try do this. >> > > I'm not sure whether it's technically possible, but I do believe this is a > bad idea: Exceptions are just the wrong tool to deal with programming > errors. Instead, you need to figure out what the expected lifetime of your > object is and why it got deleted before. Once you understand this, modify > your boost.python wrappers to use the appropriate return-value (or call) > policies so this doesn't happen again. > > Regards, > Stefan > > -- > > ...ich hab' noch einen Koffer in Berlin... > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Renato Araujo Oliveira Filho From dave at boostpro.com Wed Nov 12 20:51:01 2008 From: dave at boostpro.com (David Abrahams) Date: Wed, 12 Nov 2008 14:51:01 -0500 Subject: [C++-sig] Manage new object return policy (was Re: new to python; old to C++) In-Reply-To: <491B1EE0.7070500@pecm.nl> (Paul Melis's message of "Wed, 12 Nov 2008 19:22:24 +0100") References: <855200.53735.qm@web58305.mail.re3.yahoo.com> <60825b0f0811010046i6af8a8bk3f9754af530812ce@mail.gmail.com> <490C41FE.5000100@pecm.nl> <87mygie0h7.fsf@mcbain.luannocracy.com> <49188476.6060409@pecm.nl> <874p2f44ps.fsf@mcbain.luannocracy.com> <491B1EE0.7070500@pecm.nl> Message-ID: <87abc4soyi.fsf@mcbain.luannocracy.com> on Wed Nov 12 2008, Paul Melis wrote: > David Abrahams wrote: >> on Mon Nov 10 2008, Paul Melis wrote: >> >> >>> The FAQ entry on this doesn't really help, so perhaps I'm not using the return > policy >>> correctly or missing something else. The full test code (which is actually quite >>> small) is attached. >>> >> >> Please reduce it to its absolute minimum. Remove every single thing you >> don't need in order to produce the result. If the problem isn't >> glaringly obvious to you then, I will help. >> > Well, I tried reducing in several different ways. The bottom line is > that I can't seem to produce this without using the wrapper class (or > perhaps I missed the right combination of return policies). This is a classic example of the FAQ entry. See below. > Simplest test case (see below for the rest of the files): > > #!/usr/bin/env python > from boost import * > > class MyCallback(Callback): > > def __init__(self): > Callback.__init__(self) > > def __call__(self): > return Value() A new C++ Value instance is created here, managed by a Python Value instance. When this function returns, its caller's stack frame will own the only reference to Value. When this is invoked from C++ as an override of a C++ virtual function, its caller is a function in Boost.Python. > > cb = MyCallback() > > r = go(cb) > > > Storing a reference to the Value instance returned in __call__ keeps the > instance alive, so the dangling pointer is caused by the Value instance > that is returned getting destructed. But this only happens when the > instance is passed through go() as its return value. When calling cb > directly the Value is not destructed (as I would expect). What I don't > understand is why it is destructed when it *is* passed through go(), > shouldn't the manage_new_object policy keep it alive? I hope the other things I've written above and below answer the first question. As for the second one, manage_new_object says to manage a new C++ object's lifetime, and the problem you have is that a Python object already owns the C++ object, and that object will go away. I might have a hunch about how to fix some of these problems using a Python WeakPtr, but unfortunately I don't have any time right now to work on it. Best I can do is suggest you open a Trac ticket referencing this thread. > Regards, > Paul > > // classes.h > > #ifndef CLASSES_H > #define CLASSES_H > > #include > > class Value > { > public: > Value() { printf("Value::Value()\n"); } > Value(const Value& v) { printf("Value::Value(const Value& v)\n"); } > ~Value() { printf("Value::~Value()\n"); } > }; > > class Callback > { > public: > Callback() {} > Callback(const Callback& cb) {} > virtual ~Callback() {} > // Returns new Value instance > virtual Value* operator()() { return new Value(); } This one doesn't get executed; it's been overridden by CallbackWrap. > }; > > Value *go(Callback *cb) > { > return (*cb)(); > }; > > #endif > > > // boost.cpp > > // g++ -shared -fPIC -O3 -o boost.so -I/usr/include/python2.5 boost.cpp > -lboost_python -lpython2.5 > > #include > #include "classes.h" > > namespace bp = boost::python; > > struct CallbackWrap : Callback, bp::wrapper > { > virtual Value *operator()() > { > printf("CallbackWrap::operator()\n"); > > if (bp::override call = this->get_override("__call__")) > { A python override has been found here > printf("Have override for __call__, calling it\n"); > return call(); It is invoked in the line above. To return control to you, Boost.Python will reach into the Python Value object to pull out a pointer to the C++ Value object it manages. However, in your case, the reference count on the Python object is 1, and the C++ object will have been deleted by its owner Python object by the time the library returns. > } > > return Callback::operator()(); > } > > Value *default_call() > { > printf("CallbackWrap::default_call()\n"); > return this->Callback::operator()(); > } > }; > > > BOOST_PYTHON_MODULE(boost) > { > bp::class_< Value >("Value"); > > bp::class_< CallbackWrap, boost::noncopyable >("Callback") > .def("__call__", &Callback::operator(), > &CallbackWrap::default_call, > bp::return_value_policy()); > > bp::def("go", &go, bp::return_value_policy()); > } > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Dave Abrahams BoostPro Computing http://www.boostpro.com From alanbaljeu at yahoo.com Thu Nov 13 15:40:31 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Thu, 13 Nov 2008 06:40:31 -0800 (PST) Subject: [C++-sig] looking up functions Message-ID: <86412.25910.qm@web58301.mail.re3.yahoo.com> I'm reading the Python extension tutorial, and I cannot believe the embedding section. It tells me the only way to call a python function is to callout to python to pass back a function object which I then save so I can call it when I need to. This seems ridiculous. Surely there is a way to obtain Python function objects without going through that! Does somebody have a way to ask for a function by name? Also, perhaps somebody can explain if there is something about Python that makes this difficult, why such is not the default way of doing interop. Alan Baljeu __________________________________________________________________ Reclaim your name @ymail.com or @rocketmail.com. Get your new email address now! Go to http://ca.promos.yahoo.com/jacko/ From gjcarneiro at gmail.com Thu Nov 13 15:48:43 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Thu, 13 Nov 2008 14:48:43 +0000 Subject: [C++-sig] looking up functions In-Reply-To: <86412.25910.qm@web58301.mail.re3.yahoo.com> References: <86412.25910.qm@web58301.mail.re3.yahoo.com> Message-ID: 2008/11/13 Alan Baljeu > I'm reading the Python extension tutorial, and I cannot believe the > embedding section. It tells me the only way to call a python function is to > callout to python to pass back a function object which I then save so I can > call it when I need to. This seems ridiculous. Surely there is a way to > obtain Python function objects without going through that! Does somebody > have a way to ask for a function by name? PyObject *function_object = PyObject_GetAttrString (PyModule_Import ("modulename"), "function_name"); Leaks a module object reference, but you get the idea... -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Thu Nov 13 15:49:12 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 13 Nov 2008 09:49:12 -0500 Subject: [C++-sig] looking up functions In-Reply-To: <86412.25910.qm@web58301.mail.re3.yahoo.com> References: <86412.25910.qm@web58301.mail.re3.yahoo.com> Message-ID: <491C3E68.1070107@sympatico.ca> Alan Baljeu wrote: > I'm reading the Python extension tutorial, and I cannot believe the embedding section. It tells me the only way to call a python function is to callout to python to pass back a function object which I then save so I can call it when I need to. This seems ridiculous. Surely there is a way to obtain Python function objects without going through that! Does somebody have a way to ask for a function by name? Ask whom ? Obtain from where ? > > > Also, perhaps somebody can explain if there is something about Python that makes this difficult, why such is not the default way of doing interop. > Sorry, I don't understand what you are asking. What is not the default, and what is made difficult ? Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From meine at informatik.uni-hamburg.de Thu Nov 13 15:49:37 2008 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Thu, 13 Nov 2008 15:49:37 +0100 Subject: [C++-sig] looking up functions In-Reply-To: <86412.25910.qm@web58301.mail.re3.yahoo.com> References: <86412.25910.qm@web58301.mail.re3.yahoo.com> Message-ID: <200811131549.37513.meine@informatik.uni-hamburg.de> On Thursday 13 November 2008 15:40:31 Alan Baljeu wrote: > I'm reading the Python extension tutorial, and I cannot believe the > embedding section. It tells me the only way to call a python function is > to callout to python to pass back a function object which I then save so I > can call it when I need to. This seems ridiculous. Surely there is a way > to obtain Python function objects without going through that! Does > somebody have a way to ask for a function by name? I don't see the problem. AFAICS, "ask for a function by name" is "ask python [i.e. callout to python] for a function [which is an object that I can safe when I want to call it repeatedly] by name". Which part could be simpler? Greetings, Hans From alanbaljeu at yahoo.com Thu Nov 13 16:08:53 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Thu, 13 Nov 2008 07:08:53 -0800 (PST) Subject: [C++-sig] looking up functions References: <86412.25910.qm@web58301.mail.re3.yahoo.com> Message-ID: <595578.63507.qm@web58304.mail.re3.yahoo.com> Hans Meine wrote: >I don't see the problem. AFAICS, "ask for a function by name" is "ask python >[i.e. callout to python] for a function [which is an object that I can safe >when I want to call it repeatedly] by name". Which part could be simpler? As best I could figure, I needed to write Python code, execute a script file, get that code to call a C function that I register, in order to have that function. At least the tutorial implied that was the way. Gustavo gives a better approach. Thanks. Alan Baljeu ________________________________ From: Gustavo Carneiro To: Development of Python/C++ integration Sent: Thursday, November 13, 2008 9:48:43 AM Subject: Re: [C++-sig] looking up functions 2008/11/13 Alan Baljeu I'm reading the Python extension tutorial, and I cannot believe the embedding section. It tells me the only way to call a python function is to callout to python to pass back a function object which I then save so I can call it when I need to. This seems ridiculous. Surely there is a way to obtain Python function objects without going through that! Does somebody have a way to ask for a function by name? PyObject *function_object = PyObject_GetAttrString (PyModule_Import ("modulename"), "function_name"); Leaks a module object reference, but you get the idea... -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert __________________________________________________________________ Get a sneak peak at messages with a handy reading pane with All new Yahoo! Mail: http://ca.promos.yahoo.com/newmail/overview2/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Thu Nov 13 16:23:24 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 13 Nov 2008 10:23:24 -0500 Subject: [C++-sig] looking up functions In-Reply-To: <595578.63507.qm@web58304.mail.re3.yahoo.com> References: <86412.25910.qm@web58301.mail.re3.yahoo.com> <595578.63507.qm@web58304.mail.re3.yahoo.com> Message-ID: <491C466C.5000808@sympatico.ca> Alan Baljeu wrote: > > As best I could figure, I needed to write Python code, execute a > script file, get that code to call a C function that I register, in > order to have that function. At least the tutorial implied that was > the way. I'm confused. In your last mail you asked for how to get hold of a Python function so you could run it from within C++. Now you want to run a C function instead ? Python is an interpreted language, so you need to read (interpret) the code that you then want to run. I asked where the function that you want to store and run comes from. If it already exists in a module, you can simply import that module (using boost::python::import()), and extract the function from it: object module = import("your_module"); object function = module["your_function"]; function(); // call it If you don't want to import a module directly, but rather run a script, use exec() instead. I'm not sure how this could be any simpler. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From alanbaljeu at yahoo.com Thu Nov 13 16:35:43 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Thu, 13 Nov 2008 07:35:43 -0800 (PST) Subject: [C++-sig] looking up functions References: <86412.25910.qm@web58301.mail.re3.yahoo.com> <595578.63507.qm@web58304.mail.re3.yahoo.com> <491C466C.5000808@sympatico.ca> Message-ID: <922466.10490.qm@web58308.mail.re3.yahoo.com> I cryptically wrote: > As best I could figure, I needed to write Python code, execute a script file, get that code to call a C function that I register, in order to have that function. At least the tutorial implied that was the way. Which made Stefan respond: >I'm confused. In your last mail you asked for how to get hold of a Python function so you could run it from within C++. Now you want to run a C function instead ? I misspoke. I want a Python function. I was confused by the lack of any PyModule_GetFunction or anything resembling that in the API. >Python is an interpreted language, so you need to read (interpret) the code that you then want to run. >I asked where the function that you want to store and run comes from. If it already exists in a module, >you can simply import that module (using boost::python::import()), and extract the function from it: >object module = import("your_module"); >object function = module["your_function"]; >function(); // call it I take it this is Boost? I haven't looked at that. I was just studying the built-in API. >If you don't want to import a module directly, but rather run a script, use exec() instead. >I'm not sure how this could be any simpler. I agree. This is exactly what I was looking for but failed to find. __________________________________________________________________ Get the name you've always wanted @ymail.com or @rocketmail.com! Go to http://ca.promos.yahoo.com/jacko/ From seefeld at sympatico.ca Thu Nov 13 16:53:37 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 13 Nov 2008 10:53:37 -0500 Subject: [C++-sig] looking up functions In-Reply-To: <922466.10490.qm@web58308.mail.re3.yahoo.com> References: <86412.25910.qm@web58301.mail.re3.yahoo.com> <595578.63507.qm@web58304.mail.re3.yahoo.com> <491C466C.5000808@sympatico.ca> <922466.10490.qm@web58308.mail.re3.yahoo.com> Message-ID: <491C4D81.1040802@sympatico.ca> Alan Baljeu wrote: > > I misspoke. I want a Python function. I was confused by the lack of any PyModule_GetFunction or anything resembling that in the API. > This is because there is nothing special about functions, as far as retrieving them from a module('s namespace) is concerned. You just ask for an object, and then eventually call it. Of course, Python and C (C++) are sufficiently different to make this quite laboreous, when doing it 'by hand': As Python is dynamically typed, you need to wrap your function arguments up into tuples and dictionaries, be careful about reference counting, etc., etc. boost.python can be extremely helpful in making the required code concise. >> object module = import("your_module"); >> object function = module["your_function"]; >> function(); // call it >> > > I take it this is Boost? I haven't looked at that. I was just studying the built-in API. > Ah, that was another misunderstanding of mine. I thought that when you were talking about the 'embedding tutorial' you were referring to http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/python/embedding.html Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From dave at boostpro.com Thu Nov 13 17:13:23 2008 From: dave at boostpro.com (David Abrahams) Date: Thu, 13 Nov 2008 11:13:23 -0500 Subject: [C++-sig] looking up functions In-Reply-To: <491C466C.5000808@sympatico.ca> (Stefan Seefeld's message of "Thu, 13 Nov 2008 10:23:24 -0500") References: <86412.25910.qm@web58301.mail.re3.yahoo.com> <595578.63507.qm@web58304.mail.re3.yahoo.com> <491C466C.5000808@sympatico.ca> Message-ID: <878wrnppss.fsf@mcbain.luannocracy.com> on Thu Nov 13 2008, Stefan Seefeld wrote: > Alan Baljeu wrote: >> >> As best I could figure, I needed to write Python code, execute a script file, get >> that code to call a C function that I register, in order to have that function. At >> least the tutorial implied that was the way. > > I'm confused. In your last mail you asked for how to get hold of a Python function so > you could run it from within C++. Now you want to run a C function instead ? > > Python is an interpreted language, so you need to read (interpret) the code that you > then want to run. I asked where the function that you want to store and run comes > from. If it already exists in a module, you can simply import that module (using > boost::python::import()), and extract the function from it: > > object module = import("your_module"); > object function = module["your_function"]; object function = module.attr("your_function"); right? > function(); // call it > > If you don't want to import a module directly, but rather run a script, use exec() > instead. > > I'm not sure how this could be any simpler. :-) -- Dave Abrahams BoostPro Computing http://www.boostpro.com From renatox at gmail.com Thu Nov 13 17:27:17 2008 From: renatox at gmail.com (Renato Araujo) Date: Thu, 13 Nov 2008 13:27:17 -0300 Subject: [C++-sig] looking up functions In-Reply-To: <86412.25910.qm@web58301.mail.re3.yahoo.com> References: <86412.25910.qm@web58301.mail.re3.yahoo.com> Message-ID: <95291a80811130827v654974efn1c7b58b79036a891@mail.gmail.com> you can use: python::object callback = obj.attr["function_name"]; callback(); On Thu, Nov 13, 2008 at 11:40 AM, Alan Baljeu wrote: > I'm reading the Python extension tutorial, and I cannot believe the embedding section. It tells me the only way to call a python function is to callout to python to pass back a function object which I then save so I can call it when I need to. This seems ridiculous. Surely there is a way to obtain Python function objects without going through that! Does somebody have a way to ask for a function by name? > > Also, perhaps somebody can explain if there is something about Python that makes this difficult, why such is not the default way of doing interop. > > Alan Baljeu > > > __________________________________________________________________ > Reclaim your name @ymail.com or @rocketmail.com. Get your new email address now! Go to http://ca.promos.yahoo.com/jacko/ > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- BR Renato Araujo Oliveira Filho From tjhnson at gmail.com Sat Nov 15 12:08:42 2008 From: tjhnson at gmail.com (T J) Date: Sat, 15 Nov 2008 03:08:42 -0800 Subject: [C++-sig] C++ Signature Message-ID: For boost.python, the C++ signature is shown with a particular header [python/src/object/function_doc_signature.cpp]: char cpp_signature_tag[] = "C++ signature :"; To conform to a reST docstring format, I'd like to change it to: char cpp_signature_tag[] = "C++ signature:\n==============\n"; Is this possible without recompiling boost? Ideally, I'd like to this to be user-definable when I create my module. From paniq at paniq.org Sat Nov 15 17:11:30 2008 From: paniq at paniq.org (Leonard Ritter) Date: Sat, 15 Nov 2008 17:11:30 +0100 Subject: [C++-sig] wrapper and shared_ptr - how to combine? Message-ID: <32c88000811150811p545ccfaaqc8a566ee1301dbc1@mail.gmail.com> i am wrapping an abstract class "Actor" which is to be implemented partially in Python, using a wrapper class that looks like this: class PyActor : public Actor, public wrapper { public: .... }; the boost.python export code looks like this: class_("Actor"); this would be all good and well, until I decided to pass back Actor pointers from methods e.g. like Actor::get_parent(), which have to resolve back to original python objects, for which the FAQ suggests using shared_ptr, if altering the underlying code was possible - which it is. so there is now a method in Actor declared like this: // assumed: typedef boost::shared_ptr ActorPtr; ActorPtr Actor::get_parent(); unfortunately calling that method gives me a new wrapped Actor python object - not exactly what the FAQ promised. but I read that the shared_ptr<> has to be declared as the object holder, like this: class_ From seefeld at sympatico.ca Sat Nov 15 17:20:00 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Sat, 15 Nov 2008 11:20:00 -0500 Subject: [C++-sig] wrapper and shared_ptr - how to combine? In-Reply-To: <32c88000811150811p545ccfaaqc8a566ee1301dbc1@mail.gmail.com> References: <32c88000811150811p545ccfaaqc8a566ee1301dbc1@mail.gmail.com> Message-ID: <491EF6B0.3000208@sympatico.ca> Leonard Ritter wrote: > > class_ > which I did, but that gives me a compiler error if Actor is abstract, > and if I declare it non-abstract, calls to wrapping PyActor methods > fail in Python. I guess that this approach does not work with wrapper > classes, or the syntax is different. Please post specific code as well as specific error messages. It sounds as if you expect those methods to return references to existing (shared) objects, while boost.python expects by-value passing, i.e. wants to make copies. But without more information this is just a guess. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From paniq at paniq.org Sat Nov 15 19:07:52 2008 From: paniq at paniq.org (Leonard Ritter) Date: Sat, 15 Nov 2008 19:07:52 +0100 Subject: [C++-sig] wrapper and shared_ptr - how to combine? In-Reply-To: <491EF6B0.3000208@sympatico.ca> References: <32c88000811150811p545ccfaaqc8a566ee1301dbc1@mail.gmail.com> <491EF6B0.3000208@sympatico.ca> Message-ID: <32c88000811151007r1ef9d422x2d9a6a80ca993002@mail.gmail.com> yes, that's what i want. what details do you need? i mean, the problem posted is quite generic. i want to pass shared_ptr's of abstract classes with overridable methods back and forth from c++ to python and back. is this supposed to work, if yes, how? On Sat, Nov 15, 2008 at 5:20 PM, Stefan Seefeld wrote: > Leonard Ritter wrote: > >> >> class_> >> which I did, but that gives me a compiler error if Actor is abstract, and >> if I declare it non-abstract, calls to wrapping PyActor methods fail in >> Python. I guess that this approach does not work with wrapper classes, or >> the syntax is different. >> > Please post specific code as well as specific error messages. It sounds as > if you expect those methods to return references to existing (shared) > objects, while boost.python expects by-value passing, i.e. wants to make > copies. But without more information this is just a guess. > > Regards, > Stefan > > -- > > ...ich hab' noch einen Koffer in Berlin... > > _______________________________________________ > 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 paniq at paniq.org Sat Nov 15 20:48:13 2008 From: paniq at paniq.org (Leonard Ritter) Date: Sat, 15 Nov 2008 20:48:13 +0100 Subject: [C++-sig] wrapper and shared_ptr - how to combine? In-Reply-To: <32c88000811151007r1ef9d422x2d9a6a80ca993002@mail.gmail.com> References: <32c88000811150811p545ccfaaqc8a566ee1301dbc1@mail.gmail.com> <491EF6B0.3000208@sympatico.ca> <32c88000811151007r1ef9d422x2d9a6a80ca993002@mail.gmail.com> Message-ID: <32c88000811151148j737fc2a2m9d6a2905b3ea63a@mail.gmail.com> I think I know what the issue is, and it indeed relates to a piece of code I have not posted yet. void Actor::add(const ActorPtr &actor) { assert(actor); assert(!actor->parent); actor->parent = ActorPtr(this); children.push_back(actor); queue_redraw(); } Actor is a tree element with children and a parent pointer. I guess that assigning ActorPtr(this) appears to construct a new pointer for an object that is already registered to boost.python. On Sat, Nov 15, 2008 at 7:07 PM, Leonard Ritter wrote: > yes, that's what i want. what details do you need? i mean, the problem > posted is quite generic. i want to pass shared_ptr's of abstract classes > with overridable methods back and forth from c++ to python and back. is this > supposed to work, if yes, how? > > > On Sat, Nov 15, 2008 at 5:20 PM, Stefan Seefeld wrote: > >> Leonard Ritter wrote: >> >>> >>> class_>> >>> which I did, but that gives me a compiler error if Actor is abstract, and >>> if I declare it non-abstract, calls to wrapping PyActor methods fail in >>> Python. I guess that this approach does not work with wrapper classes, or >>> the syntax is different. >>> >> Please post specific code as well as specific error messages. It sounds as >> if you expect those methods to return references to existing (shared) >> objects, while boost.python expects by-value passing, i.e. wants to make >> copies. But without more information this is just a guess. >> >> Regards, >> Stefan >> >> -- >> >> ...ich hab' noch einen Koffer in Berlin... >> >> _______________________________________________ >> 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 paniq at paniq.org Sat Nov 15 21:58:45 2008 From: paniq at paniq.org (Leonard Ritter) Date: Sat, 15 Nov 2008 21:58:45 +0100 Subject: [C++-sig] wrapper and shared_ptr - how to combine? In-Reply-To: <32c88000811151148j737fc2a2m9d6a2905b3ea63a@mail.gmail.com> References: <32c88000811150811p545ccfaaqc8a566ee1301dbc1@mail.gmail.com> <491EF6B0.3000208@sympatico.ca> <32c88000811151007r1ef9d422x2d9a6a80ca993002@mail.gmail.com> <32c88000811151148j737fc2a2m9d6a2905b3ea63a@mail.gmail.com> Message-ID: <32c88000811151258h7ba10e1aif18ab013db4434df@mail.gmail.com> It seems I still have no idea what is going on, and I'm starting to become a bit desparate. I'm going to post as much as possible about the issue I'm having. this is the smallest reproduce for the problem i'm having. typedef boost::shared_ptr ActorPtr; class Actor { public: ActorPtr parent; std::vector children; void add(const ActorPtr &actor); void remove(const ActorPtr &actor); virtual void queue_redraw() = 0; }; class PyActor : public Actor, public wrapper { public: virtual void queue_redraw() { get_override("queue_redraw")(); } }; BOOST_PYTHON_MODULE(nucleus) { class_("Actor") .def_readwrite("children", &PyActor::children) .def_readwrite("parent", &PyActor::parent) .def("add", &PyActor::add) .def("remove", &PyActor::remove) .def("queue_redraw", pure_virtual(&Actor::queue_redraw)) ; } ================ yields =================== /usr/include/boost/python/object/pointer_holder.hpp: In constructor 'boost::python::objects::pointer_holder::pointer_holder(PyObject*) [with Pointer = boost::shared_ptr, Value = halebopp::Actor]': /usr/include/boost/python/object/make_holder.hpp:83: instantiated from 'static void boost::python::objects::make_holder<0>::apply::execute(PyObject*) [with Holder = boost::python::objects::pointer_holder, halebopp::Actor>, ArgList = boost::mpl::vector0]' /usr/include/boost/python/detail/make_keyword_range_fn.hpp:64: instantiated from 'boost::python::api::object boost::python::detail::make_keyword_range_constructor(const CallPolicies&, const boost::python::detail::keyword_range&, Holder*, ArgList*, Arity*) [with ArgList = boost::mpl::vector0, Arity = boost::mpl::size >, Holder = boost::python::objects::pointer_holder, halebopp::Actor>, CallPolicies = boost::python::default_call_policies]' /usr/include/boost/python/init.hpp:330: instantiated from 'void boost::python::detail::def_init_aux(ClassT&, const Signature&, NArgs, const CallPoliciesT&, const char*, const boost::python::detail::keyword_range&) [with ClassT = boost::python::class_, boost::noncopyable_::noncopyable, boost::python::detail::not_specified>, CallPoliciesT = boost::python::default_call_policies, Signature = boost::mpl::vector0, NArgs = boost::mpl::size >]' /usr/include/boost/python/init.hpp:399: instantiated from 'static void boost::python::detail::define_class_init_helper<0>::apply(ClassT&, const CallPoliciesT&, const Signature&, NArgs, const char*, const boost::python::detail::keyword_range&) [with ClassT = boost::python::class_, boost::noncopyable_::noncopyable, boost::python::detail::not_specified>, CallPoliciesT = boost::python::default_call_policies, Signature = boost::python::init_base::visit(classT&) const [with classT = boost::python::class_, boost::noncopyable_::noncopyable, boost::python::detail::not_specified>, DerivedT = boost::python::init]::signature, NArgs = boost::mpl::size >]' /usr/include/boost/python/init.hpp:171: instantiated from 'void boost::python::init_base::visit(classT&) const [with classT = boost::python::class_, boost::noncopyable_::noncopyable, boost::python::detail::not_specified>, DerivedT = boost::python::init]' /usr/include/boost/python/def_visitor.hpp:31: instantiated from 'static void boost::python::def_visitor_access::visit(const V&, classT&) [with V = boost::python::def_visitor >, classT = boost::python::class_, boost::noncopyable_::noncopyable, boost::python::detail::not_specified>]' /usr/include/boost/python/def_visitor.hpp:67: instantiated from 'void boost::python::def_visitor::visit(classT&) const [with classT = boost::python::class_, boost::noncopyable_::noncopyable, boost::python::detail::not_specified>, DerivedVisitor = boost::python::init]' scons: building terminated because of errors. /usr/include/boost/python/class.hpp:225: instantiated from 'boost::python::class_& boost::python::class_::def(const boost::python::def_visitor&) [with Derived = boost::python::init, W = halebopp::python::PyActor, X1 = boost::shared_ptr, X2 = boost::noncopyable_::noncopyable, X3 = boost::python::detail::not_specified]' /usr/include/boost/python/class.hpp:501: instantiated from 'void boost::python::class_::initialize(const DefVisitor&) [with DefVisitor = boost::python::init, W = halebopp::python::PyActor, X1 = boost::shared_ptr, X2 = boost::noncopyable_::noncopyable, X3 = boost::python::detail::not_specified]' /usr/include/boost/python/class.hpp:629: instantiated from 'boost::python::class_::class_(const char*, const char*) [with W = halebopp::python::PyActor, X1 = boost::shared_ptr, X2 = boost::noncopyable_::noncopyable, X3 = boost::python::detail::not_specified]' src/nucleus/export_ogl.cpp:94: instantiated from here /usr/include/boost/python/object/pointer_holder.hpp:179: error: cannot allocate an object of abstract type 'halebopp::Actor' src/nucleus/ogl.hpp:38: note: because the following virtual functions are pure within 'halebopp::Actor': src/nucleus/ogl.hpp:74: note: virtual void halebopp::Actor::queue_redraw() -------------- next part -------------- An HTML attachment was scrubbed... URL: From azan00 at yahoo.com Sat Nov 15 23:26:13 2008 From: azan00 at yahoo.com (Brian Dawn) Date: Sat, 15 Nov 2008 14:26:13 -0800 (PST) Subject: [C++-sig] Set Path to Standard Modules Message-ID: <935491.3082.qm@web54402.mail.yahoo.com> Hello everyone! I am currently embedding python, and I would like to distribute the Python Standard Modules with my application, in the event that the end user does not have python installed. I have found that I can put the modules inside the directory with my exe and it will find them, but that is very cluttered. How can I specify a sub directory for the Standard Modules? Thanks, Brian. -------------- next part -------------- An HTML attachment was scrubbed... URL: From renjipanicker at gmail.com Sun Nov 16 17:40:28 2008 From: renjipanicker at gmail.com (Renji Panicker) Date: Sun, 16 Nov 2008 22:10:28 +0530 Subject: [C++-sig] Set Path to Standard Modules In-Reply-To: <935491.3082.qm@web54402.mail.yahoo.com> References: <935491.3082.qm@web54402.mail.yahoo.com> Message-ID: <3c0065cc0811160840v6e36c5a9sfb494f5fbb161481@mail.gmail.com> You could put them in a sub-dir called "Lib" inside the dir where your exe is. -/renji 2008/11/16 Brian Dawn : > Hello everyone! > > I am currently embedding python, and I would like to distribute the Python > Standard Modules with my application, in the event that the end user does > not have python installed. I have found that I can put the modules inside > the directory with my exe and it will find them, but that is very cluttered. > How can I specify a sub directory for the Standard Modules? > > Thanks, > Brian. > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > From sipickles at hotmail.com Sun Nov 16 19:02:17 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Sun, 16 Nov 2008 18:02:17 +0000 Subject: [C++-sig] Linking to libboost_python, on Ubuntu - I've forgotten something... Message-ID: Hello, I am setting up boost python on a new Ubuntu system. I thought I had everything configured correctly, but think I may have missed a vital step. When I run the python program which attempts to use the library generated by bjam, I get this: simon at urth-webserver:~/WorkingCopies/server/trunk/hybrid_test$ python hybridTest.py Traceback (most recent call last): File "hybridTest.py", line 2, in import Hybrid ImportError: libboost_python-gcc42-d-1_37.so.1.37.0: cannot open shared object file: No such file or directory I have built boost 1.37.0 from source using: sudo bjam -sPYTHON_ROOT=/usr/local/lib/python25 -sPYTHON_VERSION=2.5 --build-type=complete --toolset=gcc stage (its a stackless python build in /usr/local/lib/python25) I've set the config files I think: #-------------------------------------------------- # user-config.jam in home directory # Compiler configuration using gcc ; # Python configuration using python : 2.5 : /usr/local/bin/python ; #----------------------------------------------------------------- # boost-build.jam in project folder boost-build /home/simon/Source/boost_1_37_0/tools/build/v2 ; #----------------------------------------------------------------- # jamroot in project folder use-project boost : /home/simon/Source/boost_1_37_0 ; # Set up the project-wide requirements that everything uses the # boost_python library defined in the project whose global ID is # /boost/python. project Hybrid : requirements /boost/python//boost_python /home/simon/Source/bullet-2.72/src /home/simon/Source/boost_1_37_0/stage/lib/libboost_python-gcc42-d-1_37.so.1.37.0 ; ## Make the definition of the python-extension rule available import python ; # Declare a Python extension python-extension Hybrid : [ glob Log/*.cpp ] [ glob Object/*.cpp ] [ glob World/*.cpp ] HybridModule.cpp ; install . : Hybrid ; #--------------------------------------------------------------------- This build successfully with bjam, and I am left with the expected Hybrid.so in the project folder. As you can see from the jamroot file, I've even tried directly specifying the library libboost_python-gcc42-d-1_37.so.1.37.0 Can anyone suggest where I have missed a trick? Many thanks Simon -- Linux User# 424693 From kirit.saelensminde at gmail.com Mon Nov 17 09:29:46 2008 From: kirit.saelensminde at gmail.com (=?ISO-8859-1?Q?Kirit_S=E6lensminde?=) Date: Mon, 17 Nov 2008 15:29:46 +0700 Subject: [C++-sig] Linking to libboost_python, on Ubuntu - I've forgotten something... In-Reply-To: References: Message-ID: <49212B7A.3020602@gmail.com> Simon Pickles wrote: > When I run the python program which attempts to use the library > generated by bjam, I get this: > > simon at urth-webserver:~/WorkingCopies/server/trunk/hybrid_test$ python > hybridTest.py > Traceback (most recent call last): > File "hybridTest.py", line 2, in > import Hybrid > ImportError: libboost_python-gcc42-d-1_37.so.1.37.0: cannot open shared > object file: No such file or directory > > > I have built boost 1.37.0 from source using: > > sudo bjam -sPYTHON_ROOT=/usr/local/lib/python25 -sPYTHON_VERSION=2.5 > --build-type=complete --toolset=gcc stage Looks like you don't have the .so anywhere it can be found. You want to either set LD_LIBRARY_PATH to the location of your Boost .so files, or you can use the Bjam option to hard code the paths in your build Python .so. You could also install the Boost files to somewhere they will be found (/usr/local/lib/ should do). K From simon at science-projects.org Mon Nov 17 10:46:23 2008 From: simon at science-projects.org (Simon Pickles) Date: Mon, 17 Nov 2008 09:46:23 +0000 Subject: [C++-sig] Linking to libboost_python, on Ubuntu - I've forgotten something... In-Reply-To: <49212B7A.3020602@gmail.com> References: <49212B7A.3020602@gmail.com> Message-ID: <49213D6F.5050808@science-projects.org> Kirit S?lensminde wrote: > Simon Pickles wrote: >> When I run the python program which attempts to use the library >> generated by bjam, I get this: >> >> simon at urth-webserver:~/WorkingCopies/server/trunk/hybrid_test$ python >> hybridTest.py >> Traceback (most recent call last): >> File "hybridTest.py", line 2, in >> import Hybrid >> ImportError: libboost_python-gcc42-d-1_37.so.1.37.0: cannot open >> shared object file: No such file or directory >> >> >> I have built boost 1.37.0 from source using: >> >> sudo bjam -sPYTHON_ROOT=/usr/local/lib/python25 -sPYTHON_VERSION=2.5 >> --build-type=complete --toolset=gcc stage > > Looks like you don't have the .so anywhere it can be found. You want > to either set LD_LIBRARY_PATH to the location of your Boost .so files, > or you can use the Bjam option to hard code the paths in > your build Python .so. > > You could also install the Boost files to somewhere they will be found > (/usr/local/lib/ should do). > That did the trick - Thanks Sure I didn't have to do that last time - never mind. It works and I can progress! > > K > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > > -- :::::::::::::::::::: Latest NEWS >> http://www.science-projects.org/news.html Our PORTFOLIO is online at >> http://www.science-projects.org/workshop/portfolio/index.html Science Projects 20 St James Street Hammersmith London W6 9RW t: +44 (0)20 8741 2305 f: +44 (0)20 8741 2307 http://www.science-projects.org http://www.the-observatory.org http://www.inspirediscoverycentre.org Science Projects Ltd. is a company limited by guarantee registered in England No: 02186073 and a registered Charity No. 298542. Its registered office address is 20 St James Street, Hammersmith, London W6 9RW. From nickm at sitius.com Tue Nov 18 21:46:49 2008 From: nickm at sitius.com (Nikolay Mladenov) Date: Tue, 18 Nov 2008 15:46:49 -0500 Subject: [C++-sig] C++ Signature In-Reply-To: References: Message-ID: T J wrote: > For boost.python, the C++ signature is shown with a particular header > [python/src/object/function_doc_signature.cpp]: > > char cpp_signature_tag[] = "C++ signature :"; > > To conform to a reST docstring format, I'd like to change it to: > > char cpp_signature_tag[] = "C++ signature:\n==============\n"; > > Is this possible without recompiling boost? Ideally, I'd like to this > to be user-definable when I create my module. Right now, you cannot, but I think this may be done as a setting in the docstring_options class. Nikolay From alanbaljeu at yahoo.com Wed Nov 19 22:29:08 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Wed, 19 Nov 2008 13:29:08 -0800 (PST) Subject: [C++-sig] console I/O Message-ID: <201826.71147.qm@web58306.mail.re3.yahoo.com> I have embedded python in a Windows app. For development purposes, this program creates a console window, then calls a python script which calls print. I don't see any output. I'm thinking the problem is I need to do something to redirect Python internal settings for stdin and stdout. This is what I did with Prolog before. I haven't found such an option for Python in the C api. Anybody? Or if you know of such for a C++ wrapper... Alan __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com. From fileoffset at gmail.com Thu Nov 20 06:29:49 2008 From: fileoffset at gmail.com (fileoffset) Date: Thu, 20 Nov 2008 16:29:49 +1100 Subject: [C++-sig] Passing C++ instance to embedded py Message-ID: <20081120161605.1C4E.FILEOFFSET@gmail.com> To best explain my problem, here is some code: struct A { A() { mTest = 1; std::cout << "Test: " << mTest; Py_Initialize(); object main_module = import("__main__"); object main_namespace = main_module.attr("__dict__"); main_namespace["A_Instance"] = this; object result = exec_file(str("test.py"), main_namespace, main_namespace); std::cout << "Test: " << mTest; } int mTest; } in main.cpp or similar: BOOST_PYTHON_MODULE(MyModule) { class_("A", init<>()) .def_readwrite("Test", &A::mTest) ; } test.py: from MyModule import * A_Instance.Test = 5 Essentially I want to be able to access and modify the properties of an instance of a C++ class at runtime. When I try code similar to above, python excepts, as I believe it attempts to copy-construct the A class instead of just pass the reference (this) so that i can modify it. Is what I want possible? If not what would be a better way to do it? -- file offset From renjipanicker at gmail.com Thu Nov 20 06:33:38 2008 From: renjipanicker at gmail.com (Renji Panicker) Date: Thu, 20 Nov 2008 11:03:38 +0530 Subject: [C++-sig] console I/O In-Reply-To: <201826.71147.qm@web58306.mail.re3.yahoo.com> References: <201826.71147.qm@web58306.mail.re3.yahoo.com> Message-ID: <3c0065cc0811192133k108ddb68g44bc787cb7bceb7d@mail.gmail.com> This is a win32 API issue, not a boost::python issue per se. You need to redirect "CONIN$" and "CONOUT$" to stdin and stdout respectively. See here: http://comsci.liu.edu/~murali/win32gui/Win32Apps.htm -/renji On Thu, Nov 20, 2008 at 2:59 AM, Alan Baljeu wrote: > I have embedded python in a Windows app. For development purposes, this program creates a console window, then calls a python script which calls print. > > I don't see any output. > > I'm thinking the problem is I need to do something to redirect Python internal settings for stdin and stdout. This is what I did with Prolog before. I haven't found such an option for Python in the C api. Anybody? Or if you know of such for a C++ wrapper... > > Alan > > > __________________________________________________________________ > Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at > http://ca.toolbar.yahoo.com. > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > From gallineral at hotmail.com Wed Nov 19 18:55:34 2008 From: gallineral at hotmail.com (jrios) Date: Wed, 19 Nov 2008 12:55:34 -0500 Subject: [C++-sig] where is the extension module? Message-ID: Hi everybody I am just learning Boost.Python and so far I was able to install Boost 1.37.0, got a free copy of Visual Studio 9 Express (thanks Microsoft!) and built all Boost libraries (a long wait of almost 3 hours). Started the tutorial, compiled the extending example under libs\python\example\quickstart, the test was successful. But where is the extending module? I did an entire search under the Boost root and the closest thing I saw was a extending.pyd.rsp file. On the python console I typed >>import extending and I got "ImportError: No module named extending" Please give me some hint....thanks From dave at boostpro.com Thu Nov 20 18:42:05 2008 From: dave at boostpro.com (David Abrahams) Date: Thu, 20 Nov 2008 12:42:05 -0500 Subject: [C++-sig] where is the extension module? In-Reply-To: References: Message-ID: <6105E528-42A4-4CE7-9BC2-470253693AD7@boostpro.com> Run bjam with "-n -a" and it will show you all the commands it executed to build and run the test. That includes the info you're looking for. Oh, unless you pass --preserve-test-targets, the extension module is deleted when the test passes, so you might want to touch the .cpp file and try again. Sent from my iPhone On Nov 19, 2008, at 12:55 PM, "jrios" wrote: > Hi everybody > I am just learning Boost.Python and so far I was able to install Boost > 1.37.0, got a free copy of Visual Studio 9 Express (thanks > Microsoft!) and > built all Boost libraries (a long wait of almost 3 hours). > Started the tutorial, compiled the extending example under > libs\python\example\quickstart, the test was successful. > But where is the extending module? I did an entire search under the > Boost > root and the closest thing I saw was a extending.pyd.rsp file. On > the python > console I typed >>import extending and I got "ImportError: No module > named > extending" > Please give me some hint....thanks > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > From nies.stephan at googlemail.com Fri Nov 21 19:03:53 2008 From: nies.stephan at googlemail.com (stephan nies) Date: Fri, 21 Nov 2008 19:03:53 +0100 Subject: [C++-sig] profiling C++ python extension Message-ID: <79f682bf0811211003j5b2bcf20g72576138cb234599@mail.gmail.com> Hello, I would like to profile an C++ python extension on Linux. I am aware that oprofile and valgrind exist. But I was hoping to get it done with gprof. Problem there is that for gprof to work you would have to use -pg -g at every compile and link step up to the main-function in classic C++ programm. Obviously thats different in python. So do I have to recompile python? Will that suffice? (I read about other problems involving dlopen() being used for imports) Has anyone of you ever done profiling a C or C++ extension with gprof? Thanks, Stephan From amohr at pixar.com Fri Nov 21 19:10:41 2008 From: amohr at pixar.com (Alex Mohr) Date: Fri, 21 Nov 2008 10:10:41 -0800 Subject: [C++-sig] profiling C++ python extension In-Reply-To: <79f682bf0811211003j5b2bcf20g72576138cb234599@mail.gmail.com> References: <79f682bf0811211003j5b2bcf20g72576138cb234599@mail.gmail.com> Message-ID: <4926F9A1.8050503@pixar.com> > I am aware that oprofile and valgrind exist. > > Has anyone of you ever done profiling > a C or C++ extension with gprof? Not me, but I have to say that valgrind's callgrind tool with kcachegrind to view the results has been perhaps the best profiling experience I've had on Linux. I've had good success using these tools to profile C++ python extensions. Alex From mark at image-engine.com Fri Nov 21 19:20:57 2008 From: mark at image-engine.com (Mark Williams) Date: Fri, 21 Nov 2008 10:20:57 -0800 Subject: [C++-sig] profiling C++ python extension In-Reply-To: <4926F9A1.8050503@pixar.com> References: <79f682bf0811211003j5b2bcf20g72576138cb234599@mail.gmail.com> <4926F9A1.8050503@pixar.com> Message-ID: <4926FC09.8060001@image-engine.com> Alex Mohr wrote: >> I am aware that oprofile and valgrind exist. >> >> Has anyone of you ever done profiling >> a C or C++ extension with gprof? > > Not me, but I have to say that valgrind's callgrind tool with > kcachegrind to view the results has been perhaps the best profiling > experience I've had on Linux. I've had good success using these tools > to profile C++ python extensions. I've had great results profiling Python extensions, too, but with Google's profiler: http://code.google.com/p/google-perftools/ Mark From nies.stephan at googlemail.com Fri Nov 21 21:12:07 2008 From: nies.stephan at googlemail.com (stephan nies) Date: Fri, 21 Nov 2008 21:12:07 +0100 Subject: [C++-sig] profiling C++ python extension In-Reply-To: <4926FC09.8060001@image-engine.com> References: <79f682bf0811211003j5b2bcf20g72576138cb234599@mail.gmail.com> <4926F9A1.8050503@pixar.com> <4926FC09.8060001@image-engine.com> Message-ID: <79f682bf0811211212we23535bx76360954e2183d18@mail.gmail.com> Hi Mark, just had a quick glance at the google-perftools documentation. Could you provide a little more Detail in how you use it with a python extension? Do you link against libprofiler.so or do you use LD_PRELOAD ? Do you do anything special other than that? Or do you just start your python application? Cheers, Stephan On Fri, Nov 21, 2008 at 7:20 PM, Mark Williams wrote: > Alex Mohr wrote: >>> >>> I am aware that oprofile and valgrind exist. >>> >>> Has anyone of you ever done profiling >>> a C or C++ extension with gprof? >> >> Not me, but I have to say that valgrind's callgrind tool with kcachegrind >> to view the results has been perhaps the best profiling experience I've had >> on Linux. I've had good success using these tools to profile C++ python >> extensions. > > > I've had great results profiling Python extensions, too, but with Google's > profiler: http://code.google.com/p/google-perftools/ > > Mark > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > From mark at image-engine.com Fri Nov 21 21:21:06 2008 From: mark at image-engine.com (Mark Williams) Date: Fri, 21 Nov 2008 12:21:06 -0800 Subject: [C++-sig] profiling C++ python extension In-Reply-To: <79f682bf0811211212we23535bx76360954e2183d18@mail.gmail.com> References: <79f682bf0811211003j5b2bcf20g72576138cb234599@mail.gmail.com> <4926F9A1.8050503@pixar.com> <4926FC09.8060001@image-engine.com> <79f682bf0811211212we23535bx76360954e2183d18@mail.gmail.com> Message-ID: <49271832.80207@image-engine.com> stephan nies wrote: > Hi Mark, > > just had a quick glance at the google-perftools documentation. > > Could you provide a little more Detail in how you use it > with a python extension? Do you link against libprofiler.so > or do you use LD_PRELOAD ? > > Do you do anything special other than that? Or do you > just start your python application? > What I ended up doing was writing a small module (linked against libprofiler.so) containing bindings for the ProfilerStart/ProfilerStop functions. Then I could just import that module in Python and call start/stop around the sections I wanted to profile. After that I just ran "pprof" over the resulting file to create a PDF showing the call graph and timings. Cheers, Mark From josearios at comcast.net Sat Nov 22 04:41:03 2008 From: josearios at comcast.net (josearios at comcast.net) Date: Fri, 21 Nov 2008 22:41:03 -0500 Subject: [C++-sig] where is the extension module? Message-ID: yes, yes, the --preserve-test-targets option saved my day! thank you very much. "David Abrahams" wrote in message news:<6105E528-42A4-4CE7-9BC2-470253693AD7 at boostpro.com>... > Run bjam with "-n -a" and it will show you all the commands it > executed to build and run the test. That includes the info you're > looking for. Oh, unless you pass --preserve-test-targets, the > extension module is deleted when the test passes, so you might want to > touch the .cpp file and try again. > > Sent from my iPhone > > On Nov 19, 2008, at 12:55 PM, "jrios" wrote: > > > Hi everybody > > I am just learning Boost.Python and so far I was able to install Boost > > 1.37.0, got a free copy of Visual Studio 9 Express (thanks > > Microsoft!) and > > built all Boost libraries (a long wait of almost 3 hours). > > Started the tutorial, compiled the extending example under > > libs\python\example\quickstart, the test was successful. > > But where is the extending module? I did an entire search under the > > Boost > > root and the closest thing I saw was a extending.pyd.rsp file. On > > the python > > console I typed >>import extending and I got "ImportError: No module > > named > > extending" > > Please give me some hint....thanks > > > > > > > > _______________________________________________ > > Cplusplus-sig mailing list > > Cplusplus-sig at python.org > > http://mail.python.org/mailman/listinfo/cplusplus-sig > > From fileoffset at gmail.com Mon Nov 24 05:50:48 2008 From: fileoffset at gmail.com (fileoffset) Date: Mon, 24 Nov 2008 15:50:48 +1100 Subject: [C++-sig] Instance access Message-ID: <20081124154031.989B.FILEOFFSET@gmail.com> Is it possible to access an object instance from within python? I am trying to automate a single class that is part of a larger, complex framework. My idea is that I could load an embedded python script on construction of the object, which would act on the 'this' or 'shared_from_this()' pointer. This would greatly simplify my automation. I have tried it myself but it doesn't seem to work: main_namespace["instance"] = this; Is that ever going to work like I would want/expect? Or is there an alternative or work around that I could use? Please help! Any information is appreciated -- file offset From renjipanicker at gmail.com Mon Nov 24 09:19:59 2008 From: renjipanicker at gmail.com (Renji Panicker) Date: Mon, 24 Nov 2008 13:49:59 +0530 Subject: [C++-sig] Instance access In-Reply-To: <20081124154031.989B.FILEOFFSET@gmail.com> References: <20081124154031.989B.FILEOFFSET@gmail.com> Message-ID: <3c0065cc0811240019m6ec88ab9ge60d256c83248165@mail.gmail.com> Try this: main_namespace["instance"] = boost::python::ptr(this); Assuming you have exposed the class to python, of course. -/renji On Mon, Nov 24, 2008 at 10:20 AM, fileoffset wrote: > > Is it possible to access an object instance from within python? > > I am trying to automate a single class that is part of a larger, complex > framework. My idea is that I could load an embedded python script on > construction of the object, which would act on the 'this' or > 'shared_from_this()' pointer. This would greatly simplify my automation. > > I have tried it myself but it doesn't seem to work: > > main_namespace["instance"] = this; > > Is that ever going to work like I would want/expect? Or is there an > alternative or work around that I could use? > > Please help! Any information is appreciated > > -- > file offset > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > From renatox at gmail.com Mon Nov 24 13:48:44 2008 From: renatox at gmail.com (Renato Araujo) Date: Mon, 24 Nov 2008 09:48:44 -0300 Subject: [C++-sig] Instance access In-Reply-To: <20081124154031.989B.FILEOFFSET@gmail.com> References: <20081124154031.989B.FILEOFFSET@gmail.com> Message-ID: <95291a80811240448w61d9445eq9d5d9a4ee0b941b1@mail.gmail.com> maybe this tutorial can help. http://wiki.python.org/moin/boost.python/EmbeddingPython On Mon, Nov 24, 2008 at 1:50 AM, fileoffset wrote: > > Is it possible to access an object instance from within python? > > I am trying to automate a single class that is part of a larger, complex > framework. My idea is that I could load an embedded python script on > construction of the object, which would act on the 'this' or > 'shared_from_this()' pointer. This would greatly simplify my automation. > > I have tried it myself but it doesn't seem to work: > > main_namespace["instance"] = this; > > Is that ever going to work like I would want/expect? Or is there an > alternative or work around that I could use? > > Please help! Any information is appreciated > > -- > file offset > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Renato Araujo Oliveira Filho From alanbaljeu at yahoo.com Mon Nov 24 20:47:01 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Mon, 24 Nov 2008 11:47:01 -0800 (PST) Subject: [C++-sig] console I/O References: <201826.71147.qm@web58306.mail.re3.yahoo.com> <3c0065cc0811192133k108ddb68g44bc787cb7bceb7d@mail.gmail.com> Message-ID: <487988.34277.qm@web58307.mail.re3.yahoo.com> I followed the directions there, but that is incomplete. I can printf from C to the console, but print from Python does nothing. Something in the Python API must be employed. But perhaps I should take this to the main Python list. Alan Baljeu ----- Original Message ---- From: Renji Panicker To: Development of Python/C++ integration Sent: Thursday, November 20, 2008 12:33:38 AM Subject: Re: [C++-sig] console I/O This is a win32 API issue, not a boost::python issue per se. You need to redirect "CONIN$" and "CONOUT$" to stdin and stdout respectively. See here: http://comsci.liu.edu/~murali/win32gui/Win32Apps.htm -/renji On Thu, Nov 20, 2008 at 2:59 AM, Alan Baljeu wrote: > I have embedded python in a Windows app. For development purposes, this program creates a console window, then calls a python script which calls print. > > I don't see any output. > > I'm thinking the problem is I need to do something to redirect Python internal settings for stdin and stdout. This is what I did with Prolog before. I haven't found such an option for Python in the C api. Anybody? Or if you know of such for a C++ wrapper... > > Alan > > > __________________________________________________________________ > Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at > http://ca.toolbar.yahoo.com. > _______________________________________________ > 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 __________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com. From seefeld at sympatico.ca Mon Nov 24 20:48:48 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 24 Nov 2008 14:48:48 -0500 Subject: [C++-sig] console I/O In-Reply-To: <487988.34277.qm@web58307.mail.re3.yahoo.com> References: <201826.71147.qm@web58306.mail.re3.yahoo.com> <3c0065cc0811192133k108ddb68g44bc787cb7bceb7d@mail.gmail.com> <487988.34277.qm@web58307.mail.re3.yahoo.com> Message-ID: <492B0520.6080007@sympatico.ca> Alan Baljeu wrote: > I followed the directions there, but that is incomplete. I can printf from C to the console, but print from Python does nothing. Something in the Python API must be employed. But perhaps I should take this to the main Python list. > Definitely. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From Scott.VanSickle at CanfieldSci.com Mon Nov 24 21:02:20 2008 From: Scott.VanSickle at CanfieldSci.com (Scott VanSickle) Date: Mon, 24 Nov 2008 15:02:20 -0500 Subject: [C++-sig] console I/O In-Reply-To: <492B0520.6080007@sympatico.ca> References: <201826.71147.qm@web58306.mail.re3.yahoo.com> <3c0065cc0811192133k108ddb68g44bc787cb7bceb7d@mail.gmail.com> <487988.34277.qm@web58307.mail.re3.yahoo.com> <492B0520.6080007@sympatico.ca> Message-ID: <997199507FD49A4EA997EE1E762647464013FEAD91@Pan.domain-01> Alan, We execute Python code in our integration to redirect its stdout; sys.stdout is the where the output of its print method goes by default. We use a wrapped Python object on Windows that sends output via OutputDebugString; but you can send your output wherever you want. You basically just need a Python object with a write method to accomplish this. Create your Python object, then just reassign sys.stdout early in your implementation code like this: sys.stdout = YourClass() Scott V Lead Software Architect Canfield Imaging Systems Fairfield, NJ -----Original Message----- From: cplusplus-sig-bounces+svansickle=canfieldsci.com at python.org [mailto:cplusplus-sig-bounces+svansickle=canfieldsci.com at python.org] On Behalf Of Stefan Seefeld Sent: Monday, November 24, 2008 2:49 PM To: Development of Python/C++ integration Subject: Re: [C++-sig] console I/O Alan Baljeu wrote: > I followed the directions there, but that is incomplete. I can printf from C to the console, but print from Python does nothing. Something in the Python API must be employed. But perhaps I should take this to the main Python list. > Definitely. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig From renatox at gmail.com Mon Nov 24 22:26:09 2008 From: renatox at gmail.com (Renato Araujo) Date: Mon, 24 Nov 2008 18:26:09 -0300 Subject: [C++-sig] shared_ptr with custom destructor Message-ID: <95291a80811241326g25a0a4f6tc819acce47928816@mail.gmail.com> Hi all, I would like to specify during the class_ declaration use a shared_ptr with custom release function. shared_ptr has a constructor where you can pass a function to call in the release, I would like to use this in a generic form. I already tried some like that: shared_ptr _custom_constructor() { return shared_ptr(new BaseClass(), _release_func); } class_ > ("BaseClass").def("__init__", make_constructor(_custom_constructor)); but this not work to class derived from BaseClass: my _custom_constructor is not called to others class. I would like something more generic where I can declare in class_ template, something like that: class_ ("MyObject") ....... There is something like that? How is the best way to do this? BR -- Renato Araujo Oliveira Filho From alanbaljeu at yahoo.com Mon Nov 24 22:44:26 2008 From: alanbaljeu at yahoo.com (Alan Baljeu) Date: Mon, 24 Nov 2008 13:44:26 -0800 (PST) Subject: [C++-sig] console I/O References: <201826.71147.qm@web58306.mail.re3.yahoo.com> <3c0065cc0811192133k108ddb68g44bc787cb7bceb7d@mail.gmail.com> <487988.34277.qm@web58307.mail.re3.yahoo.com> <492B0520.6080007@sympatico.ca> <997199507FD49A4EA997EE1E762647464013FEAD91@Pan.domain-01> Message-ID: <580136.55595.qm@web58301.mail.re3.yahoo.com> Thanks Scott. I found a very complete example based on that: http://www.ragestorm.net/samples/empython.zip Alan Baljeu ----- Original Message ---- From: Scott VanSickle To: Development of Python/C++ integration Sent: Monday, November 24, 2008 3:02:20 PM Subject: Re: [C++-sig] console I/O Alan, We execute Python code in our integration to redirect its stdout; sys.stdout is the where the output of its print method goes by default. We use a wrapped Python object on Windows that sends output via OutputDebugString; but you can send your output wherever you want. You basically just need a Python object with a write method to accomplish this. Create your Python object, then just reassign sys.stdout early in your implementation code like this: sys.stdout = YourClass() Scott V Lead Software Architect Canfield Imaging Systems Fairfield, NJ -----Original Message----- From: cplusplus-sig-bounces+svansickle=canfieldsci.com at python.org [mailto:cplusplus-sig-bounces+svansickle=canfieldsci.com at python.org] On Behalf Of Stefan Seefeld Sent: Monday, November 24, 2008 2:49 PM To: Development of Python/C++ integration Subject: Re: [C++-sig] console I/O Alan Baljeu wrote: > I followed the directions there, but that is incomplete. I can printf from C to the console, but print from Python does nothing. Something in the Python API must be employed. But perhaps I should take this to the main Python list. > Definitely. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... _______________________________________________ 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 __________________________________________________________________ Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know at http://ca.answers.yahoo.com From ndbecker2 at gmail.com Tue Nov 25 01:15:33 2008 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 24 Nov 2008 19:15:33 -0500 Subject: [C++-sig] problem w/intermodule communication Message-ID: Here is a simple example. --------------- test1.cc #include #include #include #include using namespace boost::python; struct A { A (int _x) : x (_x) {} int x; }; BOOST_PYTHON_MODULE(test1) { class_ ("A", init()); } ---------------------- test2.cc #include #include #include #include #include using namespace boost::python; struct A { A (int _x) : x (_x) {} int x; }; struct B { B (boost::shared_ptr _a) : a (_a) {} boost::shared_ptr a; }; BOOST_PYTHON_MODULE(test2) { class_ ("B", init >()) .def_readonly ("a", &B::a) ; } ------------------------- test12.py from test1 import A from test2 import B a = A(1) b = B(a) print b.a --------------------- TypeError: No to_python (by-value) converter found for C++ type: boost::shared_ptr How can I solve this? Now consider: ------------------------ test3.cc #include #include #include #include using namespace boost::python; struct A { A (int _x) : x (_x) {} int x; }; struct B { B (boost::shared_ptr _a) : a (_a) {} boost::shared_ptr a; }; BOOST_PYTHON_MODULE(test3) { class_ ("A", init()); class_ ("B", init >()) .def_readonly ("a", &B::a) ; } ------------------------- test13.py from test3 import A, B a = A(1) b = B(a) print b.a --------------------------- So if both are in same module, no problem. If they are in different modules it doesn't work. Any ideas? From wladwig at wdtinc.com Tue Nov 25 23:42:20 2008 From: wladwig at wdtinc.com (William Ladwig) Date: Tue, 25 Nov 2008 16:42:20 -0600 Subject: [C++-sig] problem w/intermodule communication In-Reply-To: References: Message-ID: <765CBD9053EA2B438625895F30D1856FD8971297@storm.wdtinc.com> Neal, Try adding: register_ptr_to_python< boost::shared_ptr >(); to your BOOST_PYTHON_MODULE(test2) section. Hope this helps, Bill ________________________________________ From: cplusplus-sig-bounces+wladwig=wdtinc.com at python.org [cplusplus-sig-bounces+wladwig=wdtinc.com at python.org] On Behalf Of Neal Becker [ndbecker2 at gmail.com] Sent: Monday, November 24, 2008 6:15 PM To: cplusplus-sig at python.org Subject: [C++-sig] problem w/intermodule communication Here is a simple example. --------------- test1.cc #include #include #include #include using namespace boost::python; struct A { A (int _x) : x (_x) {} int x; }; BOOST_PYTHON_MODULE(test1) { class_ ("A", init()); } ---------------------- test2.cc #include #include #include #include #include using namespace boost::python; struct A { A (int _x) : x (_x) {} int x; }; struct B { B (boost::shared_ptr _a) : a (_a) {} boost::shared_ptr a; }; BOOST_PYTHON_MODULE(test2) { class_ ("B", init >()) .def_readonly ("a", &B::a) ; } ------------------------- test12.py from test1 import A from test2 import B a = A(1) b = B(a) print b.a --------------------- TypeError: No to_python (by-value) converter found for C++ type: boost::shared_ptr How can I solve this? Now consider: ------------------------ test3.cc #include #include #include #include using namespace boost::python; struct A { A (int _x) : x (_x) {} int x; }; struct B { B (boost::shared_ptr _a) : a (_a) {} boost::shared_ptr a; }; BOOST_PYTHON_MODULE(test3) { class_ ("A", init()); class_ ("B", init >()) .def_readonly ("a", &B::a) ; } ------------------------- test13.py from test3 import A, B a = A(1) b = B(a) print b.a --------------------------- So if both are in same module, no problem. If they are in different modules it doesn't work. Any ideas? _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig From ndbecker2 at gmail.com Wed Nov 26 00:46:07 2008 From: ndbecker2 at gmail.com (Neal Becker) Date: Tue, 25 Nov 2008 18:46:07 -0500 Subject: [C++-sig] problem w/intermodule communication References: <765CBD9053EA2B438625895F30D1856FD8971297@storm.wdtinc.com> Message-ID: William Ladwig wrote: > Neal, > > Try adding: > > register_ptr_to_python< boost::shared_ptr >(); > > to your BOOST_PYTHON_MODULE(test2) section. > > Hope this helps, > > Bill > Thanks! That's what it needed. From anirudhvij at gmail.com Thu Nov 27 14:53:43 2008 From: anirudhvij at gmail.com (anirudh vij) Date: Thu, 27 Nov 2008 14:53:43 +0100 Subject: [C++-sig] class properties for functions returning references Message-ID: <1aa72c450811270553y7c033b79y5c5d99f69b0ca7c2@mail.gmail.com> Hi, I've been trying to make the following code work *class Bar{ public: Bar():_int(4){}; ~Bar(){}; int get() {return _int;} private: int _int; }; class Foo { public: Foo():_bar(Bar()){}; ~Foo(){}; Bar& get_bar(){return _bar;} private: Bar _bar; }; #include BOOST_PYTHON_MODULE(test) { using namespace boost::python; class_("Bar") .def("get",&Bar::get) ; class_("Foo") .add_property("bar", make_getter(&Foo::get_bar,return_value_policy()) ) ; }* The idea is to expose the _bar member via the get() function. I dont want to use def_readonly (since this is a dummy test case for a more general problem I am facing of using getters with functons returning references) I get a compile error, with the source of the error in the return value policy . The error log is attached. I also tried return_internal_reference<>(), and return_value_policy, but both give compilation errors. Any help is appreciated regards, anirudh -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: compile_log Type: application/octet-stream Size: 10236 bytes Desc: not available URL: From renatox at gmail.com Thu Nov 27 21:33:11 2008 From: renatox at gmail.com (Renato Araujo) Date: Thu, 27 Nov 2008 17:33:11 -0300 Subject: [C++-sig] class properties for functions returning references In-Reply-To: <1aa72c450811270553y7c033b79y5c5d99f69b0ca7c2@mail.gmail.com> References: <1aa72c450811270553y7c033b79y5c5d99f69b0ca7c2@mail.gmail.com> Message-ID: <95291a80811271233x37ac5283mfa41b8d875b778f9@mail.gmail.com> Hi anirudh, Try something like that: ... .add_property("bar", make_function(&Foo::get_bar, return_value_policy()) ); .... I think "make_getter" is used to create access to members in the class. BR Renato 2008/11/27 anirudh vij : > Hi, > > I've been trying to make the following code work > > class Bar{ > public: > Bar():_int(4){}; > ~Bar(){}; > int get() > {return _int;} > private: > int _int; > }; > > class Foo > { > public: > Foo():_bar(Bar()){}; > ~Foo(){}; > Bar& get_bar(){return _bar;} > private: > Bar _bar; > }; > > #include > > BOOST_PYTHON_MODULE(test) > { > using namespace boost::python; > class_("Bar") > .def("get",&Bar::get) > ; > class_("Foo") > .add_property("bar", > make_getter(&Foo::get_bar,return_value_policy()) > ) > ; > } > > The idea is to expose the _bar member via the get() function. I dont want to > use def_readonly (since this is a dummy test case for a more general problem > I am facing of using getters with functons returning references) > > I get a compile error, with the source of the error in the return value > policy . The error log is attached. > > I also tried return_internal_reference<>(), and > return_value_policy, but both give compilation errors. > > Any help is appreciated > > regards, > anirudh > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Renato Araujo Oliveira Filho From dave at boostpro.com Sat Nov 29 16:58:39 2008 From: dave at boostpro.com (David Abrahams) Date: Sat, 29 Nov 2008 10:58:39 -0500 Subject: [C++-sig] wrapper and shared_ptr - how to combine? In-Reply-To: <32c88000811151148j737fc2a2m9d6a2905b3ea63a@mail.gmail.com> (Leonard Ritter's message of "Sat, 15 Nov 2008 20:48:13 +0100") References: <32c88000811150811p545ccfaaqc8a566ee1301dbc1@mail.gmail.com> <491EF6B0.3000208@sympatico.ca> <32c88000811151007r1ef9d422x2d9a6a80ca993002@mail.gmail.com> <32c88000811151148j737fc2a2m9d6a2905b3ea63a@mail.gmail.com> Message-ID: <87wsemle1c.fsf@mcbain.luannocracy.com> on Sat Nov 15 2008, "Leonard Ritter" wrote: > I think I know what the issue is, and it indeed relates to a piece of code I have > not posted yet. > > void Actor::add(const ActorPtr &actor) { > assert(actor); > assert(!actor->parent); > actor->parent = ActorPtr(this); Whoa, this is very bad juju. Who/what is managing the lifetime of *this before this call is made? Afterwards, you have two owners for *this. > children.push_back(actor); > queue_redraw(); > } > > Actor is a tree element with children and a parent pointer. I guess > that assigning ActorPtr(this) appears to construct a new pointer for > an object that is already registered to boost.python. Given what you write above, my first guess is that you want to be using both weak_ptr and shared_from_this in your design. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From dave at boostpro.com Sat Nov 29 17:11:20 2008 From: dave at boostpro.com (David Abrahams) Date: Sat, 29 Nov 2008 11:11:20 -0500 Subject: [C++-sig] wrapper and shared_ptr - how to combine? In-Reply-To: <32c88000811151258h7ba10e1aif18ab013db4434df@mail.gmail.com> (Leonard Ritter's message of "Sat, 15 Nov 2008 21:58:45 +0100") References: <32c88000811150811p545ccfaaqc8a566ee1301dbc1@mail.gmail.com> <491EF6B0.3000208@sympatico.ca> <32c88000811151007r1ef9d422x2d9a6a80ca993002@mail.gmail.com> <32c88000811151148j737fc2a2m9d6a2905b3ea63a@mail.gmail.com> <32c88000811151258h7ba10e1aif18ab013db4434df@mail.gmail.com> Message-ID: <87skpaldg7.fsf@mcbain.luannocracy.com> on Sat Nov 15 2008, "Leonard Ritter" wrote: > ================ yields =================== > > /usr/include/boost/python/object/pointer_holder.hpp: In constructor > 'boost::python::objects::pointer_holder::pointer_holder(PyObject*) > [with Pointer = boost::shared_ptr, Value = halebopp::Actor]': > /usr/include/boost/python/object/make_holder.hpp:83: instantiated from 'static > void boost::python::objects::make_holder<0>::apply::execute Yoiks! You clearly need gSTLFilt. Please post such things as attachments or in some other way that doesn't insert linebreaks. After reconstructing your lines and 'filt-ing, I get: /usr/include/boost/python/object/pointer_holder.hpp:179: error: cannot allocate an object of abstract type 'halebopp::Actor' src/nucleus/ogl.hpp:38: note: because the following virtual functions are pure within 'halebopp::Actor': src/nucleus/ogl.hpp:74: virtual void halebopp::Actor::queue_redraw() /usr/include/boost/python/object/pointer_holder.hpp: In constructor 'boost ::python::objects::pointer_holder< boost::shared_ptr, halebopp::Actor >::pointer_holder(PyObject *)': /usr/include/boost/python/class.hpp:629: instantiated from 'boost::python ::class_ ::class_(const char*, const char*) [with W = halebopp::python::PyActor, X1 = boost::shared_ptr, X2 = boost::noncopyable_::noncopyable, X3 = boost::python::detail::not_specified]' src/nucleus/export_ogl.cpp:94: instantiated from here /usr/include/boost/python/object/make_holder.hpp:83: instantiated from 'static void boost::python::objects::make_holder< 0 >::apply< boost::python::objects::pointer_holder< boost::shared_ptr , halebopp::Actor >, boost::mpl::vector0 >::execute (PyObject *)' /usr/include/boost/python/detail/make_keyword_range_fn.hpp:64: instantiated from 'boost::python::api::object boost::python::detail ::make_keyword_range_constructor ( const boost::python::default_call_policies & , const boost::python::detail::keyword_range & , boost::python::objects::pointer_holder< boost::shared_ptr, halebopp::Actor > *, boost::mpl::vector0 * , boost::mpl::size > * )' /usr/include/boost/python/init.hpp:330: instantiated from 'void boost::python ::detail::def_init_aux( boost::python::class_< halebopp::python::PyActor, boost::shared_ptr , boost::noncopyable_::noncopyable , boost::python::detail::not_specified > &, const boost::mpl::vector0 & , boost::mpl::size > , const boost::python::default_call_policies &, const char * , const boost::python::detail::keyword_range & )' /usr/include/boost/python/init.hpp:399: instantiated from 'static void boost ::python::detail::define_class_init_helper< 0 >::apply( boost::python::class_ < halebopp::python::PyActor, boost::shared_ptr , boost::noncopyable_::noncopyable , boost::python::detail::not_specified > &, const boost::python::default_call_policies & , const boost::python::init_base > ::visit( boost::python::class_< halebopp::python::PyActor, boost::shared_ptr , boost::noncopyable_::noncopyable , boost::python::detail::not_specified > & ) const::signature & , boost::mpl::size >, const char * , const boost::python::detail::keyword_range & )' /usr/include/boost/python/init.hpp:171: instantiated from 'void boost::python ::init_base< boost::python::init >::visit( boost::python::class_< halebopp::python::PyActor, boost::shared_ptr , boost::noncopyable_::noncopyable , boost::python::detail::not_specified > & ) const' /usr/include/boost/python/def_visitor.hpp:31: instantiated from 'static void boost::python::def_visitor_access::visit( const boost::python::def_visitor > & , boost::python::class_< halebopp::python::PyActor, boost::shared_ptr , boost::noncopyable_::noncopyable , boost::python::detail::not_specified > & )' /usr/include/boost/python/def_visitor.hpp:67: instantiated from 'void boost ::python::def_visitor< boost::python::init >::visit( boost::python::class_< halebopp::python::PyActor, boost::shared_ptr , boost::noncopyable_::noncopyable , boost::python::detail::not_specified > & ) const' scons: building terminated because of errors. /usr/include/boost/python/class.hpp:225: instantiated from 'boost::python ::class_ < T, boost::shared_ptr, boost::noncopyable_::noncopyable , boost::python::detail::not_specified > & boost::python::class_< T, boost::shared_ptr, boost::noncopyable_::noncopyable , boost::python::detail::not_specified >::def( const boost::python::def_visitor > & )' /usr/include/boost/python/class.hpp:501: instantiated from 'void boost ::python::class_< T, boost::shared_ptr, boost::noncopyable_::noncopyable , boost::python::detail::not_specified >::initialize(const boost::python::init &)' /usr/include/boost/python/class.hpp:629: instantiated from 'boost::python ::class_ ::class_(const char*, const char*) [with W = halebopp::python::PyActor, X1 = boost::shared_ptr, X2 = boost::noncopyable_::noncopyable, X3 = boost::python::detail::not_specified]' src/nucleus/export_ogl.cpp:94: instantiated from here So the problem is that you're trying to wrap PyActor in a way that implies it is default-constructible. But it's not; it's an abstract class. You need no_init. See http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.constructors -- Dave Abrahams BoostPro Computing http://www.boostpro.com From kmihail at rocketmail.com Sun Nov 30 20:00:31 2008 From: kmihail at rocketmail.com (Mihail Konstantinov) Date: Sun, 30 Nov 2008 19:00:31 +0000 (GMT) Subject: [C++-sig] Pyste,Py++: equivalent of swig typemaps? Message-ID: <810439.65583.qm@web28403.mail.ukl.yahoo.com> Hello, due to limitations of swig I am currently learning Py++ and Pyste (because it seems currently to be better documented than Py++). Does Pyste or Py++ offer typemaps like swig (http://www.swig.org/Doc1.3/SWIGDocumentation.html#Python_nn53) that allow any sequence of input arguments to be wrapped to other Python arguments (like for example ...,int len,char *s,... to a Python string) in any function where they appear, even surrounded by other arguments? The only remotely similar I found documented is http://www.boost.org/doc/libs/1_37_0/libs/python/pyste/doc/wrappers.html which explains how to write a wrapper between C++ STL and Python containers. But such a wrapper has to be rewritten for each and every function. Is there a simple method that automates the repetitive task like swig typemaps? Thanks Mihail -------------- next part -------------- An HTML attachment was scrubbed... URL: From seefeld at sympatico.ca Sun Nov 30 20:29:41 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Sun, 30 Nov 2008 14:29:41 -0500 Subject: [C++-sig] Pyste,Py++: equivalent of swig typemaps? In-Reply-To: <810439.65583.qm@web28403.mail.ukl.yahoo.com> References: <810439.65583.qm@web28403.mail.ukl.yahoo.com> Message-ID: <4932E9A5.50806@sympatico.ca> Mihail Konstantinov wrote: > Hello, > due to limitations of swig I am currently learning Py++ and Pyste > (because it seems currently to be better documented than Py++). > > Does Pyste or Py++ offer typemaps like swig > (http://www.swig.org/Doc1.3/SWIGDocumentation.html#Python_nn53) that > allow any sequence of input arguments to be wrapped to other Python > arguments (like for example ...,int len,char *s,... to a Python > string) in any function where they appear, even surrounded by other > arguments? I'm not entirely sure I understand the question. boost.python provides to-python and from-python converters that seem to be equivalent to the 'typemap' you are referring to. These converters are generated automatically for C++ types you export to python ( http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/python/exposing.html), so the conversion will happen automatically when you pass such a C++ type to a python function. You may start with reading the boost.python tutorial (http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/index.html), as it explains this as well as many more features of boost.python. Py++ and Pyste are just tools that generate boost.python wrapper code automatically. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From roman.yakovenko at gmail.com Sun Nov 30 20:47:07 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 30 Nov 2008 21:47:07 +0200 Subject: [C++-sig] [pygccxml-development] Pyste, Py++: equivalent of swig typemaps? In-Reply-To: <462834.98214.qm@web28410.mail.ukl.yahoo.com> References: <462834.98214.qm@web28410.mail.ukl.yahoo.com> Message-ID: <7465b6170811301147k2852a62eh77d410d440b8108c@mail.gmail.com> On Sun, Nov 30, 2008 at 8:58 PM, Mihail Konstantinov wrote: > Hello, > due to limitations of swig I am currently learning Py++ and Pyste (because > it seems currently to be better documented than Py++). Hmm. Did you see http://language-binding.net/pyplusplus/pyplusplus.html page? If so what documentation you think it lacks or what could be improved? > Does Pyste or Py++ offer typemaps like swig > (http://www.swig..org/Doc1.3/SWIGDocumentation.html#Python_nn53) that allow > any sequence of input arguments to be wrapped to other Python arguments > (like for example ....,int len,char *s,... to a Python string) in any > function where they appear, even surrounded by other arguments? > > The only remotely similar I found documented is > http://www.boost.org/doc/libs/1_37_0/libs/python/pyste/doc/wrappers.html > which explains how to write a wrapper between C++ STL and Python containers. > But such a wrapper has to be rewritten for each and every function. Is there > a simple method that automates the repetitive task like swig typemaps? I can answer for Py++ only. If I understand right, SWIG typemaps, than you are looking for "Function Transformation" feature: http://language-binding.net/pyplusplus/documentation/functions/transformation/transformation.html If you combine it with "query" functionality ( http://language-binding.net/pygccxml/query_interface.html ) you get what you want. HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From kmihail at rocketmail.com Sun Nov 30 20:55:18 2008 From: kmihail at rocketmail.com (Mihail Konstantinov) Date: Sun, 30 Nov 2008 19:55:18 +0000 (GMT) Subject: [C++-sig] Pyste,Py++: equivalent of swig typemaps? References: <810439.65583.qm@web28403.mail.ukl.yahoo.com> <4932E9A5.50806@sympatico.ca> Message-ID: <786330.24662.qm@web28402.mail.ukl.yahoo.com> Thank you Stefan, the typemaps are required, when C++ functions are wrapped that expect arguments which have no direct corresponding type in python, or when standard types have to be interpreted differently. For example a function void parse_args(int argc, char **argv), which I want to call in python as mymodule.parse_args(["arg1","arg2","arg3"]). The wrapper would have to - count the number of python list items and pass this as argc to the C++ function - allocate and fill a list of char* with the strings from the python list. I need even more complicated conversions that are not provided by boost.python. If it is still unclear, I'll try to give more examples. I know that I'm not expressing things very clear, because I have only a basic knowledge of boost.python. Mihail ________________________________ Von: Stefan Seefeld An: Development of Python/C++ integration Gesendet: Sonntag, den 30. November 2008, 20:29:41 Uhr Betreff: Re: [C++-sig] Pyste,Py++: equivalent of swig typemaps? Mihail Konstantinov wrote: > Hello, > due to limitations of swig I am currently learning Py++ and Pyste (because it seems currently to be better documented than Py++). > > Does Pyste or Py++ offer typemaps like swig (http://www.swig.org/Doc1.3/SWIGDocumentation.html#Python_nn53) that allow any sequence of input arguments to be wrapped to other Python arguments (like for example ...,int len,char *s,.... to a Python string) in any function where they appear, even surrounded by other arguments? I'm not entirely sure I understand the question. boost.python provides to-python and from-python converters that seem to be equivalent to the 'typemap' you are referring to. These converters are generated automatically for C++ types you export to python ( http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/python/exposing.html), so the conversion will happen automatically when you pass such a C++ type to a python function. You may start with reading the boost.python tutorial (http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/index.html), as it explains this as well as many more features of boost.python. Py++ and Pyste are just tools that generate boost.python wrapper code automatically. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig From seefeld at sympatico.ca Sun Nov 30 21:56:05 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Sun, 30 Nov 2008 15:56:05 -0500 Subject: [C++-sig] Pyste,Py++: equivalent of swig typemaps? In-Reply-To: <786330.24662.qm@web28402.mail.ukl.yahoo.com> References: <810439.65583.qm@web28403.mail.ukl.yahoo.com> <4932E9A5.50806@sympatico.ca> <786330.24662.qm@web28402.mail.ukl.yahoo.com> Message-ID: <4932FDE5.9090700@sympatico.ca> Mihail Konstantinov wrote: > Thank you Stefan, > the typemaps are required, when C++ functions are wrapped that expect arguments which have no direct corresponding type in python, or when standard types have to be interpreted differently. > > For example a function void parse_args(int argc, char **argv), which I want to call in python as mymodule.parse_args(["arg1","arg2","arg3"]). The wrapper would have to > - count the number of python list items and pass this as argc to the C++ function > - allocate and fill a list of char* with the strings from the python list. > What about this ? void parse_args(list argv) { std::vector args; for (unsigned int i = 0; i != len(argv); ++i) { extract ex(argv[i]); if (ex.check()) // If this is really a string... args.push_back(ex); // ...copy it into a vector else throw some_error(); } my_function(args); } This does what you seem to want: translates a python list of strings into a std::vector, and passes it down to a function. Obviously, you can (attempt to) extract data of arbitrary types from the python list, as long as converters exist. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From roman.yakovenko at gmail.com Sun Nov 30 22:13:50 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 30 Nov 2008 23:13:50 +0200 Subject: [C++-sig] Pyste,Py++: equivalent of swig typemaps? In-Reply-To: <4932FDE5.9090700@sympatico.ca> References: <810439.65583.qm@web28403.mail.ukl.yahoo.com> <4932E9A5.50806@sympatico.ca> <786330.24662.qm@web28402.mail.ukl.yahoo.com> <4932FDE5.9090700@sympatico.ca> Message-ID: <7465b6170811301313h436a7fdblb0344fed1aa44a1e@mail.gmail.com> On Sun, Nov 30, 2008 at 10:56 PM, Stefan Seefeld wrote: > Mihail Konstantinov wrote: >> >> Thank you Stefan, >> the typemaps are required, when C++ functions are wrapped that expect >> arguments which have no direct corresponding type in python, or when >> standard types have to be interpreted differently. >> >> For example a function void parse_args(int argc, char **argv), which I >> want to call in python as mymodule.parse_args(["arg1","arg2","arg3"]). The >> wrapper would have to - count the number of python list items and pass this >> as argc to the C++ function >> - allocate and fill a list of char* with the strings from the python list. >> > > What about this ? > > void parse_args(list argv) > { > std::vector args; > for (unsigned int i = 0; i != len(argv); ++i) > { > extract ex(argv[i]); > if (ex.check()) // If this is really a string... > args.push_back(ex); // ...copy it into a vector > else > throw some_error(); > } > my_function(args); > } > > > This does what you seem to want: translates a python list of strings into a > std::vector, and passes it down to a function. > Obviously, you can (attempt to) extract data of arbitrary types from the > python list, as long as converters exist. There are few problems with this approach: * It doesn't scale well. Consider exporting the existing project, which has 50-100 functions, which require different transformation. * Although you have to write different code if you want to export [none virtual | virtual | pure virtual] [free | member] function. If I understand right SWIG feature, it allows you to define something like "call policy" which is invoked before and after function invocation, and it also allows you to define complex mapping between Python and C++ arguments. So you can define it once, and then apply it when needed. I could be wrong, but you cant define such "call policy" using Boost.Python. Code generators definitely help in this situation. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From kmihail at rocketmail.com Sun Nov 30 22:24:39 2008 From: kmihail at rocketmail.com (Mihail Konstantinov) Date: Sun, 30 Nov 2008 21:24:39 +0000 (GMT) Subject: [C++-sig] Pyste,Py++: equivalent of swig typemaps? References: <810439.65583.qm@web28403.mail.ukl.yahoo.com> <4932E9A5.50806@sympatico.ca> <786330.24662.qm@web28402.mail.ukl.yahoo.com> <4932FDE5.9090700@sympatico.ca> <7465b6170811301313h436a7fdblb0344fed1aa44a1e@mail.gmail.com> Message-ID: <882365.10877.qm@web28409.mail.ukl.yahoo.com> > If I understand right SWIG feature, it allows you to define something > like "call policy" which is invoked before and after function > invocation, and it also allows you to define complex mapping between > Python and C++ arguments. So you can define it once, and then apply it > when needed. Yes, this is what I tried to say, but couldn' t phrase it. > I could be wrong, but you cant define such "call policy" using > Boost.Python. Code generators definitely help in this situation. I'll spend more time to better understand the Py++ docs. Maybe the function transformations do what I need. Thank you Mihail