[3.8] bpo-36389: Backport debug enhancements from master by vstinner · Pull Request #16796 · python/cpython (original) (raw)
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 6876257)
Fix _PyBytesWriter API when Python is built in release mode with assertions.
(cherry picked from commit 60ec6ef)
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 "" with no other detail.
(cherry picked from commit 4d5f94b)
Add a newline between the verbose object dump and the Py_FatalError() logs for readability.
(cherry picked from commit 7775349)