bpo-27945: Fixed various segfaults with dict. by serhiy-storchaka · Pull Request #1657 · python/cpython (original) (raw)

But on other hand, Py_DECREF() can trigger the garbage collecting. The garbage collector can see the tuple with references to freed objects and expose it to user code via gc.get_objects() or gc.get_referrers(). Doing some operations with this tuple (repr() or hash()) can cause a crash. We already encountered with similar issues.

So yes, there are reasons for writing the code in that way.