[pypy-svn] r9925 - in pypy/dist/pypy/objspace/std: . test
arigo at codespeak.net
arigo at codespeak.net
Sun Mar 20 16:44:03 CET 2005
Author: arigo
Date: Sun Mar 20 16:44:03 2005
New Revision: 9925
Modified:
pypy/dist/pypy/objspace/std/restricted_int.py
pypy/dist/pypy/objspace/std/stringobject.py
pypy/dist/pypy/objspace/std/test/test_stringobject.py
Log:
- str.startswith(), str.endswith() must return booleans.
- implement str.__hash__().
Modified: pypy/dist/pypy/objspace/std/restricted_int.py
==============================================================================
--- pypy/dist/pypy/objspace/std/restricted_int.py (original)
+++ pypy/dist/pypy/objspace/std/restricted_int.py Sun Mar 20 16:44:03 2005
@@ -147,6 +147,15 @@
LONG_BIT = _bits+1
LONG_MASK = _Ltest*2-1
+LONG_TEST = _Ltest
+
+def intmask(n):
+ if isinstance(n, int):
+ return n
+ n &= LONG_MASK
+ if n >= LONG_TEST:
+ n -= 2*LONG_TEST
+ return int(n)
del _bits, _itest, _Ltest
Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py (original)
+++ pypy/dist/pypy/objspace/std/stringobject.py Sun Mar 20 16:44:03 2005
@@ -77,6 +77,7 @@
from pypy.objspace.std.objspace import *
from pypy.interpreter import gateway
from pypy.objspace.std.intobject import W_IntObject
+from pypy.objspace.std.restricted_int import intmask
from pypy.objspace.std.sliceobject import W_SliceObject
from pypy.objspace.std import slicetype
from pypy.objspace.std.listobject import W_ListObject
@@ -638,7 +639,7 @@
else:
found = 1
- return W_IntObject(space, found)
+ return space.newbool(found)
def str_startswith__String_String_ANY(space, w_self, w_prefix, w_start):
@@ -654,7 +655,7 @@
else:
found = 1
- return W_IntObject(space, found)
+ return space.newbool(found)
def _tabindent(u_token, u_tabsize):
@@ -752,7 +753,17 @@
def hash__String(space, w_str):
w_hash = w_str.w_hash
if w_hash is None:
- w_hash = W_IntObject(space, hash(w_str._value))
+ s = w_str._value
+ try:
+ x = ord(s[0]) << 7
+ except IndexError:
+ x = 0
+ else:
+ for c in s:
+ x = (1000003*x) ^ ord(c)
+ x ^= len(s)
+ # unlike CPython, there is no reason to avoid to return -1
+ w_hash = W_IntObject(space, intmask(x))
w_str.w_hash = w_hash
return w_hash
Modified: pypy/dist/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_stringobject.py (original)
+++ pypy/dist/pypy/objspace/std/test/test_stringobject.py Sun Mar 20 16:44:03 2005
@@ -268,31 +268,31 @@
assert 'aaa'.count('a', 0, -10) == 0
def test_startswith(self):
- assert 'ab'.startswith('ab') == 1
- assert 'ab'.startswith('a') == 1
- assert 'ab'.startswith('') == 1
- assert 'x'.startswith('a') == 0
- assert 'x'.startswith('x') == 1
- assert ''.startswith('') == 1
- assert ''.startswith('a') == 0
- assert 'x'.startswith('xx') == 0
- assert 'y'.startswith('xx') == 0
+ assert 'ab'.startswith('ab') is True
+ assert 'ab'.startswith('a') is True
+ assert 'ab'.startswith('') is True
+ assert 'x'.startswith('a') is False
+ assert 'x'.startswith('x') is True
+ assert ''.startswith('') is True
+ assert ''.startswith('a') is False
+ assert 'x'.startswith('xx') is False
+ assert 'y'.startswith('xx') is False
def test_startswith_more(self):
- assert 'ab'.startswith('a', 0) == 1
- assert 'ab'.startswith('a', 1) == 0
- assert 'ab'.startswith('b', 1) == 1
+ assert 'ab'.startswith('a', 0) is True
+ assert 'ab'.startswith('a', 1) is False
+ assert 'ab'.startswith('b', 1) is True
def test_endswith(self):
- assert 'ab'.endswith('ab') == 1
- assert 'ab'.endswith('b') == 1
- assert 'ab'.endswith('') == 1
- assert 'x'.endswith('a') == 0
- assert 'x'.endswith('x') == 1
- assert ''.endswith('') == 1
- assert ''.endswith('a') == 0
- assert 'x'.endswith('xx') == 0
- assert 'y'.endswith('xx') == 0
+ assert 'ab'.endswith('ab') is True
+ assert 'ab'.endswith('b') is True
+ assert 'ab'.endswith('') is True
+ assert 'x'.endswith('a') is False
+ assert 'x'.endswith('x') is True
+ assert ''.endswith('') is True
+ assert ''.endswith('a') is False
+ assert 'x'.endswith('xx') is False
+ assert 'y'.endswith('xx') is False
def test_expandtabs(self):
assert 'abc\rab\tdef\ng\thi'.expandtabs() == 'abc\rab def\ng hi'
@@ -520,4 +520,10 @@
def test_decode(self):
assert 'hello'.decode('rot-13') == 'uryyb'
assert 'hello'.decode('string-escape') == 'hello'
-
\ No newline at end of file
+
+ def test_hash(self):
+ # check that we have the same hash as CPython for at least 31 bits
+ # (but don't go checking CPython's special case -1)
+ assert hash('') == 0
+ assert hash('hello') & 0x7fffffff == 0x347697fd
+ assert hash('hello world!') & 0x7fffffff == 0x2f0bb411
More information about the Pypy-commit
mailing list