X Tutup
Skip to content

Commit 7e51b51

Browse files
committed
Python 3 fixes for urwid. Closes bpython#243.
1 parent 236e320 commit 7e51b51

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

bpython/urwid.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@
8080
'd': 'default',
8181
}
8282

83-
# Add our keys to the urwid command_map
83+
84+
def getpreferredencoding():
85+
return locale.getpreferredencoding() or "ascii"
8486

8587

8688
try:
@@ -746,7 +748,8 @@ def _populate_completion(self):
746748
func_name, args, is_bound, in_arg = self.argspec
747749
args, varargs, varkw, defaults = args[:4]
748750
if py3:
749-
kwonly, kwonly_defaults = args[4:]
751+
kwonly = self.argspec[1][4]
752+
kwonly_defaults = self.argspec[1][5] or {}
750753
else:
751754
kwonly, kwonly_defaults = [], {}
752755
markup = [('bold name', func_name),
@@ -972,14 +975,17 @@ def prompt(self, more):
972975
# We need the caption to use unicode as urwid normalizes later
973976
# input to be the same type, using ascii as encoding. If the
974977
# caption is bytes this breaks typing non-ascii into bpython.
975-
# Currently this decodes using ascii as I do not know where
976-
# ps1 is getting loaded from. If anyone wants to make
977-
# non-ascii prompts work feel free to fix this.
978978
if not more:
979-
caption = ('prompt', self.ps1.decode('ascii'))
979+
if py3:
980+
caption = ('prompt', self.ps1)
981+
else:
982+
caption = ('prompt', self.ps1.decode(getpreferredencoding()))
980983
self.stdout_hist += self.ps1
981984
else:
982-
caption = ('prompt_more', self.ps2.decode('ascii'))
985+
if py3:
986+
caption = ('prompt_more', self.ps2)
987+
else:
988+
caption = ('prompt_more', self.ps2.decode(getpreferredencoding()))
983989
self.stdout_hist += self.ps2
984990
self.edit = BPythonEdit(self.config, caption=caption)
985991

@@ -1024,7 +1030,11 @@ def handle_input(self, event):
10241030
self.history.append(inp)
10251031
self.edit.make_readonly()
10261032
# XXX what is this s_hist thing?
1027-
self.stdout_hist += inp.encode(locale.getpreferredencoding()) + '\n'
1033+
if py3:
1034+
self.stdout_hist += inp
1035+
else:
1036+
self.stdout_hist += inp.encode(locale.getpreferredencoding())
1037+
self.stdout_hist += '\n'
10281038
self.edit = None
10291039
# This may take a while, so force a redraw first:
10301040
self.main_loop.draw_screen()

0 commit comments

Comments
 (0)
X Tutup