Skip to content
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

locale.getdefaultlocale() fails on OS X #5019

Merged
merged 2 commits into from Sep 5, 2015
Merged

Conversation

jheinen
Copy link
Contributor

@jheinen jheinen commented Sep 3, 2015

For any reason locale.getdefaultlocale() fails on OS X (returns None), which subsequently produces errors and makes the matplotlib module unusable. Checking for a None result and then setting the default encoding to "utf-8" seems to help.

For any reason ``locale.getdefaultlocale()`` fails on OS X (returns None), which subsequently produces errors and makes the ``matplotlib`` module unusable. Checking for a ``None`` result and then setting the default encoding to ``"utf-8"`` seems to help.
@@ -992,6 +992,8 @@ def _open_file_or_url(fname):
else:
fname = os.path.expanduser(fname)
encoding = locale.getdefaultlocale()[1]
if encoding == None:
Copy link
Member

Choose a reason for hiding this comment

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

Don't do equality when testing against None. Do if encoding is None:.

@WeatherGod
Copy link
Member

@mdehoon, does this fallback make sense to you? I don't know about system encodings on Macs.

@jenshnielsen
Copy link
Member

On my mac locale.getdefaultlocale() returns ('en_GB', 'UTF-8') so UTF-9 should be fine but there might be other OSs where this fails?

@mdehoon
Copy link
Contributor

mdehoon commented Sep 4, 2015

No idea, sorry.

@jheinen
Copy link
Contributor Author

jheinen commented Sep 4, 2015

According to the Python documentation, the result of local.getdefaultlocale() can be None.
Excerpt from https://docs.python.org/2/library/locale.html:

Except for the code 'C', the language code corresponds to RFC 1766. language code and encoding may be None if their values cannot be determined.

So, matplotlib should check the result and fallback to a default encoding. This is a pragmatic solution. and certainly better than changing the locale or expecting an environment setting in the user's shell (LANGUAGE, LC_ALL, LANG, ...).

@tacaswell tacaswell added this to the next point release milestone Sep 4, 2015
@tacaswell
Copy link
Member

@jheinen Thanks, you short cut my research on this which was going through https://bugs.python.org/issue18378 !

I am 👍 on merging this, but as the bug was reported specifically against OSX, one of the OSX-based devs should merge this.

@efiring
Copy link
Member

efiring commented Sep 4, 2015

I am not an OSX expert by any means, but if no one else comments or merges it, I will do so within 24 hours. I don't see how it could hurt anything.

@jkseppan
Copy link
Member

jkseppan commented Sep 4, 2015

On an old Mac I have that is running OS X 10.8, locale.getdefaultlocale() returns (None, None). TextEdit defaults to UTF-8 on that Mac. Some discussion threads I find on the Internet suggest that this default was new with 10.7, and 10.6 might have used single-byte character sets by default.

I think Microsoft Office 2008 still used Mac Roman by default, so there's at least a theoretical risk if you edited your matplotlibrc in Word 2008 and used non-ASCII characters for some values.

But in any case, if passing encoding=None is problematic for UTF-8 files, my guess is that that affects a larger number of users than the inverse problem of passing encoding='UTF-8' when the file is actually Mac Roman or ISO 8859-1. I think it would be nice to give a meaningful error message to the user, though.

@jkseppan
Copy link
Member

jkseppan commented Sep 4, 2015

Plenty of software is going to fail strangely if your locale doesn't match your files. I guess utf-8 is a reasonable default in 2015, but I also think it's the user's or sysadmin's responsibility to fix the configuration.

@jkseppan
Copy link
Member

jkseppan commented Sep 4, 2015

I have a suggestion for a slightly better error message at jheinen#1.

@matthew-brett
Copy link
Contributor

On a couple of 10.6 Macs I have:

Mac 1: (None, None) for Pythons 2.5, 2.5, ('en_US', 'UTF-8') for Python 3.4.1
Mac 2: (None, None) for Pythons 2.6 through 3.4.

@jkseppan
Copy link
Member

jkseppan commented Sep 5, 2015

I think this is useful as is, so I'll merge it, and make a separate PR about the error message.

jkseppan added a commit that referenced this pull request Sep 5, 2015
Default to UTF-8 if locale.getdefaultlocale() returns None for the encoding
@jkseppan jkseppan merged commit 27606b7 into matplotlib:master Sep 5, 2015
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.

None yet

8 participants