@@ -114,7 +114,7 @@ NOTE: In the interpreter's initialization phase, some globals are currently
114114
115115static inline char * _PyUnicode_UTF8 (PyObject * op )
116116{
117- return (_PyCompactUnicodeObject_CAST (op )-> utf8 );
117+ return FT_ATOMIC_LOAD_PTR_ACQUIRE (_PyCompactUnicodeObject_CAST (op )-> utf8 );
118118}
119119
120120static inline char * PyUnicode_UTF8 (PyObject * op )
@@ -130,7 +130,7 @@ static inline char* PyUnicode_UTF8(PyObject *op)
130130
131131static inline void PyUnicode_SET_UTF8 (PyObject * op , char * utf8 )
132132{
133- _PyCompactUnicodeObject_CAST (op )-> utf8 = utf8 ;
133+ FT_ATOMIC_STORE_PTR_RELEASE ( _PyCompactUnicodeObject_CAST (op )-> utf8 , utf8 ) ;
134134}
135135
136136static inline Py_ssize_t PyUnicode_UTF8_LENGTH (PyObject * op )
@@ -700,15 +700,15 @@ _PyUnicode_CheckConsistency(PyObject *op, int check_content)
700700 CHECK (ascii -> state .compact == 0 );
701701 CHECK (data != NULL );
702702 if (ascii -> state .ascii ) {
703- CHECK (compact -> utf8 == data );
703+ CHECK (_PyUnicode_UTF8 ( op ) == data );
704704 CHECK (compact -> utf8_length == ascii -> length );
705705 }
706706 else {
707- CHECK (compact -> utf8 != data );
707+ CHECK (_PyUnicode_UTF8 ( op ) != data );
708708 }
709709 }
710710
711- if (compact -> utf8 == NULL )
711+ if (_PyUnicode_UTF8 ( op ) == NULL )
712712 CHECK (compact -> utf8_length == 0 );
713713 }
714714
@@ -1156,8 +1156,8 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
11561156
11571157 if (_PyUnicode_HAS_UTF8_MEMORY (unicode )) {
11581158 PyMem_Free (_PyUnicode_UTF8 (unicode ));
1159- PyUnicode_SET_UTF8 (unicode , NULL );
11601159 PyUnicode_SET_UTF8_LENGTH (unicode , 0 );
1160+ PyUnicode_SET_UTF8 (unicode , NULL );
11611161 }
11621162#ifdef Py_TRACE_REFS
11631163 _Py_ForgetReference (unicode );
@@ -1210,8 +1210,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
12101210 if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY (unicode ))
12111211 {
12121212 PyMem_Free (_PyUnicode_UTF8 (unicode ));
1213- PyUnicode_SET_UTF8 (unicode , NULL );
12141213 PyUnicode_SET_UTF8_LENGTH (unicode , 0 );
1214+ PyUnicode_SET_UTF8 (unicode , NULL );
12151215 }
12161216
12171217 data = (PyObject * )PyObject_Realloc (data , new_size );
@@ -1221,8 +1221,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
12211221 }
12221222 _PyUnicode_DATA_ANY (unicode ) = data ;
12231223 if (share_utf8 ) {
1224- PyUnicode_SET_UTF8 (unicode , data );
12251224 PyUnicode_SET_UTF8_LENGTH (unicode , length );
1225+ PyUnicode_SET_UTF8 (unicode , data );
12261226 }
12271227 _PyUnicode_LENGTH (unicode ) = length ;
12281228 PyUnicode_WRITE (PyUnicode_KIND (unicode ), data , length , 0 );
@@ -4216,6 +4216,21 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
42164216
42174217static int unicode_fill_utf8 (PyObject * unicode );
42184218
4219+
4220+ static int
4221+ unicode_ensure_utf8 (PyObject * unicode )
4222+ {
4223+ int err = 0 ;
4224+ if (PyUnicode_UTF8 (unicode ) == NULL ) {
4225+ Py_BEGIN_CRITICAL_SECTION (unicode );
4226+ if (PyUnicode_UTF8 (unicode ) == NULL ) {
4227+ err = unicode_fill_utf8 (unicode );
4228+ }
4229+ Py_END_CRITICAL_SECTION ();
4230+ }
4231+ return err ;
4232+ }
4233+
42194234const char *
42204235PyUnicode_AsUTF8AndSize (PyObject * unicode , Py_ssize_t * psize )
42214236{
@@ -4227,13 +4242,11 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
42274242 return NULL ;
42284243 }
42294244
4230- if (PyUnicode_UTF8 (unicode ) == NULL ) {
4231- if (unicode_fill_utf8 (unicode ) == -1 ) {
4232- if (psize ) {
4233- * psize = -1 ;
4234- }
4235- return NULL ;
4245+ if (unicode_ensure_utf8 (unicode ) == -1 ) {
4246+ if (psize ) {
4247+ * psize = -1 ;
42364248 }
4249+ return NULL ;
42374250 }
42384251
42394252 if (psize ) {
@@ -5854,6 +5867,7 @@ unicode_encode_utf8(PyObject *unicode, _Py_error_handler error_handler,
58545867static int
58555868unicode_fill_utf8 (PyObject * unicode )
58565869{
5870+ _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (unicode );
58575871 /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */
58585872 assert (!PyUnicode_IS_ASCII (unicode ));
58595873
@@ -5895,10 +5909,10 @@ unicode_fill_utf8(PyObject *unicode)
58955909 PyErr_NoMemory ();
58965910 return -1 ;
58975911 }
5898- PyUnicode_SET_UTF8 (unicode , cache );
5899- PyUnicode_SET_UTF8_LENGTH (unicode , len );
59005912 memcpy (cache , start , len );
59015913 cache [len ] = '\0' ;
5914+ PyUnicode_SET_UTF8_LENGTH (unicode , len );
5915+ PyUnicode_SET_UTF8 (unicode , cache );
59025916 _PyBytesWriter_Dealloc (& writer );
59035917 return 0 ;
59045918}
0 commit comments