churchyard / rpms / python2

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