-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Expand file tree
/
Copy pathtest_stackrefs.py
More file actions
59 lines (47 loc) · 1.8 KB
/
test_stackrefs.py
File metadata and controls
59 lines (47 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import unittest
import sys
from test.support import cpython_only
try:
import _testinternalcapi
except ImportError:
_testinternalcapi = None
@cpython_only
class TestDefinition(unittest.TestCase):
def test_equivalence(self):
def run_with_refcount_check(self, func, obj):
refcount = sys.getrefcount(obj)
res = func(obj)
self.assertEqual(sys.getrefcount(obj), refcount)
return res
funcs_with_incref = [
_testinternalcapi.stackref_from_object_new,
_testinternalcapi.stackref_from_object_steal_with_incref,
_testinternalcapi.stackref_make_heap_safe,
_testinternalcapi.stackref_make_heap_safe_with_borrow,
_testinternalcapi.stackref_strong_reference,
]
funcs_with_borrow = [
_testinternalcapi.stackref_from_object_borrow,
_testinternalcapi.stackref_dup_borrowed_with_close,
]
immortal_objs = (None, True, False, 42, '1')
for obj in immortal_objs:
results = set()
for func in funcs_with_incref + funcs_with_borrow:
res = run_with_refcount_check(self, func, obj)
results.add(res)
self.assertEqual(len(results), 1)
mortal_objs = (5000, 3+2j, range(10), object())
for obj in mortal_objs:
results = set()
for func in funcs_with_incref:
res = run_with_refcount_check(self, func, obj)
results.add(res)
self.assertEqual(len(results), 1)
results = set()
for func in funcs_with_borrow:
res = run_with_refcount_check(self, func, obj)
results.add(res)
self.assertEqual(len(results), 1)
if __name__ == "__main__":
unittest.main()