Skip to content

Commit

Permalink
Supports locale-specified encoding for rcfile.
Browse files Browse the repository at this point in the history
This commit fixes issue #1317.  The locale for the encoding is obtained from
the Python `locale` module, which can be specified by the `LANG` environment
variable.

This was tested by adding a test rc file encoded in UTF-32-BE, and testing that
it decoded correctly.
  • Loading branch information
Masud Rahman committed Sep 27, 2014
1 parent a348d7d commit 4374d8e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/matplotlib/__init__.py
Expand Up @@ -168,6 +168,8 @@ def _forward_ilshift(self, other):
except ImportError:
from urllib2 import urlopen

import io
import locale
import os
import re
import tempfile
Expand Down Expand Up @@ -947,7 +949,7 @@ def _open_file_or_url(fname):
yield _url_lines(f)
f.close()
else:
with open(fname) as f:
with io.open(fname, encoding=locale.getdefaultlocale()[1]) as f:
yield f


Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_rcparams.py
Expand Up @@ -3,6 +3,7 @@

import six

import io
import os
import sys
import warnings
Expand Down Expand Up @@ -140,6 +141,18 @@ def test_Bug_2543_newer_python():
with mpl.rc_context():
mpl.rcParams['svg.fonttype'] = True

def test_Issue_1713():
utf32_be = os.path.join(os.path.dirname(__file__),
'test_utf32_be_rcparams.rc')
old_lang = os.environ.get('LANG', None)
os.environ['LANG'] = 'en_US.UTF-32-BE'
rc = mpl.rc_params_from_file(utf32_be, True)
if old_lang:
os.environ['LANG'] = old_lang
else:
del os.environ['LANG']
print(rc.get('timezone') == 'UTC')

if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
Binary file added lib/matplotlib/tests/test_utf32_be_rcparams.rc
Binary file not shown.
3 changes: 2 additions & 1 deletion setupext.py
Expand Up @@ -680,7 +680,8 @@ def get_package_data(self):
baseline_images +
[
'tests/mpltest.ttf',
'tests/test_rcparams.rc'
'tests/test_rcparams.rc',
'tests/test_utf32_be_rcparams.rc',
]}

def get_install_requires(self):
Expand Down

0 comments on commit 4374d8e

Please sign in to comment.