Skip to content

[3.9] bpo-47182: Fix crash by named unicode characters after interpreter reinitialization (GH-32212) #32217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when using a named unicode character like ``"\N{digit nine}"``
after the main interpreter has been initialized a second time.
133 changes: 133 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -16316,7 +16316,140 @@ _PyUnicode_Fini(PyThreadState *tstate)
unicode_clear_static_strings();
}

<<<<<<< HEAD
_PyUnicode_FiniEncodings(&tstate->interp->unicode.fs_codec);
||||||| parent of 44e915028d7 (bpo-47182: Fix crash by named unicode characters after interpreter reinitialization (GH-32212))
_PyStaticType_Dealloc(&EncodingMapType);
_PyStaticType_Dealloc(&PyFieldNameIter_Type);
_PyStaticType_Dealloc(&PyFormatterIter_Type);
}


static void unicode_static_dealloc(PyObject *op)
{
PyASCIIObject *ascii = _PyASCIIObject_CAST(op);

assert(ascii->state.compact);

if (ascii->state.ascii) {
if (ascii->wstr) {
PyObject_Free(ascii->wstr);
ascii->wstr = NULL;
}
}
else {
PyCompactUnicodeObject* compact = (PyCompactUnicodeObject*)op;
void* data = (void*)(compact + 1);
if (ascii->wstr && ascii->wstr != data) {
PyObject_Free(ascii->wstr);
ascii->wstr = NULL;
compact->wstr_length = 0;
}
if (compact->utf8) {
PyObject_Free(compact->utf8);
compact->utf8 = NULL;
compact->utf8_length = 0;
}
}
}


void
_PyUnicode_Fini(PyInterpreterState *interp)
{
struct _Py_unicode_state *state = &interp->unicode;

if (_Py_IsMainInterpreter(interp)) {
// _PyUnicode_ClearInterned() must be called before _PyUnicode_Fini()
assert(interned == NULL);
}

_PyUnicode_FiniEncodings(&state->fs_codec);

unicode_clear_identifiers(state);

// Clear the single character singletons
for (int i = 0; i < 128; i++) {
unicode_static_dealloc((PyObject*)&_Py_SINGLETON(strings).ascii[i]);
}
for (int i = 0; i < 128; i++) {
unicode_static_dealloc((PyObject*)&_Py_SINGLETON(strings).latin1[i]);
}
}


void
_PyStaticUnicode_Dealloc(PyObject *op)
{
unicode_static_dealloc(op);
=======
_PyStaticType_Dealloc(&EncodingMapType);
_PyStaticType_Dealloc(&PyFieldNameIter_Type);
_PyStaticType_Dealloc(&PyFormatterIter_Type);
}


static void unicode_static_dealloc(PyObject *op)
{
PyASCIIObject *ascii = _PyASCIIObject_CAST(op);

assert(ascii->state.compact);

if (ascii->state.ascii) {
if (ascii->wstr) {
PyObject_Free(ascii->wstr);
ascii->wstr = NULL;
}
}
else {
PyCompactUnicodeObject* compact = (PyCompactUnicodeObject*)op;
void* data = (void*)(compact + 1);
if (ascii->wstr && ascii->wstr != data) {
PyObject_Free(ascii->wstr);
ascii->wstr = NULL;
compact->wstr_length = 0;
}
if (compact->utf8) {
PyObject_Free(compact->utf8);
compact->utf8 = NULL;
compact->utf8_length = 0;
}
}
}


void
_PyUnicode_Fini(PyInterpreterState *interp)
{
struct _Py_unicode_state *state = &interp->unicode;

if (_Py_IsMainInterpreter(interp)) {
// _PyUnicode_ClearInterned() must be called before _PyUnicode_Fini()
assert(interned == NULL);
// bpo-47182: force a unicodedata CAPI capsule re-import on
// subsequent initialization of main interpreter.
ucnhash_capi = NULL;
}

_PyUnicode_FiniEncodings(&state->fs_codec);

unicode_clear_identifiers(state);

// Clear the single character singletons
for (int i = 0; i < 128; i++) {
unicode_static_dealloc((PyObject*)&_Py_SINGLETON(strings).ascii[i]);
}
for (int i = 0; i < 128; i++) {
unicode_static_dealloc((PyObject*)&_Py_SINGLETON(strings).latin1[i]);
}
}


void
_PyStaticUnicode_Dealloc(PyObject *op)
{
unicode_static_dealloc(op);
>>>>>>> 44e915028d7 (bpo-47182: Fix crash by named unicode characters after interpreter reinitialization (GH-32212))
}


Expand Down