Hi,

I am attaching the two files that I want Cython to generate.

It seem to do the job. So far, I just did them by hand and I will test
my code more in the coming days. If all is fine, I will try to modify
Cython to do that automatically. The static->extern is easy, this
patch does it:

ond...@raven:~/repos/cython-devel$ hg diff
diff -r b8f0fc5153e0 Cython/Compiler/ModuleNode.py
--- a/Cython/Compiler/ModuleNode.py     Thu Mar 11 14:06:32 2010 -0800
+++ b/Cython/Compiler/ModuleNode.py     Thu Mar 11 18:13:19 2010 -0800
@@ -184,7 +184,7 @@
                 h_code.putln("")
                 for entry in api_funcs:
                     type = CPtrType(entry.type)
-                    h_code.putln("static %s;" %
type.declaration_code(entry.cname))
+                    h_code.putln("extern %s;" %
type.declaration_code(entry.cname))
             h_code.putln("")
             h_code.put_h_guard(Naming.api_func_guard + "import_module")
             h_code.put(import_module_utility_code.impl)


but the rest will be a bit more complicated. Let me know if you think
this is the right approach. I guess I could add some cython cmdline
option to control the global/local api.

Ondrej
#ifndef __PYX_HAVE_API___hermes_common
#define __PYX_HAVE_API___hermes_common
#include "Python.h"

extern PyObject *(*c2py_CooMatrix)(struct CooMatrix *);
extern PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *);
extern PyObject *(*c2py_CSCMatrix)(struct CSCMatrix *);
extern void (*cmd)(const char*);
extern void (*set_verbose_cmd)(int);
extern void (*insert_object)(const char*, PyObject *);
extern PyObject *(*get_object)(const char*);
extern PyObject *(*c2py_int)(int);
extern int (*py2c_int)(PyObject *);
extern char *(*py2c_str)(PyObject *);
extern double (*py2c_double)(PyObject *);
extern PyObject *(*c2numpy_int)(int *, int);
extern PyObject *(*c2numpy_int_inplace)(int *, int);
extern PyObject *(*c2numpy_double)(double *, int);
extern PyObject *(*c2numpy_double_inplace)(double *, int);
extern void (*numpy2c_int_inplace)(PyObject *, int **, int *);
extern void (*numpy2c_double_inplace)(PyObject *, double **, int *);
extern PyObject *(*run_cmd)(const char*, PyObject *);

extern PyObject *__Pyx_ImportModule(const char *name);
extern int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig);
extern int import__hermes_common(void);

#endif
#include "_hermes_common_api.h"

PyObject *(*c2py_CooMatrix)(struct CooMatrix *);
PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *);
PyObject *(*c2py_CSCMatrix)(struct CSCMatrix *);
void (*cmd)(const char*);
void (*set_verbose_cmd)(int);
void (*insert_object)(const char*, PyObject *);
PyObject *(*get_object)(const char*);
PyObject *(*c2py_int)(int);
int (*py2c_int)(PyObject *);
char *(*py2c_str)(PyObject *);
double (*py2c_double)(PyObject *);
PyObject *(*c2numpy_int)(int *, int);
PyObject *(*c2numpy_int_inplace)(int *, int);
PyObject *(*c2numpy_double)(double *, int);
PyObject *(*c2numpy_double_inplace)(double *, int);
void (*numpy2c_int_inplace)(PyObject *, int **, int *);
void (*numpy2c_double_inplace)(PyObject *, double **, int *);
PyObject *(*run_cmd)(const char*, PyObject *);

PyObject *__Pyx_ImportModule(const char *name) {
    PyObject *py_name = 0;
    PyObject *py_module = 0;

    #if PY_MAJOR_VERSION < 3
    py_name = PyString_FromString(name);
    #else
    py_name = PyUnicode_FromString(name);
    #endif
    if (!py_name)
        goto bad;
    py_module = PyImport_Import(py_name);
    Py_DECREF(py_name);
    return py_module;
bad:
    Py_XDECREF(py_name);
    return 0;
}

