##// END OF EJS Templates
Merge pull request #1864 from takluyver/rmagic-exception...
Thomas Kluyver -
r7361:4c940c8a merge
parent child Browse files
Show More
@@ -58,10 +58,27 b' from IPython.testing.skipdoctest import skip_doctest'
58 from IPython.core.magic_arguments import (
58 from IPython.core.magic_arguments import (
59 argument, magic_arguments, parse_argstring
59 argument, magic_arguments, parse_argstring
60 )
60 )
61 from IPython.utils.py3compat import str_to_unicode, unicode_to_str
61 from IPython.utils.py3compat import str_to_unicode, unicode_to_str, PY3
62
62
63 class RMagicError(ri.RRuntimeError):
63 class RInterpreterError(ri.RRuntimeError):
64 pass
64 """An error when running R code in a %%R magic cell."""
65 def __init__(self, line, err, stdout):
66 self.line = line
67 self.err = err.rstrip()
68 self.stdout = stdout.rstrip()
69
70 def __unicode__(self):
71 s = 'Failed to parse and evaluate line %r.\nR error message: %r' % \
72 (self.line, self.err)
73 if self.stdout and (self.stdout != self.err):
74 s += '\nR stdout:\n' + self.stdout
75 return s
76
77 if PY3:
78 __str__ = __unicode__
79 else:
80 def __str__(self):
81 return unicode_to_str(unicode(self), 'utf-8')
65
82
66 def Rconverter(Robj, dataframe=False):
83 def Rconverter(Robj, dataframe=False):
67 """
84 """
@@ -141,8 +158,7 b' class RMagics(Magics):'
141 value = ri.baseenv['eval'](ri.parse(line))
158 value = ri.baseenv['eval'](ri.parse(line))
142 except (ri.RRuntimeError, ValueError) as exception:
159 except (ri.RRuntimeError, ValueError) as exception:
143 warning_or_other_msg = self.flush() # otherwise next return seems to have copy of error
160 warning_or_other_msg = self.flush() # otherwise next return seems to have copy of error
144 raise RMagicError(unicode_to_str('parsing and evaluating line "%s".\nR error message: "%s"\n R stdout:"%s"\n' %
161 raise RInterpreterError(line, str_to_unicode(str(exception)), warning_or_other_msg)
145 (line, str_to_unicode(exception.message, 'utf-8'), warning_or_other_msg)))
146 text_output = self.flush()
162 text_output = self.flush()
147 ri.set_writeconsole(old_writeconsole)
163 ri.set_writeconsole(old_writeconsole)
148 return text_output, value
164 return text_output, value
@@ -40,6 +40,7 b' from IPython.testing.skipdoctest import skip_doctest'
40 from IPython.utils import io
40 from IPython.utils import io
41 from IPython.utils.jsonutil import json_clean
41 from IPython.utils.jsonutil import json_clean
42 from IPython.utils.process import arg_split
42 from IPython.utils.process import arg_split
43 from IPython.utils import py3compat
43 from IPython.utils.traitlets import Instance, Type, Dict, CBool, CBytes
44 from IPython.utils.traitlets import Instance, Type, Dict, CBool, CBytes
44 from IPython.utils.warn import warn, error
45 from IPython.utils.warn import warn, error
45 from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary
46 from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary
@@ -436,6 +437,27 b' class KernelMagics(Magics):'
436 error("Could not start qtconsole: %r" % e)
437 error("Could not start qtconsole: %r" % e)
437 return
438 return
438
439
440 def safe_unicode(e):
441 """unicode(e) with various fallbacks. Used for exceptions, which may not be
442 safe to call unicode() on.
443 """
444 try:
445 return unicode(e)
446 except UnicodeError:
447 pass
448
449 try:
450 return py3compat.str_to_unicode(str(e))
451 except UnicodeError:
452 pass
453
454 try:
455 return py3compat.str_to_unicode(repr(e))
456 except UnicodeError:
457 pass
458
459 return u'Unrecoverably corrupt evalue'
460
439
461
440 class ZMQInteractiveShell(InteractiveShell):
462 class ZMQInteractiveShell(InteractiveShell):
441 """A subclass of InteractiveShell for ZMQ."""
463 """A subclass of InteractiveShell for ZMQ."""
@@ -514,7 +536,7 b' class ZMQInteractiveShell(InteractiveShell):'
514 exc_content = {
536 exc_content = {
515 u'traceback' : stb,
537 u'traceback' : stb,
516 u'ename' : unicode(etype.__name__),
538 u'ename' : unicode(etype.__name__),
517 u'evalue' : unicode(evalue)
539 u'evalue' : safe_unicode(evalue)
518 }
540 }
519
541
520 dh = self.displayhook
542 dh = self.displayhook
General Comments 0
You need to be logged in to leave comments. Login now