Skip to content

Commit 6ffd412

Browse files
pretty-print exceptions in unicide
fixes crashes on display of str(exception): * view source errors * startup file io error
1 parent ad753f6 commit 6ffd412

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
EXAMPLE_CONFIG_URL = 'https://raw.githubusercontent.com/bpython/bpython/master/bpython/sample-config'
8787

8888
# This is needed for is_nop and should be removed once is_nop is fixed.
89+
# Also needed for pretty-printing exceptions for error messages
8990
if py3:
9091
unicode = str
9192

@@ -570,7 +571,7 @@ def process_control_event(self, e):
570571
self.startup()
571572
except IOError as e:
572573
self.status_bar.message(
573-
_('Executing PYTHONSTARTUP failed: %s') % (str(e)))
574+
_('Executing PYTHONSTARTUP failed: %s') % (unicode(e)))
574575

575576
elif isinstance(e, bpythonevents.UndoEvent):
576577
self.undo(n=e.n)
@@ -1540,7 +1541,7 @@ def show_source(self):
15401541
try:
15411542
source = self.get_source_of_current_name()
15421543
except SourceNotFound as e:
1543-
self.status_bar.message(str(e))
1544+
self.status_bar.message(unicode(e))
15441545
else:
15451546
if self.config.highlight_show_source:
15461547
source = format(PythonLexer().get_tokens(source),

bpython/repl.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
from bpython.history import History
5656
from bpython.translations import _, ngettext
5757

58+
if py3: # used for getting pretty-printing exceptions
59+
unicode = str
60+
5861

5962
class RuntimeTimer(object):
6063
def __init__(self):
@@ -545,11 +548,11 @@ def get_source_of_current_name(self):
545548
obj = self.get_object(line)
546549
return inspection.get_source_unicode(obj)
547550
except (AttributeError, NameError) as e:
548-
msg = _("Cannot get source: %s") % (str(e), )
551+
msg = _("Cannot get source: %s") % (unicode(e), )
549552
except IOError as e:
550-
msg = str(e)
553+
msg = unicode(e)
551554
except TypeError as e:
552-
if "built-in" in str(e):
555+
if "built-in" in unicode(e):
553556
msg = _("Cannot access source of %r") % (obj, )
554557
else:
555558
msg = _("No source code found for %s") % (self.current_line, )

0 commit comments

Comments
 (0)