=== modified file 'Include/object.h' --- Include/object.h 2008-06-09 04:58:54 +0000 +++ Include/object.h 2008-06-13 06:13:23 +0000 @@ -725,7 +725,16 @@ (*Py_TYPE(op)->tp_dealloc)((PyObject *)(op))) #endif /* !Py_TRACE_REFS */ +#ifdef Py_DEBUG +PyAPI_FUNC(void) _Py_INCREF_SanityCheck(PyObject *); +#define _Py_SANITY_CHECK_COMMA , +#else +#define _Py_INCREF_SanityCheck(op) +#define _Py_SANITY_CHECK_COMMA +#endif /* !Py_DEBUG */ + #define Py_INCREF(op) ( \ + _Py_INCREF_SanityCheck((PyObject *)(op)) _Py_SANITY_CHECK_COMMA \ _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \ ((PyObject*)(op))->ob_refcnt++) === modified file 'Objects/object.c' --- Objects/object.c 2008-06-09 04:58:54 +0000 +++ Objects/object.c 2008-06-13 06:08:35 +0000 @@ -2019,6 +2019,8 @@ #endif if (op->ob_refcnt < 0) Py_FatalError("UNREF negative refcnt"); + if (op->_ob_prev == NULL || op->_ob_next == NULL) + Py_FatalError("UNREF cleared object"); if (op == &refchain || op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) Py_FatalError("UNREF invalid object"); @@ -2105,6 +2107,18 @@ #endif +#ifdef Py_DEBUG +void +_Py_INCREF_SanityCheck(PyObject *op) +{ + if (op->ob_refcnt == 0) + Py_FatalError("INCREF of 0 refcnt"); + else if (op->ob_refcnt < 0) + Py_FatalError("INCREF of negative refcnt"); +} +#endif + + /* Hack to force loading of cobject.o */ PyTypeObject *_Py_cobject_hack = &PyCObject_Type;