Skip to content

bpo-46006: Fix _PyUnicode_EqualToASCIIId() for interned strings #30123

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,4 @@
Fix string comparisons for subinterpreters: no longer make the assumption
that two interned strings are not equal if they have different different
memory addresses. This assumption is no longer true since interned strings have
been made per interpreter in Python 3.10. Patch by Victor Stinner.
5 changes: 4 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8459,7 +8459,10 @@ update_slot(PyTypeObject *type, PyObject *name)
for (p = slotdefs; p->name; p++) {
assert(PyUnicode_CheckExact(p->name_strobj));
assert(PyUnicode_CheckExact(name));
if (p->name_strobj == name) {
// bpo-46006: subinterpreters require to compare strings contents, even
// if both strings are interned. _PyUnicode_EQ() is required to keep
// support for built-in static types in subinterpreters.
if (p->name_strobj == name || _PyUnicode_EQ(p->name_strobj, name)) {
*pp++ = p;
}
}
Expand Down
15 changes: 11 additions & 4 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -11336,11 +11336,18 @@ _PyUnicode_EqualToASCIIId(PyObject *left, _Py_Identifier *right)
return _PyUnicode_EqualToASCIIString(left, right->string);
}

if (left == right_uni)
if (left == right_uni) {
return 1;

if (PyUnicode_CHECK_INTERNED(left))
return 0;
}
// bpo-46006: The left string cannot be considered as not equal to the
// right string if the left string is interned, because the two string
// objects can belong to two interpreters.
//
// While an interpreter is supposed to only access objects that it created
// (bpo-40533), in practice in Python 3.11, it remains common that a
// subinterpreter access objects of the main interprter. For example,
// access attribute names (strings) of static types created by the main
// interpreter.

assert(_PyUnicode_HASH(right_uni) != -1);
Py_hash_t hash = _PyUnicode_HASH(left);
Expand Down