Buffers: More stable buffer release code
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Wed, 6 Aug 2008 08:16:31 +0000 (10:16 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Wed, 6 Aug 2008 08:16:31 +0000 (10:16 +0200)
Cython/Compiler/Buffer.py
Cython/Compiler/Nodes.py

index edf56965ab41e18dd5dbb9e34be242ac4332cd42..74a59f727b12688edf2a3d7da0694a9d287e194b 100644 (file)
@@ -157,8 +157,7 @@ def put_acquire_arg_buffer(entry, code, pos):
 #        entry.buffer_aux.buffer_info_var.cname))
 
 def get_release_buffer_code(entry):
-    return "if (%s != Py_None) __Pyx_ReleaseBuffer(%s, &%s)" % (
-        entry.cname,
+    return "__Pyx_ReleaseBuffer(%s, &%s)" % (
         entry.cname,
         entry.buffer_aux.buffer_info_var.cname)
 
@@ -190,11 +189,8 @@ def put_assign_to_buffer(lhs_cname, rhs_cname, buffer_aux, buffer_type,
 
     if is_initialized:
         # Release any existing buffer
-        code.put('if (%s != Py_None) ' % lhs_cname)
-        code.begin_block();
         code.putln('__Pyx_ReleaseBuffer(%s, &%s);' % (
             lhs_cname, bufstruct))
-        code.end_block()
         # Acquire
         retcode_cname = code.funcstate.allocate_temp(PyrexTypes.c_int_type)
         code.putln("%s = %s;" % (retcode_cname, getbuffer % rhs_cname))
@@ -558,6 +554,7 @@ static INLINE const char* __Pyx_BufferTypestringCheckEndian(const char* ts); /*p
 static void __Pyx_BufferNdimError(Py_buffer* buffer, int expected_ndim); /*proto*/
 """, """
 static INLINE void __Pyx_ReleaseBuffer(PyObject* obj, Py_buffer* info) {
+  if (info->buf == NULL) return;
   if (info->suboffsets == __Pyx_minusones) info->suboffsets = NULL;
   PyObject_ReleaseBuffer(obj, info);
 }
index bab1f251911f0df1c4001ec0d80a9b0c6415712e..0fae29f4396d112092ac8737ffed12db947c3ae9 100644 (file)
@@ -900,6 +900,8 @@ class FuncDefNode(StatNode, BlockNode):
         for entry in lenv.var_entries:
             if entry.type.is_pyobject and entry.init_to_none and entry.used:
                 code.put_init_var_to_py_none(entry)
+            if entry.type.is_buffer and entry.buffer_aux.buffer_info_var.used:
+                code.putln("%s.buf = NULL;" % entry.buffer_aux.buffer_info_var.cname)
         # ----- Check and convert arguments
         self.generate_argument_type_tests(code)
         # ----- Function body
@@ -2047,8 +2049,11 @@ class CClassDefNode(ClassDefNode):
     #  body               StatNode or None
     #  entry              Symtab.Entry
     #  base_type          PyExtensionType or None
+    #  bufferdefaults     dict or None      Declares defaults for a buffer
+
     
     child_attrs = ["body"]
+    bufferdefaults = None
 
     def analyse_declarations(self, env):
         #print "CClassDefNode.analyse_declarations:", self.class_name