[IronPython] Announcement: Project to get some CPython C extensions running under IronPython
tony at v-sim.com
Wed Oct 17 20:07:34 CEST 2007
Paolo Molaro wrote:
> On 10/17/07 Curt Hagenlocher wrote:
>> On 10/17/07, Paolo Molaro <lupus at ximian.com> wrote:
>>> The python API requires a couple dozen structure definitions plus a few
>>> dozen dllimport declarations. This can be about 200 lines of trivial
>>> to write declarative stuff.
>> I'm having trouble understanding what you mean by this. Let's say I've got
>> an extension method that looks like this:
>> PyObject * ModifyColumns(PyObject * self, PyObject * args)
>> In order to use this extension from IronPython, I need C implementations of
>> each of those API functions. The PySequence_ methods should to be able to
>> understand CLR arrays or any other IEnumerable-like object. I'm not sure
>> what PyTuple_New should return, but whatever it is will need to have
>> PyObject*-like semantics.
>> How does DllImport fit into this picture? How can I avoid implementing all
>> these functions in C or C++?
> You do need to write C code as the API is a C API. I didn't list in the
> last mail because the C implementation of these functions is not a
> difference between the C# impl and the C++ impl of the rest of the code.
> The dllimport declarations I mentioned above would pinvoke to these C
Those C functions you speak about will need to execute managed code.
So, either way, what needs to be done is to allow "Reverse P/Invoke" or
some form of DllExport. Assuming this project is successful, native
C/C++ will have to, at some point, execute managed code.
There are only two ways that I know of that you can accomplish this in .NET:
1. C++ Interop
2. Modifying a compiled assembly
Option 1 has the disadvantage of not working in Mono (for now), but I
think is a "cleaner" solution.
Option 2 requires a post build event and I have no idea how that will
affect Mono, as I don't use Mono or ever tried this approach. This
article briefly discusses the method:
The Cecil project might help with this, though I'm not sure:
PostSharp might be able to do this as well (again, I've never used it):
More information about the Ironpython-users