Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix memory leak upon __hash__ returning a non-integer.
  • Loading branch information
ZeroIntensity committed Oct 21, 2025
commit b1013fe5eeff45dde61f8024d98ef5f2f6bfc37a
1 change: 1 addition & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10569,6 +10569,7 @@ slot_tp_hash(PyObject *self)
return PyObject_HashNotImplemented(self);
}
if (!PyLong_Check(res)) {
Py_DECREF(res);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to move this line below PyErr_SetString(). Later, it can be used to format a better error message.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do that in a different PR, where we change the error message. I generally like to call Py_DECREF before PyErr* functions to protect against badly crafted destructors that break the exception state. But, if you feel strongly about this, I'm also fine with moving it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not feel strongly about this. It would just make the future diff a little bit smaller, and git blame would show your commit instead of the following commit.
In general, I prefer to call Py_DECREF before PyErr* functions too.

PyErr_SetString(PyExc_TypeError,
"__hash__ method should return an integer");
return -1;
Expand Down