gh-125017: Fix crash on premature access to classmethod/staticmethod annotations by JelleZijlstra · Pull Request #125636 · python/cpython
Actually, on a second look, this part is technically unsafe:
else { Py_DECREF(dict); return PyDict_SetItem(dict, name, value); }
If the dictionary's reference count is 1, then this will result in a use-after-free. Though, I guess that's never possible because the dictionary will be stored on the instance as well. In hindsight, there should have either been an assertion (or just not do it in the first place), but oh well.