Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
11 changes: 11 additions & 0 deletions Lib/test/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ def test_hashes(self):
for obj in self.hashes_to_check:
self.assertEqual(hash(obj), _default_hash(obj))

def test_invalid_hash_typeerror(self):
# GH-140406: The returned object from __hash__() would leak if it
# wasn't an integer.
class A:
def __hash__(self):
return 1.0

for _ in range(100):
with self.assertRaises(TypeError):
Copy link
Contributor

Choose a reason for hiding this comment

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

The ref leak checker should catch it even in one iteration, is 100 needed here?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a very tiny leak, so I wasn't sure if it would be caught. I don't mind removing it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

test_hash in test_builtin is also a good place. It already contains similar tests (TypeErrors and classes with custom __hash__).

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, I moved it to test_builtin.

hash(A())

class HashRandomizationTests:

# Each subclass should define a field "repr_", containing the repr() of
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix memory leak when an object's :meth:`~object.__hash__` method returns an
object that isn't an :class:`int`.
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
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
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
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
Loading