[Python-checkins] CVS: python/dist/src/Lib urllib.py,1.107,1.108

M.-A. Lemburg python-dev@python.org
Sun, 03 Dec 2000 20:21:11 +0100


"Martin v. L÷wis" wrote:
> 
> Update of /cvsroot/python/python/dist/src/Lib
> In directory slayer.i.sourceforge.net:/tmp/cvs-serv30506
> 
> Modified Files:
>         urllib.py
> Log Message:
> Convert Unicode strings to byte strings before passing them into specific
> protocols. Closes bug #119822.
> 
> ...
> +
> + def toBytes(url):
> +     """toBytes(u"URL") --> 'URL'."""
> +     # Most URL schemes require ASCII. If that changes, the conversion
> +     # can be relaxed
> +     if type(url) is types.UnicodeType:
> +         try:
> +             url = url.encode("ASCII")

You should make this: 'ascii' -- encoding names are lower case 
per convention (and the implementation has a short-cut to speed up
conversion to 'ascii' -- not for 'ASCII').

> +         except UnicodeError:
> +             raise UnicodeError("URL "+repr(url)+" contains non-ASCII characters")

Would it be better to use a simple ValueError here ? (UnicodeError
is a subclass of ValueError, but the error doesn't really have something to
do with Unicode conversions...)

> +     return url
> 
>   def unwrap(url):


-- 
Marc-Andre Lemburg
______________________________________________________________________
Company:                                        http://www.egenix.com/
Consulting:                                    http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/