New submission from Daniel Diniz <aja...@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[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. [1] http://groups.google.com/group/comp.lang.python/browse_thread/thread/b88239182f368505 [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] ---------- components: Library (Lib) files: urlopen_text.diff keywords: patch messages: 78250 nosy: ajaksu2 severity: normal status: open 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 <rep...@bugs.python.org> <http://bugs.python.org/issue4733> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com