From 1eb5949290c42fdb72532c8f812d03eb378515b5 Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Thu, 14 May 2009 15:25:49 +0200 Subject: [PATCH] Disallow obj (#313). Also removes warning for obj. --- Cython/Compiler/ExprNodes.py | 2 ++ tests/errors/pyobjcastdisallow_T313.pyx | 9 +++++++++ tests/run/pyobjcast_T313.pyx | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 tests/errors/pyobjcastdisallow_T313.pyx create mode 100644 tests/run/pyobjcast_T313.pyx 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 = a +cdef double* disallowed = 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 = a + p2 = a + return (p1, p2) + -- 2.26.2