Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Feb 11, 2025
1 parent 7c5bb0e commit 93d0977
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3244,6 +3244,64 @@ def f(arg):
def _(arg: undefined):
return "forward reference"

def test_method_equal_instances(self):
# gh-127750: Reference to self was cached
class A:
def __eq__(self, other):
return True
def __hash__(self):
return 1
@functools.singledispatchmethod
def t(self, arg):
return self

a = A()
b = A()
self.assertIs(a.t(1), a)
self.assertIs(b.t(2), b)

def test_method_bad_hash(self):
class A:
def __eq__(self, other):
raise AssertionError
def __hash__(self):
raise AssertionError
@functools.singledispatchmethod
def t(self, arg):
pass

# Should not raise
A().t(1)
hash(A().t)
A().t == A().t

def test_method_no_reference_loops(self):
# gh-127750: Created a strong reference to self
class A:
@functools.singledispatchmethod
def t(self, arg):
return weakref.ref(self)

a = A()
r = a.t(1)
self.assertIsNotNone(r())
del a # delete a after a.t
if not support.check_impl_detail(cpython=True):
support.gc_collect()
self.assertIsNone(r())

a = A()
t = a.t
del a # delete a before a.t
support.gc_collect()
r = t(1)
self.assertIsNotNone(r())
del t
if not support.check_impl_detail(cpython=True):
support.gc_collect()
self.assertIsNone(r())


class CachedCostItem:
_cost = 1

Expand Down

0 comments on commit 93d0977

Please sign in to comment.