Re: [Tutor] c++ on python
Russel Winder, 13.03.2014 17:29: > On Thu, 2014-03-13 at 16:57 +0100, Stefan Behnel wrote: > […] >> The thing is: if you have to write your own wrapper anyway (trivial or >> not), then why not write it in Cython right away and avoid the intermediate >> plain C level? > > If the task is two write an adapter (aka wrapper) then perhaps use SWIG > whcih is easier for this task than writing Cython code. Depends. SWIG is nice if you have a large API that a) you want to wrap quickly all at once and b) that matches the tool well. Once you're beyond the "matches the tool well" spot, however, you'll start having an increasingly hard time pushing the tool into matching your API. Cython has a higher learning curve to get started (it's a programming language, not a wrapper generator by itself, use something like XDress for that), but is unlimited in what it allows you to do (because it's a programming language). So things won't suddenly become harder (let alone impossible) afterwards. >> It's usually much nicer to work with object oriented code on both sides >> (assuming you understand the languages on both sides), than to try to >> squeeze them through a C-ish API bottleneck in the middle. > > It could be that "object oriented" is a red herring. Without details (*) > of what it is about the C++ code that is the connection between Python > and C++, it is difficult to generalize. Sure. I've seen both good and bad API designs in C++, as in any other language. > ctypes can be a real pain when trying to call C++ from Python using > argument values that are not primitive types. CFFI solves (currently > much, soon most) of this problem by addressing the adapter between > Python and C++ in a different way to that employed by ctypes. In both > cases, both are a lot easier than writing Cython code. Now, that's a bit overly generalising, wouldn't you say? Even in the cases where cffi is as simple as Cython, I'd still prefer the portability and simplicity advantage of having statically compiled (and tested) wrapper code over a mix of a hand written C++-to-C wrapper and some dynamically generated glue code with its own set of runtime dependencies. But I can certainly accept that tools like ctypes and cffi have their niche, too. If you're comfortable with them, and they fit your needs, then sure, use them. There isn't one tool that caters for everyone. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
James Chapman, 13.03.2014 17:35: > Perhaps I should look into Cython as I'm currently working on a > project that utilises a C API. > > I've been finding that getting the data types to be exactly what the C > API is expecting to be the hardest part. > > With the original question in mind, here's an example calling into a > C++ external C API: > > (Works if compiled is VisualStudio. The DLL produced by MinGW-G++ didn't > work). > > --- > // main.h > > #ifndef __MAIN_H__ > #define __MAIN_H__ > > #include > > #define DLL_EXPORT __declspec(dllexport) > > #ifdef __cplusplus > extern "C" > { > #endif > > int DLL_EXPORT add(int a, int b); > > #ifdef __cplusplus > } > #endif > > #endif // __MAIN_H__ > --- > > --- > //main.cpp > > #include "main.h" > > // a sample exported function > int DLL_EXPORT add(int a, int b) > { > return(a + b); > } > > extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD > fdwReason, LPVOID lpvReserved) > { > switch (fdwReason) > { > case DLL_PROCESS_ATTACH: > // attach to process > // return FALSE to fail DLL load > break; > > case DLL_PROCESS_DETACH: > // detach from process > break; > > case DLL_THREAD_ATTACH: > // attach to thread > break; > > case DLL_THREAD_DETACH: > // detach from thread > break; > } > return TRUE; // succesful > } > --- > > --- > # -*- coding: utf-8 -*- > # dll.py > > import ctypes > > > class DllInterface(object): > > dll_handle = None > > def __init__(self, dll_file): > self.dll_handle = ctypes.WinDLL(dll_file) > > def add_a_and_b(self, a=0, b=0): > return self.dll_handle.add(a, b) > > > if __name__ == '__main__': > dll_file = 'PythonDLL.dll' > external_lib = DllInterface(dll_file) > int_a = ctypes.c_int(1) > int_b = ctypes.c_int(2) > result = external_lib.add_a_and_b(int_a, int_b) > print(result) In Cython, that would essentially be cdef extern from "main.h": int add(int a, int b) print(add(1, 2)) It compiles down to C(++), i.e. it interfaces at the API level, not the ABI level, as ctypes would. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
Perhaps I should look into Cython as I'm currently working on a project that utilises a C API. I've been finding that getting the data types to be exactly what the C API is expecting to be the hardest part. With the original question in mind, here's an example calling into a C++ external C API: (Works if compiled is VisualStudio. The DLL produced by MinGW-G++ didn't work). --- // main.h #ifndef __MAIN_H__ #define __MAIN_H__ #include #define DLL_EXPORT __declspec(dllexport) #ifdef __cplusplus extern "C" { #endif int DLL_EXPORT add(int a, int b); #ifdef __cplusplus } #endif #endif // __MAIN_H__ --- --- //main.cpp #include "main.h" // a sample exported function int DLL_EXPORT add(int a, int b) { return(a + b); } extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_PROCESS_ATTACH: // attach to process // return FALSE to fail DLL load break; case DLL_PROCESS_DETACH: // detach from process break; case DLL_THREAD_ATTACH: // attach to thread break; case DLL_THREAD_DETACH: // detach from thread break; } return TRUE; // succesful } --- --- # -*- coding: utf-8 -*- # dll.py import ctypes class DllInterface(object): dll_handle = None def __init__(self, dll_file): self.dll_handle = ctypes.WinDLL(dll_file) def add_a_and_b(self, a=0, b=0): return self.dll_handle.add(a, b) if __name__ == '__main__': dll_file = 'PythonDLL.dll' external_lib = DllInterface(dll_file) int_a = ctypes.c_int(1) int_b = ctypes.c_int(2) result = external_lib.add_a_and_b(int_a, int_b) print(result) --- -- James -- James On 13 March 2014 15:57, Stefan Behnel wrote: > Alan Gauld, 12.03.2014 23:05: >> On 12/03/14 16:49, Stefan Behnel wrote: >>> Alan Gauld, 12.03.2014 10:11: If it were a library then you would have to call the individual C++ functions directly using something like ctypes, which is usually more complex. >>> >>> ctypes won't talk to C++, but Cython can do it quite easily. >> >> I thought it would work provided the interface functions >> were declared as C functions? That might involve >> writing a wrapper around it but that is usually >> trivial if you have to compile the source anyway. > > The thing is: if you have to write your own wrapper anyway (trivial or > not), then why not write it in Cython right away and avoid the intermediate > plain C level? > > It's usually much nicer to work with object oriented code on both sides > (assuming you understand the languages on both sides), than to try to > squeeze them through a C-ish API bottleneck in the middle. > > Stefan > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On Thu, 2014-03-13 at 16:57 +0100, Stefan Behnel wrote: […] > The thing is: if you have to write your own wrapper anyway (trivial or > not), then why not write it in Cython right away and avoid the intermediate > plain C level? If the task is two write an adapter (aka wrapper) then perhaps use SWIG whcih is easier for this task than writing Cython code. > It's usually much nicer to work with object oriented code on both sides > (assuming you understand the languages on both sides), than to try to > squeeze them through a C-ish API bottleneck in the middle. It could be that "object oriented" is a red herring. Without details (*) of what it is about the C++ code that is the connection between Python and C++, it is difficult to generalize. ctypes can be a real pain when trying to call C++ from Python using argument values that are not primitive types. CFFI solves (currently much, soon most) of this problem by addressing the adapter between Python and C++ in a different way to that employed by ctypes. In both cases, both are a lot easier than writing Cython code. (*) I may have just missed this detail in which case apologies. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
Alan Gauld, 12.03.2014 23:05: > On 12/03/14 16:49, Stefan Behnel wrote: >> Alan Gauld, 12.03.2014 10:11: >>> If it were a library then you would have to call >>> the individual C++ functions directly using >>> something like ctypes, which is usually more >>> complex. >> >> ctypes won't talk to C++, but Cython can do it quite easily. > > I thought it would work provided the interface functions > were declared as C functions? That might involve > writing a wrapper around it but that is usually > trivial if you have to compile the source anyway. The thing is: if you have to write your own wrapper anyway (trivial or not), then why not write it in Cython right away and avoid the intermediate plain C level? It's usually much nicer to work with object oriented code on both sides (assuming you understand the languages on both sides), than to try to squeeze them through a C-ish API bottleneck in the middle. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On 7 March 2014 14:29, Gabriele Brambilla wrote: > Hi, > in the next days I will receive a c++ code that I would like to run in > python (http://docs.python.org/2/extending/index.html). > It should be self consistent (no extraroutines). > I want to be ready to use it... Has someone some C++ code examples available > that I can try to run easily before getting that code? Hi Gabriele, I recommend either Alan's suggestion of running the C++ code as an external application (with e.g. subprocess) or using cython to connect the C++ code to Python. Which is appropriate depends on what you need to do and how much flexibility you need from within the Python code. I learned C and C++ before Python and consider myself reasonably proficient in all 3 languages and capable of understanding (dis-)assembly and linkage and so on and I still find ctypes to be a bit of handful (I have less experience of cffi). Using Cython or just compiling the C++ code with an external compiler is much better for the error checking and so on. Using ctypes you get very little error-checking, just seg faults and corruption. Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On Wed, 2014-03-12 at 22:05 +, Alan Gauld wrote: > On 12/03/14 16:49, Stefan Behnel wrote: > > Alan Gauld, 12.03.2014 10:11: > >> If it were a library then you would have to call > >> the individual C++ functions directly using > >> something like ctypes, which is usually more > >> complex. > > > > ctypes won't talk to C++, but Cython can do it quite easily. > > I thought it would work provided the interface functions > were declared as C functions? That might involve > writing a wrapper around it but that is usually > trivial if you have to compile the source anyway. ctypes (and CFFI) talks quite happily to C++ functions as long as they are declared with C linkage (so as to avoid any "name mangling"): export "C" x f(…){…} makes f accessible via ctypes if f is in a shared object/dynamic link library. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On 12/03/14 16:49, Stefan Behnel wrote: Alan Gauld, 12.03.2014 10:11: If it were a library then you would have to call the individual C++ functions directly using something like ctypes, which is usually more complex. ctypes won't talk to C++, but Cython can do it quite easily. I thought it would work provided the interface functions were declared as C functions? That might involve writing a wrapper around it but that is usually trivial if you have to compile the source anyway. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
Alan Gauld, 12.03.2014 10:11: > If it were a library then you would have to call > the individual C++ functions directly using > something like ctypes, which is usually more > complex. ctypes won't talk to C++, but Cython can do it quite easily. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On 11/03/14 14:27, Gabriele Brambilla wrote: Is it a library or a program? A program. it should use only standard C++ libraries. OK, If it's a program, that is, it compiles into an executable that you can run then you can run it from within Python using the subprocess module. Do you know how you interact with it? Can you give it start up arguments? Does it read from stdin? Does it write to stdout? Does it read a data file? Does it write a file out? You will need to know that to be able to use the program from inside Python. What do you mean about using Python to work with it? > How do you usually do? I mean running the C++ program and exchanging data with it. That's what subprocess allows you to do. If it were a library then you would have to call the individual C++ functions directly using something like ctypes, which is usually more complex. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
I answer in the text 2014-03-11 9:32 GMT-04:00 ALAN GAULD : > CC'ing the list > Please use ReplyAll when responding. > > > -- > *From:* Gabriele Brambilla > *To:* Alan Gauld > *Sent:* Tuesday, 11 March 2014, 12:54 > *Subject:* Re: [Tutor] c++ on python > > I think (because I've not received the code yet) I will receive the source > code (.c or .cpp file) > and I want to compile it in the way to use it and maybe make small > changes. So I think I want > to embed the code as a Python module (but it's not properly a library). > > What is your experience level with C/C++? > Are you familiar with building C/C++ libraries or even object files? > I know C/C++, I am not able to do everything but I think that I can compile a simple programm with object files and libraries > There are documents and tools to help you turn C code into Python > libraries > but that's really outside the scope of the tutor list. > > About the dependencies I am not so sure as before. > So I mistaken the list? which one is the right one? > > I suspect you may want a different list. But you will need to be clear > about what you are > trying to do. It's still not clear what exactly this source code will be. > Is it a library or a program? > A program. it should use only standard C++ libraries. > Do you think is it better that I install a C compiler and I don't use > python? I use Anaconda... > > You will need a C compiler regardless, if you receive C source code. > Python cannot work with C in source format, only after it is compiled. > But that does not mean you can't use Python to work with it, and that is > probably easier than trying to write your whole application in > C++ - especially if you are not already fluent in C++. > Yes I would prefer to use Python as much as possible. What do you mean about using Python to work with it? How do you usually do? I've no experience of Anaconda but it looks like it might be hard to find > an equivalent > in the C++ world, especially if you have already written a lot of > Python/Anaconda code. > > Alan Gauld > Author of the Learn To Program website > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > Thanks Gabriele ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
CC'ing the list Please use ReplyAll when responding. > > From: Gabriele Brambilla >To: Alan Gauld >Sent: Tuesday, 11 March 2014, 12:54 >Subject: Re: [Tutor] c++ on python > > > >I think (because I've not received the code yet) I will receive the source >code (.c or .cpp file) >and I want to compile it in the way to use it and maybe make small changes. So >I think I want >to embed the code as a Python module (but it's not properly a library). > >What is your experience level with C/C++? Are you familiar with building C/C++ libraries or even object files? There are documents and tools to help you turn C code into Python libraries but that's really outside the scope of the tutor list. About the dependencies I am not so sure as before. >So I mistaken the list? which one is the right one? >I suspect you may want a different list. But you will need to be clear about >what you are trying to do. It's still not clear what exactly this source code will be. Is it a library or a program? Do you think is it better that I install a C compiler and I don't use python? I use Anaconda... >You will need a C compiler regardless, if you receive C source code. Python cannot work with C in source format, only after it is compiled. But that does not mean you can't use Python to work with it, and that is probably easier than trying to write your whole application in C++ - especially if you are not already fluent in C++. I've no experience of Anaconda but it looks like it might be hard to find an equivalent in the C++ world, especially if you have already written a lot of Python/Anaconda code. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On 07/03/14 14:29, Gabriele Brambilla wrote: in the next days I will receive a c++ code that I would like to run in python (http://docs.python.org/2/extending/index.html). What do you mean? Are you receiving C++ source code? If so is it for an executable program, a library or just a single object file? When you say run it in python do you mean you want to launch the executable program from Python (see subprocess module) or call functions in the library (maybe ctypes) or do you want to to embed the C code as a Python module? If its an executable to be run via subprocess thats fine for this list. If its using ctypes to call functions in a library thats about the extreme edge of this list.But there is a ctypes list that is probably more appropriate. If its embedding the C library as a python module that is definitely off topic and you should probably try the main Python list It should be self consistent (no extraroutines). Again I'm not sure what you mean by that. Do you mean it has no dependencies on other code - not even glibc? Or just that it is a standalone executable? I want to be ready to use it... Has someone some C++ code examples available that I can try to run easily before getting that code? You need to be a lot more specific about what you mean. What exactly are you trying to do? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On Fri, Mar 7, 2014 at 9:29 AM, Gabriele Brambilla wrote: > > in the next days I will receive a c++ code that I would like to run in > python (http://docs.python.org/2/extending/index.html). > It should be self consistent (no extraroutines). > I want to be ready to use it... Has someone some C++ code examples available > that I can try to run easily before getting that code? Writing CPython extension modules is probably off topic for python-tutor. ctypes is on topic, but manually wrapping a C++ library with a C API that's accessible via ctypes is a lot of work. I recommend using SWIG or Cython instead. http://cython.readthedocs.org/en/latest/src/userguide/wrapping_CPlusPlus.html cython-users forum: http://groups.google.com/group/cython-users http://swig.org/Doc2.0/Python.html#Python ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor