]> granicus.if.org Git - python/commitdiff
str_subtype_new, unicode_subtype_new:
authorTim Peters <tim.peters@gmail.com>
Wed, 12 Sep 2001 05:18:58 +0000 (05:18 +0000)
committerTim Peters <tim.peters@gmail.com>
Wed, 12 Sep 2001 05:18:58 +0000 (05:18 +0000)
+ 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.

Lib/test/test_descr.py
Objects/stringobject.c
Objects/unicodeobject.c

index b791037aa580087f4e9354eff230ee7a6b345b4f..8d9e81e12e05c4569d7b94d467e15e88d4eeefaf 100644 (file)
@@ -1366,6 +1366,7 @@ def inherits():
     a = hexint(12345)
     verify(int(a) == 12345)
     verify(int(a).__class__ is int)
+    verify(hash(a) == hash(12345))
     verify((+a).__class__ is int)
     verify((a >> 0).__class__ is int)
     verify((a << 0).__class__ is int)
@@ -1388,6 +1389,7 @@ def inherits():
     verify(str(5 + octlong(3000)) == "05675")
     a = octlong(12345)
     verify(long(a) == 12345L)
+    verify(hash(a) == hash(12345L))
     verify(long(a).__class__ is long)
     verify((+a).__class__ is long)
     verify((-a).__class__ is long)
@@ -1425,6 +1427,7 @@ def inherits():
     a = precfloat(12345)
     verify(float(a) == 12345.0)
     verify(float(a).__class__ is float)
+    verify(hash(a) == hash(12345.0))
     verify((+a).__class__ is float)
 
     class madtuple(tuple):
@@ -1447,6 +1450,7 @@ def inherits():
     a = madtuple((1,2,3,4,5))
     verify(tuple(a) == (1,2,3,4,5))
     verify(tuple(a).__class__ is tuple)
+    verify(hash(a) == hash((1,2,3,4,5)))
     verify(a[:].__class__ is tuple)
     verify((a * 1).__class__ is tuple)
     verify((a * 0).__class__ is tuple)
@@ -1485,6 +1489,9 @@ def inherits():
     s = madstring(base)
     verify(str(s) == base)
     verify(str(s).__class__ is str)
+    verify(hash(s) == hash(base))
+    verify({s: 1}[base] == 1)
+    verify({base: 1}[s] == 1)
     verify((s + "").__class__ is str)
     verify(s + "" == base)
     verify(("" + s).__class__ is str)
@@ -1538,6 +1545,9 @@ def inherits():
     u = madunicode(base)
     verify(unicode(u) == base)
     verify(unicode(u).__class__ is unicode)
+    verify(hash(u) == hash(base))
+    verify({u: 1}[base] == 1)
+    verify({base: 1}[u] == 1)
     verify(u.strip().__class__ is unicode)
     verify(u.strip() == base)
     verify(u.lstrip().__class__ is unicode)
index b220859a6294b9c278eeda6a0e6bccb0fc1bcae4..3c03b9ee2a09f1fd09711e11e7c2e08682c704d4 100644 (file)
@@ -2671,7 +2671,7 @@ string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 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 @@ str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
        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[] =
index c5912b516763c349f25b5a648000a77f8be3ad92..5080eb86661132f63dfa12e808b3b67be80dbc31 100644 (file)
@@ -5331,7 +5331,7 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 static PyObject *
 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-       PyUnicodeObject *tmp, *new;
+       PyUnicodeObject *tmp, *pnew;
        int n;
 
        assert(PyType_IsSubtype(type, &PyUnicode_Type));
@@ -5339,19 +5339,20 @@ unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
        if (tmp == NULL)
                return NULL;
        assert(PyUnicode_Check(tmp));
-       new = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);
-       if (new == NULL)
+       pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);
+       if (pnew == NULL)
                return NULL;
-       new->str = PyMem_NEW(Py_UNICODE, n+1);
-       if (new->str == NULL) {
-               _Py_ForgetReference((PyObject *)new);
-               PyObject_DEL(new);
+       pnew->str = PyMem_NEW(Py_UNICODE, n+1);
+       if (pnew->str == NULL) {
+               _Py_ForgetReference((PyObject *)pnew);
+               PyObject_DEL(pnew);
                return NULL;
        }
-       Py_UNICODE_COPY(new->str, tmp->str, n+1);
-       new->length = n;
+       Py_UNICODE_COPY(pnew->str, tmp->str, n+1);
+       pnew->length = n;
+       pnew->hash = tmp->hash;
        Py_DECREF(tmp);
-       return (PyObject *)new;
+       return (PyObject *)pnew;
 }
 
 static char unicode_doc[] =