Skip to content

Commit 0a82e96

Browse files
Improvements
1 parent 871c013 commit 0a82e96

File tree

2 files changed

+15
-49
lines changed

2 files changed

+15
-49
lines changed

Lib/test/test_stackrefs.py

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,50 @@
66
except ImportError:
77
_testinternalcapi = None
88

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-
309
@cpython_only
31-
class TestStackRef(unittest.TestCase):
10+
class TestDefinition(unittest.TestCase):
3211

33-
def test_equivalent(self):
12+
def test_equivalence(self):
3413
def run_with_refcount_check(self, func, obj):
3514
refcount = sys.getrefcount(obj)
3615
res = func(obj)
3716
self.assertEqual(sys.getrefcount(obj), refcount)
3817
return res
3918

40-
def stackref_from_object_borrow(obj):
41-
return _testinternalcapi.stackref_from_object_borrow(obj)
42-
43-
funcs = [
19+
funcs_with_incref = [
4420
_testinternalcapi.stackref_from_object_new,
45-
_testinternalcapi.stackref_from_object_new2,
4621
_testinternalcapi.stackref_from_object_steal_with_incref,
4722
_testinternalcapi.stackref_make_heap_safe,
4823
_testinternalcapi.stackref_make_heap_safe_with_borrow,
4924
_testinternalcapi.stackref_strong_reference,
5025
]
5126

52-
funcs2 = [
27+
funcs_with_borrow = [
5328
_testinternalcapi.stackref_from_object_borrow,
5429
_testinternalcapi.stackref_dup_borrowed_with_close,
5530
]
5631

57-
for obj in (None, True, False, 42, '1'):
32+
immortal_objs = (None, True, False, 42, '1')
33+
34+
for obj in immortal_objs:
5835
results = set()
59-
for func in funcs + funcs2:
36+
for func in funcs_with_incref + funcs_with_borrow:
6037
res = run_with_refcount_check(self, func, obj)
61-
#print(func.__name__, obj, res)
6238
results.add(res)
6339
self.assertEqual(len(results), 1)
6440

65-
for obj in (5000, range(10)):
41+
mortal_objs = (5000, 3+2j, range(10))
42+
43+
for obj in mortal_objs:
6644
results = set()
67-
for func in funcs:
45+
for func in funcs_with_incref:
6846
res = run_with_refcount_check(self, func, obj)
69-
#print(func.__name__, obj, res)
7047
results.add(res)
7148
self.assertEqual(len(results), 1)
7249

7350
results = set()
74-
for func in funcs2:
51+
for func in funcs_with_borrow:
7552
res = run_with_refcount_check(self, func, obj)
76-
#print(func.__name__, obj, res)
7753
results.add(res)
7854
self.assertEqual(len(results), 1)
7955

Modules/_testinternalcapi.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,23 +2439,14 @@ stackref_from_object_new(PyObject *self, PyObject *op)
24392439
return obj;
24402440
}
24412441

2442-
static PyObject *
2443-
stackref_from_object_new2(PyObject *self, PyObject *op)
2444-
{
2445-
_PyStackRef ref = PyStackRef_FromPyObjectNew(op);
2446-
PyObject *obj = stackref_to_tuple(ref, op);
2447-
PyObject *op2 = PyStackRef_AsPyObjectSteal(ref);
2448-
Py_DECREF(op2);
2449-
return obj;
2450-
}
2451-
24522442
static PyObject *
24532443
stackref_from_object_steal_with_incref(PyObject *self, PyObject *op)
24542444
{
24552445
Py_INCREF(op);
24562446
_PyStackRef ref = PyStackRef_FromPyObjectSteal(op);
24572447
PyObject *obj = stackref_to_tuple(ref, op);
2458-
PyStackRef_CLOSE(ref);
2448+
PyObject *op2 = PyStackRef_AsPyObjectSteal(ref);
2449+
Py_DECREF(op2);
24592450
return obj;
24602451
}
24612452

@@ -2622,7 +2613,6 @@ static PyMethodDef module_functions[] = {
26222613
{"simple_pending_call", simple_pending_call, METH_O},
26232614
{"set_vectorcall_nop", set_vectorcall_nop, METH_O},
26242615
{"stackref_from_object_new", stackref_from_object_new, METH_O},
2625-
{"stackref_from_object_new2", stackref_from_object_new2, METH_O},
26262616
{"stackref_from_object_steal_with_incref", stackref_from_object_steal_with_incref, METH_O},
26272617
{"stackref_make_heap_safe", stackref_make_heap_safe, METH_O},
26282618
{"stackref_make_heap_safe_with_borrow", stackref_make_heap_safe_with_borrow, METH_O},

0 commit comments

Comments
 (0)