[Cython] Status

John Skaller2 skaller at internode.on.net
Thu Jan 30 04:31:05 EST 2020


OMG. I don’t believe it. 

Renaming the binary from *.dylib to *.so fixed it.
Both the Cython and Felix shared libraries can be imported and both run.

Sorry for the noise. Just didn’t occur to me that Python would want *.so filenames on MacOS.
Until I remembered seeing some in Cython build directory ..

Here is the Felix program, just for interest, excluding the binding code, note
I defined “PyObject” in Felix to be “PyObject*” in C.

///////
fun ptest(q:PyObject,w:PyObject):PyObject {
  println$ Py_GetVersion();
  Py_Initialize();

  var x : PyObject = PyLong_FromLong (42);
  var y : long = PyLong_AsLong (x);
  println$ PyLong_Check x;
  println$ y;
  println$ (f"%x" header_version);
  return x;
}

C_hack::ignore (ptest(Py_None,Py_None));

export python fun ptest of (PyObject * PyObject) as "ptest”;
//////////

The ptest fun at the start is an ordinary Felix function that just happens
to use the CPython API bindings I just generated to make a function
with the type actually required for CPython.

The C_hack::ignore (ptest (Py_None, Py_None));

generates a C function call that actually runs it from C. 
The hack is needed because Felix function are not allowed to have
side effects, and, if the result of a function is unused, the function then
cannot serve any useful purpose, and is deleted. The “hack” fools the
compiler into believing the result of the function is actually used.

That call is done as part of the library initialisation.

The final line is the interesting one. It generates a wrapper for the
function ptest, which is a plain C function AND it tells the compiler,
there is a Python function floating about that has to be put into
a Python Module table. So the compiler builds a module table,
and puts the function in it, and then defines PyInit_modulename() 
which returns the module table address, and exports it.

When imported from Python, the initialisation is not done, but the function
can be run from the module dictionary., 

So this shared library can be used BOTH from C, and from Python.
It’s a Felix program AND a Python extenion as well.

The idea has been, you can write ultra-high performance code in Felix,
and then with some work to make a CPython binding to the operations,
you can make the resulting binary library accessible not only to C (and C++),
but also to Python, by simply tell the compiler to export functions to python.

—
John Skaller
skaller at internode.on.net







More information about the cython-devel mailing list