[3.8] bpo-36389: Backport debug enhancements from master (GH-16796)
* bpo-36389: _PyObject_CheckConsistency() available in release mode (GH-16612)
bpo-36389, bpo-38376: The _PyObject_CheckConsistency() function is
now also available in release mode. For example, it can be used to
debug a crash in the visit_decref() function of the GC.
Modify the following functions to also work in release mode:
* _PyDict_CheckConsistency()
* _PyObject_CheckConsistency()
* _PyType_CheckConsistency()
* _PyUnicode_CheckConsistency()
Other changes:
* _PyMem_IsPtrFreed(ptr) now also returns 1 if ptr is NULL
(equals to 0).
* _PyBytesWriter_CheckConsistency() now returns 1 and is only used
with assert().
* Reorder _PyObject_Dump() to write safe fields first, and only
attempt to render repr() at the end.
(cherry picked from commit 6876257eaabdb30f27ebcbd7d2557278ce2e5705)
* bpo-36389: Fix _PyBytesWriter in release mode (GH-16624)
Fix _PyBytesWriter API when Python is built in release mode with
assertions.
(cherry picked from commit 60ec6efd96d95476fe5e38c491491add04f026e5)
* bpo-38070: Enhance visit_decref() debug trace (GH-16631)
subtract_refs() now pass the parent object to visit_decref() which
pass it to _PyObject_ASSERT(). So if the "is freed" assertion fails,
the parent is used in debug trace, rather than the freed object. The
parent object is more likely to contain useful information. Freed
objects cannot be inspected are are displayed as "<object at xxx is
freed>" with no other detail.
(cherry picked from commit 4d5f94b8cd20f804c7868c5395a15aa6032f874c)
* Fix also a typo in PYMEM_DEADBYTE macro comment
* bpo-36389: Add newline to _PyObject_AssertFailed() (GH-16629)
Add a newline between the verbose object dump and the Py_FatalError()
logs for readability.
(cherry picked from commit 7775349895088a7ae68cecf0c74cf817f15e2c74)
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index a6ddf70..5dad1b6 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -459,23 +459,26 @@
int
_PyDict_CheckConsistency(PyObject *op, int check_content)
{
-#ifndef NDEBUG
- _PyObject_ASSERT(op, PyDict_Check(op));
+#define CHECK(expr) \
+ do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
+
+ assert(op != NULL);
+ CHECK(PyDict_Check(op));
PyDictObject *mp = (PyDictObject *)op;
PyDictKeysObject *keys = mp->ma_keys;
int splitted = _PyDict_HasSplitTable(mp);
Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
- _PyObject_ASSERT(op, 0 <= mp->ma_used && mp->ma_used <= usable);
- _PyObject_ASSERT(op, IS_POWER_OF_2(keys->dk_size));
- _PyObject_ASSERT(op, 0 <= keys->dk_usable && keys->dk_usable <= usable);
- _PyObject_ASSERT(op, 0 <= keys->dk_nentries && keys->dk_nentries <= usable);
- _PyObject_ASSERT(op, keys->dk_usable + keys->dk_nentries <= usable);
+ CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
+ CHECK(IS_POWER_OF_2(keys->dk_size));
+ CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
+ CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
+ CHECK(keys->dk_usable + keys->dk_nentries <= usable);
if (!splitted) {
/* combined table */
- _PyObject_ASSERT(op, keys->dk_refcnt == 1);
+ CHECK(keys->dk_refcnt == 1);
}
if (check_content) {
@@ -484,7 +487,7 @@
for (i=0; i < keys->dk_size; i++) {
Py_ssize_t ix = dictkeys_get_index(keys, i);
- _PyObject_ASSERT(op, DKIX_DUMMY <= ix && ix <= usable);
+ CHECK(DKIX_DUMMY <= ix && ix <= usable);
}
for (i=0; i < usable; i++) {
@@ -494,32 +497,33 @@
if (key != NULL) {
if (PyUnicode_CheckExact(key)) {
Py_hash_t hash = ((PyASCIIObject *)key)->hash;
- _PyObject_ASSERT(op, hash != -1);
- _PyObject_ASSERT(op, entry->me_hash == hash);
+ CHECK(hash != -1);
+ CHECK(entry->me_hash == hash);
}
else {
/* test_dict fails if PyObject_Hash() is called again */
- _PyObject_ASSERT(op, entry->me_hash != -1);
+ CHECK(entry->me_hash != -1);
}
if (!splitted) {
- _PyObject_ASSERT(op, entry->me_value != NULL);
+ CHECK(entry->me_value != NULL);
}
}
if (splitted) {
- _PyObject_ASSERT(op, entry->me_value == NULL);
+ CHECK(entry->me_value == NULL);
}
}
if (splitted) {
/* splitted table */
for (i=0; i < mp->ma_used; i++) {
- _PyObject_ASSERT(op, mp->ma_values[i] != NULL);
+ CHECK(mp->ma_values[i] != NULL);
}
}
}
-#endif
return 1;
+
+#undef CHECK
}