@@ -5869,6 +5869,23 @@ PyUnicode_AsUTF16String(PyObject *unicode)
5869
5869
return _PyUnicode_EncodeUTF16 (unicode , NULL , 0 );
5870
5870
}
5871
5871
5872
+ _PyUnicode_Name_CAPI *
5873
+ _PyUnicode_GetNameCAPI (void )
5874
+ {
5875
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
5876
+ _PyUnicode_Name_CAPI * ucnhash_capi ;
5877
+
5878
+ ucnhash_capi = _Py_atomic_load_ptr (& interp -> unicode .ucnhash_capi );
5879
+ if (ucnhash_capi == NULL ) {
5880
+ ucnhash_capi = (_PyUnicode_Name_CAPI * )PyCapsule_Import (
5881
+ PyUnicodeData_CAPSULE_NAME , 1 );
5882
+
5883
+ // It's fine if we overwite the value here. It's always the same value.
5884
+ _Py_atomic_store_ptr (& interp -> unicode .ucnhash_capi , ucnhash_capi );
5885
+ }
5886
+ return ucnhash_capi ;
5887
+ }
5888
+
5872
5889
/* --- Unicode Escape Codec ----------------------------------------------- */
5873
5890
5874
5891
PyObject *
@@ -5884,7 +5901,6 @@ _PyUnicode_DecodeUnicodeEscapeInternal(const char *s,
5884
5901
PyObject * errorHandler = NULL ;
5885
5902
PyObject * exc = NULL ;
5886
5903
_PyUnicode_Name_CAPI * ucnhash_capi ;
5887
- PyInterpreterState * interp = _PyInterpreterState_GET ();
5888
5904
5889
5905
// so we can remember if we've seen an invalid escape char or not
5890
5906
* first_invalid_escape = NULL ;
@@ -6032,19 +6048,13 @@ _PyUnicode_DecodeUnicodeEscapeInternal(const char *s,
6032
6048
6033
6049
/* \N{name} */
6034
6050
case 'N' :
6035
- ucnhash_capi = interp -> unicode . ucnhash_capi ;
6051
+ ucnhash_capi = _PyUnicode_GetNameCAPI () ;
6036
6052
if (ucnhash_capi == NULL ) {
6037
- /* load the unicode data module */
6038
- ucnhash_capi = (_PyUnicode_Name_CAPI * )PyCapsule_Import (
6039
- PyUnicodeData_CAPSULE_NAME , 1 );
6040
- if (ucnhash_capi == NULL ) {
6041
- PyErr_SetString (
6053
+ PyErr_SetString (
6042
6054
PyExc_UnicodeError ,
6043
6055
"\\N escapes not supported (can't load unicodedata module)"
6044
- );
6045
- goto onError ;
6046
- }
6047
- interp -> unicode .ucnhash_capi = ucnhash_capi ;
6056
+ );
6057
+ goto onError ;
6048
6058
}
6049
6059
6050
6060
message = "malformed \\N character escape" ;
0 commit comments