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