diff Objects/stringobject.c @ 19887:b28fc8c36e3d legacy-trunk

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.
author Tim Peters <tim.peters@gmail.com>
date Wed, 12 Sep 2001 07:54:51 +0000
parents 3b0da13ace48
children 5b1ad7acabb5
line wrap: on
line diff
--- a/Objects/stringobject.c	Wed Sep 12 05:18:58 2001 +0000
+++ b/Objects/stringobject.c	Wed Sep 12 07:54:51 2001 +0000
@@ -3553,10 +3553,26 @@
 		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();
 }