Skip to content

Commit 95ad4ef

Browse files
author
y-p
committed
Merge pull request #3409 from y-p/GH3360c
BUG: don't rely on sys.getdefaultencoding if we don't need to
2 parents 5bf8adb + 44a400d commit 95ad4ef

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pandas/core/format.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ def _encode_diff_func():
165165
encoding = get_option("display.encoding")
166166

167167
def _encode_diff(x):
168-
return len(x) - len(x.decode(encoding))
168+
if not isinstance(x,unicode):
169+
return len(x) - len(x.decode(encoding))
170+
return 0
169171

170172
return _encode_diff
171173

@@ -1639,13 +1641,14 @@ def reset_printoptions():
16391641
FutureWarning)
16401642
reset_option("^display\.")
16411643

1642-
1644+
_initial_defencoding = None
16431645
def detect_console_encoding():
16441646
"""
16451647
Try to find the most capable encoding supported by the console.
16461648
slighly modified from the way IPython handles the same issue.
16471649
"""
16481650
import locale
1651+
global _initial_defencoding
16491652

16501653
encoding = None
16511654
try:
@@ -1662,6 +1665,11 @@ def detect_console_encoding():
16621665
if not encoding or 'ascii' in encoding.lower(): # when all else fails. this will usually be "ascii"
16631666
encoding = sys.getdefaultencoding()
16641667

1668+
# GH3360, save the reported defencoding at import time
1669+
# MPL backends may change it. Make available for debugging.
1670+
if not _initial_defencoding:
1671+
_initial_defencoding = sys.getdefaultencoding()
1672+
16651673
return encoding
16661674

16671675

0 commit comments

Comments
 (0)