Skip to content

Commit 7ef6a0b

Browse files
committed
Don't hardcode initial refcount in refcount tests
The initial value will differ depending on whether a owned or borrowed reference is loaded onto the operand stack.
1 parent 66f5351 commit 7ef6a0b

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

Lib/test/test_ctypes/test_refcounts.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,64 +24,63 @@ def test_1(self):
2424
def callback(value):
2525
return value
2626

27-
self.assertEqual(sys.getrefcount(callback), 2)
27+
orig_refcount = sys.getrefcount(callback)
2828
cb = MyCallback(callback)
2929

30-
self.assertGreater(sys.getrefcount(callback), 2)
30+
self.assertGreater(sys.getrefcount(callback), orig_refcount)
3131
result = f(-10, cb)
3232
self.assertEqual(result, -18)
3333
cb = None
3434

3535
gc.collect()
3636

37-
self.assertEqual(sys.getrefcount(callback), 2)
37+
self.assertEqual(sys.getrefcount(callback), orig_refcount)
3838

3939
@support.refcount_test
4040
def test_refcount(self):
4141
def func(*args):
4242
pass
43-
# this is the standard refcount for func
44-
self.assertEqual(sys.getrefcount(func), 2)
43+
orig_refcount = sys.getrefcount(func)
4544

4645
# the CFuncPtr instance holds at least one refcount on func:
4746
f = OtherCallback(func)
48-
self.assertGreater(sys.getrefcount(func), 2)
47+
self.assertGreater(sys.getrefcount(func), orig_refcount)
4948

5049
# and may release it again
5150
del f
52-
self.assertGreaterEqual(sys.getrefcount(func), 2)
51+
self.assertGreaterEqual(sys.getrefcount(func), orig_refcount)
5352

5453
# but now it must be gone
5554
gc.collect()
56-
self.assertEqual(sys.getrefcount(func), 2)
55+
self.assertEqual(sys.getrefcount(func), orig_refcount)
5756

5857
class X(ctypes.Structure):
5958
_fields_ = [("a", OtherCallback)]
6059
x = X()
6160
x.a = OtherCallback(func)
6261

6362
# the CFuncPtr instance holds at least one refcount on func:
64-
self.assertGreater(sys.getrefcount(func), 2)
63+
self.assertGreater(sys.getrefcount(func), orig_refcount)
6564

6665
# and may release it again
6766
del x
68-
self.assertGreaterEqual(sys.getrefcount(func), 2)
67+
self.assertGreaterEqual(sys.getrefcount(func), orig_refcount)
6968

7069
# and now it must be gone again
7170
gc.collect()
72-
self.assertEqual(sys.getrefcount(func), 2)
71+
self.assertEqual(sys.getrefcount(func), orig_refcount)
7372

7473
f = OtherCallback(func)
7574

7675
# the CFuncPtr instance holds at least one refcount on func:
77-
self.assertGreater(sys.getrefcount(func), 2)
76+
self.assertGreater(sys.getrefcount(func), orig_refcount)
7877

7978
# create a cycle
8079
f.cycle = f
8180

8281
del f
8382
gc.collect()
84-
self.assertEqual(sys.getrefcount(func), 2)
83+
self.assertEqual(sys.getrefcount(func), orig_refcount)
8584

8685

8786
class AnotherLeak(unittest.TestCase):

Lib/test/test_ctypes/test_stringptr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class X(Structure):
2020
# NULL pointer access
2121
self.assertRaises(ValueError, getattr, x.str, "contents")
2222
b = create_string_buffer(b"Hello, World")
23-
self.assertEqual(sys.getrefcount(b), 2)
23+
orig_refcount = sys.getrefcount(b)
2424
x.str = b
25-
self.assertEqual(sys.getrefcount(b), 3)
25+
self.assertEqual(sys.getrefcount(b), orig_refcount + 1)
2626

2727
# POINTER(c_char) and Python string is NOT compatible
2828
# POINTER(c_char) and create_string_buffer() is compatible

0 commit comments

Comments
 (0)