gh-140406: Fix memory leak upon __hash__ returning a non-integer (GH-140411)

This commit is contained in:
Peter Bierma 2025-10-21 08:10:01 -04:00 committed by GitHub
parent fe4b60208e
commit 71db05a12d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 0 deletions

View file

@ -1184,6 +1184,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')