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.

Author ajaksu2
Recipients ajaksu2
Date 2008-12-23.21:44:05
SpamBayes Score 4.41869e-14
Marked as misclassified No
Message-id <>
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[1]. 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.

[Executive summary]
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.
[/Executive summary]
Date User Action Args
2008-12-23 21:44:15ajaksu2setrecipients: + ajaksu2
2008-12-23 21:44:14ajaksu2setmessageid: <>
2008-12-23 21:44:13ajaksu2linkissue4733 messages
2008-12-23 21:44:11ajaksu2create