Skip to content

Commit 1991216

Browse files
authored
Merge pull request bpython#702 from thomasballinger/fix-urwid
fix encoding issues in urwid and curses
2 parents 0ddda16 + 143757c commit 1991216

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

bpython/cli.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def __init__(self, scr, interp, statusbar, config, idle=None):
330330
repl.Repl.__init__(self, interp, config)
331331
self.interp.writetb = self.writetb
332332
self.scr = scr
333-
self.stdout_hist = ''
333+
self.stdout_hist = '' # native str (bytes in Py2, unicode in Py3)
334334
self.list_win = newwin(get_colpair(config, 'background'), 1, 1, 1, 1)
335335
self.cpos = 0
336336
self.do_exit = False
@@ -1066,13 +1066,19 @@ def prompt(self, more):
10661066
"""Show the appropriate Python prompt"""
10671067
if not more:
10681068
self.echo("\x01%s\x03%s" % (self.config.color_scheme['prompt'], self.ps1))
1069-
self.stdout_hist += self.ps1
1069+
if py3:
1070+
self.stdout_hist += self.ps1
1071+
else:
1072+
self.stdout_hist += self.ps1.encode(getpreferredencoding())
10701073
self.s_hist.append('\x01%s\x03%s\x04' %
10711074
(self.config.color_scheme['prompt'], self.ps1))
10721075
else:
10731076
prompt_more_color = self.config.color_scheme['prompt_more']
10741077
self.echo("\x01%s\x03%s" % (prompt_more_color, self.ps2))
1075-
self.stdout_hist += self.ps2
1078+
if py3:
1079+
self.stdout_hist += self.ps2
1080+
else:
1081+
self.stdout_hist += self.ps2.encode(getpreferredencoding())
10761082
self.s_hist.append('\x01%s\x03%s\x04' % (prompt_more_color, self.ps2))
10771083

10781084
def push(self, s, insert_into_history=True):

bpython/urwid.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ def __init__(self, event_loop, palette, interpreter, config):
596596
self.tooltip = urwid.ListBox(urwid.SimpleListWalker([]))
597597
self.tooltip.grid = None
598598
self.overlay = Tooltip(self.listbox, self.tooltip)
599-
self.stdout_hist = ''
599+
self.stdout_hist = '' # native str (bytes in Py2, unicode in Py3)
600600

601601
self.frame = urwid.Frame(self.overlay)
602602

@@ -976,17 +976,17 @@ def prompt(self, more):
976976
# input to be the same type, using ascii as encoding. If the
977977
# caption is bytes this breaks typing non-ascii into bpython.
978978
if not more:
979+
caption = ('prompt', self.ps1)
979980
if py3:
980-
caption = ('prompt', self.ps1)
981+
self.stdout_hist += self.ps1
981982
else:
982-
caption = ('prompt', self.ps1.decode(getpreferredencoding()))
983-
self.stdout_hist += self.ps1
983+
self.stdout_hist += self.ps1.encode(getpreferredencoding())
984984
else:
985+
caption = ('prompt_more', self.ps2)
985986
if py3:
986-
caption = ('prompt_more', self.ps2)
987+
self.stdout_hist += self.ps2
987988
else:
988-
caption = ('prompt_more', self.ps2.decode(getpreferredencoding()))
989-
self.stdout_hist += self.ps2
989+
self.stdout_hist += self.ps2.encode(getpreferredencoding())
990990
self.edit = BPythonEdit(self.config, caption=caption)
991991

992992
urwid.connect_signal(self.edit, 'change', self.on_input_change)

0 commit comments

Comments
 (0)