@@ -1924,7 +1924,7 @@ _PyUnicode_FromId(_Py_Identifier *id)
1924
1924
return obj ;
1925
1925
}
1926
1926
1927
- static int
1927
+ static void
1928
1928
_PyUnicode_Immortalize (PyObject * obj )
1929
1929
{
1930
1930
assert (!_PyObject_IS_IMMORTAL (obj ));
@@ -1937,8 +1937,11 @@ _PyUnicode_Immortalize(PyObject *obj)
1937
1937
Py_ssize_t capacity = imm -> capacity ;
1938
1938
PyObject * * new_array = resize_array (imm -> array , & capacity );
1939
1939
if (new_array == NULL ) {
1940
+ PyErr_WriteUnraisable (NULL );
1940
1941
_PyMutex_unlock (& imm -> mutex );
1941
- return -1 ;
1942
+ // mark the object as immortal anyways
1943
+ _PyObject_SetImmortal (obj );
1944
+ return ;
1942
1945
}
1943
1946
1944
1947
imm -> array = new_array ;
@@ -1949,7 +1952,6 @@ _PyUnicode_Immortalize(PyObject *obj)
1949
1952
imm -> array [index ] = obj ;
1950
1953
imm -> size ++ ;
1951
1954
_PyMutex_unlock (& imm -> mutex );
1952
- return 0 ;
1953
1955
}
1954
1956
1955
1957
@@ -14667,6 +14669,12 @@ PyUnicode_InternInPlace(PyObject **p)
14667
14669
set_interned_dict (interned );
14668
14670
}
14669
14671
14672
+ if (!_Py_ThreadLocal (s ) && !_PyObject_IS_IMMORTAL (s )) {
14673
+ /* Make a copy so that we can safely immortalize the string. */
14674
+ s = _PyUnicode_Copy (s );
14675
+ Py_SETREF (* p , s );
14676
+ }
14677
+
14670
14678
PyObject * t = PyDict_SetDefault (interned , s , s );
14671
14679
if (t == NULL ) {
14672
14680
PyErr_Clear ();
@@ -14680,15 +14688,11 @@ PyUnicode_InternInPlace(PyObject **p)
14680
14688
14681
14689
_PyUnicode_STATE (s ).interned = 1 ;
14682
14690
14683
- if (_Py_ThreadLocal (t ) && _PyUnicode_Immortalize (t ) == 0 ) {
14684
- /* Nothing to do if we immortalize the string */
14685
- return ;
14691
+ if (_Py_ThreadLocal (t )) {
14692
+ _PyUnicode_Immortalize (t );
14686
14693
}
14687
14694
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 );
14695
+ assert (_PyObject_IS_IMMORTAL (t ));
14692
14696
}
14693
14697
}
14694
14698
0 commit comments