I want to create an app that will be extensible via plugins.
I know that I have 2 options.
I can create my own interpreted language and app with a built-in interpreter for this language.
I can use one of the existing languages such as Python, Lua or another scripting language.
I want to use option 2. And I know that I must create a layer for external language to enable communication between this language and my app. But I don't know how to do it. Maybe I must use interprocess communication or something like that.
Let's assume that I have an application written in C++. In the beginning, it may be even a simple console app that displays a few options. And I want to write a plugin in Python like this:
option = "additional option"
myApp.addOption(option)
And then:
I launch my app
My app loads the plugin
I see my app with this additional option displayed
I want to do this simple thing to understand how it works and then I will be able to do something more complicated.
You could start by looking at the languages' documentation(if you're new):
Python -->https://docs.python.org/3/
Lua --> https://www.lua.org/docs.html
C++ libraries can also be called in C(If you're careful enough),you could look at this too
https://www.teddy.ch/c++_library_in_c/
You should be aware that, with care, a C++ library can be called from a C program, mostly by appropriately using extern "C" to disable name mangling. On Linux, read also the C++ dlopen mini Howto.
Then you need to read the chapter Extending and embedding the Python interpreter
At last, Python is open source, so please study its source code.
I can use one of the existing languages such as Python, Lua or another scripting language.
I strongly recommend considering using GNU Guile or extending Ocaml.
And both TensorFlow or NumPy could inspire you, since they are open source libraries (coded in C and/or C++) usable from Python.
You know, we can bind every C library on Python or Perl programming languages. A good instance is PyQt; PyQt is bound from Qt.
My question is: Can I do the reverse of the above? I mean: suppose I have a library in Python or Perl, and I want to convert it to a C library...can it be done?
However, you can think to convert a web program to shared library or a set of functions.
My Goal: I want to improve a set of security features.
Yes, you can. The term of art is "embedding," as in "embedded Python" or "embed a Python interpreter." Python has a document about it here: https://docs.python.org/2/extending/embedding.html - the general idea is you must use (at least a small part of) the Python C API to launch Python within a C or C++ application.
Once you realize you can embed a scripting language in C, and that scripting languages can invoke C, you then realize you can also embed one scripting language in another, using C as the bridge between them. For example, RubyLuaBridge: https://bitbucket.org/neomantra/rubyluabridge
A lot of commercial applications embed a scripting language interpreter within a C or C++ host program. A good, well-documented example is Adobe Lightroom, which is roughly half C++ and half Lua. You can read about that from the horse's mouth starting on page xi here: http://www.lua.org/gems/front.pdf
Yes, for python at least: Convert Python program to C/C++ code?.
And for Perl: http://perldoc.perl.org/5.8.9/perlcompile.html.
[EDIT] Per the comment, I'll expand. First, the question "Can I translate Python to C?" has already been answered on SO. See the link.
Second, Perl is actually an interpreted language (just like Python), and has the capability to take that intermediate code and translate it into full blown C for native executables. This is done using the 'B' module and it's other companion modules, such as B::C. There's also a standalone program, 'perlcc' for doing just this. [/EDIT]
We want to use functions written in matlab in our new python application. We want to use ctypes because the user won't need matlab on his machine.
We are testing this method but can't get it to work. We lack c knowledge (and much more...).
This is our simple test matlab function:
function [ z ] = adding( x,y )
z = x + y;
end
We compiled this with matlab into a shared library .dll. In a python interpreter we have:
import ctypes
dl = ctypes.CDLL('adding.dll')
Now we are stuck because we can't find the command to access the function in matlab.
What should we do ?
Short answer - No.
You can not export code written in MATLAB as C in form of DLL and interface with it using ctypes on python side, so that you can afterwards expect a serious performance boost over usual communication via unix pipe (as in mlabwrapper).
The problem is that such DLL is dependent on MCR (matlab runtime). The DLL contains your source code in an obfuscated form. When you call exported function - the DLL is loaded, which then unpacks the source code, creates an instance of MATLAB (an interpreter) and communicates your code and its results with the MATLAB JIT. This functionality is called "MATLAB compiler toolbox". Alternatively, it can produce OS executables (that follow the same logic).
Rewrite in C/C++ (losing dependency on MATLAB)
If you are not lucky to code-generate your project as in here. Consider rewriting your code in plain C or using C++ libraries as IT++ or Armadillo.
There are many resources/tutorials available explaining how to use ctypes and call functions inside a dll. See for example this SO question.
If I remember correctly the matlab compiler should properly export all the functions from the dll so they should be accessible from ctypes. However, you will have to ensure the matlab libraries / runtime are in your library path when you try to load the dll. The matlab site has plenty of docs for this, see for example this tutorial.
I have a bunch (or will have a bunch) of Python code that uses the OpenCV libraries, as well as SimpleCV. I also have a bunch of Haskell code that does some other stuff, but wants to call one function that I define in the Python. This one function returns a three-tuple of doubles.
What's the best way to go about calling this function in Haskell?
For instance, a simplified case is if I have a function in Python
# foo.py
import SimpleCV
def foo():
return (1.0,2.0,3.0)
I want to be able to do this in Haskell
-- bar.hs
main = do
putStrLn $ show pyThingy.foo
I've tried using MissingPy (http://hackage.haskell.org/package/MissingPy), but whenever I try to import the local file I just get
*** Exception: <<MissingPy.Python.Types.PyException>>
Thanks!
You can use Thrift. It's for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.
In python, under what circumstances is SWIG a better choice than ctypes for calling entry points in shared libraries? Let's assume you don't already have the SWIG interface file(s). What are the performance metrics of the two?
I have a rich experience of using swig. SWIG claims that it is a rapid solution for wrapping things. But in real life...
Cons:
SWIG is developed to be general, for everyone and for 20+ languages. Generally, it leads to drawbacks:
- needs configuration (SWIG .i templates), sometimes it is tricky,
- lack of treatment of some special cases (see python properties further),
- lack of performance for some languages.
Python cons:
1) Code style inconsistency. C++ and python have very different code styles (that is obvious, certainly), the possibilities of a swig of making target code more Pythonish is very limited. As an example, it is butt-heart to create properties from getters and setters. See this q&a
2) Lack of broad community. SWIG has some good documentation. But if one caught something that is not in the documentation, there is no information at all. No blogs nor googling helps. So one has to heavily dig SWIG generated code in such cases... That is terrible, I could say...
Pros:
In simple cases, it is really rapid, easy and straight forward
If you produced swig interface files once, you can wrap this C++ code to ANY of other 20+ languages (!!!).
One big concern about SWIG is a performance. Since version 2.04 SWIG includes '-builtin' flag which makes SWIG even faster than other automated ways of wrapping. At least some benchmarks shows this.
When to USE SWIG?
So I concluded for myself two cases when the swig is good to use:
2) If one needs to wrap C++ code for several languages. Or if potentially there could be a time when one needs to distribute the code for several languages. Using SWIG is reliable in this case.
1) If one needs to rapidly wrap just several functions from some C++ library for end use.
Live experience
Update :
It is a year and a half passed as we did a conversion of our library by using SWIG.
First, we made a python version. There were several moments when we experienced troubles with SWIG - it is true. But right now we expanded our library to Java and .NET. So we have 3 languages with 1 SWIG. And I could say that SWIG rocks in terms of saving a LOT of time.
Update 2:
It is two years as we use SWIG for this library. SWIG is integrated into our build system. Recently we had major API change of C++ library. SWIG worked perfectly. The only thing we needed to do is to add several %rename to .i files so our CppCamelStyleFunctions() now looks_more_pythonish in python. First I was concerned about some problems that could arise, but nothing went wrong. It was amazing. Just several edits and everything distributed in 3 languages. Now I am confident that it was a good solution to use SWIG in our case.
Update 3:
It is 3+ years we use SWIG for our library. Major change: python part was totally rewritten in pure python. The reason is that Python is used for the majority of applications of our library now. Even if the pure python version works slower than C++ wrapping, it is more convenient for users to work with pure python, not struggling with native libraries.
SWIG is still used for .NET and Java versions.
The Main question here "Would we use SWIG for python if we started the project from the beginning?". We would! SWIG allowed us to rapidly distribute our product to many languages. It worked for a period of time which gave us the opportunity for better understanding our users requirements.
SWIG generates (rather ugly) C or C++ code. It is straightforward to use for simple functions (things that can be translated directly) and reasonably easy to use for more complex functions (such as functions with output parameters that need an extra translation step to represent in Python.) For more powerful interfacing you often need to write bits of C as part of the interface file. For anything but simple use you will need to know about CPython and how it represents objects -- not hard, but something to keep in mind.
ctypes allows you to directly access C functions, structures and other data, and load arbitrary shared libraries. You do not need to write any C for this, but you do need to understand how C works. It is, you could argue, the flip side of SWIG: it doesn't generate code and it doesn't require a compiler at runtime, but for anything but simple use it does require that you understand how things like C datatypes, casting, memory management and alignment work. You also need to manually or automatically translate C structs, unions and arrays into the equivalent ctypes datastructure, including the right memory layout.
It is likely that in pure execution, SWIG is faster than ctypes -- because the management around the actual work is done in C at compiletime rather than in Python at runtime. However, unless you interface a lot of different C functions but each only a few times, it's unlikely the overhead will be really noticeable.
In development time, ctypes has a much lower startup cost: you don't have to learn about interface files, you don't have to generate .c files and compile them, you don't have to check out and silence warnings. You can just jump in and start using a single C function with minimal effort, then expand it to more. And you get to test and try things out directly in the Python interpreter. Wrapping lots of code is somewhat tedious, although there are attempts to make that simpler (like ctypes-configure.)
SWIG, on the other hand, can be used to generate wrappers for multiple languages (barring language-specific details that need filling in, like the custom C code I mentioned above.) When wrapping lots and lots of code that SWIG can handle with little help, the code generation can also be a lot simpler to set up than the ctypes equivalents.
CTypes is very cool and much easier than SWIG, but it has the drawback that poorly or malevolently-written python code can actually crash the python process. You should also consider boost python. IMHO it's actually easier than swig while giving you more control over the final python interface. If you are using C++ anyway, you also don't add any other languages to your mix.
In my experience, ctypes does have a big disadvantage: when something goes wrong (and it invariably will for any complex interfaces), it's a hell to debug.
The problem is that a big part of your stack is obscured by ctypes/ffi magic and there is no easy way to determine how did you get to a particular point and why parameter values are what they are..
You can also use Pyrex, which can act as glue between high-level Python code and low-level C code. lxml is written in Pyrex, for instance.
ctypes is great, but does not handle C++ classes. I've also found ctypes is about 10% slower than a direct C binding, but that will highly depend on what you are calling.
If you are going to go with ctypes, definitely check out the Pyglet and Pyopengl projects, that have massive examples of ctype bindings.
I'm going to be contrarian and suggest that, if you can, you should write your extension library using the standard Python API. It's really well-integrated from both a C and Python perspective... if you have any experience with the Perl API, you will find it a very pleasant surprise.
Ctypes is nice too, but as others have said, it doesn't do C++.
How big is the library you're trying to wrap? How quickly does the codebase change? Any other maintenance issues? These will all probably affect the choice of the best way to write the Python bindings.
Just wanted to add a few more considerations that I didn't see mentioned yet.
[EDIT: Ooops, didn't see Mike Steder's answer]
If you want to try using a non Cpython implementation (like PyPy, IronPython or Jython), then ctypes is about the only way to go. PyPy doesn't allow writing C-extensions, so that rules out pyrex/cython and Boost.python. For the same reason, ctypes is the only mechanism that will work for IronPython and (eventually, once they get it all working) jython.
As someone else mentioned, no compilation is required. This means that if a new version of the .dll or .so comes out, you can just drop it in, and load that new version. As long as the none of the interfaces changed, it's a drop in replacement.
Something to keep in mind is that SWIG targets only the CPython implementation. Since ctypes is also supported by the PyPy and IronPython implementations it may be worth writing your modules with ctypes for compatibility with the wider Python ecosystem.
I have found SWIG to be be a little bloated in its approach (in general, not just Python) and difficult to implement without having to cross the sore point of writing Python code with an explicit mindset to be SWIG friendly, rather than writing clean well-written Python code. It is, IMHO, a much more straightforward process to write C bindings to C++ (if using C++) and then use ctypes to interface to any C layer.
If the library you are interfacing to has a C interface as part of the library, another advantage of ctypes is that you don't have to compile a separate python-binding library to access third-party libraries. This is particularly nice in formulating a pure-python solution that avoids cross-platform compilation issues (for those third-party libs offered on disparate platforms). Having to embed compiled code into a package you wish to deploy on something like PyPi in a cross-platform friendly way is a pain; one of my most irritating points about Python packages using SWIG or underlying explicit C code is their general inavailability cross-platform. So consider this if you are working with cross-platform available third party libraries and developing a python solution around them.
As a real-world example, consider PyGTK. This (I believe) uses SWIG to generate C code to interface to the GTK C calls. I used this for the briefest time only to find it a real pain to set up and use, with quirky odd errors if you didn't do things in the correct order on setup and just in general. It was such a frustrating experience, and when I looked at the interace definitions provided by GTK on the web I realized what a simple excercise it would be to write a translator of those interface to python ctypes interface. A project called PyGGI was born, and in ONE day I was able to rewrite PyGTK to be a much more functiona and useful product that matches cleanly to the GTK C-object-oriented interfaces. And it required no compilation of C-code making it cross-platform friendly. (I was actually after interfacing to webkitgtk, which isn't so cross-platform). I can also easily deploy PyGGI to any platform supporting GTK.