carlwgeorge / rpms / python34

Forked from rpms/python34 6 years ago
Clone
e32ce18
Index: Include/object.h
e32ce18
===================================================================
e32ce18
--- Include/object.h	(revision 87911)
e32ce18
+++ Include/object.h	(working copy)
e32ce18
@@ -914,6 +914,49 @@
e32ce18
 _PyObject_DebugTypeStats(FILE *out);
e32ce18
 #endif /* ifndef Py_LIMITED_API */
e32ce18
 
e32ce18
+/* 
e32ce18
+   Define a pair of assertion macros.
e32ce18
+
e32ce18
+   These work like the regular C assert(), in that they will abort the
e32ce18
+   process with a message on stderr if the given condition fails to hold,
e32ce18
+   but compile away to nothing if NDEBUG is defined.
e32ce18
+
e32ce18
+   However, before aborting, Python will also try to call _PyObject_Dump() on
e32ce18
+   the given object.  This may be of use when investigating bugs in which a
e32ce18
+   particular object is corrupt (e.g. buggy a tp_visit method in an extension
e32ce18
+   module breaking the garbage collector), to help locate the broken objects.
e32ce18
+
e32ce18
+   The WITH_MSG variant allows you to supply an additional message that Python
e32ce18
+   will attempt to print to stderr, after the object dump.
e32ce18
+*/
e32ce18
+#ifdef NDEBUG
e32ce18
+/* No debugging: compile away the assertions: */
e32ce18
+#define PyObject_ASSERT_WITH_MSG(obj, expr, msg) ((void)0)
e32ce18
+#else
e32ce18
+/* With debugging: generate checks: */
e32ce18
+#define PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
e32ce18
+  ((expr)                                           \
e32ce18
+   ? (void)(0)                                      \
e32ce18
+   : _PyObject_AssertFailed((obj),                  \
e32ce18
+                            (msg),                  \
e32ce18
+                            (__STRING(expr)),       \
e32ce18
+                            (__FILE__),             \
e32ce18
+                            (__LINE__),             \
e32ce18
+                            (__PRETTY_FUNCTION__)))
e32ce18
+#endif
e32ce18
+
e32ce18
+#define PyObject_ASSERT(obj, expr) \
e32ce18
+  PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
e32ce18
+
e32ce18
+/* 
e32ce18
+   Declare and define the entrypoint even when NDEBUG is defined, to avoid
e32ce18
+   causing compiler/linker errors when building extensions without NDEBUG
e32ce18
+   against a Python built with NDEBUG defined
e32ce18
+*/
e32ce18
+PyAPI_FUNC(void) _PyObject_AssertFailed(PyObject *,  const char *,
e32ce18
+                                        const char *, const char *, int,
e32ce18
+                                        const char *);
e32ce18
+
e32ce18
 #ifdef __cplusplus
e32ce18
 }
e32ce18
 #endif
e32ce18
Index: Objects/object.c
e32ce18
===================================================================
e32ce18
--- Objects/object.c	(revision 87911)
e32ce18
+++ Objects/object.c	(working copy)
e32ce18
@@ -1899,6 +1899,35 @@
e32ce18
     }
e32ce18
 }
e32ce18
 
e32ce18
+PyAPI_FUNC(void)
e32ce18
+_PyObject_AssertFailed(PyObject *obj, const char *msg, const char *expr,
e32ce18
+		       const char *file, int line, const char *function)
e32ce18
+{
e32ce18
+    fprintf(stderr,
e32ce18
+            "%s:%d: %s: Assertion \"%s\" failed.\n",
e32ce18
+            file, line, function, expr);
e32ce18
+    if (msg) {
e32ce18
+        fprintf(stderr, "%s\n", msg);
e32ce18
+    }
e32ce18
+
e32ce18
+    fflush(stderr);
e32ce18
+
e32ce18
+    if (obj) {
e32ce18
+        /* This might succeed or fail, but we're about to abort, so at least
e32ce18
+           try to provide any extra info we can: */
e32ce18
+        _PyObject_Dump(obj);
e32ce18
+    }
e32ce18
+    else {
e32ce18
+        fprintf(stderr, "NULL object\n");
e32ce18
+    }
e32ce18
+
e32ce18
+    fflush(stdout);
e32ce18
+    fflush(stderr);
e32ce18
+
e32ce18
+    /* Terminate the process: */
e32ce18
+    abort();
e32ce18
+}
e32ce18
+
e32ce18
 #ifndef Py_TRACE_REFS
e32ce18
 /* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
e32ce18
    Define this here, so we can undefine the macro. */
