|
6 | 6 | except ImportError: |
7 | 7 | _testinternalcapi = None |
8 | 8 |
|
9 | | - |
10 | | -class StackRef: |
11 | | - def __init__(self, obj, flags): |
12 | | - self.obj = obj |
13 | | - self.flags = flags |
14 | | - self.refcount = sys.getrefcount(obj) |
15 | | - |
16 | | - def __str__(self): |
17 | | - return f"StackRef(obj={self.obj}, flags={self.flags}, refcount={self.refcount})" |
18 | | - |
19 | | - def __eq__(self, other): |
20 | | - return ( |
21 | | - self.obj is other.obj |
22 | | - and self.flags == other.flags |
23 | | - and self.refcount == other.refcount |
24 | | - ) |
25 | | - |
26 | | - def __hash__(self): |
27 | | - return tuple.__hash__((self.obj, self.flags, self.refcount)) |
28 | | - |
29 | | - |
30 | 9 | @cpython_only |
31 | | -class TestStackRef(unittest.TestCase): |
| 10 | +class TestDefinition(unittest.TestCase): |
32 | 11 |
|
33 | | - def test_equivalent(self): |
| 12 | + def test_equivalence(self): |
34 | 13 | def run_with_refcount_check(self, func, obj): |
35 | 14 | refcount = sys.getrefcount(obj) |
36 | 15 | res = func(obj) |
37 | 16 | self.assertEqual(sys.getrefcount(obj), refcount) |
38 | 17 | return res |
39 | 18 |
|
40 | | - def stackref_from_object_borrow(obj): |
41 | | - return _testinternalcapi.stackref_from_object_borrow(obj) |
42 | | - |
43 | | - funcs = [ |
| 19 | + funcs_with_incref = [ |
44 | 20 | _testinternalcapi.stackref_from_object_new, |
45 | | - _testinternalcapi.stackref_from_object_new2, |
46 | 21 | _testinternalcapi.stackref_from_object_steal_with_incref, |
47 | 22 | _testinternalcapi.stackref_make_heap_safe, |
48 | 23 | _testinternalcapi.stackref_make_heap_safe_with_borrow, |
49 | 24 | _testinternalcapi.stackref_strong_reference, |
50 | 25 | ] |
51 | 26 |
|
52 | | - funcs2 = [ |
| 27 | + funcs_with_borrow = [ |
53 | 28 | _testinternalcapi.stackref_from_object_borrow, |
54 | 29 | _testinternalcapi.stackref_dup_borrowed_with_close, |
55 | 30 | ] |
56 | 31 |
|
57 | | - for obj in (None, True, False, 42, '1'): |
| 32 | + immortal_objs = (None, True, False, 42, '1') |
| 33 | + |
| 34 | + for obj in immortal_objs: |
58 | 35 | results = set() |
59 | | - for func in funcs + funcs2: |
| 36 | + for func in funcs_with_incref + funcs_with_borrow: |
60 | 37 | res = run_with_refcount_check(self, func, obj) |
61 | | - #print(func.__name__, obj, res) |
62 | 38 | results.add(res) |
63 | 39 | self.assertEqual(len(results), 1) |
64 | 40 |
|
65 | | - for obj in (5000, range(10)): |
| 41 | + mortal_objs = (5000, 3+2j, range(10)) |
| 42 | + |
| 43 | + for obj in mortal_objs: |
66 | 44 | results = set() |
67 | | - for func in funcs: |
| 45 | + for func in funcs_with_incref: |
68 | 46 | res = run_with_refcount_check(self, func, obj) |
69 | | - #print(func.__name__, obj, res) |
70 | 47 | results.add(res) |
71 | 48 | self.assertEqual(len(results), 1) |
72 | 49 |
|
73 | 50 | results = set() |
74 | | - for func in funcs2: |
| 51 | + for func in funcs_with_borrow: |
75 | 52 | res = run_with_refcount_check(self, func, obj) |
76 | | - #print(func.__name__, obj, res) |
77 | 53 | results.add(res) |
78 | 54 | self.assertEqual(len(results), 1) |
79 | 55 |
|
|
0 commit comments