Buffers: Correct order of buffer argument typetest/acquisition
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Wed, 6 Aug 2008 09:18:07 +0000 (11:18 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Wed, 6 Aug 2008 09:18:07 +0000 (11:18 +0200)
Cython/Compiler/Nodes.py
tests/run/bufaccess.pyx

index 0fae29f4396d112092ac8737ffed12db947c3ae9..07cd837638e6493cc6c7a9bac029db713d91b8c9 100644 (file)
@@ -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
index e5b6b4f5664dd59c1ad5986f1597f94b9e3c0b84..7acb4632c21376b18ae9607d1f23d34f99a57751 100644 (file)
@@ -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
+    
+