e32ce18
Index: Lib/test/test_gc.py
e32ce18
===================================================================
e32ce18
--- Lib/test/test_gc.py	(revision 87911)
e32ce18
+++ Lib/test/test_gc.py	(working copy)
e32ce18
@@ -1,6 +1,6 @@
e32ce18
 import unittest
e32ce18
 from test.support import (verbose, refcount_test, run_unittest,
e32ce18
-                            strip_python_stderr, cpython_only)
e32ce18
+                            strip_python_stderr, cpython_only, import_module)
e32ce18
 from test.script_helper import assert_python_ok, make_script, temp_dir
e32ce18
e32ce18
 import sys
e32ce18
@@ -512,6 +512,48 @@
e32ce18
         self.assertEqual(len(gc.garbage), 0)
e32ce18
 
e32ce18
 
e32ce18
+    def test_refcount_errors(self):
e32ce18
+        self.preclean()
e32ce18
+        # Verify the "handling" of objects with broken refcounts
e32ce18
+        import_module("ctypes") #skip if not supported
e32ce18
+
e32ce18
+        import subprocess
e32ce18
+        code = '''if 1:
e32ce18
+        a = []
e32ce18
+        b = [a]
e32ce18
+
e32ce18
+        # Simulate the refcount of "a" being too low (compared to the
e32ce18
+        # references held on it by live data), but keeping it above zero
e32ce18
+        # (to avoid deallocating it):
e32ce18
+        import ctypes
e32ce18
+        ctypes.pythonapi.Py_DecRef(ctypes.py_object(a))
e32ce18
+
e32ce18
+        # The garbage collector should now have a fatal error when it reaches
e32ce18
+        # the broken object:
e32ce18
+        import gc
e32ce18
+        gc.collect()
e32ce18
+        '''
e32ce18
+        p = subprocess.Popen([sys.executable, "-c", code],
e32ce18
+                             stdout=subprocess.PIPE,
e32ce18
+                             stderr=subprocess.PIPE)
e32ce18
+        stdout, stderr = p.communicate()
e32ce18
+        p.stdout.close()
e32ce18
+        p.stderr.close()
e32ce18
+        # Verify that stderr has a useful error message:
e32ce18
+        self.assertRegex(stderr,
e32ce18
+            b'Modules/gcmodule.c:[0-9]+: visit_decref: Assertion "\(\(gc\)->gc.gc_refs >> \(1\)\) != 0" failed.')
e32ce18
+        self.assertRegex(stderr,
e32ce18
+            b'refcount was too small')
e32ce18
+        self.assertRegex(stderr,
e32ce18
+            b'object  : \[\]')
e32ce18
+        self.assertRegex(stderr,
e32ce18
+            b'type    : list')
e32ce18
+        self.assertRegex(stderr,
e32ce18
+            b'refcount: 1')
e32ce18
+        self.assertRegex(stderr,
e32ce18
+            b'address : 0x[0-9a-f]+')
e32ce18
+
e32ce18
+
e32ce18
 class GCTogglingTests(unittest.TestCase):
e32ce18
     def setUp(self):
e32ce18
         gc.enable()
e32ce18
diff -up Modules/gcmodule.c.orig2 Modules/gcmodule.c
e32ce18
--- Modules/gcmodule.c.orig2	2014-12-08 10:54:14.251742911 +0100
e32ce18
+++ Modules/gcmodule.c	2014-12-08 10:52:45.674771917 +0100
e32ce18
@@ -341,7 +341,8 @@ update_refs(PyGC_Head *containers)
e32ce18
 {
e32ce18
     PyGC_Head *gc = containers->gc.gc_next;
e32ce18
     for (; gc != containers; gc = gc->gc.gc_next) {
e32ce18
-        assert(_PyGCHead_REFS(gc) == GC_REACHABLE);
e32ce18
+        PyObject_ASSERT(FROM_GC(gc),
e32ce18
+                        _PyGCHead_REFS(gc) == GC_REACHABLE);
e32ce18
         _PyGCHead_SET_REFS(gc, Py_REFCNT(FROM_GC(gc)));
e32ce18
         /* Python's cyclic gc should never see an incoming refcount
e32ce18
          * of 0:  if something decref'ed to 0, it should have been
e32ce18
@@ -361,7 +362,8 @@ update_refs(PyGC_Head *containers)
e32ce18
          * so serious that maybe this should be a release-build
e32ce18
          * check instead of an assert?
e32ce18
          */
e32ce18
-        assert(_PyGCHead_REFS(gc) != 0);
e32ce18
+        PyObject_ASSERT(FROM_GC(gc),
e32ce18
+                        _PyGCHead_REFS(gc) != 0);
e32ce18
     }
e32ce18
 }
