You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improper reliance on local encoding of remote resource #1546
This is in response to issue #1402 which was addressed with commit 1a32c50.
The issue there was regarding platforms where UTF-8 is not the default encoding, for example, Windows, where cp1252 may be the default.
This only occurs with python3 as only there is _binary_to_string_handle needed.
The solution to that issue was to add the following to _binary_to_string_handle:
+ try:
+ # If this is a network handle from urllib,
+ # the HTTP headers may tell us the encoding.
+ encoding = handle.headers.get_content_charset()
+ except AttributeError:
+ encoding = locale.getpreferredencoding(False)
+ wrapped = io.TextIOWrapper(io.BufferedReader(handle), encoding=encoding)
This has a number of problems:
encoding = handle.headers.get_content_charset() may return None rather than raise an AttributeError. In this instance, the call to io.TextIOWrapper with encoding=None will default to the system's default encoding, which on Windows may be cp1252, which is a revert back to the initial issue.
A reliance on locale.getpreferredencoding(False) is reverting to the default encoding of the user's system, which has no relation to the encoding of a remote resource. According to the HTTP protocol parameters from W3C:
When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP.
ISO-8859-1 is also known as latin-1. Therefore, when no encoding information is available, the default should revert to latin-1 and not the user's preferred encoding or the user's default encoding.
The default charset of ISO-8859-1 for text media types has been
removed; the default is now whatever the media type definition says.
Likewise, special treatment of ISO-8859-1 has been removed from the
Accept-Charset header field. (Section 3.1.1.3 and Section 5.3.3)
guess the encoding
a) sniff & test the encoding (Because sniffing may fail - processing needs error handling & sensible error message.)
b) assume UTF-8 encoding (Because this may fail - processing needs error handling & sensible error message.)
Step 2. a) and 2. b) are arbitrarily ordered. I don't know if there is a clear benefit to putting one or the other first in the control flow as long as the first failing triggers the other.
Sniffing is fiddly, we'd need to buffer what is typically read-once handle, so I prefer assuming URF-8 for online resources.
Can you test locally with something like this?:
try:
# If this is a network handle from urllib,# the HTTP headers may tell us the encoding.encoding=handle.headers.get_content_charset()
ifencodingisNone:
# Had a header, so should be an online resource, assume UTF-8encoding="utf8"exceptAttributeError:
# No header attribute, probably not an online resource, assume localencoding=locale.getpreferredencoding(False)
wrapped=io.TextIOWrapper(io.BufferedReader(handle), encoding=encoding)
Activity
fabianegli commentedon Jan 21, 2019
Assuming text transfers to be
ISO-8859-1a.k.a.latin-1encoded seems to be obsolete for HTTP 1.1 (contrary to HTTP 1.0).From https://tools.ietf.org/html/rfc7231
Appendix B. Changes from RFC 2616:peterjc commentedon Jan 21, 2019
We can add:
doing the same thing as the
AttributeErrorhandler.So what is the default encoding now if the headers don't say? Is
latin-1a better choice than the local system default?fabianegli commentedon Jan 21, 2019
Being used on a staggering 92.9% (as of 01/21/2019) of websites, the
UTF-8encoding permeates the web. See https://w3techs.com/technologies/details/en-utf8/all/all for current numbers. I think the following is the most reasonable and robust approach.Choose the first that holds true for documents downloaded from the web:
a) sniff & test the encoding (Because sniffing may fail - processing needs error handling & sensible error message.)
b) assume
UTF-8encoding (Because this may fail - processing needs error handling & sensible error message.)Step 2. a) and 2. b) are arbitrarily ordered. I don't know if there is a clear benefit to putting one or the other first in the control flow as long as the first failing triggers the other.
peterjc commentedon Jan 22, 2019
Sniffing is fiddly, we'd need to buffer what is typically read-once handle, so I prefer assuming URF-8 for online resources.
Can you test locally with something like this?: