Disallow <double*>obj (#313). Also removes warning for <void*>obj.
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Thu, 14 May 2009 13:25:49 +0000 (15:25 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Thu, 14 May 2009 13:25:49 +0000 (15:25 +0200)
Cython/Compiler/ExprNodes.py
tests/errors/pyobjcastdisallow_T313.pyx [new file with mode: 0644]
tests/run/pyobjcast_T313.pyx [new file with mode: 0644]

index e2e25af25dce8a41d55f0468210e51f895cee770..ef0664037f7aba366bac0f4adaa15069650b1f9f 100644 (file)
@@ -3937,6 +3937,8 @@ class TypecastNode(NewTempExprNode):
         elif from_py and not to_py:
             if self.type.from_py_function:
                 self.operand = self.operand.coerce_to(self.type, env)
+            elif self.type.is_ptr and not self.type.base_type.is_void:
+                error(self.pos, "Python objects can only be cast to void*")
             else:
                 warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.type, self.operand.type))
         elif from_py and to_py:
diff --git a/tests/errors/pyobjcastdisallow_T313.pyx b/tests/errors/pyobjcastdisallow_T313.pyx
new file mode 100644 (file)
index 0000000..3d8ea1c
--- /dev/null
@@ -0,0 +1,9 @@
+
+a = 3
+
+cdef void* allowed = <void*>a
+cdef double* disallowed = <double*>a
+
+_ERRORS = u"""
+5:26: Python objects can only be cast to void*
+"""
diff --git a/tests/run/pyobjcast_T313.pyx b/tests/run/pyobjcast_T313.pyx
new file mode 100644 (file)
index 0000000..178f1d7
--- /dev/null
@@ -0,0 +1,18 @@
+# Ensure casting still works to void*
+
+"""
+>>> f()
+('teststring', 'teststring')
+"""
+
+cdef extern from *:
+    ctypedef void PyObject
+
+def f():
+    cdef void* p1
+    cdef PyObject* p2
+    a = "teststring"
+    p1 = <void*>a
+    p2 = <PyObject*>a
+    return (<object>p1, <object>p2)
+