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

Skip to content

Commit b859c07

Browse files
committed
SF bug #800796: Difference between hash() and __hash__()
slice(5).__hash__() now raises a TypeError.
1 parent a1a1dba commit b859c07

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/test/test_slice.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def test_constructor(self):
1414
def test_repr(self):
1515
self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
1616

17+
def test_hash(self):
18+
# Verify clearing of SF bug #800796
19+
self.assertRaises(TypeError, hash, slice(5))
20+
self.assertRaises(TypeError, slice(5).__hash__)
21+
1722
def test_cmp(self):
1823
s1 = slice(1, 2, 3)
1924
s2 = slice(1, 2, 3)

Objects/sliceobject.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ slice_compare(PySliceObject *v, PySliceObject *w)
278278
return result;
279279
}
280280

281+
static long
282+
slice_hash(PySliceObject *v)
283+
{
284+
PyErr_SetString(PyExc_TypeError, "unhashable type");
285+
return -1L;
286+
}
287+
281288
PyTypeObject PySlice_Type = {
282289
PyObject_HEAD_INIT(&PyType_Type)
283290
0, /* Number of items for varobject */
@@ -293,7 +300,7 @@ PyTypeObject PySlice_Type = {
293300
0, /* tp_as_number */
294301
0, /* tp_as_sequence */
295302
0, /* tp_as_mapping */
296-
0, /* tp_hash */
303+
(hashfunc)slice_hash, /* tp_hash */
297304
0, /* tp_call */
298305
0, /* tp_str */
299306
PyObject_GenericGetAttr, /* tp_getattro */

0 commit comments

Comments
 (0)