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