On 29 August 2011 10:39, Stefan Behnel <stefan...@behnel.de> wrote: > In the CPython backend, the header files are normally #included by the > generated C code, so they are used at C compilation time. > > Cython has its own view on the header files in separate declaration files > (.pxd). Basically looks like this: > > # file "mymath.pxd" > cdef extern from "aheader.h": > double PI > double E > double abs(double x) > > These declaration files usually only contain the parts of a header file that > are used in the user code, either manually copied over or extracted by > scripts (that's what I was referring to in my reply to Terry). The complete > 'real' content of the header file is then used by the C compiler at C > compilation time. > > The user code employs a "cimport" statement to import the declarations at > Cython compilation time, e.g. > > # file "mymodule.pyx" > cimport mymath > print mymath.PI + mymath.E > > would result in C code that #includes "aheader.h", adds the C constants "PI" > and "E", converts the result to a Python float object and prints it out > using the normal CPython machinery.
One thing that would make it easier for me to understand the role of Cython in this context would be to see a simple example of the type of "thin wrapper" we're talking about here. The above code is nearly this, but the pyx file executes "real code". For example, how do I simply expose pi and abs from math.h? Based on the above, I tried a pyx file containing just the code cdef extern from "math.h": double pi double abs(double x) but the resulting module exported no symbols. What am I doing wrong? Could you show a working example of writing such a wrapper? This is probably a bit off-topic, but it seems to me that whenever Cython comes up in these discussions, the implications of Cython-as-an-implementation-of-python obscure the idea of simply using Cython as a means of writing thin library wrappers. Just to clarify - the above code (if it works) seems to me like a nice simple means of writing wrappers. Something involving this in a pxd file, plus a pyx file with a whole load of dummy def abs(x): return cimported_module.abs(x) definitions, seems ok, but annoyingly clumsy. (Particularly for big APIs). I've kept python-dev in this response, on the assumption that others on the list might be glad of seeing a concrete example of using Cython to build wrapper code. But anything further should probably be taken off-list... Thanks, Paul. PS This would also probably be a useful addition to the Cython wiki and/or the manual. I searched both and found very little other than a page on wrapping C++ classes (which is not very helpful for simple C global functions and constants). _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com