mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-140406: Fix memory leak upon __hash__ returning a non-integer (GH-140411) (GH-140417)
gh-140406: Fix memory leak upon `__hash__` returning a non-integer (GH-140411)
(cherry picked from commit 71db05a12d)
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
This commit is contained in:
parent
98d4c21c09
commit
c0f0eca4da
3 changed files with 13 additions and 0 deletions
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Fix memory leak when an object's :meth:`~object.__hash__` method returns an
|
||||
object that isn't an :class:`int`.
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue