@@ -2500,30 +2500,42 @@ test_threadstate_set_stack_protection(PyObject *self, PyObject *Py_UNUSED(args))
25002500static PyObject *
25012501stackref_from_object_new (PyObject * self , PyObject * op )
25022502{
2503+ // Make a new strong reference and give ownership of it to ref.
25032504 _PyStackRef ref = PyStackRef_FromPyObjectNew (op );
25042505 PyObject * res = _PyStackRef_AsTuple (ref , op );
2506+ // Remove a strong reference by closing ref.
25052507 PyStackRef_CLOSE (ref );
25062508 return res ;
25072509}
25082510
25092511static PyObject *
25102512stackref_from_object_steal_with_incref (PyObject * self , PyObject * op )
25112513{
2512- // The next two lines are equivalent to PyStackRef_FromPyObjectNew.
2514+ // Combination of Py_INCREF and PyStackRef_FromPyObjectSteal
2515+ // is equivalent to PyStackRef_FromPyObjectNew.
2516+
25132517 Py_INCREF (op );
2518+ // Transfer ownership of a strong reference from op to ref.
25142519 _PyStackRef ref = PyStackRef_FromPyObjectSteal (op );
2520+
25152521 PyObject * res = _PyStackRef_AsTuple (ref , op );
2516- // The next two lines are equivalent to PyStackRef_CLOSE.
2522+
2523+ // Combination of PyStackRef_AsPyObjectSteal and Py_DECREF
2524+ // is equivalent to PyStackRef_CLOSE.
2525+
2526+ // Transfer ownership of a strong reference from ref to op2.
25172527 PyObject * op2 = PyStackRef_AsPyObjectSteal (ref );
25182528 Py_DECREF (op2 );
2529+
25192530 return res ;
25202531}
25212532
25222533static PyObject *
25232534stackref_make_heap_safe (PyObject * self , PyObject * op )
25242535{
25252536 _PyStackRef ref = PyStackRef_FromPyObjectNew (op );
2526- // For no-GIL release build ref2 is equal to ref.
2537+ // Transfer ownership of a strong reference to op from ref to ref2,
2538+ // ref shouldn't be used anymore.
25272539 _PyStackRef ref2 = PyStackRef_MakeHeapSafe (ref );
25282540 PyObject * res = _PyStackRef_AsTuple (ref2 , op );
25292541 PyStackRef_CLOSE (ref2 );
@@ -2534,7 +2546,10 @@ static PyObject *
25342546stackref_make_heap_safe_with_borrow (PyObject * self , PyObject * op )
25352547{
25362548 _PyStackRef ref = PyStackRef_FromPyObjectNew (op );
2549+ // Create a borrowed reference to op, ref keeps a strong reference.
25372550 _PyStackRef ref2 = PyStackRef_Borrow (ref );
2551+ // Make a new strong reference to op from ref2,
2552+ // ref2 shouldn't be used anymore.
25382553 _PyStackRef ref3 = PyStackRef_MakeHeapSafe (ref2 );
25392554 // We can close ref, since ref3 is heap safe.
25402555 PyStackRef_CLOSE (ref );
@@ -2546,7 +2561,8 @@ stackref_make_heap_safe_with_borrow(PyObject *self, PyObject *op)
25462561static PyObject *
25472562stackref_strong_reference (PyObject * self , PyObject * op )
25482563{
2549- // The next two lines are equivalent to op2 = Py_NewRef(op).
2564+ // Combination of PyStackRef_FromPyObjectBorrow and
2565+ // PyStackRef_AsPyObjectSteal is equivalent to Py_NewRef.
25502566 _PyStackRef ref = PyStackRef_FromPyObjectBorrow (op );
25512567 PyObject * op2 = PyStackRef_AsPyObjectSteal (ref );
25522568 _PyStackRef ref2 = PyStackRef_FromPyObjectSteal (op2 );
@@ -2560,6 +2576,7 @@ stackref_from_object_borrow(PyObject *self, PyObject *op)
25602576{
25612577 _PyStackRef ref = PyStackRef_FromPyObjectBorrow (op );
25622578 PyObject * res = _PyStackRef_AsTuple (ref , op );
2579+ // Closing borrowed ref is no-op.
25632580 PyStackRef_CLOSE (ref );
25642581 return res ;
25652582}
@@ -2568,9 +2585,9 @@ static PyObject *
25682585stackref_dup_borrowed_with_close (PyObject * self , PyObject * op )
25692586{
25702587 _PyStackRef ref = PyStackRef_FromPyObjectBorrow (op );
2571- // For no-GIL release build ref2 is equal to ref.
2588+ // Duplicate reference, ref and ref2 both will be
2589+ // correct borrowed references from op.
25722590 _PyStackRef ref2 = PyStackRef_DUP (ref );
2573- // Closing borrowed ref is no-op.
25742591 PyStackRef_XCLOSE (ref );
25752592 PyObject * res = _PyStackRef_AsTuple (ref2 , op );
25762593 PyStackRef_XCLOSE (ref2 );
0 commit comments