@@ -309,6 +309,7 @@ type_cache_clear(struct type_cache *cache, PyObject *value)
309
309
void
310
310
_PyType_InitCache (PyInterpreterState * interp )
311
311
{
312
+ int is_main = _Py_IsMainInterpreter (interp );
312
313
struct type_cache * cache = & interp -> types .type_cache ;
313
314
for (Py_ssize_t i = 0 ; i < (1 << MCACHE_SIZE_EXP ); i ++ ) {
314
315
struct type_cache_entry * entry = & cache -> hashtable [i ];
@@ -317,11 +318,27 @@ _PyType_InitCache(PyInterpreterState *interp)
317
318
entry -> version = 0 ;
318
319
// Set to None so _PyType_Lookup() can use Py_SETREF(),
319
320
// rather than using slower Py_XSETREF().
320
- entry -> name = Py_NewRef (Py_None );
321
+ entry -> name = Py_None ;
322
+ // Currently, this happens before the GIL has been created,
323
+ // which is a problem without "immortality" (PEP 683).
324
+ // Until we have immortal objects, we must avoid an INCREF
325
+ // in the main interpreter here. Instead, we fix the refcount
326
+ // in the main interpreer as soon as the GIL has been created.
327
+ // (See pycore_create_interpreter() in pylifecycle.c.)
328
+ if (!is_main ) {
329
+ Py_INCREF (Py_None );
330
+ }
321
331
entry -> value = NULL ;
322
332
}
323
333
}
324
334
335
+ // This is the temporary fix used by pycore_create_interpreter().
336
+ void
337
+ _PyType_FixCacheRefcounts (void )
338
+ {
339
+ _Py_RefcntAdd (Py_None , (1 << MCACHE_SIZE_EXP ));
340
+ }
341
+
325
342
326
343
static unsigned int
327
344
_PyType_ClearCache (PyInterpreterState * interp )
0 commit comments