Skip to content

Commit 5c7cdfb

Browse files
authored
Merge pull request #458 from ghanshyam-lele/newstr-hash
Bug fix #454: Implement __hash__() in newstr
2 parents a5949bf + 064a12a commit 5c7cdfb

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/future/types/newstr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def __repr__(self):
105105
"""
106106
Without the u prefix
107107
"""
108+
108109
value = super(newstr, self).__repr__()
109110
# assert value[0] == u'u'
110111
return value[1:]
@@ -292,6 +293,13 @@ def __eq__(self, other):
292293
else:
293294
return False
294295

296+
def __hash__(self):
297+
if (isinstance(self, unicode) or
298+
isinstance(self, bytes) and not isnewbytes(self)):
299+
return super(newstr, self).__hash__()
300+
else:
301+
raise NotImplementedError()
302+
295303
def __ne__(self, other):
296304
if (isinstance(other, unicode) or
297305
isinstance(other, bytes) and not isnewbytes(other)):

tests/test_future/test_str.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ def test_eq(self):
363363
self.assertFalse(b'ABCD' == s)
364364
self.assertFalse(bytes(b'ABCD') == s)
365365

366+
def test_hash(self):
367+
s = str('ABCD')
368+
self.assertIsInstance(hash(s),int)
369+
366370
def test_ne(self):
367371
s = str('ABCD')
368372
self.assertNotEqual('A', s)

0 commit comments

Comments
 (0)