@@ -114,7 +114,7 @@ NOTE: In the interpreter's initialization phase, some globals are currently
114
114
115
115
static inline char * _PyUnicode_UTF8 (PyObject * op )
116
116
{
117
- return (_PyCompactUnicodeObject_CAST (op )-> utf8 );
117
+ return FT_ATOMIC_LOAD_PTR_ACQUIRE (_PyCompactUnicodeObject_CAST (op )-> utf8 );
118
118
}
119
119
120
120
static inline char * PyUnicode_UTF8 (PyObject * op )
@@ -130,7 +130,7 @@ static inline char* PyUnicode_UTF8(PyObject *op)
130
130
131
131
static inline void PyUnicode_SET_UTF8 (PyObject * op , char * utf8 )
132
132
{
133
- _PyCompactUnicodeObject_CAST (op )-> utf8 = utf8 ;
133
+ FT_ATOMIC_STORE_PTR_RELEASE ( _PyCompactUnicodeObject_CAST (op )-> utf8 , utf8 ) ;
134
134
}
135
135
136
136
static inline Py_ssize_t PyUnicode_UTF8_LENGTH (PyObject * op )
@@ -700,16 +700,17 @@ _PyUnicode_CheckConsistency(PyObject *op, int check_content)
700
700
CHECK (ascii -> state .compact == 0 );
701
701
CHECK (data != NULL );
702
702
if (ascii -> state .ascii ) {
703
- CHECK (compact -> utf8 == data );
703
+ CHECK (_PyUnicode_UTF8 ( op ) == data );
704
704
CHECK (compact -> utf8_length == ascii -> length );
705
705
}
706
706
else {
707
- CHECK (compact -> utf8 != data );
707
+ CHECK (_PyUnicode_UTF8 ( op ) != data );
708
708
}
709
709
}
710
-
711
- if (compact -> utf8 == NULL )
710
+ #ifndef Py_GIL_DISABLED
711
+ if (_PyUnicode_UTF8 ( op ) == NULL )
712
712
CHECK (compact -> utf8_length == 0 );
713
+ #endif
713
714
}
714
715
715
716
/* check that the best kind is used: O(n) operation */
@@ -1156,8 +1157,8 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
1156
1157
1157
1158
if (_PyUnicode_HAS_UTF8_MEMORY (unicode )) {
1158
1159
PyMem_Free (_PyUnicode_UTF8 (unicode ));
1159
- PyUnicode_SET_UTF8 (unicode , NULL );
1160
1160
PyUnicode_SET_UTF8_LENGTH (unicode , 0 );
1161
+ PyUnicode_SET_UTF8 (unicode , NULL );
1161
1162
}
1162
1163
#ifdef Py_TRACE_REFS
1163
1164
_Py_ForgetReference (unicode );
@@ -1210,8 +1211,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
1210
1211
if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY (unicode ))
1211
1212
{
1212
1213
PyMem_Free (_PyUnicode_UTF8 (unicode ));
1213
- PyUnicode_SET_UTF8 (unicode , NULL );
1214
1214
PyUnicode_SET_UTF8_LENGTH (unicode , 0 );
1215
+ PyUnicode_SET_UTF8 (unicode , NULL );
1215
1216
}
1216
1217
1217
1218
data = (PyObject * )PyObject_Realloc (data , new_size );
@@ -1221,8 +1222,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
1221
1222
}
1222
1223
_PyUnicode_DATA_ANY (unicode ) = data ;
1223
1224
if (share_utf8 ) {
1224
- PyUnicode_SET_UTF8 (unicode , data );
1225
1225
PyUnicode_SET_UTF8_LENGTH (unicode , length );
1226
+ PyUnicode_SET_UTF8 (unicode , data );
1226
1227
}
1227
1228
_PyUnicode_LENGTH (unicode ) = length ;
1228
1229
PyUnicode_WRITE (PyUnicode_KIND (unicode ), data , length , 0 );
@@ -4216,6 +4217,21 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
4216
4217
4217
4218
static int unicode_fill_utf8 (PyObject * unicode );
4218
4219
4220
+
4221
+ static int
4222
+ unicode_ensure_utf8 (PyObject * unicode )
4223
+ {
4224
+ int err = 0 ;
4225
+ if (PyUnicode_UTF8 (unicode ) == NULL ) {
4226
+ Py_BEGIN_CRITICAL_SECTION (unicode );
4227
+ if (PyUnicode_UTF8 (unicode ) == NULL ) {
4228
+ err = unicode_fill_utf8 (unicode );
4229
+ }
4230
+ Py_END_CRITICAL_SECTION ();
4231
+ }
4232
+ return err ;
4233
+ }
4234
+
4219
4235
const char *
4220
4236
PyUnicode_AsUTF8AndSize (PyObject * unicode , Py_ssize_t * psize )
4221
4237
{
@@ -4227,13 +4243,11 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
4227
4243
return NULL ;
4228
4244
}
4229
4245
4230
- if (PyUnicode_UTF8 (unicode ) == NULL ) {
4231
- if (unicode_fill_utf8 (unicode ) == -1 ) {
4232
- if (psize ) {
4233
- * psize = -1 ;
4234
- }
4235
- return NULL ;
4246
+ if (unicode_ensure_utf8 (unicode ) == -1 ) {
4247
+ if (psize ) {
4248
+ * psize = -1 ;
4236
4249
}
4250
+ return NULL ;
4237
4251
}
4238
4252
4239
4253
if (psize ) {
@@ -5854,6 +5868,7 @@ unicode_encode_utf8(PyObject *unicode, _Py_error_handler error_handler,
5854
5868
static int
5855
5869
unicode_fill_utf8 (PyObject * unicode )
5856
5870
{
5871
+ _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (unicode );
5857
5872
/* the string cannot be ASCII, or PyUnicode_UTF8() would be set */
5858
5873
assert (!PyUnicode_IS_ASCII (unicode ));
5859
5874
@@ -5895,10 +5910,10 @@ unicode_fill_utf8(PyObject *unicode)
5895
5910
PyErr_NoMemory ();
5896
5911
return -1 ;
5897
5912
}
5898
- PyUnicode_SET_UTF8 (unicode , cache );
5899
- PyUnicode_SET_UTF8_LENGTH (unicode , len );
5900
5913
memcpy (cache , start , len );
5901
5914
cache [len ] = '\0' ;
5915
+ PyUnicode_SET_UTF8_LENGTH (unicode , len );
5916
+ PyUnicode_SET_UTF8 (unicode , cache );
5902
5917
_PyBytesWriter_Dealloc (& writer );
5903
5918
return 0 ;
5904
5919
}
0 commit comments