Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b19380

Browse files
committed
fixed a locale encoding bug described in sf bug#1744154
svn path=/trunk/matplotlib/; revision=3420
1 parent b6d1d2f commit 0b19380

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/matplotlib/cbook.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from the Python Cookbook -- hence the name cbook
44
"""
55
from __future__ import generators
6-
import re, os, errno, sys, StringIO, traceback
6+
import re, os, errno, sys, StringIO, traceback, locale
77
import time, datetime
88
import numpy as npy
99

@@ -14,6 +14,13 @@
1414
major, minor1, minor2, s, tmp = sys.version_info
1515

1616

17+
# on some systems, locale.getpreferredencoding returns None, which can break unicode
18+
preferredencoding = locale.getpreferredencoding()
19+
20+
def unicode_safe(s):
21+
if preferredencoding is None: return unicode(s)
22+
else: return unicode(s, preferredencoding)
23+
1724
class converter:
1825
"""
1926
Base class for handling string -> python type with support for

lib/matplotlib/dates.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
from dateutil.relativedelta import relativedelta
9393
import dateutil.parser
9494

95+
from matplotlib import cbook
96+
9597
UTC = timezone('UTC')
9698

9799
def _get_rc_timezone():
@@ -295,7 +297,7 @@ def strftime(self, dt, fmt):
295297
fmt = self.illegal_s.sub(r"\1", fmt)
296298
fmt = fmt.replace("%s", "s")
297299
if dt.year > 1900:
298-
return unicode(dt.strftime(fmt), locale.getpreferredencoding())
300+
return cbook.unicode_safe(dt.strftime(fmt))
299301

300302
year = dt.year
301303
# For every non-leap year century, advance by
@@ -323,7 +325,9 @@ def strftime(self, dt, fmt):
323325
for site in sites:
324326
s = s[:site] + syear + s[site+4:]
325327

326-
return unicode(s, locale.getpreferredencoding())
328+
return cbook.unicode_safe(s)
329+
330+
327331

328332
class IndexDateFormatter(Formatter):
329333
"""
@@ -347,7 +351,7 @@ def __call__(self, x, pos=0):
347351

348352
dt = num2date(self.t[ind], self.tz)
349353

350-
return unicode(dt.strftime(self.fmt), locale.getpreferredencoding())
354+
return cbook.unicode_safe(dt.strftime(self.fmt))
351355

352356

353357
class AutoDateFormatter(Formatter):

0 commit comments

Comments
 (0)