Fix some tests I broke. (More to follow.)

This commit is contained in:
Guido van Rossum 2007-08-27 21:47:52 +00:00
parent 1f2ca56e29
commit 98b349f8e6
3 changed files with 17 additions and 4 deletions

View File

@ -57,6 +57,8 @@ class UserString:
return self.data >= string
def __contains__(self, char):
if isinstance(char, UserString):
char = char.data
return char in self.data
def __len__(self): return len(self.data)
@ -88,6 +90,8 @@ class UserString:
def center(self, width, *args):
return self.__class__(self.data.center(width, *args))
def count(self, sub, start=0, end=sys.maxint):
if isinstance(sub, UserString):
sub = sub.data
return self.data.count(sub, start, end)
def decode(self, encoding=None, errors=None): # XXX improve this?
if encoding:
@ -110,6 +114,8 @@ class UserString:
def expandtabs(self, tabsize=8):
return self.__class__(self.data.expandtabs(tabsize))
def find(self, sub, start=0, end=sys.maxint):
if isinstance(sub, UserString):
sub = sub.data
return self.data.find(sub, start, end)
def index(self, sub, start=0, end=sys.maxint):
return self.data.index(sub, start, end)
@ -130,6 +136,10 @@ class UserString:
def partition(self, sep):
return self.data.partition(sep)
def replace(self, old, new, maxsplit=-1):
if isinstance(old, UserString):
old = old.data
if isinstance(new, UserString):
new = new.data
return self.__class__(self.data.replace(old, new, maxsplit))
def rfind(self, sub, start=0, end=sys.maxint):
return self.data.rfind(sub, start, end)

View File

@ -28,7 +28,8 @@ __all__ = [
def _translate(s, altchars):
assert isinstance(s, bytes), type(s)
if not isinstance(s, bytes):
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
translation = bytes(range(256))
for k, v in altchars.items():
translation[ord(k)] = v[0]
@ -323,7 +324,8 @@ def decode(input, output):
def encodestring(s):
"""Encode a string into multiple lines of base-64 data."""
assert isinstance(s, bytes), repr(s)
if not isinstance(s, bytes):
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
pieces = []
for i in range(0, len(s), MAXBINSIZE):
chunk = s[i : i + MAXBINSIZE]
@ -333,7 +335,8 @@ def encodestring(s):
def decodestring(s):
"""Decode a string."""
assert isinstance(s, bytes), repr(s)
if not isinstance(s, bytes):
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
return binascii.a2b_base64(s)

View File

@ -998,7 +998,7 @@ class HandlerTests(unittest.TestCase):
# expect one request without authorization, then one with
self.assertEqual(len(http_handler.requests), 2)
self.assertFalse(http_handler.requests[0].has_header(auth_header))
userpass = '%s:%s' % (user, password)
userpass = bytes('%s:%s' % (user, password), "ascii")
auth_hdr_value = 'Basic ' + str(base64.encodestring(userpass)).strip()
self.assertEqual(http_handler.requests[1].get_header(auth_header),
auth_hdr_value)