@@ -64,6 +64,7 @@ PyAPI_FUNC(PyObject *) _Py_stackref_close(_PyStackRef ref, const char *filename,
6464PyAPI_FUNC (_PyStackRef ) _Py_stackref_create (PyObject * obj , uint16_t flags , const char * filename , int linenumber );
6565PyAPI_FUNC (void ) _Py_stackref_record_borrow (_PyStackRef ref , const char * filename , int linenumber );
6666PyAPI_FUNC (void ) _Py_stackref_set_borrowed_from (_PyStackRef ref , _PyStackRef borrowed_from , const char * filename , int linenumber );
67+ PyAPI_FUNC (void ) _Py_stackref_copy_borrowed_from (_PyStackRef ref , _PyStackRef ref_orig , const char * filename , int linenumber );
6768extern void _Py_stackref_associate (PyInterpreterState * interp , PyObject * obj , _PyStackRef ref );
6869
6970static const _PyStackRef PyStackRef_NULL = { .index = 0 };
@@ -249,7 +250,11 @@ _PyStackRef_DUP(_PyStackRef ref, const char *filename, int linenumber)
249250 } else {
250251 flags = Py_TAG_REFCNT ;
251252 }
252- return _Py_stackref_create (obj , flags , filename , linenumber );
253+ _PyStackRef new_ref = _Py_stackref_create (obj , flags , filename , linenumber );
254+ if (flags == Py_TAG_REFCNT && !_Py_IsImmortal (obj )) {
255+ _Py_stackref_copy_borrowed_from (new_ref , ref , filename , linenumber );
256+ }
257+ return new_ref ;
253258}
254259#define PyStackRef_DUP (REF ) _PyStackRef_DUP(REF, __FILE__, __LINE__)
255260
0 commit comments