File tree Expand file tree Collapse file tree 2 files changed +11
-3
lines changed Expand file tree Collapse file tree 2 files changed +11
-3
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,10 @@ typedef struct _Py_Identifier {
39
39
// Index in PyInterpreterState.unicode.ids.array. It is process-wide
40
40
// unique and must be initialized to -1.
41
41
Py_ssize_t index ;
42
+ // Hidden PyMutex struct for non free-threaded build.
43
+ struct {
44
+ uint8_t v ;
45
+ } mutex ;
42
46
} _Py_Identifier ;
43
47
44
48
#ifndef Py_BUILD_CORE
Original file line number Diff line number Diff line change @@ -1897,6 +1897,7 @@ PyUnicode_FromString(const char *u)
1897
1897
PyObject *
1898
1898
_PyUnicode_FromId (_Py_Identifier * id )
1899
1899
{
1900
+ PyMutex_Lock ((PyMutex * )& id -> mutex );
1900
1901
PyInterpreterState * interp = _PyInterpreterState_GET ();
1901
1902
struct _Py_unicode_ids * ids = & interp -> unicode .ids ;
1902
1903
@@ -1923,14 +1924,14 @@ _PyUnicode_FromId(_Py_Identifier *id)
1923
1924
obj = ids -> array [index ];
1924
1925
if (obj ) {
1925
1926
// Return a borrowed reference
1926
- return obj ;
1927
+ goto end ;
1927
1928
}
1928
1929
}
1929
1930
1930
1931
obj = PyUnicode_DecodeUTF8Stateful (id -> string , strlen (id -> string ),
1931
1932
NULL , NULL );
1932
1933
if (!obj ) {
1933
- return NULL ;
1934
+ goto end ;
1934
1935
}
1935
1936
PyUnicode_InternInPlace (& obj );
1936
1937
@@ -1941,7 +1942,8 @@ _PyUnicode_FromId(_Py_Identifier *id)
1941
1942
PyObject * * new_array = PyMem_Realloc (ids -> array , new_size * item_size );
1942
1943
if (new_array == NULL ) {
1943
1944
PyErr_NoMemory ();
1944
- return NULL ;
1945
+ obj = NULL ;
1946
+ goto end ;
1945
1947
}
1946
1948
memset (& new_array [ids -> size ], 0 , (new_size - ids -> size ) * item_size );
1947
1949
ids -> array = new_array ;
@@ -1951,6 +1953,8 @@ _PyUnicode_FromId(_Py_Identifier *id)
1951
1953
// The array stores a strong reference
1952
1954
ids -> array [index ] = obj ;
1953
1955
1956
+ end :
1957
+ PyMutex_Unlock ((PyMutex * )& id -> mutex );
1954
1958
// Return a borrowed reference
1955
1959
return obj ;
1956
1960
}
You can’t perform that action at this time.
0 commit comments