##// END OF EJS Templates
saner default encoding mechanism
Brandon Parsons -
Show More
@@ -26,7 +26,7 b' import sys'
26 from IPython.external import argparse
26 from IPython.external import argparse
27 from IPython.utils.path import filefind, get_ipython_dir
27 from IPython.utils.path import filefind, get_ipython_dir
28 from IPython.utils import py3compat, text, warn
28 from IPython.utils import py3compat, text, warn
29 from IPython.utils.encoding import getdefaultencoding
29 from IPython.utils.encoding import DEFAULT_ENCODING
30
30
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 # Exceptions
32 # Exceptions
@@ -440,7 +440,7 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
440 """decode argv if bytes, using stin.encoding, falling back on default enc"""
440 """decode argv if bytes, using stin.encoding, falling back on default enc"""
441 uargv = []
441 uargv = []
442 if enc is None:
442 if enc is None:
443 enc = getdefaultencoding()
443 enc = DEFAULT_ENCODING
444 for arg in argv:
444 for arg in argv:
445 if not isinstance(arg, unicode):
445 if not isinstance(arg, unicode):
446 # only decode if not already decoded
446 # only decode if not already decoded
@@ -604,7 +604,7 b' class ArgParseConfigLoader(CommandLineConfigLoader):'
604 def _parse_args(self, args):
604 def _parse_args(self, args):
605 """self.parser->self.parsed_data"""
605 """self.parser->self.parsed_data"""
606 # decode sys.argv to support unicode command-line options
606 # decode sys.argv to support unicode command-line options
607 enc = getdefaultencoding()
607 enc = DEFAULT_ENCODING
608 uargs = [py3compat.cast_unicode(a, enc) for a in args]
608 uargs = [py3compat.cast_unicode(a, enc) for a in args]
609 self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)
609 self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)
610
610
@@ -11,7 +11,7 b' import re'
11 import sys
11 import sys
12
12
13 from IPython.utils import py3compat
13 from IPython.utils import py3compat
14 from IPython.utils.encoding import getdefaultencoding
14 from IPython.utils.encoding import DEFAULT_ENCODING
15
15
16 coding_declaration = re.compile(r"#\s*coding[:=]\s*([-\w.]+)")
16 coding_declaration = re.compile(r"#\s*coding[:=]\s*([-\w.]+)")
17
17
@@ -36,7 +36,7 b' class Macro(object):'
36 lines.append(line)
36 lines.append(line)
37 code = "\n".join(lines)
37 code = "\n".join(lines)
38 if isinstance(code, bytes):
38 if isinstance(code, bytes):
39 code = code.decode(enc or getdefaultencoding())
39 code = code.decode(enc or DEFAULT_ENCODING)
40 self.value = code + '\n'
40 self.value = code + '\n'
41
41
42 def __str__(self):
42 def __str__(self):
@@ -57,7 +57,7 b' from IPython.core.pylabtools import mpl_runner'
57 from IPython.testing.skipdoctest import skip_doctest
57 from IPython.testing.skipdoctest import skip_doctest
58 from IPython.utils import py3compat
58 from IPython.utils import py3compat
59 from IPython.utils import openpy
59 from IPython.utils import openpy
60 from IPython.utils.encoding import getdefaultencoding
60 from IPython.utils.encoding import DEFAULT_ENCODING
61 from IPython.utils.io import file_read, nlprint
61 from IPython.utils.io import file_read, nlprint
62 from IPython.utils.module_paths import find_mod
62 from IPython.utils.module_paths import find_mod
63 from IPython.utils.path import get_py_filename, unquote_filename
63 from IPython.utils.path import get_py_filename, unquote_filename
@@ -950,7 +950,7 b' Currently the magic system has the following functions:\\n"""'
950 try:
950 try:
951 vstr = str(var)
951 vstr = str(var)
952 except UnicodeEncodeError:
952 except UnicodeEncodeError:
953 vstr = unicode(var).encode(getdefaultencoding(),
953 vstr = unicode(var).encode(DEFAULT_ENCODING,
954 'backslashreplace')
954 'backslashreplace')
955 vstr = vstr.replace('\n','\\n')
955 vstr = vstr.replace('\n','\\n')
956 if len(vstr) < 50:
956 if len(vstr) < 50:
@@ -45,7 +45,7 b' 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 from IPython.utils.encoding import getdefaultencoding
48 from IPython.utils.encoding import DEFAULT_ENCODING
49
49
50 from . import decorators as dec
50 from . import decorators as dec
51 from . import skipdoctest
51 from . import skipdoctest
@@ -323,7 +323,7 b' else:'
323 # so we need a class that can handle both.
323 # so we need a class that can handle both.
324 class MyStringIO(StringIO):
324 class MyStringIO(StringIO):
325 def write(self, s):
325 def write(self, s):
326 s = py3compat.cast_unicode(s, encoding=getdefaultencoding())
326 s = py3compat.cast_unicode(s, encoding=DEFAULT_ENCODING)
327 super(MyStringIO, self).write(s)
327 super(MyStringIO, self).write(s)
328
328
329 notprinted_msg = """Did not find {0!r} in printed output (on {1}):
329 notprinted_msg = """Did not find {0!r} in printed output (on {1}):
@@ -26,7 +26,7 b' from .autoattr import auto_attr'
26 from ._process_common import getoutput, arg_split
26 from ._process_common import getoutput, arg_split
27 from IPython.utils import text
27 from IPython.utils import text
28 from IPython.utils import py3compat
28 from IPython.utils import py3compat
29 from IPython.utils.encoding import getdefaultencoding
29 from IPython.utils.encoding import DEFAULT_ENCODING
30
30
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 # Function definitions
32 # Function definitions
@@ -129,7 +129,7 b' class ProcessHandler(object):'
129 int : child's exitstatus
129 int : child's exitstatus
130 """
130 """
131 # Get likely encoding for the output.
131 # Get likely encoding for the output.
132 enc = getdefaultencoding()
132 enc = DEFAULT_ENCODING
133
133
134 # Patterns to match on the output, for pexpect. We read input and
134 # Patterns to match on the output, for pexpect. We read input and
135 # allow either a short timeout or EOF
135 # allow either a short timeout or EOF
@@ -29,7 +29,7 b' from subprocess import STDOUT'
29 from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split
29 from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split
30 from . import py3compat
30 from . import py3compat
31 from . import text
31 from . import text
32 from .encoding import getdefaultencoding
32 from .encoding import DEFAULT_ENCODING
33
33
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35 # Function definitions
35 # Function definitions
@@ -95,7 +95,7 b' def _find_cmd(cmd):'
95
95
96 def _system_body(p):
96 def _system_body(p):
97 """Callback for _system."""
97 """Callback for _system."""
98 enc = getdefaultencoding()
98 enc = DEFAULT_ENCODING
99 for line in read_no_interrupt(p.stdout).splitlines():
99 for line in read_no_interrupt(p.stdout).splitlines():
100 line = line.decode(enc, 'replace')
100 line = line.decode(enc, 'replace')
101 print(line, file=sys.stdout)
101 print(line, file=sys.stdout)
@@ -52,3 +52,5 b' def getdefaultencoding():'
52 except Exception:
52 except Exception:
53 pass
53 pass
54 return enc or sys.getdefaultencoding()
54 return enc or sys.getdefaultencoding()
55
56 DEFAULT_ENCODING = getdefaultencoding()
@@ -17,7 +17,7 b' import types'
17 from datetime import datetime
17 from datetime import datetime
18
18
19 from IPython.utils import py3compat
19 from IPython.utils import py3compat
20 from IPython.utils.encoding import getdefaultencoding
20 from IPython.utils.encoding import DEFAULT_ENCODING
21 from IPython.utils import text
21 from IPython.utils import text
22 next_attr_name = '__next__' if py3compat.PY3 else 'next'
22 next_attr_name = '__next__' if py3compat.PY3 else 'next'
23
23
@@ -136,7 +136,7 b' def json_clean(obj):'
136 return obj
136 return obj
137
137
138 if isinstance(obj, bytes):
138 if isinstance(obj, bytes):
139 return obj.decode(getdefaultencoding(), 'replace')
139 return obj.decode(DEFAULT_ENCODING, 'replace')
140
140
141 if isinstance(obj, container_to_list) or (
141 if isinstance(obj, container_to_list) or (
142 hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)):
142 hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)):
@@ -6,9 +6,7 b' import sys'
6 import re
6 import re
7 import types
7 import types
8
8
9 from .encoding import getdefaultencoding
9 from .encoding import DEFAULT_ENCODING
10
11 default_encoding = getdefaultencoding()
12
10
13 orig_open = open
11 orig_open = open
14
12
@@ -16,11 +14,11 b' def no_code(x, encoding=None):'
16 return x
14 return x
17
15
18 def decode(s, encoding=None):
16 def decode(s, encoding=None):
19 encoding = encoding or default_encoding
17 encoding = encoding or DEFAULT_ENCODING
20 return s.decode(encoding, "replace")
18 return s.decode(encoding, "replace")
21
19
22 def encode(u, encoding=None):
20 def encode(u, encoding=None):
23 encoding = encoding or default_encoding
21 encoding = encoding or DEFAULT_ENCODING
24 return u.encode(encoding, "replace")
22 return u.encode(encoding, "replace")
25
23
26
24
General Comments 0
You need to be logged in to leave comments. Login now