mirror of
https://github.com/python/cpython.git
synced 2025-07-19 01:05:26 +00:00
If interning an instance of a string subclass, intern a real string object
with the same value instead. This ensures that a string (or string subclass) object's ob_sinterned pointer is always a str (or NULL), and that the dict of interned strings only has strs as keys.
This commit is contained in:
parent
af90b3e610
commit
111f60964e
2 changed files with 34 additions and 4 deletions
|
@ -3553,10 +3553,26 @@ PyString_InternInPlace(PyObject **p)
|
|||
Py_DECREF(s);
|
||||
return;
|
||||
}
|
||||
t = (PyObject *)s;
|
||||
if (PyDict_SetItem(interned, t, t) == 0) {
|
||||
s->ob_sinterned = t;
|
||||
return;
|
||||
/* Ensure that only true string objects appear in the intern dict,
|
||||
and as the value of ob_sinterned. */
|
||||
if (PyString_CheckExact(s)) {
|
||||
t = (PyObject *)s;
|
||||
if (PyDict_SetItem(interned, t, t) == 0) {
|
||||
s->ob_sinterned = t;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
t = PyString_FromStringAndSize(PyString_AS_STRING(s),
|
||||
PyString_GET_SIZE(s));
|
||||
if (t != NULL) {
|
||||
if (PyDict_SetItem(interned, t, t) == 0) {
|
||||
*p = s->ob_sinterned = t;
|
||||
Py_DECREF(s);
|
||||
return;
|
||||
}
|
||||
Py_DECREF(t);
|
||||
}
|
||||
}
|
||||
PyErr_Clear();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue