summaryrefslogtreecommitdiff
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2004-08-07 19:20:05 +0000
committerJeremy Hylton <jeremy@alum.mit.edu>2004-08-07 19:20:05 +0000
commit68afd55ea92823228709cf9b48795959a01715f9 (patch)
tree1a3e6f4122e9a7d4fc954a5438957ef4f33b4ae8 /Objects/stringobject.c
parente4262f146805f1373d21dea41f12980294eff9fc (diff)
downloadcpython-68afd55ea92823228709cf9b48795959a01715f9.tar.gz
Subclasses of string can no longer be interned. The semantics of
interning were not clear here -- a subclass could be mutable, for example -- and had bugs. Explicitly interning a subclass of string via intern() will raise a TypeError. Internal operations that attempt to intern a string subclass will have no effect. Added a few tests to test_builtin that includes the old buggy code and verifies that calls like PyObject_SetAttr() don't fail. Perhaps these tests should have gone in test_string.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r--Objects/stringobject.c34
1 files changed, 12 insertions, 22 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 29562a9a8f..f29929b0ec 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -4313,6 +4313,10 @@ PyString_InternInPlace(PyObject **p)
PyObject *t;
if (s == NULL || !PyString_Check(s))
Py_FatalError("PyString_InternInPlace: strings only please!");
+ /* If it's a string subclass, we don't really know what putting
+ it in the interned dict might do. */
+ if (!PyString_CheckExact(s))
+ return;
if (PyString_CHECK_INTERNED(s))
return;
if (interned == NULL) {
@@ -4322,36 +4326,22 @@ PyString_InternInPlace(PyObject **p)
return;
}
}
- if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) {
+ t = PyDict_GetItem(interned, (PyObject *)s);
+ if (t) {
Py_INCREF(t);
Py_DECREF(*p);
*p = t;
return;
}
- /* Ensure that only true string objects appear in the intern dict */
- if (!PyString_CheckExact(s)) {
- t = PyString_FromStringAndSize(PyString_AS_STRING(s),
- PyString_GET_SIZE(s));
- if (t == NULL) {
- PyErr_Clear();
- return;
- }
- } else {
- t = (PyObject*) s;
- Py_INCREF(t);
- }
- if (PyDict_SetItem(interned, t, t) == 0) {
- /* The two references in interned are not counted by
- refcnt. The string deallocator will take care of this */
- ((PyObject *)t)->ob_refcnt-=2;
- PyString_CHECK_INTERNED(t) = SSTATE_INTERNED_MORTAL;
- Py_DECREF(*p);
- *p = t;
+ if (PyDict_SetItem(interned, s, s) < 0) {
+ PyErr_Clear();
return;
}
- Py_DECREF(t);
- PyErr_Clear();
+ /* The two references in interned are not counted by refcnt.
+ The string deallocator will take care of this */
+ (*p)->ob_refcnt -= 2;
+ PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL;
}
void