e32ce18
 
e32ce18
@@ -376,7 +378,9 @@ visit_decref(PyObject *op, void *data)
e32ce18
          * generation being collected, which can be recognized
e32ce18
          * because only they have positive gc_refs.
e32ce18
          */
e32ce18
-        assert(_PyGCHead_REFS(gc) != 0); /* else refcount was too small */
e32ce18
+        PyObject_ASSERT_WITH_MSG(FROM_GC(gc),
e32ce18
+                        _PyGCHead_REFS(gc) != 0,
e32ce18
+                        "refcount was too small"); /* else refcount was too small */
e32ce18
         if (_PyGCHead_REFS(gc) > 0)
e32ce18
             _PyGCHead_DECREF(gc);
e32ce18
     }
e32ce18
@@ -436,9 +440,10 @@ visit_reachable(PyObject *op, PyGC_Head
e32ce18
          * If gc_refs == GC_UNTRACKED, it must be ignored.
e32ce18
          */
e32ce18
          else {
e32ce18
-            assert(gc_refs > 0
e32ce18
-                   || gc_refs == GC_REACHABLE
e32ce18
-                   || gc_refs == GC_UNTRACKED);
e32ce18
+             PyObject_ASSERT(FROM_GC(gc),
e32ce18
+                             gc_refs > 0
e32ce18
+                             || gc_refs == GC_REACHABLE
e32ce18
+                             || gc_refs == GC_UNTRACKED);
e32ce18
          }
e32ce18
     }
e32ce18
     return 0;
