Is it possible to use the Python C-API with PyPy on Windows?
Has it ever been tested?

I tried to build and import a simple hello world extension module using the C-API with PyPy on Windows. It fails. You can find the code (and its expected and observed behavior) at the end of this post.

One reason it fails is that the definition of PyMODINIT_FUNC in modsupport.h is incorrect. On Windows the module initialization function needs a __declspec(dllexport) declaration or it will not be exported from the DLL.

Currently modsupport.h contains the code

    #ifdef __cplusplus
    #define PyMODINIT_FUNC extern "C" void
    #else
    #define PyMODINIT_FUNC void
    #endif

This should be something like

    #ifdef __cplusplus
    #  ifdef _WIN32
    #    define PyMODINIT_FUNC extern "C" __declspec(dllexport) void
    #  else
    #    define PyMODINIT_FUNC extern "C" void
    #  endif
    #else
    #  ifdef _WIN32
    #    define PyMODINIT_FUNC __declspec(dllexport) void
    #  else
    #    define PyMODINIT_FUNC void
    #  endif
    #endif

But even after I fixed this issue, I'm still unable to import the extension module using the Python C-API on Windows. (The observed behavior was the same, before and after I fixed this.)

-------------------------------------------------------------------------------------------------------------------------

C code:

    #include <Python.h>

    static PyObject* bar(void)
    {
        return PyString_FromString("Hello world!");
    }

    static PyMethodDef fooMethods[] = {
        {"bar", (PyCFunction)&bar, METH_NOARGS, ""},
        {NULL, NULL, 0, NULL}
    };

    PyMODINIT_FUNC initfoo(void)
    {
        Py_InitModule("foo", fooMethods);
    }

Python code:

    import foo
    print foo.bar()

Expected behavior and observed behavior with CPython:

    Hello world!

Observed behavior with PyPy:

    Traceback (most recent call last):
      File "app_main.py", line 72, in run_toplevel
      File "C:\Test\test\test.py", line 1, in <module>
        import foo
    ImportError: No module named foo

---------------------------------------------------------------------------------------

--Johan


_______________________________________________
pypy-dev mailing list
pypy-dev@python.org
https://mail.python.org/mailman/listinfo/pypy-dev

Reply via email to