Uh oh!
There was an error while loading. Please reload this page.
[2.7] bpo-30409: locale.getpreferredencoding doesn't return result - #1672
Conversation
you might need to rename this PR to |
There was a problem hiding this comment.
Please remove "return result" from the other if block (2 lives above).
There was a problem hiding this comment.
Why? This function would still have inconsistent behavior, if CODESET is set and if on windows it is still returning a value. Are suggesting that this function should not return anything?
There was a problem hiding this comment.
I propose to replace "if ...: ... return result else: ... return result" with "if ...: ... else ...; return result", just factorize the code which is in common. That's all.
seanmccully
commented
May 20, 2017
Not sure entirely if there's any reason for calling setlocale after nl_langinfo? |
This was going to be my suggested change before the force push: diff --git a/Lib/locale.py b/Lib/locale.py
index be34c5ddcd..e3674aba48 100644
--- a/Lib/locale.py+++ b/Lib/locale.py@@ -610,29 +610,24 @@ else:
def getpreferredencoding(do_setlocale = True):
"""Return the charset that the user is likely using,
according to the system configuration."""
+ result = nl_langinfo(CODESET)+ if not result and sys.platform == 'darwin':+ # nl_langinfo can return an empty string+ # when the setting has an invalid value.+ # Default to UTF-8 in that case because+ # UTF-8 is the default charset on OSX and+ # returning nothing will crash the+ # interpreter.+ result = 'UTF-8'
if do_setlocale:
oldloc = setlocale(LC_CTYPE)
try:
setlocale(LC_CTYPE, "")
except Error:
pass
- result = nl_langinfo(CODESET)- if not result and sys.platform == 'darwin':- # nl_langinfo can return an empty string- # when the setting has an invalid value.- # Default to UTF-8 in that case because- # UTF-8 is the default charset on OSX and- # returning nothing will crash the- # interpreter.- result = 'UTF-8'
setlocale(LC_CTYPE, oldloc)
- return result- else:- result = nl_langinfo(CODESET)- if not result and sys.platform == 'darwin':- # See above for explanation- result = 'UTF-8'+ return result
### DatabaseI uploaded the diff to bpo too. |
6f0d12b to
190ab84CompareThere was a problem hiding this comment.
This line is useless. Please remove it.
vstinner
left a comment
There was a problem hiding this comment.
LGTM...
... Oh, I'm really sorry but ask you one more thing, but I missed that it is your first contribution. Pleeeease, can you add yourself to Misc/ACKS? See the header for name order.
vstinner
commented
May 20, 2017
By the way, thank your for going even further than just adding the missing "return result" and for having merged the duplicate code! :-) |
seanmccully
commented
May 20, 2017
No problem |
Default behavior of locale.getpreferredencoding doesn't return result.