137137
138138NON_GENERATED_IMMORTAL_OBJECTS = [
139139 # The generated ones come from generate_runtime_init().
140- '(PyObject *)&_Py_SINGLETON(bytes_empty)' ,
141- '(PyObject *)&_Py_SINGLETON(tuple_empty)' ,
142- '(PyObject *)&_Py_SINGLETON(hamt_bitmap_node_empty)' ,
143- '(PyObject *)&_Py_INTERP_SINGLETON(interp, hamt_empty)' ,
144- '(PyObject *)&_Py_SINGLETON(context_token_missing)' ,
140+
141+ # b'' (empty bytes string)
142+ ('_PyStaticObject_CheckBytesSingleton({}, 0, 0);' ,
143+ '(PyObject *)&_Py_SINGLETON(bytes_empty)' ),
144+ # () (empty tuple)
145+ ('_PyStaticObject_CheckSingleton({}, &PyTuple_Type);' ,
146+ '(PyObject *)&_Py_SINGLETON(tuple_empty)' ),
147+ ('_PyStaticObject_CheckSingleton({}, &_PyHamt_BitmapNode_Type);' ,
148+ '(PyObject *)&_Py_SINGLETON(hamt_bitmap_node_empty)' ),
149+ ('_PyStaticObject_CheckSingleton({}, &_PyHamt_Type);' ,
150+ '(PyObject *)&_Py_INTERP_SINGLETON(interp, hamt_empty)' ),
151+ ('_PyStaticObject_CheckSingleton({}, &_PyContextTokenMissing_Type);' ,
152+ '(PyObject *)&_Py_SINGLETON(context_token_missing)' ),
153+ # False
154+ ('_PyStaticObject_CheckLongSingleton({}, 0, 1);' ,
155+ '(PyObject *)&_Py_FalseStruct ' ),
156+ # True
157+ ('_PyStaticObject_CheckLongSingleton({}, 1, 1);' ,
158+ '(PyObject *)&_Py_TrueStruct' ),
159+ # None
160+ ('_PyStaticObject_CheckSingleton({}, &_PyNone_Type);' ,
161+ '(PyObject *)&_Py_NoneStruct' ),
162+ # Ellipsis (...)
163+ ('_PyStaticObject_CheckSingleton({}, &PyEllipsis_Type);' ,
164+ '(PyObject *)&_Py_EllipsisObject' ),
165+ # Py_NotImplemented
166+ ('_PyStaticObject_CheckSingleton({}, &_PyNotImplemented_Type);' ,
167+ '(PyObject *)&_Py_NotImplementedStruct' ),
145168]
146169
147170
@@ -299,28 +322,45 @@ def generate_runtime_init(identifiers, strings):
299322 with printer .block ('#define _Py_small_ints_INIT' , continuation = True ):
300323 for i in range (- nsmallnegints , nsmallposints ):
301324 printer .write (f'_PyLong_DIGIT_INIT({ i } ),' )
302- immortal_objects .append (f'(PyObject *)&_Py_SINGLETON(small_ints)[_PY_NSMALLNEGINTS + { i } ]' )
325+ immortal_objects .append ((
326+ f'_PyStaticObject_CheckLongSingleton({{}}, { i } , 0);' ,
327+ f'(PyObject *)&_Py_SINGLETON(small_ints)[_PY_NSMALLNEGINTS + { i } ]' ,
328+ ))
303329 printer .write ('' )
304330 with printer .block ('#define _Py_bytes_characters_INIT' , continuation = True ):
305331 for i in range (256 ):
306332 printer .write (f'_PyBytes_CHAR_INIT({ i } ),' )
307- immortal_objects .append (f'(PyObject *)&_Py_SINGLETON(bytes_characters)[{ i } ]' )
333+ immortal_objects .append ((
334+ f'_PyStaticObject_CheckBytesSingleton({{}}, 1, { i } );' ,
335+ f'(PyObject *)&_Py_SINGLETON(bytes_characters)[{ i } ]' ,
336+ ))
308337 printer .write ('' )
309338 with printer .block ('#define _Py_str_literals_INIT' , continuation = True ):
310339 for literal , name in sorted (strings .items (), key = lambda x : x [1 ]):
311340 printer .write (f'INIT_STR({ name } , "{ literal } "),' )
312- immortal_objects .append (f'(PyObject *)&_Py_STR({ name } )' )
341+ escaped = literal .replace ("{" , "{{" ).replace ("}" , "}}" )
342+ immortal_objects .append ((
343+ f'_PyStaticObject_CheckUnicodeSingleton({{}}, "{ escaped } ", { len (literal )} );' ,
344+ f'(PyObject *)&_Py_STR({ name } )' ,
345+ ))
313346 printer .write ('' )
314347 with printer .block ('#define _Py_str_identifiers_INIT' , continuation = True ):
315348 for name in sorted (identifiers ):
316349 assert name .isidentifier (), name
317350 printer .write (f'INIT_ID({ name } ),' )
318- immortal_objects .append (f'(PyObject *)&_Py_ID({ name } )' )
351+ assert all (ch not in "{}" for ch in name )
352+ immortal_objects .append ((
353+ f'_PyStaticObject_CheckUnicodeSingleton({{}}, "{ name } ", { len (name )} );' ,
354+ f'(PyObject *)&_Py_ID({ name } )' ,
355+ ))
319356 printer .write ('' )
320357 with printer .block ('#define _Py_str_ascii_INIT' , continuation = True ):
321358 for i in range (128 ):
322359 printer .write (f'_PyASCIIObject_INIT("\\ x{ i :02x} "),' )
323- immortal_objects .append (f'(PyObject *)&_Py_SINGLETON(strings).ascii[{ i } ]' )
360+ immortal_objects .append ((
361+ f'_PyStaticObject_CheckUnicodeCharSingleton({{}}, 1, { i } );' ,
362+ f'(PyObject *)&_Py_SINGLETON(strings).ascii[{ i } ]' ,
363+ ))
324364 printer .write ('' )
325365 with printer .block ('#define _Py_str_latin1_INIT' , continuation = True ):
326366 for i in range (128 , 256 ):
@@ -329,7 +369,10 @@ def generate_runtime_init(identifiers, strings):
329369 utf8 .append (f"\\ x{ c :02x} " )
330370 utf8 .append ('"' )
331371 printer .write (f'_PyUnicode_LATIN1_INIT("\\ x{ i :02x} ", { "" .join (utf8 )} ),' )
332- immortal_objects .append (f'(PyObject *)&_Py_SINGLETON(strings).latin1[{ i } - 128]' )
372+ immortal_objects .append ((
373+ f'_PyStaticObject_CheckUnicodeCharSingleton({{}}, 1, { i } );' ,
374+ f'(PyObject *)&_Py_SINGLETON(strings).latin1[{ i } - 128]' ,
375+ ))
333376 printer .write (END )
334377 printer .write (after )
335378 return immortal_objects
@@ -391,16 +434,18 @@ def generate_global_object_finalizers(generated_immortal_objects):
391434 printer .write (before )
392435 printer .write (START )
393436 printer .write ('#ifdef Py_DEBUG' )
437+ printer .write ('// Check static objects consistency: detect if a C extension modified' )
438+ printer .write ('// an immutable singleton by mistake.' )
394439 printer .write ("static inline void" )
395440 with printer .block (
396- "_PyStaticObjects_CheckRefcnt (PyInterpreterState *interp)" ):
441+ "_PyStaticObjects_CheckAll (PyInterpreterState *interp)" ):
397442 printer .write ('/* generated runtime-global */' )
398443 printer .write ('// (see pycore_runtime_init_generated.h)' )
399- for ref in generated_immortal_objects :
400- printer .write (f'_PyStaticObject_CheckRefcnt( { ref } );' )
444+ for fmt , ref in generated_immortal_objects :
445+ printer .write (fmt . format ( ref ) )
401446 printer .write ('/* non-generated */' )
402- for ref in NON_GENERATED_IMMORTAL_OBJECTS :
403- printer .write (f'_PyStaticObject_CheckRefcnt( { ref } );' )
447+ for fmt , ref in NON_GENERATED_IMMORTAL_OBJECTS :
448+ printer .write (fmt . format ( ref ) )
404449 printer .write ('#endif // Py_DEBUG' )
405450 printer .write (END )
406451 printer .write (after )
0 commit comments