8000 gh-100227: Make the Global Interned Dict Safe for Isolated Interpreters by ericsnowcurrently · Pull Request #102858 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-100227: Make the Global Interned Dict Safe for Isolated Interpreters #102858

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

Prev Previous commit
Next Next commit
Store a thread state to use just for interned strings.
  • Loading branch information
ericsnowcurrently committed Mar 20, 2023
commit eadd48a92801dc401ddfaa64ec828d2c2607e6ff
5 changes: 5 additions & 0 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ struct _Py_unicode_runtime_ids {
};

struct _Py_unicode_runtime_state {
struct {
PyThreadState *tstate;
/* The actual interned dict is at
_PyRuntime.cached_objects.interned_strings. */
} interned;
struct _Py_unicode_runtime_ids ids;
};

Expand Down
29 changes: 29 additions & 0 deletions Objects/unicodeobject.c
6C1B
Original file line number Diff line number Diff line change
Expand Up @@ -14584,6 +14584,33 @@ _PyUnicode_InitTypes(PyInterpreterState *interp)
}


static PyThreadState *
get_interned_tstate(void)
{
PyThreadState *tstate = _PyRuntime.unicode_state.interned.tstate;
if (tstate == NULL) {
PyInterpreterState *main_interp = _PyInterpreterState_Main();
/* We do not "bind" the thread state here. */
tstate = _PyThreadState_New(main_interp);
if (tstate == NULL) {
PyErr_Clear();
return NULL;
}
}
return tstate;
}

static void
clear_interned_tstate(void)
{
PyThreadState *tstate = _PyRuntime.unicode_state.interned.tstate;
if (tstate != NULL) {
_PyRuntime.unicode_state.interned.tstate = NULL;
PyThreadState_Clear(tstate);
PyThreadState_Delete(tstate);
}
}

static inline PyObject *
store_interned(PyObject *obj)
{
Expand Down Expand Up @@ -14709,6 +14736,8 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
PyDict_Clear(interned);
Py_DECREF(interned);
set_interned_dict(NULL);

clear_interned_tstate();
}


Expand Down
0