Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,16 @@ def __hash__(self):
return self
self.assertEqual(hash(Z(42)), hash(42))

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

with self.assertRaises(TypeError):
hash(A())

def test_hex(self):
self.assertEqual(hex(16), '0x10')
self.assertEqual(hex(-16), '-0x10')
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 @@ -10195,6 +10195,7 @@ slot_tp_hash(PyObject *self)
return PyObject_HashNotImplemented(self);
}
if (!PyLong_Check(res)) {
Py_DECREF(res);
PyErr_SetString(PyExc_TypeError,
"__hash__ method should return an integer");
return -1;
Expand Down
Loading