gh-111972: unicode: Make `ucnhash_capi`capsule initialization thread-safe by Eclips4 · Pull Request #112249 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/internal/pycore_ucnhash.h
32 changes: 21 additions & 11 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5869,6 +5869,23 @@ PyUnicode_AsUTF16String(PyObject *unicode)
return _PyUnicode_EncodeUTF16(unicode, NULL, 0);
}

_PyUnicode_Name_CAPI *
Comment thread
erlend-aasland marked this conversation as resolved.
_PyUnicode_GetNameCAPI(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyUnicode_Name_CAPI *ucnhash_capi;

ucnhash_capi = _Py_atomic_load_ptr(&interp->unicode.ucnhash_capi);
if (ucnhash_capi == NULL) {
ucnhash_capi = (_PyUnicode_Name_CAPI *)PyCapsule_Import(
PyUnicodeData_CAPSULE_NAME, 1);

// It's fine if we overwite the value here. It's always the same value.
_Py_atomic_store_ptr(&interp->unicode.ucnhash_capi, ucnhash_capi);
}
return ucnhash_capi;
}

/* --- Unicode Escape Codec ----------------------------------------------- */

PyObject *
Expand All @@ -5884,7 +5901,6 @@ _PyUnicode_DecodeUnicodeEscapeInternal(const char *s,
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
_PyUnicode_Name_CAPI *ucnhash_capi;
PyInterpreterState *interp = _PyInterpreterState_GET();

// so we can remember if we've seen an invalid escape char or not
*first_invalid_escape = NULL;
Expand Down Expand Up @@ -6032,19 +6048,13 @@ _PyUnicode_DecodeUnicodeEscapeInternal(const char *s,

/* \N{name} */
case 'N':
ucnhash_capi = interp->unicode.ucnhash_capi;
ucnhash_capi = _PyUnicode_GetNameCAPI();
if (ucnhash_capi == NULL) {
/* load the unicode data module */
ucnhash_capi = (_PyUnicode_Name_CAPI *)PyCapsule_Import(
PyUnicodeData_CAPSULE_NAME, 1);
if (ucnhash_capi == NULL) {
PyErr_SetString(
PyErr_SetString(
PyExc_UnicodeError,
"\\N escapes not supported (can't load unicodedata module)"
);
goto onError;
}
interp->unicode.ucnhash_capi = ucnhash_capi;
);
goto onError;
}

message = "malformed \\N character escape";
Expand Down
12 changes: 3 additions & 9 deletions Python/codecs.c