From 49ef0f6bb06f44ee8c4cd1486d6c701c4f117a95 Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Wed, 14 Oct 2009 18:45:46 -0300 Subject: [PATCH] Public module C-API is broken under Python 3.2 (ticket #407) --HG-- extra : rebase_source : b760de58ec941046c86e5ecadb5afc4c56d9ddaa --- Cython/Compiler/ModuleNode.py | 42 +++++++++++++++++------------- Cython/Compiler/Nodes.py | 48 ++++++++++++++++------------------- 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 0b43ebd4..68dd56f5 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -2198,11 +2198,6 @@ static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *s """, impl = r""" static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) { -#if PY_VERSION_HEX < 0x02050000 - char *api = (char *)"%(API)s"; -#else - const char *api = "%(API)s"; -#endif PyObject *d = 0; PyObject *cobj = 0; union { @@ -2210,19 +2205,22 @@ static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *s void *p; } tmp; - - d = PyObject_GetAttrString(%(MODULE)s, api); + d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s"); if (!d) { PyErr_Clear(); d = PyDict_New(); if (!d) goto bad; Py_INCREF(d); - if (PyModule_AddObject(%(MODULE)s, api, d) < 0) + if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0) goto bad; } tmp.fp = f; +#if PY_VERSION_HEX < 0x03010000 cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0); +#else + cobj = PyCapsule_New(tmp.p, sig, 0); +#endif if (!cobj) goto bad; if (PyDict_SetItemString(d, name, cobj) < 0) @@ -2238,8 +2236,6 @@ bad: """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name} ) -#------------------------------------------------------------------------------------ - function_import_utility_code = UtilityCode( proto = """ static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/ @@ -2248,21 +2244,17 @@ impl = """ #ifndef __PYX_HAVE_RT_ImportFunction #define __PYX_HAVE_RT_ImportFunction static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig) { -#if PY_VERSION_HEX < 0x02050000 - char *api = (char *)"%(API)s"; -#else - const char *api = "%(API)s"; -#endif PyObject *d = 0; PyObject *cobj = 0; - const char *desc; - const char *s1, *s2; union { void (*fp)(void); void *p; } tmp; +#if PY_VERSION_HEX < 0x03010000 + const char *desc, *s1, *s2; +#endif - d = PyObject_GetAttrString(module, api); + d = PyObject_GetAttrString(module, (char *)"%(API)s"); if (!d) goto bad; cobj = PyDict_GetItemString(d, funcname); @@ -2272,6 +2264,7 @@ static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (** PyModule_GetName(module), funcname); goto bad; } +#if PY_VERSION_HEX < 0x03010000 desc = (const char *)PyCObject_GetDesc(cobj); if (!desc) goto bad; @@ -2284,7 +2277,18 @@ static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (** 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: @@ -2295,6 +2299,8 @@ bad: """ % dict(API = Naming.api_name) ) +#------------------------------------------------------------------------------------ + register_cleanup_utility_code = UtilityCode( proto = """ static int __Pyx_RegisterCleanup(void); /*proto*/ diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 1cad2602..4e1e409a 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -5425,22 +5425,20 @@ static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/ """, impl = """ static int __Pyx_SetVtable(PyObject *dict, void *vtable) { - PyObject *pycobj = 0; - int result; - - pycobj = PyCObject_FromVoidPtr(vtable, 0); - if (!pycobj) +#if PY_VERSION_HEX < 0x03010000 + PyObject *ob = PyCObject_FromVoidPtr(vtable, 0); +#else + PyObject *ob = PyCapsule_New(vtable, 0, 0); +#endif + if (!ob) goto bad; - if (PyDict_SetItemString(dict, "__pyx_vtable__", pycobj) < 0) + if (PyDict_SetItemString(dict, "__pyx_vtable__", ob) < 0) goto bad; - result = 0; - goto done; - + Py_DECREF(ob); + return 0; bad: - result = -1; -done: - Py_XDECREF(pycobj); - return result; + Py_XDECREF(ob); + return -1; } """) @@ -5452,23 +5450,21 @@ static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/ """, impl = r""" static int __Pyx_GetVtable(PyObject *dict, void *vtabptr) { - int result; - PyObject *pycobj; - - pycobj = PyMapping_GetItemString(dict, (char *)"__pyx_vtable__"); - if (!pycobj) + PyObject *ob = PyMapping_GetItemString(dict, (char *)"__pyx_vtable__"); + if (!ob) goto bad; - *(void **)vtabptr = PyCObject_AsVoidPtr(pycobj); +#if PY_VERSION_HEX < 0x03010000 + *(void **)vtabptr = PyCObject_AsVoidPtr(ob); +#else + *(void **)vtabptr = PyCapsule_GetPointer(ob, 0); +#endif if (!*(void **)vtabptr) goto bad; - result = 0; - goto done; - + Py_DECREF(ob); + return 0; bad: - result = -1; -done: - Py_XDECREF(pycobj); - return result; + Py_XDECREF(ob); + return -1; } """) -- 2.26.2