Skip to content

Commit 1f2ce89

Browse files
Introduce _Py_stackref_get_borrowed_from
1 parent 83aef9f commit 1f2ce89

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

Include/internal/pycore_stackref.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ PyAPI_FUNC(PyObject *) _Py_stackref_get_object(_PyStackRef ref);
6363
PyAPI_FUNC(PyObject *) _Py_stackref_close(_PyStackRef ref, const char *filename, int linenumber);
6464
PyAPI_FUNC(_PyStackRef) _Py_stackref_create(PyObject *obj, uint16_t flags, const char *filename, int linenumber);
6565
PyAPI_FUNC(void) _Py_stackref_record_borrow(_PyStackRef ref, const char *filename, int linenumber);
66+
PyAPI_FUNC(void) _Py_stackref_get_borrowed_from(_PyStackRef ref, _PyStackRef *p_borrowed_from, const char *filename, int linenumber);
6667
PyAPI_FUNC(void) _Py_stackref_set_borrowed_from(_PyStackRef ref, _PyStackRef borrowed_from, const char *filename, int linenumber);
6768
PyAPI_FUNC(void) _Py_stackref_copy_borrowed_from(_PyStackRef ref, _PyStackRef ref_orig, const char *filename, int linenumber);
6869
extern void _Py_stackref_associate(PyInterpreterState *interp, PyObject *obj, _PyStackRef ref);
@@ -252,7 +253,9 @@ _PyStackRef_DUP(_PyStackRef ref, const char *filename, int linenumber)
252253
}
253254
_PyStackRef new_ref = _Py_stackref_create(obj, flags, filename, linenumber);
254255
if (flags == Py_TAG_REFCNT && !_Py_IsImmortal(obj)) {
255-
_Py_stackref_copy_borrowed_from(new_ref, ref, filename, linenumber);
256+
_PyStackRef borrowed_from;
257+
_Py_stackref_get_borrowed_from(ref, &borrowed_from, filename, linenumber);
258+
_Py_stackref_set_borrowed_from(new_ref, borrowed_from, filename, linenumber);
256259
}
257260
return new_ref;
258261
}

Python/stackrefs.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
184202
void
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-
213230
void
214231
_Py_stackref_associate(PyInterpreterState *interp, PyObject *obj, _PyStackRef ref)
215232
{

0 commit comments

Comments
 (0)