From eb3e1da129643a63de93d1a3f27765363143258a Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Mon, 4 Aug 2008 09:59:21 +0200 Subject: [PATCH] Fixed some typedef bugs with buffers --- Cython/Compiler/PyrexTypes.py | 2 + tests/errors/e_bufaccess2.pyx | 10 +++++ tests/errors/e_bufaccess_pxd.pxd | 3 ++ tests/run/bufaccess.pyx | 77 ++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 tests/errors/e_bufaccess2.pyx create mode 100644 tests/errors/e_bufaccess_pxd.pxd diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index b188088e..6132baf0 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -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 index 00000000..55217ca6 --- /dev/null +++ b/tests/errors/e_bufaccess2.pyx @@ -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 index 00000000..f30877fe --- /dev/null +++ b/tests/errors/e_bufaccess_pxd.pxd @@ -0,0 +1,3 @@ +# See e_bufaccess2.pyx + +ctypedef nothing T diff --git a/tests/run/bufaccess.pyx b/tests/run/bufaccess.pyx index a40a937f..d7a04778 100644 --- a/tests/run/bufaccess.pyx +++ b/tests/run/bufaccess.pyx @@ -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: + (buf)[0] = 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: (buf)[0] = value -- 2.26.2