Issue 13541: HTTPResponse (urllib) has no attribute read1 needed for TextIOWrapper (original) (raw)

Use case: I want to open an HTTP URL, and treat the handle as though it were opened in text mode (i.e. unicode strings not bytes).

$ python3 Python 3.2 (r32:88445, Feb 28 2011, 17:04:33) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import io import urllib.request f_bytes = urllib.request.urlopen("http://www.python.org") for line in io.TextIOWrapper(f_bytes, "iso-8859-1"): print(line) ... Traceback (most recent call last): File "", line 1, in AttributeError: 'HTTPResponse' object has no attribute 'read1'

See also Slide 122 of http://www.slideshare.net/dabeaz/mastering-python-3-io-version-2 which reports the same exception.

See also issue 5628 on a related issue, where Benjamin Peterson's issue 5628 message 84970 suggests double wrapping with BufferIOBase [sic] to solve this, but neither of the following works for me:

f_bytes = urllib.request.urlopen("http://www.python.org") for line in io.TextIOWrapper(io.BufferedIOBase(f_bytes), "iso-8859-1"): print(line) ... Traceback (most recent call last): File "", line 1, in io.UnsupportedOperation: not readable

f_bytes = urllib.request.urlopen("http://www.python.org") for line in io.TextIOWrapper(io.BufferedReader(f_bytes), "iso-8859-1"): print(line) ... Traceback (most recent call last): File "", line 1, in AttributeError: 'HTTPResponse' object has no attribute 'readinto'

Nor incidentally does this:

f_bytes = urllib.request.urlopen("http://www.python.org") for line in io.BufferedReader(f_bytes): print(line) ... Traceback (most recent call last): File "", line 1, in AttributeError: 'HTTPResponse' object has no attribute 'readinto'

See also issue 4996.

It is entirely possible I have simply failed to understand the proper way to do this, so at very least an example on the urllib documentation would be a welcome improvement. However it is my impression that the file-like object from urllib is not file-like enough.