summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xLib/UserString.py10
-rwxr-xr-xLib/base64.py9
-rw-r--r--Lib/test/test_urllib2.py2
3 files changed, 17 insertions, 4 deletions
diff --git a/Lib/UserString.py b/Lib/UserString.py
index 62e73bbc9d..500cd12c15 100755
--- a/Lib/UserString.py
+++ b/Lib/UserString.py
@@ -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)
diff --git a/Lib/base64.py b/Lib/base64.py
index cec6422975..5d42065e2d 100755
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -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)
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index 22979f6947..5cbc652208 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -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)