diff options
author | Victor Stinner <vstinner@python.org> | 2019-10-07 16:42:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-07 16:42:01 (GMT) |
commit | 6876257eaabdb30f27ebcbd7d2557278ce2e5705 (patch) | |
tree | 05597a0310d1e330c0c156c97f0fbc8a6386675e /Objects/dictobject.c | |
parent | 321def805abc5b7c92c7e90ca90cb2434fdab855 (diff) | |
download | cpython-6876257eaabdb30f27ebcbd7d2557278ce2e5705.zip cpython-6876257eaabdb30f27ebcbd7d2557278ce2e5705.tar.gz cpython-6876257eaabdb30f27ebcbd7d2557278ce2e5705.tar.bz2 |
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.
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r-- | Objects/dictobject.c | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 164fe2a..99908a8 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -459,23 +459,26 @@ static PyObject *empty_values[1] = { NULL }; 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 @@ _PyDict_CheckConsistency(PyObject *op, int check_content) 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 @@ _PyDict_CheckConsistency(PyObject *op, int check_content) 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 } |