@@ -159,11 +159,27 @@ _PyDebug_PrintTotalRefs(void) {
159159
160160#ifdef Py_TRACE_REFS
161161
162- #define REFCHAIN (interp ) &interp->object_state.refchain
162+ #define REFCHAIN (interp ) interp->object_state.refchain
163+
164+ static inline int
165+ has_own_refchain (PyInterpreterState * interp )
166+ {
167+ if (interp -> feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC ) {
168+ return (_Py_IsMainInterpreter (interp )
169+ || _PyInterpreterState_Main () == NULL );
170+ }
171+ return 1 ;
172+ }
163173
164174static inline void
165175init_refchain (PyInterpreterState * interp )
166176{
177+ if (!has_own_refchain (interp )) {
178+ // Legacy subinterpreters share a refchain with the main interpreter.
179+ REFCHAIN (interp ) = REFCHAIN (_PyInterpreterState_Main ());
180+ return ;
181+ }
182+ REFCHAIN (interp ) = & interp -> object_state ._refchain_obj ;
167183 PyObject * refchain = REFCHAIN (interp );
168184 refchain -> _ob_prev = refchain ;
169185 refchain -> _ob_next = refchain ;
@@ -2010,9 +2026,7 @@ void
20102026_PyObject_InitState (PyInterpreterState * interp )
20112027{
20122028#ifdef Py_TRACE_REFS
2013- if (!_Py_IsMainInterpreter (interp )) {
2014- init_refchain (interp );
2015- }
2029+ init_refchain (interp );
20162030#endif
20172031}
20182032
@@ -2218,42 +2232,6 @@ _Py_NewReferenceNoTotal(PyObject *op)
22182232
22192233
22202234#ifdef Py_TRACE_REFS
2221- /* Make sure the ref is associated with the right interpreter.
2222- * This only needs special attention for heap-allocated objects
2223- * that have been immortalized, and only when the object might
2224- * outlive the interpreter where it was created. That means the
2225- * object was necessarily created using a global allocator
2226- * (i.e. from the main interpreter). Thus in that specific case
2227- * we move the object over to the main interpreter's refchain.
2228- *
2229- * This was added for the sake of the immortal interned strings,
2230- * where legacy subinterpreters share the main interpreter's
2231- * interned dict (and allocator), and therefore the strings can
2232- * outlive the subinterpreter.
2233- *
2234- * It may make sense to fold this into _Py_SetImmortalUntracked(),
2235- * but that requires further investigation. In the meantime, it is
2236- * up to the caller to know if this is needed. There should be
2237- * very few cases.
2238- */
2239- void
2240- _Py_NormalizeImmortalReference (PyObject * op )
2241- {
2242- assert (_Py_IsImmortal (op ));
2243- PyInterpreterState * interp = _PyInterpreterState_GET ();
2244- if (!_PyRefchain_IsTraced (interp , op )) {
2245- return ;
2246- }
2247- PyInterpreterState * main_interp = _PyInterpreterState_Main ();
2248- if (interp != main_interp
2249- && interp -> feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC )
2250- {
2251- assert (!_PyRefchain_IsTraced (main_interp , op ));
2252- _PyRefchain_Remove (interp , op );
2253- _PyRefchain_Trace (main_interp , op );
2254- }
2255- }
2256-
22572235void
22582236_Py_ForgetReference (PyObject * op )
22592237{
0 commit comments