This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2011年12月06日 16:44 by maubp, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Messages (3) | |||
|---|---|---|---|
| msg148928 - (view) | Author: Peter (maubp) | Date: 2011年12月06日 16:44 | |
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.
|
|||
| msg148935 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2011年12月06日 19:57 | |
For the record, readinto is handled in issue 13464. |
|||
| msg181503 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2013年02月06日 08:54 | |
Issue12591 added support of raw streams without read1() in io.TextIOWrapper(). |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:24 | admin | set | github: 57750 |
| 2013年02月06日 08:54:33 | serhiy.storchaka | set | status: open -> closed nosy: + serhiy.storchaka messages: + msg181503 resolution: out of date stage: needs patch -> resolved |
| 2011年12月06日 19:57:23 | pitrou | set | type: behavior components: + Library (Lib) versions: + Python 3.3 nosy: + pitrou messages: + msg148935 stage: needs patch |
| 2011年12月06日 16:44:59 | maubp | create | |