diff options
-rw-r--r-- | Lib/test/test_descr.py | 10 | ||||
-rw-r--r-- | Objects/stringobject.c | 20 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 21 |
3 files changed, 36 insertions, 15 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index b791037..8d9e81e 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -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) diff --git a/Objects/stringobject.c b/Objects/stringobject.c index b220859..3c03b9e 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -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[] = diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index c5912b5..5080eb8 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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[] = |