int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig) {
    PyObject *d = 0;
    PyObject *cobj = 0;
    union {
        void (*fp)(void);
        void *p;
    } tmp;
#if PY_VERSION_HEX < 0x03010000
    const char *desc, *s1, *s2;
#endif

    d = PyObject_GetAttrString(module, (char *)"__pyx_capi__");
    if (!d)
        goto bad;
    cobj = PyDict_GetItemString(d, funcname);
    if (!cobj) {
        PyErr_Format(PyExc_ImportError,
            "%s does not export expected C function %s",
                PyModule_GetName(module), funcname);
        goto bad;
    }
#if PY_VERSION_HEX < 0x03010000
    desc = (const char *)PyCObject_GetDesc(cobj);
    if (!desc)
        goto bad;
    s1 = desc; s2 = sig;
    while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; }
    if (*s1 != *s2) {
        PyErr_Format(PyExc_TypeError,
            "C function %s.%s has wrong signature (expected %s, got %s)",
             PyModule_GetName(module), funcname, sig, desc);
        goto bad;
    }
    tmp.p = PyCObject_AsVoidPtr(cobj);
#else
    if (!PyCapsule_IsValid(cobj, sig)) {
        PyErr_Format(PyExc_TypeError,
            "C function %s.%s has wrong signature (expected %s, got %s)",
             PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj));
        goto bad;
    }
    tmp.p = PyCapsule_GetPointer(cobj, sig);
#endif
    *f = tmp.fp;
    if (!(*f))
        goto bad;
    Py_DECREF(d);
    return 0;
bad:
    Py_XDECREF(d);
    return -1;
}

int import__hermes_common(void) {
  PyObject *module = 0;
  module = __Pyx_ImportModule("_hermes_common");
  if (!module) goto bad;
  if (__Pyx_ImportFunction(module, "c2py_CooMatrix", (void (**)(void))&c2py_CooMatrix, "PyObject *(struct CooMatrix *)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "c2py_CSRMatrix", (void (**)(void))&c2py_CSRMatrix, "PyObject *(struct CSRMatrix *)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "c2py_CSCMatrix", (void (**)(void))&c2py_CSCMatrix, "PyObject *(struct CSCMatrix *)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "cmd", (void (**)(void))&cmd, "void (const char*)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "set_verbose_cmd", (void (**)(void))&set_verbose_cmd, "void (int)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "insert_object", (void (**)(void))&insert_object, "void (const char*, PyObject *)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "get_object", (void (**)(void))&get_object, "PyObject *(const char*)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "c2py_int", (void (**)(void))&c2py_int, "PyObject *(int)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "py2c_int", (void (**)(void))&py2c_int, "int (PyObject *)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "py2c_str", (void (**)(void))&py2c_str, "char *(PyObject *)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "py2c_double", (void (**)(void))&py2c_double, "double (PyObject *)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "c2numpy_int", (void (**)(void))&c2numpy_int, "PyObject *(int *, int)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "c2numpy_int_inplace", (void (**)(void))&c2numpy_int_inplace, "PyObject *(int *, int)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "c2numpy_double", (void (**)(void))&c2numpy_double, "PyObject *(double *, int)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "c2numpy_double_inplace", (void (**)(void))&c2numpy_double_inplace, "PyObject *(double *, int)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "numpy2c_int_inplace", (void (**)(void))&numpy2c_int_inplace, "void (PyObject *, int **, int *)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "numpy2c_double_inplace", (void (**)(void))&numpy2c_double_inplace, "void (PyObject *, double **, int *)") < 0) goto bad;
  if (__Pyx_ImportFunction(module, "run_cmd", (void (**)(void))&run_cmd, "PyObject *(const char*, PyObject *)") < 0) goto bad;
  Py_DECREF(module); module = 0;
  return 0;
  bad:
  Py_XDECREF(module);
  return -1;
}
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to