From: Dag Sverre Seljebotn <dagss@student.matnat.uio.no>
Date: Thu, 14 May 2009 13:25:49 +0000 (+0200)
Subject: Disallow <double*>obj (#313). Also removes warning for <void*>obj.
X-Git-Tag: 0.11.2.rc1~10^2~10
X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1eb5949290c42fdb72532c8f812d03eb378515b5;p=cython.git

Disallow <double*>obj (#313). Also removes warning for <void*>obj.
---

diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
index e2e25af2..ef066403 100644
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -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
index 00000000..3d8ea1ce
--- /dev/null
+++ b/tests/errors/pyobjcastdisallow_T313.pyx
@@ -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
index 00000000..178f1d74
--- /dev/null
+++ b/tests/run/pyobjcast_T313.pyx
@@ -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)
+