casting to typedef pointer/array types (ticket #518)
authorLisandro Dalcin <dalcinl@gmail.com>
Wed, 10 Mar 2010 20:03:40 +0000 (17:03 -0300)
committerLisandro Dalcin <dalcinl@gmail.com>
Wed, 10 Mar 2010 20:03:40 +0000 (17:03 -0300)
Cython/Compiler/PyrexTypes.py
tests/compile/cast_ctypedef_array_T518.pyx [new file with mode: 0644]
tests/compile/cast_ctypedef_array_T518_helper.h [new file with mode: 0644]

index cf7b8b7b1c5e6dc166150744b1e91c8ac4821acd..3d6a011f0b3db764ebe835b660d31a7ede8d0c02 100755 (executable)
@@ -228,8 +228,9 @@ class CTypedefType(BaseType):
     def cast_code(self, expr_code):
         # If self is really an array (rather than pointer), we can't cast.
         # For example, the gmp mpz_t. 
-        if self.typedef_base_type.is_ptr:
-            return self.typedef_base_type.cast_code(expr_code)
+        if self.typedef_base_type.is_array:
+            base_type = self.typedef_base_type.base_type
+            return CPtrType(base_type).cast_code(expr_code)
         else:
             return BaseType.cast_code(self, expr_code)
 
diff --git a/tests/compile/cast_ctypedef_array_T518.pyx b/tests/compile/cast_ctypedef_array_T518.pyx
new file mode 100644 (file)
index 0000000..5d10561
--- /dev/null
@@ -0,0 +1,15 @@
+cdef extern from "cast_ctypedef_array_T518_helper.h":
+    cdef struct __foo_struct:
+       int i, j
+    ctypedef __foo_struct foo_t[1]
+
+    void foo_init(foo_t)
+    void foo_clear(foo_t)
+
+cdef foo_t value
+foo_init(value) 
+foo_clear(value) 
+
+cdef void *pointer = <void*> value
+foo_init(<foo_t>pointer) 
+foo_clear(<foo_t>pointer) 
diff --git a/tests/compile/cast_ctypedef_array_T518_helper.h b/tests/compile/cast_ctypedef_array_T518_helper.h
new file mode 100644 (file)
index 0000000..6989227
--- /dev/null
@@ -0,0 +1,5 @@
+struct __foo_struct { int i, j; };
+typedef struct __foo_struct foo_t[1];
+
+static void foo_init  (foo_t v) { v[0].i = 0; v[0].j = 0; }
+static void foo_clear (foo_t v) { v[0].i = 0; v[0].j = 0; }