[New-bugs-announce] [issue13541] HTTPResponse (urllib) has no attribute read1 needed for TextIOWrapper

Peter report at bugs.python.org
Tue Dec 6 17:44:59 CET 2011


New submission from Peter <p.j.a.cock at googlemail.com>:

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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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.

----------
messages: 148928
nosy: maubp
priority: normal
severity: normal
status: open
title: HTTPResponse (urllib) has no attribute read1 needed for TextIOWrapper
versions: Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13541>
_______________________________________


More information about the New-bugs-announce mailing list