Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31365 > unrolled thread
| Started by | aaron.l.france@gmail.com |
|---|---|
| First post | 2012-10-16 01:11 -0700 |
| Last post | 2012-10-16 04:46 -0700 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
Providing a Python wrapper to a C++ type. aaron.l.france@gmail.com - 2012-10-16 01:11 -0700
Re: Providing a Python wrapper to a C++ type. Marco Nawijn <nawijn@gmail.com> - 2012-10-16 03:17 -0700
Re: Providing a Python wrapper to a C++ type. Stefan Behnel <stefan_ml@behnel.de> - 2012-10-16 13:39 +0200
Re: Providing a Python wrapper to a C++ type. Marco Nawijn <nawijn@gmail.com> - 2012-10-16 04:46 -0700
Re: Providing a Python wrapper to a C++ type. Stefan Behnel <stefan_ml@behnel.de> - 2012-10-16 14:16 +0200
Re: Re: Providing a Python wrapper to a C++ type. Evan Driscoll <driscoll@cs.wisc.edu> - 2012-10-16 11:42 -0500
Re: Providing a Python wrapper to a C++ type. Christian Gollwitzer <auriocus@gmx.de> - 2012-10-16 21:03 +0200
Re: Providing a Python wrapper to a C++ type. Marco Nawijn <nawijn@gmail.com> - 2012-10-16 04:46 -0700
| From | aaron.l.france@gmail.com |
|---|---|
| Date | 2012-10-16 01:11 -0700 |
| Subject | Providing a Python wrapper to a C++ type. |
| Message-ID | <b71bb1ac-cfe0-45eb-a384-4d6109c99543@googlegroups.com> |
Hi, I have a C++ module where I have a defined, working type. How would I make a wrapper for this type to be able to be used in Python? I am familiar(-ish) with the C-API for functions but I can't see concretely how one would include an interface to a type. Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html Regards, Aaron
[toc] | [next] | [standalone]
| From | Marco Nawijn <nawijn@gmail.com> |
|---|---|
| Date | 2012-10-16 03:17 -0700 |
| Message-ID | <0423426c-c19a-4634-9f67-5bf92d766f61@googlegroups.com> |
| In reply to | #31365 |
On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote: > Hi, > > > > I have a C++ module where I have a defined, working type. How would I make a wrapper for this type to be able to be used in Python? I am familiar(-ish) with the C-API for functions but I can't see concretely how one would include an interface to a type. > > > > Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html > > > > Regards, > > Aaron Hi Aaron, There are a few ways of doing this. At least three come to my mind: 1. Wrap the C++ type yourself by using handcrafted code implemented with the Python C API 2. Use SWIG to wrap the C++ code and (semi) automatically create the wrapper (http://www.swig.org/) 3. Use BOOST Python to wrap the C++ code (http://www.boost.org/doc/libs/1_51_0/libs/python/doc/index.html) I would highly discourage (1) unless you are very brave and curious. Ofcourse it is a nice excercise, but if you want something to work quickly I would recommend to use either (2) or (3). I have used both SWIG and BOOST Python and either of them worked pretty well for me. In the end I selected BOOST Python, because I was only interested in the Python wrapping (SWIG could generate many other wrappers as well). Regards, Marco
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2012-10-16 13:39 +0200 |
| Message-ID | <mailman.2263.1350387583.27098.python-list@python.org> |
| In reply to | #31376 |
Marco Nawijn, 16.10.2012 12:17: > On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote: >> I have a C++ module where I have a defined, working type. How would I >> make a wrapper for this type to be able to be used in Python? I am >> familiar(-ish) with the C-API for functions but I can't see concretely how >> one would include an interface to a type. >> >> Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html > > There are a few ways of doing this. At least three come to my mind: > 1. Wrap the C++ type yourself by using handcrafted code implemented with the Python C API > 2. Use SWIG to wrap the C++ code and (semi) automatically create the wrapper (http://www.swig.org/) > 3. Use BOOST Python to wrap the C++ code (http://www.boost.org/doc/libs/1_51_0/libs/python/doc/index.html) > > I would highly discourage (1) unless you are very brave and curious. Ofcourse it is a nice excercise, but if you want something to work quickly I would recommend to use either (2) or (3). > > I have used both SWIG and BOOST Python and either of them worked pretty well for me. In the end I selected BOOST Python, because I was only interested in the Python wrapping (SWIG could generate many other wrappers as well). There's also Cython, which provides a very flexible way (unlike SWIG) of doing these things easily (unlike C++ with Boost). http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html I agree with discouraging 1) in specific. Stefan
[toc] | [prev] | [next] | [standalone]
| From | Marco Nawijn <nawijn@gmail.com> |
|---|---|
| Date | 2012-10-16 04:46 -0700 |
| Message-ID | <d2c77c2a-21de-4f39-8c10-d1b44fdf23d7@googlegroups.com> |
| In reply to | #31387 |
On Tuesday, October 16, 2012 1:39:44 PM UTC+2, Stefan Behnel wrote: > Marco Nawijn, 16.10.2012 12:17: > > > On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote: > > >> I have a C++ module where I have a defined, working type. How would I > > >> make a wrapper for this type to be able to be used in Python? I am > > >> familiar(-ish) with the C-API for functions but I can't see concretely how > > >> one would include an interface to a type. > > >> > > >> Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html > > > > > > There are a few ways of doing this. At least three come to my mind: > > > 1. Wrap the C++ type yourself by using handcrafted code implemented with the Python C API > > > 2. Use SWIG to wrap the C++ code and (semi) automatically create the wrapper (http://www.swig.org/) > > > 3. Use BOOST Python to wrap the C++ code (http://www.boost.org/doc/libs/1_51_0/libs/python/doc/index.html) > > > > > > I would highly discourage (1) unless you are very brave and curious. Ofcourse it is a nice excercise, but if you want something to work quickly I would recommend to use either (2) or (3). > > > > > > I have used both SWIG and BOOST Python and either of them worked pretty well for me. In the end I selected BOOST Python, because I was only interested in the Python wrapping (SWIG could generate many other wrappers as well). > > > > There's also Cython, which provides a very flexible way (unlike SWIG) of > > doing these things easily (unlike C++ with Boost). > > > > http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html > > > > I agree with discouraging 1) in specific. > > > > Stefan Hi Stefan, I never worked with Cython (but I know it is very powerful and interesting) but in my mind there are slight differences in usage scenario between e.g. Boost Python and Cython. For me the idea of Cython is that your main code is in Python, but you want to improve the performance of specific parts of the code. In that case, Cython is the way to go. In case of Boost Python, the scenario for me is that you have a main program/library in C++, but you want to be able use the functionality from Python. Do you agree with this view? Marco
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2012-10-16 14:16 +0200 |
| Message-ID | <mailman.2268.1350389790.27098.python-list@python.org> |
| In reply to | #31389 |
Marco Nawijn, 16.10.2012 13:46: > I never worked with Cython (but I know it is very powerful and > interesting) but in my mind there are slight differences in usage > scenario between e.g. Boost Python and Cython. For me the idea of Cython > is that your main code is in Python Normally, yes. You can embed Cython code in C++ just like any other C code (with the caveat of needing to make sure the Python runtime is properly set up), but that certainly isn't the most popular use case. > but you want to improve the > performance of specific parts of the code. In that case, Cython is the > way to go. In case of Boost Python, the scenario for me is that you have > a main program/library in C++, but you want to be able use the > functionality from Python. That's really just a slight difference. What kind of code initially started up an application is quite irrelevant once it has been running for a while. The distinction between embedding and extending, as the Python docs put it, is actually quite fuzzy when it comes to the actual code interaction. Note also that this isn't the use case here, the OP asked for wrapping a C++ type for use in Python. Stefan
[toc] | [prev] | [next] | [standalone]
| From | Evan Driscoll <driscoll@cs.wisc.edu> |
|---|---|
| Date | 2012-10-16 11:42 -0500 |
| Message-ID | <mailman.2291.1350406602.27098.python-list@python.org> |
| In reply to | #31389 |
[Stefan gave part of an answer here, but I've got an addition too.] On 10/16/2012 6:46 AM, Marco Nawijn wrote: > I never worked with Cython (but I know it is very powerful and interesting) but in my mind there are slight differences in usage scenario between e.g. Boost Python and Cython. For me the idea of Cython is that your main code is in Python, but you want to improve the performance of specific parts of the code. In that case, Cython is the way to go. In case of Boost Python, the scenario for me is that you have a main program/library in C++, but you want to be able use the functionality from Python. > > Do you agree with this view? Sort of, but sort of not. I've only used Cython a little bit, but it seems to work equally well if you want to write a C module for some reason (e.g. to interface with existing C code) but don't want to deal with the standard CPython C API directly. For instance, I used it to wrap the OS's opendir/readdir (FindFirstFile/FindNextFile) functions: I just wrote a bit of code using Cython's extensions, and I get a module I can import and use as normal. I certainly didn't do it for speed, I did it because it seemed like a reasonable way to get access to those APIs. (ctypes was insufficient for my needs.) So wrapping a C++ class using Cython also seems pretty natural to me, assuming that Cython does OK with C++ and isn't restricted to C (which I have no idea about). Evan
[toc] | [prev] | [next] | [standalone]
| From | Christian Gollwitzer <auriocus@gmx.de> |
|---|---|
| Date | 2012-10-16 21:03 +0200 |
| Message-ID | <k5kb1k$h5e$1@dont-email.me> |
| In reply to | #31419 |
Am 16.10.12 18:42, schrieb Evan Driscoll: > For instance, I used it to wrap the OS's opendir/readdir > (FindFirstFile/FindNextFile) functions: I just wrote a bit of code using > Cython's extensions, and I get a module I can import and use as normal. Well, but for an existing library in C++ possibly SWIG will be the easiest way to go; since you already have a header file, it might be enough to do swig -python -c++ myheader.hpp to get the wrappers generated. This works well in simple cases. For complicated cases like trying to wrap large template libraries like STL or Boost, it is also necessary to twiddle with SWIGs interface language. In many cases, however, this can be #ifdef'ed in the header file itself, therefore it is easy to keep the interface in sync with the class library. Christian
[toc] | [prev] | [next] | [standalone]
| From | Marco Nawijn <nawijn@gmail.com> |
|---|---|
| Date | 2012-10-16 04:46 -0700 |
| Message-ID | <mailman.2265.1350387982.27098.python-list@python.org> |
| In reply to | #31387 |
On Tuesday, October 16, 2012 1:39:44 PM UTC+2, Stefan Behnel wrote: > Marco Nawijn, 16.10.2012 12:17: > > > On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote: > > >> I have a C++ module where I have a defined, working type. How would I > > >> make a wrapper for this type to be able to be used in Python? I am > > >> familiar(-ish) with the C-API for functions but I can't see concretely how > > >> one would include an interface to a type. > > >> > > >> Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html > > > > > > There are a few ways of doing this. At least three come to my mind: > > > 1. Wrap the C++ type yourself by using handcrafted code implemented with the Python C API > > > 2. Use SWIG to wrap the C++ code and (semi) automatically create the wrapper (http://www.swig.org/) > > > 3. Use BOOST Python to wrap the C++ code (http://www.boost.org/doc/libs/1_51_0/libs/python/doc/index.html) > > > > > > I would highly discourage (1) unless you are very brave and curious. Ofcourse it is a nice excercise, but if you want something to work quickly I would recommend to use either (2) or (3). > > > > > > I have used both SWIG and BOOST Python and either of them worked pretty well for me. In the end I selected BOOST Python, because I was only interested in the Python wrapping (SWIG could generate many other wrappers as well). > > > > There's also Cython, which provides a very flexible way (unlike SWIG) of > > doing these things easily (unlike C++ with Boost). > > > > http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html > > > > I agree with discouraging 1) in specific. > > > > Stefan Hi Stefan, I never worked with Cython (but I know it is very powerful and interesting) but in my mind there are slight differences in usage scenario between e.g. Boost Python and Cython. For me the idea of Cython is that your main code is in Python, but you want to improve the performance of specific parts of the code. In that case, Cython is the way to go. In case of Boost Python, the scenario for me is that you have a main program/library in C++, but you want to be able use the functionality from Python. Do you agree with this view? Marco
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web