e32ce18
@@ -480,7 +485,7 @@ move_unreachable(PyGC_Head *young, PyGC_
e32ce18
              */
e32ce18
             PyObject *op = FROM_GC(gc);
e32ce18
             traverseproc traverse = Py_TYPE(op)->tp_traverse;
e32ce18
-            assert(_PyGCHead_REFS(gc) > 0);
e32ce18
+            PyObject_ASSERT(op, _PyGCHead_REFS(gc) > 0);
e32ce18
             _PyGCHead_SET_REFS(gc, GC_REACHABLE);
e32ce18
             (void) traverse(op,
e32ce18
                             (visitproc)visit_reachable,
e32ce18
@@ -543,7 +548,7 @@ move_legacy_finalizers(PyGC_Head *unreac
e32ce18
     for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
e32ce18
         PyObject *op = FROM_GC(gc);
e32ce18
 
e32ce18
-        assert(IS_TENTATIVELY_UNREACHABLE(op));
e32ce18
+        PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
e32ce18
         next = gc->gc.gc_next;
e32ce18
 
e32ce18
         if (has_legacy_finalizer(op)) {
e32ce18
@@ -619,7 +624,7 @@ handle_weakrefs(PyGC_Head *unreachable,
e32ce18
         PyWeakReference **wrlist;
e32ce18
 
e32ce18
         op = FROM_GC(gc);
e32ce18
-        assert(IS_TENTATIVELY_UNREACHABLE(op));
e32ce18
+        PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
e32ce18
         next = gc->gc.gc_next;
e32ce18
 
e32ce18
         if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
e32ce18
@@ -640,9 +645,9 @@ handle_weakrefs(PyGC_Head *unreachable,
e32ce18
              * the callback pointer intact.  Obscure:  it also
e32ce18
              * changes *wrlist.
e32ce18
              */
e32ce18
-            assert(wr->wr_object == op);
e32ce18
+            PyObject_ASSERT(wr->wr_object, wr->wr_object == op);
e32ce18
             _PyWeakref_ClearRef(wr);
e32ce18
-            assert(wr->wr_object == Py_None);
e32ce18
+            PyObject_ASSERT(wr->wr_object, wr->wr_object == Py_None);
e32ce18
             if (wr->wr_callback == NULL)
e32ce18
                 continue;                       /* no callback */
e32ce18
 
e32ce18
@@ -676,7 +681,7 @@ handle_weakrefs(PyGC_Head *unreachable,
e32ce18
      */
e32ce18
             if (IS_TENTATIVELY_UNREACHABLE(wr))
e32ce18
                 continue;
e32ce18
-            assert(IS_REACHABLE(wr));
e32ce18
+            PyObject_ASSERT(op, IS_REACHABLE(wr));
e32ce18
 
e32ce18
             /* Create a new reference so that wr can't go away
e32ce18
              * before we can process it again.
e32ce18
@@ -685,7 +690,8 @@ handle_weakrefs(PyGC_Head *unreachable,
e32ce18
 
e32ce18
             /* Move wr to wrcb_to_call, for the next pass. */
e32ce18
             wrasgc = AS_GC(wr);
e32ce18
-            assert(wrasgc != next); /* wrasgc is reachable, but
e32ce18
+            PyObject_ASSERT(op, wrasgc != next);
e32ce18
+                                    /* wrasgc is reachable, but
e32ce18
                                        next isn't, so they can't
e32ce18
                                        be the same */
e32ce18
             gc_list_move(wrasgc, &wrcb_to_call);
e32ce18
@@ -701,11 +707,11 @@ handle_weakrefs(PyGC_Head *unreachable,
e32ce18
 
e32ce18
         gc = wrcb_to_call.gc.gc_next;
e32ce18
         op = FROM_GC(gc);
e32ce18
-        assert(IS_REACHABLE(op));
e32ce18
-        assert(PyWeakref_Check(op));
e32ce18
+        PyObject_ASSERT(op, IS_REACHABLE(op));
e32ce18
+        PyObject_ASSERT(op, PyWeakref_Check(op));
e32ce18
         wr = (PyWeakReference *)op;
e32ce18
         callback = wr->wr_callback;
e32ce18
-        assert(callback != NULL);
e32ce18
+        PyObject_ASSERT(op, callback != NULL);
e32ce18
 
e32ce18
         /* copy-paste of weakrefobject.c's handle_callback() */
e32ce18
         temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
e32ce18
@@ -822,12 +828,14 @@ check_garbage(PyGC_Head *collectable)
e32ce18
     for (gc = collectable->gc.gc_next; gc != collectable;
e32ce18
          gc = gc->gc.gc_next) {
e32ce18
         _PyGCHead_SET_REFS(gc, Py_REFCNT(FROM_GC(gc)));
e32ce18
-        assert(_PyGCHead_REFS(gc) != 0);
e32ce18
+        PyObject_ASSERT(FROM_GC(gc),
e32ce18
+                        _PyGCHead_REFS(gc) != 0);
e32ce18
     }
e32ce18
     subtract_refs(collectable);
e32ce18
     for (gc = collectable->gc.gc_next; gc != collectable;
e32ce18
          gc = gc->gc.gc_next) {
e32ce18
-        assert(_PyGCHead_REFS(gc) >= 0);
e32ce18
+        PyObject_ASSERT(FROM_GC(gc),
e32ce18
+                        _PyGCHead_REFS(gc) >= 0);
e32ce18
         if (_PyGCHead_REFS(gc) != 0)
e32ce18
             return -1;
e32ce18
     }
e32ce18
diff -up Lib/test/test_gc.py.old Lib/test/test_gc.py
e32ce18
--- Lib/test/test_gc.py.old	2014-12-10 11:19:33.503982288 +0100
e32ce18
+++ Lib/test/test_gc.py	2014-12-10 11:21:13.220021364 +0100
e32ce18
@@ -49,6 +49,8 @@ class GC_Detector(object):
e32ce18
         # gc collects it.
e32ce18
         self.wr = weakref.ref(C1055820(666), it_happened)
e32ce18
 
e32ce18
+BUILD_WITH_NDEBUG = ('-DNDEBUG' in sysconfig.get_config_vars()['PY_CFLAGS'])
e32ce18
+
e32ce18
 @with_tp_del
e32ce18
 class Uncollectable(object):
e32ce18
     """Create a reference cycle with multiple __del__ methods.
e32ce18
@@ -854,6 +856,8 @@ class GCCallbackTests(unittest.TestCase)
e32ce18
         self.assertEqual(len(gc.garbage), 0)
e32ce18
 
e32ce18
 
e32ce18
+    @unittest.skipIf(BUILD_WITH_NDEBUG,
e32ce18
+                     'built with -NDEBUG')
e32ce18
     def test_refcount_errors(self):
e32ce18
         self.preclean()
e32ce18
         # Verify the "handling" of objects with broken refcounts
e32ce18
diff -up Lib/test/test_gc.py.old Lib/test/test_gc.py
e32ce18
--- Lib/test/test_gc.py.old	2014-12-10 12:50:58.252121318 +0100
e32ce18
+++ Lib/test/test_gc.py	2014-12-10 12:51:08.594266653 +0100
e32ce18
@@ -4,6 +4,7 @@ from test.support import (verbose, refco
e32ce18
 from test.script_helper import assert_python_ok, make_script, temp_dir
e32ce18
 
e32ce18
 import sys
e32ce18
+import sysconfig
e32ce18
 import time
e32ce18
 import gc
e32ce18
 import weakref