From 61ee2ad01d52eff40996d58fa53b8adc924464be Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Wed, 6 Aug 2008 10:16:31 +0200 Subject: [PATCH] Buffers: More stable buffer release code --- Cython/Compiler/Buffer.py | 7 ++----- Cython/Compiler/Nodes.py | 5 +++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cython/Compiler/Buffer.py b/Cython/Compiler/Buffer.py index edf56965..74a59f72 100644 --- a/Cython/Compiler/Buffer.py +++ b/Cython/Compiler/Buffer.py @@ -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); } diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index bab1f251..0fae29f4 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -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 -- 2.26.2