diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-09-12 07:54:51 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-09-12 07:54:51 (GMT) |
commit | 111f60964ee438e64d82cb884f9dfdec10321b7c (patch) | |
tree | 18d0b7bf790ec9fc702aa3e5e95290e2ae0c046f | |
parent | af90b3e610212a4994962246875e5bfc5574dff6 (diff) | |
download | cpython-111f60964ee438e64d82cb884f9dfdec10321b7c.zip cpython-111f60964ee438e64d82cb884f9dfdec10321b7c.tar.gz cpython-111f60964ee438e64d82cb884f9dfdec10321b7c.tar.bz2 |
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.
-rw-r--r-- | Lib/test/test_descr.py | 14 | ||||
-rw-r--r-- | Objects/stringobject.c | 24 |
2 files changed, 34 insertions, 4 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 8d9e81e..b029979 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1529,6 +1529,20 @@ def inherits(): verify(s.lower().__class__ is str) verify(s.lower() == base) + s = madstring("x y") + verify(intern(s).__class__ is str) + verify(intern(s) is intern("x y")) + verify(intern(s) == "x y") + + i = intern("y x") + s = madstring("y x") + verify(intern(s).__class__ is str) + verify(intern(s) is i) + + s = madstring(i) + verify(intern(s).__class__ is str) + verify(intern(s) is i) + class madunicode(unicode): _rev = None def rev(self): diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 3c03b9e..99a16ed 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -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(); } |