@@ -1847,6 +1847,21 @@ PyUnicode_FromString(const char *u)
1847
1847
return PyUnicode_DecodeUTF8Stateful (u , (Py_ssize_t )size , NULL , NULL );
1848
1848
}
1849
1849
1850
+ static PyObject * *
1851
+ resize_array (PyObject * * array , Py_ssize_t * capacity )
1852
+ {
1853
+ Py_ssize_t old_size = * capacity ;
1854
+ Py_ssize_t new_size = Py_MAX (old_size * 2 , 16 );
1855
+ Py_ssize_t item_size = sizeof (array [0 ]);
1856
+ PyObject * * new_array = PyMem_Realloc (array , new_size * item_size );
1857
+ if (new_array == NULL ) {
1858
+ PyErr_NoMemory ();
1859
+ return NULL ;
1860
+ }
1861
+ memset (& new_array [old_size ], 0 , (new_size - old_size ) * item_size );
1862
+ * capacity = new_size ;
1863
+ return new_array ;
1864
+ }
1850
1865
1851
1866
PyObject *
1852
1867
_PyUnicode_FromId (_Py_Identifier * id )
@@ -1909,6 +1924,34 @@ _PyUnicode_FromId(_Py_Identifier *id)
1909
1924
return obj ;
1910
1925
}
1911
1926
1927
+ static int
1928
+ _PyUnicode_Immortalize (PyObject * obj )
1929
+ {
1930
+ assert (!_PyObject_IS_IMMORTAL (obj ));
1931
+
1932
+ struct _Py_immortalized_objects * imm = & _PyRuntime .static_objects .immortal ;
1933
+
1934
+ _PyMutex_lock (& imm -> mutex );
1935
+ Py_ssize_t index = imm -> size ;
1936
+ if (index >= imm -> capacity ) {
1937
+ Py_ssize_t capacity = imm -> capacity ;
1938
+ PyObject * * new_array = resize_array (imm -> array , & capacity );
1939
+ if (new_array == NULL ) {
1940
+ _PyMutex_unlock (& imm -> mutex );
1941
+ return -1 ;
1942
+ }
1943
+
1944
+ imm -> array = new_array ;
1945
+ imm -> capacity = capacity ;
1946
+ }
1947
+
1948
+ _PyObject_SetImmortal (obj );
1949
+ imm -> array [index ] = obj ;
1950
+ imm -> size ++ ;
1951
+ _PyMutex_unlock (& imm -> mutex );
1952
+ return 0 ;
1953
+ }
1954
+
1912
1955
1913
1956
static void
1914
1957
unicode_clear_identifiers (struct _Py_unicode_state * state )
@@ -1924,6 +1967,18 @@ unicode_clear_identifiers(struct _Py_unicode_state *state)
1924
1967
// after Py_Finalize().
1925
1968
}
1926
1969
1970
+ static void
1971
+ unicode_free_immortalized (_PyRuntimeState * runtime )
1972
+ {
1973
+ struct _Py_immortalized_objects * imm = & runtime -> static_objects .immortal ;
1974
+ for (Py_ssize_t i = 0 ; i < imm -> size ; i ++ ) {
1975
+ _PyUnicode_ExactDealloc (imm -> array [i ]);
1976
+ }
1977
+ imm -> size = 0 ;
1978
+ PyMem_Free (imm -> array );
1979
+ imm -> array = NULL ;
1980
+ imm -> capacity = 0 ;
1981
+ }
1927
1982
1928
1983
/* Internal function, doesn't check maximum character */
1929
1984
@@ -14623,11 +14678,18 @@ PyUnicode_InternInPlace(PyObject **p)
14623
14678
return ;
14624
14679
}
14625
14680
14626
- /* The two references in interned dict (key and value) are not counted by
14627
- refcnt. unicode_dealloc() and _PyUnicode_ClearInterned() take care of
14628
- this. */
14629
- Py_SET_REFCNT (s , Py_REFCNT (s ) - 2 );
14630
14681
_PyUnicode_STATE (s ).interned = 1 ;
14682
+
14683
+ if (_Py_ThreadLocal (t ) && _PyUnicode_Immortalize (t ) == 0 ) {
14684
+ /* Nothing to do if we immortalize the string */
14685
+ return ;
14686
+ }
14687
+ else {
14688
+ /* The two references in interned dict (key and value) are not counted by
14689
+ refcnt. unicode_dealloc() and _PyUnicode_ClearInterned() take care of
14690
+ this. */
14691
+ Py_SET_REFCNT (s , Py_REFCNT (s ) - 2 );
14692
+ }
14631
14693
}
14632
14694
14633
14695
// Function kept for the stable ABI.
@@ -15136,6 +15198,10 @@ _PyUnicode_Fini(PyInterpreterState *interp)
15136
15198
interp -> unicode .ucnhash_capi = NULL ;
15137
15199
15138
15200
unicode_clear_identifiers (state );
15201
+
15202
+ if (_Py_IsMainInterpreter (interp )) {
15203
+ unicode_free_immortalized (& _PyRuntime );
15204
+ }
15139
15205
}
15140
15206
15141
15207
/* A _string module, to export formatter_parser and formatter_field_name_split
0 commit comments