[New-bugs-announce] [issue4733] Add a "decode to declared encoding" version of urlopen to urllib
report at bugs.python.org
Tue Dec 23 22:44:14 CET 2008
New submission from Daniel Diniz <ajaksu at gmail.com>:
This patch adds a version of urlopen that uses available encoding
information to return strings instead of bytes.
The main goal is to provide a shortcut for users that don't want to
handle the decoding in the easy cases. One added benefit it that the
failures of such a function would be make it clear why 2.x style "str is
either bytes or text" is flawed for network IO.
Currently, charset detection simply uses addinfourl.get_charset(), but
optionally checking for HTTP headers might be more robust.
Glenn G. Chappell wrote:
"2to3 doesn't catch it, and, in any case, why should read() return
bytes, not string?"
Carl Banks wrote:
It returns bytes because it doesn't know what encoding to use.
HOWEVER... [...] It's reasonable that IF a url request's
"Content-type" is text, and/or the "Content-encoding" is given, for
urllib to have an option to automatically decode and return a string
instead of bytes.
Christian Heimes wrote:
There is no generic and simple way to detect the encoding of a
remote site. Sometimes the encoding is mentioned in the HTTP header,
sometimes it's embedded in the <head> section of the HTML document.
Daniel Diniz wrote:
[... A] "decode to declared HTTP header encoding" version of urlopen
could be useful to give some users the output they want (text from
network io) or to make it clear why bytes is the safe way.
components: Library (Lib)
title: Add a "decode to declared encoding" version of urlopen to urllib
type: feature request
versions: Python 3.1
Added file: http://bugs.python.org/file12437/urlopen_text.diff
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce