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.
This commit is contained in:
Tim Peters 2001-09-12 05:18:58 +00:00
parent 7a29bd5861
commit af90b3e610
3 changed files with 36 additions and 15 deletions

View 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)

View 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[] =

View 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[] =