Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 111f609

Browse files
committed
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.
1 parent af90b3e commit 111f609

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

Lib/test/test_descr.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,20 @@ def rev(self):
15291529
verify(s.lower().__class__ is str)
15301530
verify(s.lower() == base)
15311531

1532+
s = madstring("x y")
1533+
verify(intern(s).__class__ is str)
1534+
verify(intern(s) is intern("x y"))
1535+
verify(intern(s) == "x y")
1536+
1537+
i = intern("y x")
1538+
s = madstring("y x")
1539+
verify(intern(s).__class__ is str)
1540+
verify(intern(s) is i)
1541+
1542+
s = madstring(i)
1543+
verify(intern(s).__class__ is str)
1544+
verify(intern(s) is i)
1545+
15321546
class madunicode(unicode):
15331547
_rev = None
15341548
def rev(self):

Objects/stringobject.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,10 +3553,26 @@ PyString_InternInPlace(PyObject **p)
35533553
Py_DECREF(s);
35543554
return;
35553555
}
3556-
t = (PyObject *)s;
3557-
if (PyDict_SetItem(interned, t, t) == 0) {
3558-
s->ob_sinterned = t;
3559-
return;
3556+
/* Ensure that only true string objects appear in the intern dict,
3557+
and as the value of ob_sinterned. */
3558+
if (PyString_CheckExact(s)) {
3559+
t = (PyObject *)s;
3560+
if (PyDict_SetItem(interned, t, t) == 0) {
3561+
s->ob_sinterned = t;
3562+
return;
3563+
}
3564+
}
3565+
else {
3566+
t = PyString_FromStringAndSize(PyString_AS_STRING(s),
3567+
PyString_GET_SIZE(s));
3568+
if (t != NULL) {
3569+
if (PyDict_SetItem(interned, t, t) == 0) {
3570+
*p = s->ob_sinterned = t;
3571+
Py_DECREF(s);
3572+
return;
3573+
}
3574+
Py_DECREF(t);
3575+
}
35603576
}
35613577
PyErr_Clear();
35623578
}

0 commit comments

Comments
 (0)