From eedd37bcefdf3b8a6675c3cc4a3827612e092362 Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Wed, 6 Aug 2008 11:18:07 +0200 Subject: [PATCH] Buffers: Correct order of buffer argument typetest/acquisition --- Cython/Compiler/Nodes.py | 6 +++-- tests/run/bufaccess.pyx | 54 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 0fae29f4..07cd8376 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -894,8 +894,6 @@ class FuncDefNode(StatNode, BlockNode): for entry in lenv.arg_entries: if entry.type.is_pyobject and lenv.control_flow.get_state((entry.name, 'source')) != 'arg': code.put_var_incref(entry) - if entry.type.is_buffer: - Buffer.put_acquire_arg_buffer(entry, code, self.pos) # ----- Initialise local variables for entry in lenv.var_entries: if entry.type.is_pyobject and entry.init_to_none and entry.used: @@ -904,6 +902,10 @@ class FuncDefNode(StatNode, BlockNode): code.putln("%s.buf = NULL;" % entry.buffer_aux.buffer_info_var.cname) # ----- Check and convert arguments self.generate_argument_type_tests(code) + # ----- Acquire buffer arguments + for entry in lenv.arg_entries: + if entry.type.is_buffer: + Buffer.put_acquire_arg_buffer(entry, code, self.pos) # ----- Function body self.body.generate_execution_code(code) # ----- Default return value diff --git a/tests/run/bufaccess.pyx b/tests/run/bufaccess.pyx index e5b6b4f5..7acb4632 100644 --- a/tests/run/bufaccess.pyx +++ b/tests/run/bufaccess.pyx @@ -709,10 +709,8 @@ def printbuf_cytypedef2(object[cytypedef2] buf, shape): print - - # -# Testcase support code +# Testcase support code (more tests below!, because of scope rules) # @@ -895,6 +893,9 @@ cdef class UnsignedShortMockBuffer(MockBuffer): return 0 cdef get_itemsize(self): return sizeof(unsigned short) cdef get_default_format(self): return "=H" + +cdef class IntStridedMockBuffer(MockBuffer): + cdef __cythonbufferdefaults__ = {"mode" : "strided"} cdef class ErrorBuffer: cdef object label @@ -908,3 +909,50 @@ cdef class ErrorBuffer: def __releasebuffer__(MockBuffer self, Py_buffer* buffer): raise Exception("releasing %s" % self.label) +# +# Typed buffers +# +@testcase +def typedbuffer1(obj): + """ + >>> typedbuffer1(IntMockBuffer("A", range(10))) + acquired A + released A + >>> typedbuffer1(None) + >>> typedbuffer1(4) + Traceback (most recent call last): + ... + TypeError: Cannot convert int to bufaccess.IntMockBuffer + """ + cdef IntMockBuffer[int, 1] buf = obj + +@testcase +def typedbuffer2(IntMockBuffer[int, 1] obj): + """ + >>> typedbuffer2(IntMockBuffer("A", range(10))) + acquired A + released A + >>> typedbuffer2(None) + >>> typedbuffer2(4) + Traceback (most recent call last): + ... + TypeError: Argument 'obj' has incorrect type (expected bufaccess.IntMockBuffer, got int) + """ + pass + +# +# Test __cythonbufferdefaults__ +# +@testcase +def bufdefaults1(IntStridedMockBuffer[int, 1] buf): + """ + >>> A = IntStridedMockBuffer("A", range(10)) + >>> bufdefaults1(A) + acquired A + released A + >>> A.recieved_flags + ['FORMAT', 'ND', 'STRIDES'] + """ + pass + + -- 2.26.2