Skip to content

Conversation

@seanmccully
Copy link

Default behavior of locale.getpreferredencoding doesn't return result.

@seanmccully seanmccully changed the title locale.getpreferredencoding doesn't return result locale.getpreferredencoding doesn't return result Issue 30409 May 20, 2017
@AraHaan
Copy link
Contributor

AraHaan commented May 20, 2017

you might need to rename this PR to bpo-30409: locale.getpreferredencoding doesn't return result to fix the issue number part on this PR.

@seanmccully seanmccully changed the title locale.getpreferredencoding doesn't return result Issue 30409 PR to 30409: locale.getpreferredencoding doesn't return result May 20, 2017
@seanmccully seanmccully changed the title PR to 30409: locale.getpreferredencoding doesn't return result bpo-30409: locale.getpreferredencoding doesn't return result May 20, 2017
@vstinner vstinner changed the title bpo-30409: locale.getpreferredencoding doesn't return result [2.7] bpo-30409: locale.getpreferredencoding doesn't return result May 20, 2017
Lib/locale.py Outdated
Copy link
Member

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).

Copy link
Contributor

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Author

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?

Copy link
Member

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.

@seanmccully
Copy link
Author

Not sure entirely if there's any reason for calling setlocale after nl_langinfo?

@AraHaan
Copy link
Contributor

AraHaan commented May 20, 2017

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
 
 
 ### Database

I uploaded the diff to bpo too.

@seanmccully seanmccully force-pushed the 2.7 branch 2 times, most recently from 6f0d12b to 190ab84 Compare May 20, 2017 06:44
Lib/locale.py Outdated
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

@vstinner vstinner left a 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.

@vstinner
Copy link
Member

By the way, thank your for going even further than just adding the missing "return result" and for having merged the duplicate code! :-)

@seanmccully
Copy link
Author

No problem

@vstinner vstinner merged commit cef8b17 into python:2.7 May 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants