churchyard / rpms / python2

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