##// END OF EJS Templates
add text.getdefaultencoding() for central default encoding guess...
MinRK -
Show More
@@ -24,7 +24,7 import sys
24 24
25 25 from IPython.external import argparse
26 26 from IPython.utils.path import filefind, get_ipython_dir
27 from IPython.utils import py3compat, warn
27 from IPython.utils import py3compat, text, warn
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Exceptions
@@ -425,7 +425,7 class KeyValueConfigLoader(CommandLineConfigLoader):
425 425 """decode argv if bytes, using stin.encoding, falling back on default enc"""
426 426 uargv = []
427 427 if enc is None:
428 enc = sys.stdin.encoding or sys.getdefaultencoding()
428 enc = text.getdefaultencoding()
429 429 for arg in argv:
430 430 if not isinstance(arg, unicode):
431 431 # only decode if not already decoded
@@ -586,7 +586,8 class ArgParseConfigLoader(CommandLineConfigLoader):
586 586 def _parse_args(self, args):
587 587 """self.parser->self.parsed_data"""
588 588 # decode sys.argv to support unicode command-line options
589 uargs = [py3compat.cast_unicode(a) for a in args]
589 enc = text.getdefaultencoding()
590 uargs = [py3compat.cast_unicode(a, enc) for a in args]
590 591 self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)
591 592
592 593 def _convert_to_config(self):
@@ -23,6 +23,7 from subprocess import STDOUT
23 23
24 24 # our own imports
25 25 from ._process_common import read_no_interrupt, process_handler
26 from . import text
26 27
27 28 #-----------------------------------------------------------------------------
28 29 # Function definitions
@@ -88,7 +89,7 def _find_cmd(cmd):
88 89
89 90 def _system_body(p):
90 91 """Callback for _system."""
91 enc = sys.stdin.encoding or sys.getdefaultencoding()
92 enc = text.getdefaultencoding()
92 93 for line in read_no_interrupt(p.stdout).splitlines():
93 94 line = line.decode(enc, 'replace')
94 95 print(line, file=sys.stdout)
@@ -17,6 +17,7 import types
17 17 from datetime import datetime
18 18
19 19 from IPython.utils import py3compat
20 from IPython.utils import text
20 21 next_attr_name = '__next__' if py3compat.PY3 else 'next'
21 22
22 23 #-----------------------------------------------------------------------------
@@ -134,7 +135,7 def json_clean(obj):
134 135 return obj
135 136
136 137 if isinstance(obj, bytes):
137 return obj.decode(sys.getdefaultencoding(), 'replace')
138 return obj.decode(text.getdefaultencoding(), 'replace')
138 139
139 140 if isinstance(obj, container_to_list) or (
140 141 hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)):
@@ -16,9 +16,11 Utilities for working with strings and text.
16 16
17 17 import __main__
18 18
19 import locale
19 20 import os
20 21 import re
21 22 import shutil
23 import sys
22 24 import textwrap
23 25 from string import Formatter
24 26
@@ -31,6 +33,28 from IPython.utils.data import flatten
31 33 # Code
32 34 #-----------------------------------------------------------------------------
33 35
36 # Less conservative replacement for sys.getdefaultencoding, that will try
37 # to match the environment.
38 # Defined here as central function, so if we find better choices, we
39 # won't need to make changes all over IPython.
40 def getdefaultencoding():
41 """Return IPython's guess for the default encoding for bytes as text.
42
43 Asks for stdin.encoding first, to match the calling Terminal, but that
44 is often None for subprocesses. Fall back on locale.getpreferredencoding()
45 which should be a sensible platform default (that respects LANG environment),
46 and finally to sys.getdefaultencoding() which is the most conservative option,
47 and usually ASCII.
48 """
49 enc = sys.stdin.encoding
50 if not enc:
51 try:
52 # There are reports of getpreferredencoding raising errors
53 # in some cases, which may well be fixed, but let's be conservative here.
54 enc = locale.getpreferredencoding(False)
55 except Exception:
56 pass
57 return enc or sys.getdefaultencoding()
34 58
35 59 def unquote_ends(istr):
36 60 """Remove a single pair of quotes from the endpoints of a string."""
@@ -4,7 +4,7 from io import StringIO
4 4
5 5 from session import extract_header, Message
6 6
7 from IPython.utils import io
7 from IPython.utils import io, text
8 8
9 9 #-----------------------------------------------------------------------------
10 10 # Globals
@@ -69,7 +69,7 class OutStream(object):
69 69 else:
70 70 # Make sure that we're handling unicode
71 71 if not isinstance(string, unicode):
72 enc = sys.stdin.encoding or sys.getdefaultencoding()
72 enc = text.getdefaultencoding()
73 73 string = string.decode(enc, 'replace')
74 74
75 75 self._buffer.write(string)
General Comments 0
You need to be logged in to leave comments. Login now