Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af90b3e

Browse files
committed
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.
1 parent 7a29bd5 commit af90b3e

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

Lib/test/test_descr.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,7 @@ def __add__(self, other):
13661366
a = hexint(12345)
13671367
verify(int(a) == 12345)
13681368
verify(int(a).__class__ is int)
1369+
verify(hash(a) == hash(12345))
13691370
verify((+a).__class__ is int)
13701371
verify((a >> 0).__class__ is int)
13711372
verify((a << 0).__class__ is int)
@@ -1388,6 +1389,7 @@ def __add__(self, other):
13881389
verify(str(5 + octlong(3000)) == "05675")
13891390
a = octlong(12345)
13901391
verify(long(a) == 12345L)
1392+
verify(hash(a) == hash(12345L))
13911393
verify(long(a).__class__ is long)
13921394
verify((+a).__class__ is long)
13931395
verify((-a).__class__ is long)
@@ -1425,6 +1427,7 @@ def __repr__(self):
14251427
a = precfloat(12345)
14261428
verify(float(a) == 12345.0)
14271429
verify(float(a).__class__ is float)
1430+
verify(hash(a) == hash(12345.0))
14281431
verify((+a).__class__ is float)
14291432

14301433
class madtuple(tuple):
@@ -1447,6 +1450,7 @@ def rev(self):
14471450
a = madtuple((1,2,3,4,5))
14481451
verify(tuple(a) == (1,2,3,4,5))
14491452
verify(tuple(a).__class__ is tuple)
1453+
verify(hash(a) == hash((1,2,3,4,5)))
14501454
verify(a[:].__class__ is tuple)
14511455
verify((a * 1).__class__ is tuple)
14521456
verify((a * 0).__class__ is tuple)
@@ -1485,6 +1489,9 @@ def rev(self):
14851489
s = madstring(base)
14861490
verify(str(s) == base)
14871491
verify(str(s).__class__ is str)
1492+
verify(hash(s) == hash(base))
1493+
verify({s: 1}[base] == 1)
1494+
verify({base: 1}[s] == 1)
14881495
verify((s + "").__class__ is str)
14891496
verify(s + "" == base)
14901497
verify(("" + s).__class__ is str)
@@ -1538,6 +1545,9 @@ def rev(self):
15381545
u = madunicode(base)
15391546
verify(unicode(u) == base)
15401547
verify(unicode(u).__class__ is unicode)
1548+
verify(hash(u) == hash(base))
1549+
verify({u: 1}[base] == 1)
1550+
verify({base: 1}[u] == 1)
15411551
verify(u.strip().__class__ is unicode)
15421552
verify(u.strip() == base)
15431553
verify(u.lstrip().__class__ is unicode)

Objects/stringobject.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,19 +2671,29 @@ string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26712671
static PyObject *
26722672
str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26732673
{
2674-
PyObject *tmp, *new;
2674+
PyObject *tmp, *pnew;
26752675
int n;
26762676

26772677
assert(PyType_IsSubtype(type, &PyString_Type));
26782678
tmp = string_new(&PyString_Type, args, kwds);
26792679
if (tmp == NULL)
26802680
return NULL;
26812681
assert(PyString_CheckExact(tmp));
2682-
new = type->tp_alloc(type, n = PyString_GET_SIZE(tmp));
2683-
if (new != NULL)
2684-
memcpy(PyString_AS_STRING(new), PyString_AS_STRING(tmp), n+1);
2682+
n = PyString_GET_SIZE(tmp);
2683+
pnew = type->tp_alloc(type, n);
2684+
if (pnew != NULL) {
2685+
memcpy(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1);
2686+
#ifdef CACHE_HASH
2687+
((PyStringObject *)pnew)->ob_shash =
2688+
((PyStringObject *)tmp)->ob_shash;
2689+
#endif
2690+
#ifdef INTERN_STRINGS
2691+
((PyStringObject *)pnew)->ob_sinterned =
2692+
((PyStringObject *)tmp)->ob_sinterned;
2693+
#endif
2694+
}
26852695
Py_DECREF(tmp);
2686-
return new;
2696+
return pnew;
26872697
}
26882698

26892699
static char string_doc[] =

Objects/unicodeobject.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5331,27 +5331,28 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
53315331
static PyObject *
53325332
unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
53335333
{
5334-
PyUnicodeObject *tmp, *new;
5334+
PyUnicodeObject *tmp, *pnew;
53355335
int n;
53365336

53375337
assert(PyType_IsSubtype(type, &PyUnicode_Type));
53385338
tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds);
53395339
if (tmp == NULL)
53405340
return NULL;
53415341
assert(PyUnicode_Check(tmp));
5342-
new = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);
5343-
if (new == NULL)
5342+
pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);
5343+
if (pnew == NULL)
53445344
return NULL;
5345-
new->str = PyMem_NEW(Py_UNICODE, n+1);
5346-
if (new->str == NULL) {
5347-
_Py_ForgetReference((PyObject *)new);
5348-
PyObject_DEL(new);
5345+
pnew->str = PyMem_NEW(Py_UNICODE, n+1);
5346+
if (pnew->str == NULL) {
5347+
_Py_ForgetReference((PyObject *)pnew);
5348+
PyObject_DEL(pnew);
53495349
return NULL;
53505350
}
5351-
Py_UNICODE_COPY(new->str, tmp->str, n+1);
5352-
new->length = n;
5351+
Py_UNICODE_COPY(pnew->str, tmp->str, n+1);
5352+
pnew->length = n;
5353+
pnew->hash = tmp->hash;
53535354
Py_DECREF(tmp);
5354-
return (PyObject *)new;
5355+
return (PyObject *)pnew;
53555356
}
53565357

53575358
static char unicode_doc[] =

0 commit comments

Comments
 (0)