Commit 1301d2a5 authored by Stefan Behnel's avatar Stefan Behnel

use reset-before-decref to safely clear references in closures

parent 695dc278
...@@ -1321,19 +1321,30 @@ class CCodeWriter(object): ...@@ -1321,19 +1321,30 @@ class CCodeWriter(object):
if entry.type.is_pyobject: if entry.type.is_pyobject:
self.putln("__Pyx_XDECREF(%s);" % self.entry_as_pyobject(entry)) self.putln("__Pyx_XDECREF(%s);" % self.entry_as_pyobject(entry))
def put_var_decref_clear(self, entry):
if entry.type.is_pyobject:
self.putln("__Pyx_DECREF(%s); %s = 0;" % (
self.entry_as_pyobject(entry), entry.cname))
def put_var_xdecref(self, entry): def put_var_xdecref(self, entry):
if entry.type.is_pyobject: if entry.type.is_pyobject:
self.putln("__Pyx_XDECREF(%s);" % self.entry_as_pyobject(entry)) self.putln("__Pyx_XDECREF(%s);" % self.entry_as_pyobject(entry))
def put_var_decref_clear(self, entry):
self._put_var_decref_clear(entry, null_check=False)
def put_var_xdecref_clear(self, entry): def put_var_xdecref_clear(self, entry):
self._put_var_decref_clear(entry, null_check=True)
def _put_var_decref_clear(self, entry, null_check):
if entry.type.is_pyobject: if entry.type.is_pyobject:
self.putln("__Pyx_XDECREF(%s); %s = 0;" % ( if entry.in_closure:
self.entry_as_pyobject(entry), entry.cname)) # reset before DECREF to make sure closure state is
# consistent during call to DECREF()
self.putln("__Pyx_%sCLEAR(%s);" % (
null_check and 'X' or '',
entry.cname))
else:
self.putln("__Pyx_%sDECREF(%s); %s = 0;" % (
self.entry_as_pyobject(entry),
entry.cname,
null_check and 'X' or '',
entry.cname))
def put_var_decrefs(self, entries, used_only = 0): def put_var_decrefs(self, entries, used_only = 0):
for entry in entries: for entry in entries:
......
...@@ -2966,6 +2966,9 @@ proto=""" ...@@ -2966,6 +2966,9 @@ proto="""
#define __Pyx_XGOTREF(r) #define __Pyx_XGOTREF(r)
#define __Pyx_XGIVEREF(r) #define __Pyx_XGIVEREF(r)
#endif /* CYTHON_REFNANNY */ #endif /* CYTHON_REFNANNY */
#define __Pyx_CLEAR(r) do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0)
#define __Pyx_XCLEAR(r) do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0)
""", """,
impl=""" impl="""
#if CYTHON_REFNANNY #if CYTHON_REFNANNY
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment