An example of ctypes on IronPython?

2019-07-30 20:04发布

问题:

I'm trying to understand ctypes, and it's relationship to IronClad, on IronPython. (Ctypes is supposed to be implemented in the latest IronPython release.) Can somebody give a simple example of ctypes in IronPython that works on Mono/OSX? When trying the standard demos, I get:

import ctypes
SystemError: libdl.so

Am I missing something obvious?

More generally, how does ctypes relate to the IronClad project?

回答1:

I don't know the answer to your first question (I don't use Mono - sorry), but I can answer your general question.

IronClad is an adaptor that allows existing CPython extension modules, written against the CPython API, to work seamlessly on IronPython. Ctypes, on the other hand, is an FFI (Foreign Function Interface) that allows Python code to call into native code in a platform-independent way.

Before ctypes, the only way to access native code from Python was to write a CPython extension that was tightly coupled to CPython; ctypes removes that coupling and allows it to work on any Python implementation that implements ctypes. IronClad exists to allow all of the legacy extensions to continue to work on IronPython. For new stuff, though, ctypes is the way to go.