diff Objects/stringobject.c @ 19886:3b0da13ace48 legacy-trunk

str_subtype_new, unicode_subtype_new: + These were leaving the hash fields at 0, which all string and unicode routines believe is a legitimate hash code. As a result, hash() applied to str and unicode subclass instances always returned 0, which in turn confused dict operations, etc. + Changed local names "new"; no point to antagonizing C++ compilers.
author Tim Peters <tim.peters@gmail.com>
date Wed, 12 Sep 2001 05:18:58 +0000
parents df66b4dd0b0d
children b28fc8c36e3d
line wrap: on
line diff
--- a/Objects/stringobject.c	Wed Sep 12 03:03:31 2001 +0000
+++ b/Objects/stringobject.c	Wed Sep 12 05:18:58 2001 +0000
@@ -2671,7 +2671,7 @@
 static PyObject *
 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-	PyObject *tmp, *new;
+	PyObject *tmp, *pnew;
 	int n;
 
 	assert(PyType_IsSubtype(type, &PyString_Type));
@@ -2679,11 +2679,21 @@
 	if (tmp == NULL)
 		return NULL;
 	assert(PyString_CheckExact(tmp));
-	new = type->tp_alloc(type, n = PyString_GET_SIZE(tmp));
-	if (new != NULL)
-		memcpy(PyString_AS_STRING(new), PyString_AS_STRING(tmp), n+1);
+	n = PyString_GET_SIZE(tmp);
+	pnew = type->tp_alloc(type, n);
+	if (pnew != NULL) {
+		memcpy(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1);
+#ifdef CACHE_HASH
+		((PyStringObject *)pnew)->ob_shash =
+			((PyStringObject *)tmp)->ob_shash;
+#endif
+#ifdef INTERN_STRINGS
+		((PyStringObject *)pnew)->ob_sinterned =
+			((PyStringObject *)tmp)->ob_sinterned;
+#endif
+	}
 	Py_DECREF(tmp);
-	return new;
+	return pnew;
 }
 
 static char string_doc[] =