fix strict aliasing issues for type importing code in __Pyx_GetVtable()
authorStefan Behnel <scoder@users.berlios.de>
Thu, 30 Dec 2010 18:17:02 +0000 (19:17 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Thu, 30 Dec 2010 18:17:02 +0000 (19:17 +0100)
Cython/Compiler/ModuleNode.py
Cython/Compiler/Nodes.py

index 65bec8309d4f696b8ba559ccfe784a7bc1d574ad..24ab14c0e62a22f66c104e0079d2392866f700a7 100644 (file)
@@ -2051,12 +2051,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
                                        code.error_goto_if_null(type.typeptr_cname, pos))
         self.use_type_import_utility_code(env)
         if type.vtabptr_cname:
-            code.putln(
-                "if (__Pyx_GetVtable(%s->tp_dict, &%s) < 0) %s" % (
-                    type.typeptr_cname,
-                    type.vtabptr_cname,
-                    code.error_goto(pos)))
             env.use_utility_code(Nodes.get_vtable_utility_code)
+            code.putln("%s = (struct %s*)__Pyx_GetVtable(%s->tp_dict); %s" % (
+                type.vtabptr_cname,
+                type.vtabstruct_cname,
+                type.typeptr_cname,
+                code.error_goto_if_null(type.vtabptr_cname, pos)))
         env.types_imported[type] = 1
 
     py3_type_name_map = {'str' : 'bytes', 'unicode' : 'str'}
index 92c051acf7fc422f2b0813b302f0f3d427399904..32ad3efcd4f01e85164662ea8f3d7a34d9f88c2d 100644 (file)
@@ -6244,25 +6244,26 @@ bad:
 
 get_vtable_utility_code = UtilityCode(
 proto = """
-static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/
+static void* __Pyx_GetVtable(PyObject *dict); /*proto*/
 """,
 impl = r"""
-static int __Pyx_GetVtable(PyObject *dict, void *vtabptr) {
+static void* __Pyx_GetVtable(PyObject *dict) {
+    void* ptr;
     PyObject *ob = PyMapping_GetItemString(dict, (char *)"__pyx_vtable__");
     if (!ob)
         goto bad;
 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
-    *(void **)vtabptr = PyCapsule_GetPointer(ob, 0);
+    ptr = PyCapsule_GetPointer(ob, 0);
 #else
-    *(void **)vtabptr = PyCObject_AsVoidPtr(ob);
+    ptr = PyCObject_AsVoidPtr(ob);
 #endif
-    if (!*(void **)vtabptr)
-        goto bad;
+    if (!ptr && !PyErr_Occurred())
+        PyErr_SetString(PyExc_RuntimeError, "invalid vtable found for imported type");
     Py_DECREF(ob);
-    return 0;
+    return ptr;
 bad:
     Py_XDECREF(ob);
-    return -1;
+    return NULL;
 }
 """)