Don't require Unicode support.

This commit is contained in:
Guido van Rossum 2002-05-24 17:58:05 +00:00
parent 0cc8c37357
commit 4b46c0a15f
1 changed files with 9 additions and 2 deletions

View File

@ -906,11 +906,18 @@ def basejoin(base, url):
# unquote('abc%20def') -> 'abc def' # unquote('abc%20def') -> 'abc def'
# quote('abc def') -> 'abc%20def') # quote('abc def') -> 'abc%20def')
if hasattr(types, "UnicodeType"):
def _is_unicode(x):
return isinstance(x, unicode)
else:
def _is_unicode(x):
return 0
def toBytes(url): def toBytes(url):
"""toBytes(u"URL") --> 'URL'.""" """toBytes(u"URL") --> 'URL'."""
# Most URL schemes require ASCII. If that changes, the conversion # Most URL schemes require ASCII. If that changes, the conversion
# can be relaxed # can be relaxed
if type(url) is types.UnicodeType: if _is_unicode(url):
try: try:
url = url.encode("ASCII") url = url.encode("ASCII")
except UnicodeError: except UnicodeError:
@ -1189,7 +1196,7 @@ def urlencode(query,doseq=0):
if type(v) == types.StringType: if type(v) == types.StringType:
v = quote_plus(v) v = quote_plus(v)
l.append(k + '=' + v) l.append(k + '=' + v)
elif type(v) == types.UnicodeType: elif _is_unicode(v):
# is there a reasonable way to convert to ASCII? # is there a reasonable way to convert to ASCII?
# encode generates a string, but "replace" or "ignore" # encode generates a string, but "replace" or "ignore"
# lose information and "strict" can raise UnicodeError # lose information and "strict" can raise UnicodeError