@@ -111,9 +111,8 @@ _Py_stackref_close(_PyStackRef ref, const char *filename, int linenumber)
111111 _Py_FatalErrorFormat (__func__ ,
112112 "Invalid borrowed StackRef with ID %" PRIu64 " at %s:%d\n" ,
113113 borrowed_from .index , filename , linenumber );
114- } else {
115- entry_borrowed -> borrows -- ;
116114 }
115+ entry_borrowed -> borrows -- ;
117116 }
118117 if (entry -> borrows > 0 ) {
119118 _Py_FatalErrorFormat (__func__ ,
@@ -181,9 +180,32 @@ _Py_stackref_record_borrow(_PyStackRef ref, const char *filename, int linenumber
181180 entry -> linenumber_borrow = linenumber ;
182181}
183182
183+ void
184+ _Py_stackref_get_borrowed_from (_PyStackRef ref , _PyStackRef * p_borrowed_from , const char * filename , int linenumber )
185+ {
186+ assert (!PyStackRef_IsError (ref ));
187+ PyInterpreterState * interp = PyInterpreterState_Get ();
188+
189+ TableEntry * entry = _Py_hashtable_get (interp -> open_stackrefs_table , (void * )ref .index );
190+ if (entry == NULL ) {
191+ _Py_FatalErrorFormat (__func__ ,
192+ "Invalid StackRef with ID %" PRIu64 " at %s:%d\n" ,
193+ ref .index , filename , linenumber );
194+ }
195+
196+ if (p_borrowed_from != NULL ) {
197+ * p_borrowed_from = entry -> borrowed_from ;
198+ }
199+ }
200+
201+ // This function should be used no more than once per ref.
184202void
185203_Py_stackref_set_borrowed_from (_PyStackRef ref , _PyStackRef borrowed_from , const char * filename , int linenumber )
186204{
205+ if (PyStackRef_IsNull (borrowed_from )) {
206+ return ;
207+ }
208+
187209 assert (!PyStackRef_IsError (ref ));
188210 PyInterpreterState * interp = PyInterpreterState_Get ();
189211
@@ -205,11 +227,6 @@ _Py_stackref_set_borrowed_from(_PyStackRef ref, _PyStackRef borrowed_from, const
205227 entry_borrowed -> borrows ++ ;
206228}
207229
208- void
209- _Py_stackref_copy_borrowed_from (_PyStackRef ref , _PyStackRef ref_orig , const char * filename , int linenumber )
210- {
211- }
212-
213230void
214231_Py_stackref_associate (PyInterpreterState * interp , PyObject * obj , _PyStackRef ref )
215232{
0 commit comments