cython-devel

changeset 2488:8a58f1544bd8

Public module C-API is broken under Python 3.2 (ticket #407)
author Lisandro Dalcin <dalcinl@gmail.com>
date Wed Oct 14 18:45:46 2009 -0300 (2 years ago)
parents d7526bc3c818
children 014a3ca7e197
files Cython/Compiler/ModuleNode.py Cython/Compiler/Nodes.py
line diff
1.1 --- a/Cython/Compiler/ModuleNode.py Thu Oct 15 12:49:20 2009 -0700 1.2 +++ b/Cython/Compiler/ModuleNode.py Wed Oct 14 18:45:46 2009 -0300 1.3 @@ -2198,11 +2198,6 @@ 1.4 """, 1.5 impl = r""" 1.6 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) { 1.7 -#if PY_VERSION_HEX < 0x02050000 1.8 - char *api = (char *)"%(API)s"; 1.9 -#else 1.10 - const char *api = "%(API)s"; 1.11 -#endif 1.12 PyObject *d = 0; 1.13 PyObject *cobj = 0; 1.14 union { 1.15 @@ -2210,19 +2205,22 @@ 1.16 void *p; 1.17 } tmp; 1.18 1.19 - 1.20 - d = PyObject_GetAttrString(%(MODULE)s, api); 1.21 + d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s"); 1.22 if (!d) { 1.23 PyErr_Clear(); 1.24 d = PyDict_New(); 1.25 if (!d) 1.26 goto bad; 1.27 Py_INCREF(d); 1.28 - if (PyModule_AddObject(%(MODULE)s, api, d) < 0) 1.29 + if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0) 1.30 goto bad; 1.31 } 1.32 tmp.fp = f; 1.33 +#if PY_VERSION_HEX < 0x03010000 1.34 cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0); 1.35 +#else 1.36 + cobj = PyCapsule_New(tmp.p, sig, 0); 1.37 +#endif 1.38 if (!cobj) 1.39 goto bad; 1.40 if (PyDict_SetItemString(d, name, cobj) < 0) 1.41 @@ -2238,8 +2236,6 @@ 1.42 """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name} 1.43 ) 1.44 1.45 -#------------------------------------------------------------------------------------ 1.46 - 1.47 function_import_utility_code = UtilityCode( 1.48 proto = """ 1.49 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/ 1.50 @@ -2248,21 +2244,17 @@ 1.51 #ifndef __PYX_HAVE_RT_ImportFunction 1.52 #define __PYX_HAVE_RT_ImportFunction 1.53 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig) { 1.54 -#if PY_VERSION_HEX < 0x02050000 1.55 - char *api = (char *)"%(API)s"; 1.56 -#else 1.57 - const char *api = "%(API)s"; 1.58 -#endif 1.59 PyObject *d = 0; 1.60 PyObject *cobj = 0; 1.61 - const char *desc; 1.62 - const char *s1, *s2; 1.63 union { 1.64 void (*fp)(void); 1.65 void *p; 1.66 } tmp; 1.67 +#if PY_VERSION_HEX < 0x03010000 1.68 + const char *desc, *s1, *s2; 1.69 +#endif 1.70 1.71 - d = PyObject_GetAttrString(module, api); 1.72 + d = PyObject_GetAttrString(module, (char *)"%(API)s"); 1.73 if (!d) 1.74 goto bad; 1.75 cobj = PyDict_GetItemString(d, funcname); 1.76 @@ -2272,6 +2264,7 @@ 1.77 PyModule_GetName(module), funcname); 1.78 goto bad; 1.79 } 1.80 +#if PY_VERSION_HEX < 0x03010000 1.81 desc = (const char *)PyCObject_GetDesc(cobj); 1.82 if (!desc) 1.83 goto bad; 1.84 @@ -2284,7 +2277,18 @@ 1.85 goto bad; 1.86 } 1.87 tmp.p = PyCObject_AsVoidPtr(cobj); 1.88 +#else 1.89 + if (!PyCapsule_IsValid(cobj, sig)) { 1.90 + PyErr_Format(PyExc_TypeError, 1.91 + "C function %%s.%%s has wrong signature (expected %%s, got %%s)", 1.92 + PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj)); 1.93 + goto bad; 1.94 + } 1.95 + tmp.p = PyCapsule_GetPointer(cobj, sig); 1.96 +#endif 1.97 *f = tmp.fp; 1.98 + if (!(*f)) 1.99 + goto bad; 1.100 Py_DECREF(d); 1.101 return 0; 1.102 bad: 1.103 @@ -2295,6 +2299,8 @@ 1.104 """ % dict(API = Naming.api_name) 1.105 ) 1.106 1.107 +#------------------------------------------------------------------------------------ 1.108 + 1.109 register_cleanup_utility_code = UtilityCode( 1.110 proto = """ 1.111 static int __Pyx_RegisterCleanup(void); /*proto*/
2.1 --- a/Cython/Compiler/Nodes.py Thu Oct 15 12:49:20 2009 -0700 2.2 +++ b/Cython/Compiler/Nodes.py Wed Oct 14 18:45:46 2009 -0300 2.3 @@ -5425,22 +5425,20 @@ 2.4 """, 2.5 impl = """ 2.6 static int __Pyx_SetVtable(PyObject *dict, void *vtable) { 2.7 - PyObject *pycobj = 0; 2.8 - int result; 2.9 - 2.10 - pycobj = PyCObject_FromVoidPtr(vtable, 0); 2.11 - if (!pycobj) 2.12 +#if PY_VERSION_HEX < 0x03010000 2.13 + PyObject *ob = PyCObject_FromVoidPtr(vtable, 0); 2.14 +#else 2.15 + PyObject *ob = PyCapsule_New(vtable, 0, 0); 2.16 +#endif 2.17 + if (!ob) 2.18 goto bad; 2.19 - if (PyDict_SetItemString(dict, "__pyx_vtable__", pycobj) < 0) 2.20 + if (PyDict_SetItemString(dict, "__pyx_vtable__", ob) < 0) 2.21 goto bad; 2.22 - result = 0; 2.23 - goto done; 2.24 - 2.25 + Py_DECREF(ob); 2.26 + return 0; 2.27 bad: 2.28 - result = -1; 2.29 -done: 2.30 - Py_XDECREF(pycobj); 2.31 - return result; 2.32 + Py_XDECREF(ob); 2.33 + return -1; 2.34 } 2.35 """) 2.36 2.37 @@ -5452,23 +5450,21 @@ 2.38 """, 2.39 impl = r""" 2.40 static int __Pyx_GetVtable(PyObject *dict, void *vtabptr) { 2.41 - int result; 2.42 - PyObject *pycobj; 2.43 - 2.44 - pycobj = PyMapping_GetItemString(dict, (char *)"__pyx_vtable__"); 2.45 - if (!pycobj) 2.46 + PyObject *ob = PyMapping_GetItemString(dict, (char *)"__pyx_vtable__"); 2.47 + if (!ob) 2.48 goto bad; 2.49 - *(void **)vtabptr = PyCObject_AsVoidPtr(pycobj); 2.50 +#if PY_VERSION_HEX < 0x03010000 2.51 + *(void **)vtabptr = PyCObject_AsVoidPtr(ob); 2.52 +#else 2.53 + *(void **)vtabptr = PyCapsule_GetPointer(ob, 0); 2.54 +#endif 2.55 if (!*(void **)vtabptr) 2.56 goto bad; 2.57 - result = 0; 2.58 - goto done; 2.59 - 2.60 + Py_DECREF(ob); 2.61 + return 0; 2.62 bad: 2.63 - result = -1; 2.64 -done: 2.65 - Py_XDECREF(pycobj); 2.66 - return result; 2.67 + Py_XDECREF(ob); 2.68 + return -1; 2.69 } 2.70 """) 2.71