gh-91156: Use locale.getencoding() instead of getpreferredencoding#91732
Conversation
instead of locale.getpreferredencoding()
| # Retrieve the default encoding for the xml declaration | ||
| import locale | ||
| declared_encoding = locale.getpreferredencoding() | ||
| declared_encoding = locale.getpreferredencoding(False) |
There was a problem hiding this comment.
I keep using getpreferredencoding(False) here because this function should use UTF-8 in UTF-8 mode.
There was a problem hiding this comment.
Would you mind to write a separated PR for this change? It's not directly unrelated, and you should document it with a separated NEWS entry.
| self.addCleanup(setattr, locale, 'getpreferredencoding', | ||
| getpreferredencoding) | ||
| locale.getpreferredencoding = lambda: 'ascii' | ||
|
|
There was a problem hiding this comment.
This hack doesn't work for most cases.
There was a problem hiding this comment.
Please remove import locale at the top.
This code is correct. I don't think that the locale encoding is still used: MimeType.read() calls open(filename, encoding="utf-8") since Python 3.3: commit 82ac9bc.
| # Retrieve the default encoding for the xml declaration | ||
| import locale | ||
| declared_encoding = locale.getpreferredencoding() | ||
| declared_encoding = locale.getpreferredencoding(False) |
There was a problem hiding this comment.
Would you mind to write a separated PR for this change? It's not directly unrelated, and you should document it with a separated NEWS entry.
Co-authored-by: Victor Stinner <vstinner@python.org>
| @@ -37,7 +37,7 @@ Linux and the BSD variants of Unix. | |||
|
|
|||
There was a problem hiding this comment.
Remark unrelated to your PR.
you have to call:func:
locale.setlocalein the application
This is wrong: Python now always call setlocale(LC_CTYPE, "") at startup.
Calling locale.setlocale(locale.LC_ALL, '') is no longer needed.
Moreover, curses likely use mbstowcs() and wcstombs() functions, rather than nl_langinfo() (nl_langinfo(CODESET)?).
There was a problem hiding this comment.
OK. Let's remove this note.
| @@ -924,7 +924,7 @@ the following methods and attributes: | |||
| Encoding used to encode method arguments (Unicode strings and characters). | |||
| The encoding attribute is inherited from the parent window when a subwindow | |||
| is created, for example with :meth:`window.subwin`. By default, the locale | |||
There was a problem hiding this comment.
Would you mind to replace "locale encoding" with "current locale encoding"? Just to remind that it can be changed at runtime.
For example, the encoding used by the readline module is the currrent encoding, the encoding is not stored anywere. Its C code uses PyUnicode_EncodeLocale() and PyUnicode_DecodeLocale(): current LC_CTYPE locale encoding.
|
|
||
| def test_getencoding(self): | ||
| # Invoke getencoding to make sure it does not cause exceptions. | ||
| enc = locale.getencoding() |
There was a problem hiding this comment.
I suggest to test the type: add self.assertIsInstance(enc, str).
Maybe also ensure that the string is not empty? add self.assertNotEqual(enc, "").
| # Invoke getencoding to make sure it does not cause exceptions. | ||
| enc = locale.getencoding() | ||
| # make sure it is valid | ||
| codecs.lookup(enc) |
There was a problem hiding this comment.
_PyUnicode_InitEncodings() fails it config.filesystem_encoding or config.stdio_encoding is not known by codecs.lookup(name). So this call should not fail.
If tomorrow this test fails, I suggest to remove it and only check that the string is non-empty.
There was a problem hiding this comment.
When UTF-8 mode is enabled, both of stdio encoding and filesystem encoding are UTF-8, not locale encoding.
| self.addCleanup(setattr, locale, 'getpreferredencoding', | ||
| getpreferredencoding) | ||
| locale.getpreferredencoding = lambda: 'ascii' | ||
|
|
There was a problem hiding this comment.
Please remove import locale at the top.
This code is correct. I don't think that the locale encoding is still used: MimeType.read() calls open(filename, encoding="utf-8") since Python 3.3: commit 82ac9bc.
Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner
left a comment
There was a problem hiding this comment.
LGTM. It seems like this PR fix multiple bugs in the doc and mojibake issues when the UTF-8 mode is used, nice!
|
patchcheck failed on the Azure Ubuntu job: |
No description provided.