Message148928
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. |
|
Date |
User |
Action |
Args |
2011-12-06 16:44:59 | maubp | set | recipients:
+ maubp |
2011-12-06 16:44:59 | maubp | set | messageid: <1323189899.67.0.308842457665.issue13541@psf.upfronthosting.co.za> |
2011-12-06 16:44:59 | maubp | link | issue13541 messages |
2011-12-06 16:44:58 | maubp | create | |
|