From: Dag Sverre Seljebotn Date: Mon, 2 Feb 2009 21:48:39 +0000 (+0100) Subject: Make refnanny trap and complain about NULL arguments to xxxREF X-Git-Tag: 0.11.rc~93^2~17 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=9db97fa58e04caa6c9b4d3bd53bbd9e6b10889ac;p=cython.git Make refnanny trap and complain about NULL arguments to xxxREF --- diff --git a/Cython/Runtime/refnanny.pyx b/Cython/Runtime/refnanny.pyx index c5bea9ee..b1d1321f 100644 --- a/Cython/Runtime/refnanny.pyx +++ b/Cython/Runtime/refnanny.pyx @@ -19,15 +19,21 @@ class RefnannyContext(object): self.refs = {} # id -> (count, [lineno]) self.errors = [] - def regref(self, obj, lineno): - log(LOG_ALL, 'regref', obj, lineno) + def regref(self, obj, lineno, is_null): + log(LOG_ALL, 'regref', "" if is_null else obj, lineno) + if is_null: + self.errors.append("NULL argument on line %d" % lineno) + return id_ = id(obj) count, linenumbers = self.refs.get(id_, (0, [])) self.refs[id_] = (count + 1, linenumbers) linenumbers.append(lineno) - def delref(self, obj, lineno): - log(LOG_ALL, 'delref', obj, lineno) + def delref(self, obj, lineno, is_null): + log(LOG_ALL, 'delref', "" if is_null else obj, lineno) + if is_null: + self.errors.append("NULL argument on line %d" % lineno) + return id_ = id(obj) count, linenumbers = self.refs.get(id_, (0, [])) if count == 0: @@ -56,12 +62,15 @@ cdef public void* __Pyx_Refnanny_NewContext(char* funcname, int lineno) except N Py_INCREF(ctx) return ctx -cdef public void __Pyx_Refnanny_GOTREF(void* ctx, object obj, int lineno): +cdef public void __Pyx_Refnanny_GOTREF(void* ctx, void* p_obj, int lineno): cdef exc.PyObject* type, *value, *tb if ctx == NULL: return exc.PyErr_Fetch(&type, &value, &tb) try: - (ctx).regref(obj, lineno) + if p_obj is NULL: + (ctx).regref(None, lineno, True) + else: + (ctx).regref(p_obj, lineno, False) exc.PyErr_Restore(type, value, tb) except: Py_XDECREF(type) @@ -69,12 +78,15 @@ cdef public void __Pyx_Refnanny_GOTREF(void* ctx, object obj, int lineno): Py_XDECREF(tb) raise -cdef public void __Pyx_Refnanny_GIVEREF(void* ctx, object obj, int lineno): +cdef public void __Pyx_Refnanny_GIVEREF(void* ctx, void* p_obj, int lineno): cdef exc.PyObject* type, *value, *tb if ctx == NULL: return exc.PyErr_Fetch(&type, &value, &tb) try: - (ctx).delref(obj, lineno) + if p_obj is NULL: + (ctx).delref(None, lineno, True) + else: + (ctx).delref(p_obj, lineno, False) exc.PyErr_Restore(type, value, tb) except: Py_XDECREF(type) @@ -82,15 +94,15 @@ cdef public void __Pyx_Refnanny_GIVEREF(void* ctx, object obj, int lineno): Py_XDECREF(tb) raise -cdef public void __Pyx_Refnanny_INCREF(void* ctx, object obj, int lineno): - Py_INCREF(obj) +cdef public void __Pyx_Refnanny_INCREF(void* ctx, void* obj, int lineno): + if obj is not NULL: Py_INCREF(obj) __Pyx_Refnanny_GOTREF(ctx, obj, lineno) -cdef public void __Pyx_Refnanny_DECREF(void* ctx, object obj, int lineno): +cdef public void __Pyx_Refnanny_DECREF(void* ctx, void* obj, int lineno): # GIVEREF raises exception if we hit 0 # __Pyx_Refnanny_GIVEREF(ctx, obj, lineno) - Py_DECREF(obj) + if obj is not NULL: Py_DECREF(obj) cdef public int __Pyx_Refnanny_FinishContext(void* ctx) except -1: cdef exc.PyObject* type, *value, *tb