Skip to content

Commit ef3f8c9

Browse files
committed
add singledispatchmethod test for dangling references
1 parent 6c6e8b8 commit ef3f8c9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Lib/test/test_functools.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import collections
44
import collections.abc
55
import copy
6+
import gc
67
from itertools import permutations
78
import pickle
89
from random import choice
@@ -3246,6 +3247,33 @@ def dispatch(self, x):
32463247
assert id(t1) == t1.dispatch(2)
32473248
assert id(t2) == t2.dispatch(2) # gh-127750
32483249

3250+
def test_singledispatchmethod_object_references(self):
3251+
class ReferenceTest:
3252+
instance_counter = 0
3253+
3254+
def __init__(self):
3255+
ReferenceTest.instance_counter = ReferenceTest.instance_counter + 1
3256+
3257+
def __del__(self):
3258+
ReferenceTest.instance_counter = ReferenceTest.instance_counter - 1
3259+
3260+
@functools.singledispatchmethod
3261+
def go(self, item):
3262+
pass
3263+
3264+
assert ReferenceTest.instance_counter == 0
3265+
t=ReferenceTest()
3266+
assert ReferenceTest.instance_counter == 1
3267+
x = []
3268+
for ii in range(1000):
3269+
t = ReferenceTest()
3270+
t.go(ii)
3271+
x.append(t)
3272+
del t
3273+
del x
3274+
gc.collect()
3275+
assert ReferenceTest.instance_counter == 0
3276+
32493277

32503278
class CachedCostItem:
32513279
_cost = 1

0 commit comments

Comments
 (0)