-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
[2.7] bpo-30409: locale.getpreferredencoding doesn't return result #1672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
you might need to rename this PR to |
Lib/locale.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove "return result" from the other if block (2 lives above).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean remove line 630?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
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
190ab84
Compare
Lib/locale.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is useless. Please remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
vstinner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
By the way, thank your for going even further than just adding the missing "return result" and for having merged the duplicate code! :-) |
|
No problem |
Default behavior of locale.getpreferredencoding doesn't return result.