##// END OF EJS Templates
Merge pull request #770 from minrk/jsonclean...
Fernando Perez -
r4797:ede79361 merge
parent child Browse files
Show More
@@ -24,7 +24,7 b' 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 b' 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 b' 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 b' 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 b' 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 b' 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 b' 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 b' 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 b' 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 b' 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 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 = 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)
@@ -303,6 +303,7 b' class Kernel(Configurable):'
303 303 time.sleep(self._execute_sleep)
304 304
305 305 # Send the reply.
306 reply_content = json_clean(reply_content)
306 307 reply_msg = self.session.send(self.shell_socket, u'execute_reply',
307 308 reply_content, parent, ident=ident)
308 309 self.log.debug(str(reply_msg))
@@ -321,6 +322,7 b' class Kernel(Configurable):'
321 322 matches = {'matches' : matches,
322 323 'matched_text' : txt,
323 324 'status' : 'ok'}
325 matches = json_clean(matches)
324 326 completion_msg = self.session.send(self.shell_socket, 'complete_reply',
325 327 matches, parent, ident)
326 328 self.log.debug(str(completion_msg))
@@ -358,6 +360,7 b' class Kernel(Configurable):'
358 360 else:
359 361 hist = []
360 362 content = {'history' : list(hist)}
363 content = json_clean(content)
361 364 msg = self.session.send(self.shell_socket, 'history_reply',
362 365 content, parent, ident)
363 366 self.log.debug(str(msg))
@@ -409,7 +412,7 b' class Kernel(Configurable):'
409 412 sys.stdout.flush()
410 413
411 414 # Send the input request.
412 content = dict(prompt=prompt)
415 content = json_clean(dict(prompt=prompt))
413 416 msg = self.session.send(self.stdin_socket, u'input_request', content, parent)
414 417
415 418 # Await a response.
@@ -30,6 +30,7 b' from IPython.core.macro import Macro'
30 30 from IPython.core.magic import MacroToEdit
31 31 from IPython.core.payloadpage import install_payload_page
32 32 from IPython.utils import io
33 from IPython.utils.jsonutil import json_clean
33 34 from IPython.utils.path import get_py_filename
34 35 from IPython.utils.traitlets import Instance, Type, Dict, CBool
35 36 from IPython.utils.warn import warn
@@ -69,7 +70,7 b' class ZMQDisplayPublisher(DisplayPublisher):'
69 70 content['data'] = data
70 71 content['metadata'] = metadata
71 72 self.session.send(
72 self.pub_socket, u'display_data', content,
73 self.pub_socket, u'display_data', json_clean(content),
73 74 parent=self.parent_header
74 75 )
75 76
@@ -144,7 +145,7 b' class ZMQInteractiveShell(InteractiveShell):'
144 145 dh = self.displayhook
145 146 # Send exception info over pub socket for other clients than the caller
146 147 # to pick up
147 exc_msg = dh.session.send(dh.pub_socket, u'pyerr', exc_content, dh.parent_header)
148 exc_msg = dh.session.send(dh.pub_socket, u'pyerr', json_clean(exc_content), dh.parent_header)
148 149
149 150 # FIXME - Hack: store exception info in shell object. Right now, the
150 151 # caller is reading this info after the fact, we need to fix this logic
General Comments 0
You need to be logged in to leave comments. Login now