[New-bugs-announce] [issue17624] Confusing TypeError in urllib.urlopen

Stefan Bucur report at bugs.python.org
Wed Apr 3 12:45:05 CEST 2013

New submission from Stefan Bucur:

When calling urllib.urlopen with a string containing the NULL ('\x00') character, a TypeError exception is thrown, as in the following example:


  File "/home/bucur/onion/python-bin/lib/python2.7/urllib.py", line 86, in urlopen
    return opener.open(url)
  File "/home/bucur/onion/python-bin/lib/python2.7/urllib.py", line 207, in open
    return getattr(self, name)(url)
  File "/home/bucur/onion/python-bin/lib/python2.7/urllib.py", line 462, in open_file
    return self.open_local_file(url)
  File "/home/bucur/onion/python-bin/lib/python2.7/urllib.py", line 474, in open_local_file
    stats = os.stat(localname)
TypeError: must be encoded string without NULL bytes, not str

This exception is confusing, since apparently the right type (a string) is passed to the function.  Since this behavior cannot change, it would be good to mention this exception in the function documentation.

I can imagine code that composes a URL based on user-supplied input and passes it to urlopen crashing if it doesn't properly sanitize the URL and/or doesn't catch TypeError.

components: Library (Lib)
messages: 185913
nosy: Stefan.Bucur
priority: normal
severity: normal
status: open
title: Confusing TypeError in urllib.urlopen
type: behavior
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list