##// END OF EJS Templates
moved getdefaultencoding from text to py3compat
Brandon Parsons -
Show More
@@ -439,7 +439,7 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
439 439 """decode argv if bytes, using stin.encoding, falling back on default enc"""
440 440 uargv = []
441 441 if enc is None:
442 enc = text.getdefaultencoding()
442 enc = py3compat.getdefaultencoding()
443 443 for arg in argv:
444 444 if not isinstance(arg, unicode):
445 445 # only decode if not already decoded
@@ -603,7 +603,7 b' class ArgParseConfigLoader(CommandLineConfigLoader):'
603 603 def _parse_args(self, args):
604 604 """self.parser->self.parsed_data"""
605 605 # decode sys.argv to support unicode command-line options
606 enc = text.getdefaultencoding()
606 enc = py3compat.getdefaultencoding()
607 607 uargs = [py3compat.cast_unicode(a, enc) for a in args]
608 608 self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)
609 609
@@ -35,7 +35,7 b' class Macro(object):'
35 35 lines.append(line)
36 36 code = "\n".join(lines)
37 37 if isinstance(code, bytes):
38 code = code.decode(enc or sys.getdefaultencoding())
38 code = code.decode(enc or py3compat.getdefaultencoding())
39 39 self.value = code + '\n'
40 40
41 41 def __str__(self):
@@ -949,7 +949,7 b' Currently the magic system has the following functions:\\n"""'
949 949 try:
950 950 vstr = str(var)
951 951 except UnicodeEncodeError:
952 vstr = unicode(var).encode(sys.getdefaultencoding(),
952 vstr = unicode(var).encode(py3compat.getdefaultencoding(),
953 953 'backslashreplace')
954 954 vstr = vstr.replace('\n','\\n')
955 955 if len(vstr) < 50:
@@ -52,7 +52,7 b' def test_cache():'
52 52 def setUp():
53 53 # Check we're in a proper Python 2 environment (some imports, such
54 54 # as GTK, can change the default encoding, which can hide bugs.)
55 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
55 nt.assert_equal(py3compat.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
56 56
57 57 def test_cache_unicode():
58 58 cp = compilerop.CachingCompiler()
@@ -23,7 +23,7 b' from IPython.core.history import HistoryManager, extract_hist_ranges'
23 23 from IPython.utils import py3compat
24 24
25 25 def setUp():
26 nt.assert_equal(sys.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
26 nt.assert_equal(py3compat.getdefaultencoding(), "utf-8" if py3compat.PY3 else "ascii")
27 27
28 28 def test_history():
29 29 ip = get_ipython()
@@ -128,9 +128,9 b' if not _PY3K:'
128 128 return unicode(obj)
129 129 # Else encode it... but how? There are many choices... :)
130 130 # Replace unprintables with escape codes?
131 #return unicode(obj).encode(sys.getdefaultencoding(), 'backslashreplace_errors')
131 #return unicode(obj).encode(py3compat.getdefaultencoding(), 'backslashreplace_errors')
132 132 # Replace unprintables with question marks?
133 #return unicode(obj).encode(sys.getdefaultencoding(), 'replace')
133 #return unicode(obj).encode(py3compat.getdefaultencoding(), 'replace')
134 134 # ...
135 135 else:
136 136 _ustr = str
@@ -42,7 +42,7 b' except ImportError:'
42 42
43 43 from IPython.config.loader import Config
44 44 from IPython.utils.process import find_cmd, getoutputerror
45 from IPython.utils.text import list_strings, getdefaultencoding
45 from IPython.utils.text import list_strings
46 46 from IPython.utils.io import temp_pyfile, Tee
47 47 from IPython.utils import py3compat
48 48
@@ -322,7 +322,7 b' else:'
322 322 # so we need a class that can handle both.
323 323 class MyStringIO(StringIO):
324 324 def write(self, s):
325 s = py3compat.cast_unicode(s, encoding=getdefaultencoding())
325 s = py3compat.cast_unicode(s, encoding=py3compat.getdefaultencoding())
326 326 super(MyStringIO, self).write(s)
327 327
328 328 notprinted_msg = """Did not find {0!r} in printed output (on {1}):
@@ -128,7 +128,7 b' class ProcessHandler(object):'
128 128 int : child's exitstatus
129 129 """
130 130 # Get likely encoding for the output.
131 enc = text.getdefaultencoding()
131 enc = py3compat.getdefaultencoding()
132 132
133 133 # Patterns to match on the output, for pexpect. We read input and
134 134 # allow either a short timeout or EOF
@@ -94,7 +94,7 b' def _find_cmd(cmd):'
94 94
95 95 def _system_body(p):
96 96 """Callback for _system."""
97 enc = text.getdefaultencoding()
97 enc = py3compat.getdefaultencoding()
98 98 for line in read_no_interrupt(p.stdout).splitlines():
99 99 line = line.decode(enc, 'replace')
100 100 print(line, file=sys.stdout)
@@ -14,6 +14,7 b' from __future__ import print_function'
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 import os
17 18 import sys
18 19 import tempfile
19 20
@@ -135,7 +135,7 b' def json_clean(obj):'
135 135 return obj
136 136
137 137 if isinstance(obj, bytes):
138 return obj.decode(text.getdefaultencoding(), 'replace')
138 return obj.decode(py3compat.getdefaultencoding(), 'replace')
139 139
140 140 if isinstance(obj, container_to_list) or (
141 141 hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)):
@@ -5,6 +5,7 b' import functools'
5 5 import sys
6 6 import re
7 7 import types
8 import locale
8 9
9 10 orig_open = open
10 11
@@ -18,12 +19,35 b' def get_stream_enc(stream, default=None):'
18 19 else:
19 20 return stream.encoding
20 21
22 # Less conservative replacement for sys.getdefaultencoding, that will try
23 # to match the environment.
24 # Defined here as central function, so if we find better choices, we
25 # won't need to make changes all over IPython.
26 def getdefaultencoding():
27 """Return IPython's guess for the default encoding for bytes as text.
28
29 Asks for stdin.encoding first, to match the calling Terminal, but that
30 is often None for subprocesses. Fall back on locale.getpreferredencoding()
31 which should be a sensible platform default (that respects LANG environment),
32 and finally to sys.getdefaultencoding() which is the most conservative option,
33 and usually ASCII.
34 """
35 enc = get_stream_enc(sys.stdin)
36 if not enc or enc=='ascii':
37 try:
38 # There are reports of getpreferredencoding raising errors
39 # in some cases, which may well be fixed, but let's be conservative here.
40 enc = locale.getpreferredencoding()
41 except Exception:
42 pass
43 return enc or sys.getdefaultencoding()
44
21 45 def decode(s, encoding=None):
22 encoding = get_stream_enc(sys.stdin, encoding) or sys.getdefaultencoding()
46 encoding = get_stream_enc(sys.stdin, encoding) or getdefaultencoding()
23 47 return s.decode(encoding, "replace")
24 48
25 49 def encode(u, encoding=None):
26 encoding = get_stream_enc(sys.stdin, encoding) or sys.getdefaultencoding()
50 encoding = get_stream_enc(sys.stdin, encoding) or getdefaultencoding()
27 51 return u.encode(encoding, "replace")
28 52
29 53
@@ -16,7 +16,6 b' Utilities for working with strings and text.'
16 16
17 17 import __main__
18 18
19 import locale
20 19 import os
21 20 import re
22 21 import shutil
@@ -34,29 +33,6 b' from IPython.utils.data import flatten'
34 33 # Code
35 34 #-----------------------------------------------------------------------------
36 35
37 # Less conservative replacement for sys.getdefaultencoding, that will try
38 # to match the environment.
39 # Defined here as central function, so if we find better choices, we
40 # won't need to make changes all over IPython.
41 def getdefaultencoding():
42 """Return IPython's guess for the default encoding for bytes as text.
43
44 Asks for stdin.encoding first, to match the calling Terminal, but that
45 is often None for subprocesses. Fall back on locale.getpreferredencoding()
46 which should be a sensible platform default (that respects LANG environment),
47 and finally to sys.getdefaultencoding() which is the most conservative option,
48 and usually ASCII.
49 """
50 enc = py3compat.get_stream_enc(sys.stdin)
51 if not enc or enc=='ascii':
52 try:
53 # There are reports of getpreferredencoding raising errors
54 # in some cases, which may well be fixed, but let's be conservative here.
55 enc = locale.getpreferredencoding()
56 except Exception:
57 pass
58 return enc or sys.getdefaultencoding()
59
60 36 def unquote_ends(istr):
61 37 """Remove a single pair of quotes from the endpoints of a string."""
62 38
@@ -4,7 +4,7 b' from io import StringIO'
4 4
5 5 from session import extract_header, Message
6 6
7 from IPython.utils import io, text
7 from IPython.utils import io, text, py3compat
8 8
9 9 #-----------------------------------------------------------------------------
10 10 # Globals
@@ -69,7 +69,7 b' class OutStream(object):'
69 69 else:
70 70 # Make sure that we're handling unicode
71 71 if not isinstance(string, unicode):
72 enc = text.getdefaultencoding()
72 enc = py3compat.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