[3.10] bpo-46006: Revert "bpo-40521: Per-interpreter interned strings (GH-20085)" (GH-30422) by vstinner · Pull Request #30425 · 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
12 changes: 3 additions & 9 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix a regression when a type method like ``__init__()`` is modified in a
subinterpreter. Fix a regression in ``_PyUnicode_EqualToASCIIId()`` and type
``update_slot()``. Revert the change which made the Unicode dictionary of
interned strings compatible with subinterpreters: the internal interned
dictionary is shared again by all interpreters. Patch by Victor Stinner.
22 changes: 22 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ typedef struct PySlot_Offset {
} PySlot_Offset;


/* bpo-40521: Interned strings are shared by all subinterpreters */
#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
# define INTERN_NAME_STRINGS
#endif

/* alphabetical order */
_Py_IDENTIFIER(__abstractmethods__);
_Py_IDENTIFIER(__annotations__);
Expand Down Expand Up @@ -3988,6 +3993,7 @@ type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
if (name == NULL)
return -1;
}
#ifdef INTERN_NAME_STRINGS
if (!PyUnicode_CHECK_INTERNED(name)) {
PyUnicode_InternInPlace(&name);
if (!PyUnicode_CHECK_INTERNED(name)) {
Expand All @@ -3997,6 +4003,7 @@ type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
return -1;
}
}
#endif
}
else {
/* Will fail in _PyObject_GenericSetAttrWithDict. */
Expand Down Expand Up @@ -8344,10 +8351,17 @@ _PyTypes_InitSlotDefs(void)
for (slotdef *p = slotdefs; p->name; p++) {
/* Slots must be ordered by their offset in the PyHeapTypeObject. */
assert(!p[1].name || p->offset <= p[1].offset);
#ifdef INTERN_NAME_STRINGS
p->name_strobj = PyUnicode_InternFromString(p->name);
if (!p->name_strobj || !PyUnicode_CHECK_INTERNED(p->name_strobj)) {
return _PyStatus_NO_MEMORY();
}
#else
p->name_strobj = PyUnicode_FromString(p->name);
if (!p->name_strobj) {
return _PyStatus_NO_MEMORY();
}
#endif
}
slotdefs_initialized = 1;
return _PyStatus_OK();
Expand All @@ -8372,16 +8386,24 @@ update_slot(PyTypeObject *type, PyObject *name)
int offset;

assert(PyUnicode_CheckExact(name));
#ifdef INTERN_NAME_STRINGS
assert(PyUnicode_CHECK_INTERNED(name));
#endif

assert(slotdefs_initialized);
pp = ptrs;
for (p = slotdefs; p->name; p++) {
assert(PyUnicode_CheckExact(p->name_strobj));
assert(PyUnicode_CheckExact(name));
#ifdef INTERN_NAME_STRINGS
if (p->name_strobj == name) {
*pp++ = p;
}
#else
if (p->name_strobj == name || _PyUnicode_EQ(p->name_strobj, name)) {
*pp++ = p;
}
#endif
}
*pp = NULL;
for (pp = ptrs; *pp; pp++) {
Expand Down
63 changes: 46 additions & 17 deletions Objects/unicodeobject.c