Fixed some typedef bugs with buffers
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Mon, 4 Aug 2008 07:59:21 +0000 (09:59 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Mon, 4 Aug 2008 07:59:21 +0000 (09:59 +0200)
Cython/Compiler/PyrexTypes.py
tests/errors/e_bufaccess2.pyx [new file with mode: 0644]
tests/errors/e_bufaccess_pxd.pxd [new file with mode: 0644]
tests/run/bufaccess.pyx

index b188088eddd406a7befe8734c047fb43249b6f34..6132baf0d0bfeb4f73847e5d4e1b815c247e5852 100644 (file)
@@ -149,6 +149,7 @@ class CTypedefType(BaseType):
     #  typedef_base_type   PyrexType
     
     is_typedef = 1
+    typestring = None # Because typedefs are not known exactly
     
     def __init__(self, cname, base_type):
         self.typedef_cname = cname
@@ -1043,6 +1044,7 @@ class ErrorType(PyrexType):
     exception_check    = 0
     to_py_function = "dummy"
     from_py_function = "dummy"
+    typestring = None
     
     def declaration_code(self, entity_code, 
             for_display = 0, dll_linkage = None, pyrex = 0):
diff --git a/tests/errors/e_bufaccess2.pyx b/tests/errors/e_bufaccess2.pyx
new file mode 100644 (file)
index 0000000..55217ca
--- /dev/null
@@ -0,0 +1,10 @@
+cimport e_bufaccess_pxd # was needed to provoke a bug involving ErrorType
+
+def f():
+    cdef object[e_bufaccess_pxd.T] buf
+
+_ERRORS = u"""
+3:17: Syntax error in ctypedef statement
+4:31: 'T' is not a type identifier
+4:31: 'T' is not declared
+"""
diff --git a/tests/errors/e_bufaccess_pxd.pxd b/tests/errors/e_bufaccess_pxd.pxd
new file mode 100644 (file)
index 0000000..f30877f
--- /dev/null
@@ -0,0 +1,3 @@
+# See e_bufaccess2.pyx
+
+ctypedef nothing T
index a40a937fc06e3e1738eb48d9790097f73f811ef2..d7a0477825e1a33fdc9c8d0d0a8877d52654d9a8 100644 (file)
@@ -576,6 +576,76 @@ def printbuf_float(o, shape):
     print
 
 
+#
+# Typedefs
+#
+ctypedef int cytypedef_int
+cdef extern from "bufaccess.h":
+    ctypedef cytypedef_int htypedef_short # Defined as short, but Cython doesn't know this!
+ctypedef htypedef_short cytypedef2
+
+@testcase
+def printbuf_cytypedef_int(object[cytypedef_int] buf, shape):
+    """
+    >>> printbuf_cytypedef_int(IntMockBuffer("A", range(3)), (3,))
+    acquired A
+    0 1 2
+    released A
+    >>> printbuf_cytypedef_int(ShortMockBuffer("B", range(3)), (3,))
+    Traceback (most recent call last):
+       ...
+    ValueError: Buffer datatype mismatch (rejecting on 'h')
+    
+    """
+    cdef int i
+    for i in range(shape[0]):
+        print buf[i],
+    print
+
+@testcase
+def printbuf_htypedef_short(object[htypedef_short] buf, shape):
+    """
+    >>> printbuf_htypedef_short(ShortMockBuffer("A", range(3)), (3,))
+    acquired A
+    0 1 2
+    released A
+    >>> printbuf_htypedef_short(IntMockBuffer("B", range(3)), (3,))
+    Traceback (most recent call last):
+       ...
+    ValueError: Buffer datatype mismatch (rejecting on 'i')
+    """
+    
+    cdef int i
+    for i in range(shape[0]):
+        print buf[i],
+    print
+
+@testcase
+def printbuf_cytypedef2(object[cytypedef2] buf, shape):
+    """
+    >>> printbuf_cytypedef2(ShortMockBuffer("A", range(3)), (3,))
+    acquired A
+    0 1 2
+    released A
+    >>> printbuf_cytypedef2(IntMockBuffer("B", range(3)), (3,))
+    Traceback (most recent call last):
+       ...
+    ValueError: Buffer datatype mismatch (rejecting on 'i')
+    """
+    
+    cdef int i
+    for i in range(shape[0]):
+        print buf[i],
+    print
+
+
+
+
+#
+# Testcase support code
+#
+
+
 available_flags = (
     ('FORMAT', python_buffer.PyBUF_FORMAT),
     ('INDIRECT', python_buffer.PyBUF_INDIRECT),
@@ -736,6 +806,13 @@ cdef class IntMockBuffer(MockBuffer):
     cdef get_itemsize(self): return sizeof(int)
     cdef get_default_format(self): return "=i"
 
+cdef class ShortMockBuffer(MockBuffer):
+    cdef int write(self, char* buf, object value) except -1:
+        (<short*>buf)[0] = <short>value
+        return 0
+    cdef get_itemsize(self): return sizeof(short)
+    cdef get_default_format(self): return "=h"
+
 cdef class UnsignedShortMockBuffer(MockBuffer):
     cdef int write(self, char* buf, object value) except -1:
         (<unsigned short*>buf)[0] = <unsigned short>value