|
|
b7580a |
diff -up Python-2.7.3/Lib/test/test_gc.py.gc-assertions Python-2.7.3/Lib/test/test_gc.py
|
|
|
b7580a |
--- Python-2.7.3/Lib/test/test_gc.py.gc-assertions 2013-02-20 16:28:20.890536607 -0500
|
|
|
b7580a |
+++ Python-2.7.3/Lib/test/test_gc.py 2013-02-20 16:39:52.720489297 -0500
|
|
|
b7580a |
@@ -1,6 +1,7 @@
|
|
|
b7580a |
import unittest
|
|
|
b7580a |
-from test.test_support import verbose, run_unittest
|
|
|
b7580a |
+from test.test_support import verbose, run_unittest, import_module
|
|
|
b7580a |
import sys
|
|
|
b7580a |
+import sysconfig
|
|
|
b7580a |
import time
|
|
|
b7580a |
import gc
|
|
|
b7580a |
import weakref
|
|
|
b7580a |
@@ -32,6 +33,8 @@ class GC_Detector(object):
|
|
|
b7580a |
self.wr = weakref.ref(C1055820(666), it_happened)
|
|
|
b7580a |
|
|
|
b7580a |
|
|
|
b7580a |
+BUILT_WITH_NDEBUG = ('-DNDEBUG' in sysconfig.get_config_vars()['PY_CFLAGS'])
|
|
|
b7580a |
+
|
|
|
b7580a |
### Tests
|
|
|
b7580a |
###############################################################################
|
|
|
b7580a |
|
|
|
b7580a |
@@ -476,6 +479,49 @@ class GCTests(unittest.TestCase):
|
|
|
b7580a |
# would be damaged, with an empty __dict__.
|
|
|
b7580a |
self.assertEqual(x, None)
|
|
|
b7580a |
|
|
|
b7580a |
+ @unittest.skipIf(BUILT_WITH_NDEBUG,
|
|
|
b7580a |
+ 'built with -NDEBUG')
|
|
|
b7580a |
+ def test_refcount_errors(self):
|
|
|
b7580a |
+ # Verify the "handling" of objects with broken refcounts
|
|
|
b7580a |
+
|
|
|
b7580a |
+ import_module("ctypes") #skip if not supported
|
|
|
b7580a |
+
|
|
|
b7580a |
+ import subprocess
|
|
|
b7580a |
+ code = '''if 1:
|
|
|
b7580a |
+ a = []
|
|
|
b7580a |
+ b = [a]
|
|
|
b7580a |
+
|
|
|
b7580a |
+ # Simulate the refcount of "a" being too low (compared to the
|
|
|
b7580a |
+ # references held on it by live data), but keeping it above zero
|
|
|
b7580a |
+ # (to avoid deallocating it):
|
|
|
b7580a |
+ import ctypes
|
|
|
b7580a |
+ ctypes.pythonapi.Py_DecRef(ctypes.py_object(a))
|
|
|
b7580a |
+
|
|
|
b7580a |
+ # The garbage collector should now have a fatal error when it reaches
|
|
|
b7580a |
+ # the broken object:
|
|
|
b7580a |
+ import gc
|
|
|
b7580a |
+ gc.collect()
|
|
|
b7580a |
+ '''
|
|
|
b7580a |
+ p = subprocess.Popen([sys.executable, "-c", code],
|
|
|
b7580a |
+ stdout=subprocess.PIPE,
|
|
|
b7580a |
+ stderr=subprocess.PIPE)
|
|
|
b7580a |
+ stdout, stderr = p.communicate()
|
|
|
b7580a |
+ p.stdout.close()
|
|
|
b7580a |
+ p.stderr.close()
|
|
|
b7580a |
+ # Verify that stderr has a useful error message:
|
|
|
b7580a |
+ self.assertRegexpMatches(stderr,
|
|
|
b7580a |
+ b'Modules/gcmodule.c:[0-9]+: visit_decref: Assertion "gc->gc.gc_refs != 0" failed.')
|
|
|
b7580a |
+ self.assertRegexpMatches(stderr,
|
|
|
b7580a |
+ b'refcount was too small')
|
|
|
b7580a |
+ self.assertRegexpMatches(stderr,
|
|
|
b7580a |
+ b'object : \[\]')
|
|
|
b7580a |
+ self.assertRegexpMatches(stderr,
|
|
|
b7580a |
+ b'type : list')
|
|
|
b7580a |
+ self.assertRegexpMatches(stderr,
|
|
|
b7580a |
+ b'refcount: 1')
|
|
|
b7580a |
+ self.assertRegexpMatches(stderr,
|
|
|
b7580a |
+ b'address : 0x[0-9a-f]+')
|
|
|
b7580a |
+
|
|
|
b7580a |
class GCTogglingTests(unittest.TestCase):
|
|
|
b7580a |
def setUp(self):
|
|
|
b7580a |
gc.enable()
|
|
|
b7580a |
diff -up Python-2.7.3/Modules/gcmodule.c.gc-assertions Python-2.7.3/Modules/gcmodule.c
|
|
|
b7580a |
--- Python-2.7.3/Modules/gcmodule.c.gc-assertions 2012-04-09 19:07:34.000000000 -0400
|
|
|
b7580a |
+++ Python-2.7.3/Modules/gcmodule.c 2013-02-20 16:28:21.029536600 -0500
|
|
|
b7580a |
@@ -21,6 +21,73 @@
|
|
|
b7580a |
#include "Python.h"
|
|
|
b7580a |
#include "frameobject.h" /* for PyFrame_ClearFreeList */
|
|
|
b7580a |
|
|
|
b7580a |
+/*
|
|
|
b7580a |
+ Define a pair of assertion macros.
|
|
|
b7580a |
+
|
|
|
b7580a |
+ These work like the regular C assert(), in that they will abort the
|
|
|
b7580a |
+ process with a message on stderr if the given condition fails to hold,
|
|
|
b7580a |
+ but compile away to nothing if NDEBUG is defined.
|
|
|
b7580a |
+
|
|
|
b7580a |
+ However, before aborting, Python will also try to call _PyObject_Dump() on
|
|
|
b7580a |
+ the given object. This may be of use when investigating bugs in which a
|
|
|
b7580a |
+ particular object is corrupt (e.g. buggy a tp_visit method in an extension
|
|
|
b7580a |
+ module breaking the garbage collector), to help locate the broken objects.
|
|
|
b7580a |
+
|
|
|
b7580a |
+ The WITH_MSG variant allows you to supply an additional message that Python
|
|
|
b7580a |
+ will attempt to print to stderr, after the object dump.
|
|
|
b7580a |
+*/
|
|
|
b7580a |
+#ifdef NDEBUG
|
|
|
b7580a |
+/* No debugging: compile away the assertions: */
|
|
|
b7580a |
+#define PyObject_ASSERT_WITH_MSG(obj, expr, msg) ((void)0)
|
|
|
b7580a |
+#else
|
|
|
b7580a |
+/* With debugging: generate checks: */
|
|
|
b7580a |
+#define PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
|
|
|
b7580a |
+ ((expr) \
|
|
|
b7580a |
+ ? (void)(0) \
|
|
|
b7580a |
+ : _PyObject_AssertFailed((obj), \
|
|
|
b7580a |
+ (msg), \
|
|
|
b7580a |
+ (__STRING(expr)), \
|
|
|
b7580a |
+ (__FILE__), \
|
|
|
b7580a |
+ (__LINE__), \
|
|
|
b7580a |
+ (__PRETTY_FUNCTION__)))
|
|
|
b7580a |
+#endif
|
|
|
b7580a |
+
|
|
|
b7580a |
+#define PyObject_ASSERT(obj, expr) \
|
|
|
b7580a |
+ PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
|
|
|
b7580a |
+
|
|
|
b7580a |
+static void _PyObject_AssertFailed(PyObject *, const char *,
|
|
|
b7580a |
+ const char *, const char *, int,
|
|
|
b7580a |
+ const char *);
|
|
|
b7580a |
+
|
|
|
b7580a |
+static void
|
|
|
b7580a |
+_PyObject_AssertFailed(PyObject *obj, const char *msg, const char *expr,
|
|
|
b7580a |
+ const char *file, int line, const char *function)
|
|
|
b7580a |
+{
|
|
|
b7580a |
+ fprintf(stderr,
|
|
|
b7580a |
+ "%s:%d: %s: Assertion \"%s\" failed.\n",
|
|
|
b7580a |
+ file, line, function, expr);
|
|
|
b7580a |
+ if (msg) {
|
|
|
b7580a |
+ fprintf(stderr, "%s\n", msg);
|
|
|
b7580a |
+ }
|
|
|
b7580a |
+
|
|
|
b7580a |
+ fflush(stderr);
|
|
|
b7580a |
+
|
|
|
b7580a |
+ if (obj) {
|
|
|
b7580a |
+ /* This might succeed or fail, but we're about to abort, so at least
|
|
|
b7580a |
+ try to provide any extra info we can: */
|
|
|
b7580a |
+ _PyObject_Dump(obj);
|
|
|
b7580a |
+ }
|
|
|
b7580a |
+ else {
|
|
|
b7580a |
+ fprintf(stderr, "NULL object\n");
|
|
|
b7580a |
+ }
|
|
|
b7580a |
+
|
|
|
b7580a |
+ fflush(stdout);
|
|
|
b7580a |
+ fflush(stderr);
|
|
|
b7580a |
+
|
|
|
b7580a |
+ /* Terminate the process: */
|
|
|
b7580a |
+ abort();
|
|
|
b7580a |
+}
|
|
|
b7580a |
+
|
|
|
b7580a |
/* Get an object's GC head */
|
|
|
b7580a |
#define AS_GC(o) ((PyGC_Head *)(o)-1)
|
|
|
b7580a |
|
|
|
b7580a |
@@ -288,7 +355,8 @@ update_refs(PyGC_Head *containers)
|
|
|
b7580a |
{
|
|
|
b7580a |
PyGC_Head *gc = containers->gc.gc_next;
|
|
|
b7580a |
for (; gc != containers; gc = gc->gc.gc_next) {
|
|
|
b7580a |
- assert(gc->gc.gc_refs == GC_REACHABLE);
|
|
|
b7580a |
+ PyObject_ASSERT(FROM_GC(gc),
|
|
|
b7580a |
+ gc->gc.gc_refs == GC_REACHABLE);
|
|
|
b7580a |
gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc));
|
|
|
b7580a |
/* Python's cyclic gc should never see an incoming refcount
|
|
|
b7580a |
* of 0: if something decref'ed to 0, it should have been
|
|
|
b7580a |
@@ -308,7 +376,8 @@ update_refs(PyGC_Head *containers)
|
|
|
b7580a |
* so serious that maybe this should be a release-build
|
|
|
b7580a |
* check instead of an assert?
|
|
|
b7580a |
*/
|
|
|
b7580a |
- assert(gc->gc.gc_refs != 0);
|
|
|
b7580a |
+ PyObject_ASSERT(FROM_GC(gc),
|
|
|
b7580a |
+ gc->gc.gc_refs != 0);
|
|
|
b7580a |
}
|
|
|
b7580a |
}
|
|
|
b7580a |
|
|
|
b7580a |
@@ -323,7 +392,9 @@ visit_decref(PyObject *op, void *data)
|
|
|
b7580a |
* generation being collected, which can be recognized
|
|
|
b7580a |
* because only they have positive gc_refs.
|
|
|
b7580a |
*/
|
|
|
b7580a |
- assert(gc->gc.gc_refs != 0); /* else refcount was too small */
|
|
|
b7580a |
+ PyObject_ASSERT_WITH_MSG(FROM_GC(gc),
|
|
|
b7580a |
+ gc->gc.gc_refs != 0,
|
|
|
b7580a |
+ "refcount was too small");
|
|
|
b7580a |
if (gc->gc.gc_refs > 0)
|
|
|
b7580a |
gc->gc.gc_refs--;
|
|
|
b7580a |
}
|
|
|
b7580a |
@@ -383,9 +454,10 @@ visit_reachable(PyObject *op, PyGC_Head
|
|
|
b7580a |
* If gc_refs == GC_UNTRACKED, it must be ignored.
|
|
|
b7580a |
*/
|
|
|
b7580a |
else {
|
|
|
b7580a |
- assert(gc_refs > 0
|
|
|
b7580a |
- || gc_refs == GC_REACHABLE
|
|
|
b7580a |
- || gc_refs == GC_UNTRACKED);
|
|
|
b7580a |
+ PyObject_ASSERT(FROM_GC(gc),
|
|
|
b7580a |
+ gc_refs > 0
|
|
|
b7580a |
+ || gc_refs == GC_REACHABLE
|
|
|
b7580a |
+ || gc_refs == GC_UNTRACKED);
|
|
|
b7580a |
}
|
|
|
b7580a |
}
|
|
|
b7580a |
return 0;
|
|
|
b7580a |
@@ -427,7 +499,7 @@ move_unreachable(PyGC_Head *young, PyGC_
|
|
|
b7580a |
*/
|
|
|
b7580a |
PyObject *op = FROM_GC(gc);
|
|
|
b7580a |
traverseproc traverse = Py_TYPE(op)->tp_traverse;
|
|
|
b7580a |
- assert(gc->gc.gc_refs > 0);
|
|
|
b7580a |
+ PyObject_ASSERT(op, gc->gc.gc_refs > 0);
|
|
|
b7580a |
gc->gc.gc_refs = GC_REACHABLE;
|
|
|
b7580a |
(void) traverse(op,
|
|
|
b7580a |
(visitproc)visit_reachable,
|
|
|
b7580a |
@@ -494,7 +566,8 @@ move_finalizers(PyGC_Head *unreachable,
|
|
|
b7580a |
for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
|
|
|
b7580a |
PyObject *op = FROM_GC(gc);
|
|
|
b7580a |
|
|
|
b7580a |
- assert(IS_TENTATIVELY_UNREACHABLE(op));
|
|
|
b7580a |
+ PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
|
|
|
b7580a |
+
|
|
|
b7580a |
next = gc->gc.gc_next;
|
|
|
b7580a |
|
|
|
b7580a |
if (has_finalizer(op)) {
|
|
|
b7580a |
@@ -570,7 +643,7 @@ handle_weakrefs(PyGC_Head *unreachable,
|
|
|
b7580a |
PyWeakReference **wrlist;
|
|
|
b7580a |
|
|
|
b7580a |
op = FROM_GC(gc);
|
|
|
b7580a |
- assert(IS_TENTATIVELY_UNREACHABLE(op));
|
|
|
b7580a |
+ PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
|
|
|
b7580a |
next = gc->gc.gc_next;
|
|
|
b7580a |
|
|
|
b7580a |
if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
|
|
|
b7580a |
@@ -591,9 +664,9 @@ handle_weakrefs(PyGC_Head *unreachable,
|
|
|
b7580a |
* the callback pointer intact. Obscure: it also
|
|
|
b7580a |
* changes *wrlist.
|
|
|
b7580a |
*/
|
|
|
b7580a |
- assert(wr->wr_object == op);
|
|
|
b7580a |
+ PyObject_ASSERT(wr->wr_object, wr->wr_object == op);
|
|
|
b7580a |
_PyWeakref_ClearRef(wr);
|
|
|
b7580a |
- assert(wr->wr_object == Py_None);
|
|
|
b7580a |
+ PyObject_ASSERT(wr->wr_object, wr->wr_object == Py_None);
|
|
|
b7580a |
if (wr->wr_callback == NULL)
|
|
|
b7580a |
continue; /* no callback */
|
|
|
b7580a |
|
|
|
b7580a |
@@ -627,7 +700,7 @@ handle_weakrefs(PyGC_Head *unreachable,
|
|
|
b7580a |
*/
|
|
|
b7580a |
if (IS_TENTATIVELY_UNREACHABLE(wr))
|
|
|
b7580a |
continue;
|
|
|
b7580a |
- assert(IS_REACHABLE(wr));
|
|
|
b7580a |
+ PyObject_ASSERT(op, IS_REACHABLE(wr));
|
|
|
b7580a |
|
|
|
b7580a |
/* Create a new reference so that wr can't go away
|
|
|
b7580a |
* before we can process it again.
|
|
|
b7580a |
@@ -636,7 +709,8 @@ handle_weakrefs(PyGC_Head *unreachable,
|
|
|
b7580a |
|
|
|
b7580a |
/* Move wr to wrcb_to_call, for the next pass. */
|
|
|
b7580a |
wrasgc = AS_GC(wr);
|
|
|
b7580a |
- assert(wrasgc != next); /* wrasgc is reachable, but
|
|
|
b7580a |
+ PyObject_ASSERT(op, wrasgc != next);
|
|
|
b7580a |
+ /* wrasgc is reachable, but
|
|
|
b7580a |
next isn't, so they can't
|
|
|
b7580a |
be the same */
|
|
|
b7580a |
gc_list_move(wrasgc, &wrcb_to_call);
|
|
|
b7580a |
@@ -652,11 +726,11 @@ handle_weakrefs(PyGC_Head *unreachable,
|
|
|
b7580a |
|
|
|
b7580a |
gc = wrcb_to_call.gc.gc_next;
|
|
|
b7580a |
op = FROM_GC(gc);
|
|
|
b7580a |
- assert(IS_REACHABLE(op));
|
|
|
b7580a |
- assert(PyWeakref_Check(op));
|
|
|
b7580a |
+ PyObject_ASSERT(op, IS_REACHABLE(op));
|
|
|
b7580a |
+ PyObject_ASSERT(op, PyWeakref_Check(op));
|
|
|
b7580a |
wr = (PyWeakReference *)op;
|
|
|
b7580a |
callback = wr->wr_callback;
|
|
|
b7580a |
- assert(callback != NULL);
|
|
|
b7580a |
+ PyObject_ASSERT(op, callback != NULL);
|
|
|
b7580a |
|
|
|
b7580a |
/* copy-paste of weakrefobject.c's handle_callback() */
|
|
|
b7580a |
temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
|
|
|
b7580a |
@@ -759,7 +833,7 @@ delete_garbage(PyGC_Head *collectable, P
|
|
|
b7580a |
PyGC_Head *gc = collectable->gc.gc_next;
|
|
|
b7580a |
PyObject *op = FROM_GC(gc);
|
|
|
b7580a |
|
|
|
b7580a |
- assert(IS_TENTATIVELY_UNREACHABLE(op));
|
|
|
b7580a |
+ PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
|
|
|
b7580a |
if (debug & DEBUG_SAVEALL) {
|
|
|
b7580a |
PyList_Append(garbage, op);
|
|
|
b7580a |
}
|