Skip to content

Commit e12038a

Browse files
committed
Fix readline._histline for Python 3
`unicode` is `str` here, and resulted in: > TypeError: decoding str is not supported
1 parent 278c207 commit e12038a

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pyrepl/readline.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
from pyrepl.unix_console import UnixConsole, _error
3535
try:
3636
unicode
37+
PY3 = False
3738
except NameError:
39+
PY3 = True
3840
unicode = str
3941
unichr = chr
4042
basestring = bytes, str
@@ -207,8 +209,9 @@ def raw_input(self, prompt=''):
207209
reader.ps1 = prompt
208210

209211
ret = reader.readline(startup_hook=self.startup_hook)
210-
if sys.version_info < (3, ):
212+
if not PY3:
211213
return ret
214+
212215
# Unicode/str is required for Python 3 (3.5.2).
213216
# Ref: https://bitbucket.org/pypy/pyrepl/issues/20/#comment-30647029
214217
return unicode(ret, ENCODING)
@@ -247,6 +250,9 @@ def get_completer_delims(self):
247250

248251
def _histline(self, line):
249252
line = line.rstrip('\n')
253+
if PY3:
254+
return line
255+
250256
try:
251257
return unicode(line, ENCODING)
252258
except UnicodeDecodeError: # bah, silently fall back...

testing/test_readline.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
from pyrepl.readline import _ReadlineWrapper
21
import os
32
import pty
43
import sys
54

5+
import pytest
6+
from pyrepl.readline import _ReadlineWrapper
7+
8+
9+
@pytest.fixture
10+
def readline_wrapper():
11+
master, slave = pty.openpty()
12+
return _ReadlineWrapper(slave, slave)
13+
14+
615
if sys.version_info < (3, ):
716
bytes_type = str
817
unicode_type = unicode # noqa: F821
@@ -43,3 +52,17 @@ def test_raw_input():
4352
else:
4453
assert result == 'input'
4554
assert isinstance(result, unicode_type)
55+
56+
57+
def test_read_history_file(readline_wrapper, tmp_path):
58+
histfile = tmp_path / "history"
59+
histfile.touch()
60+
61+
assert readline_wrapper.reader is None
62+
63+
readline_wrapper.read_history_file(str(histfile))
64+
assert readline_wrapper.reader.history == []
65+
66+
histfile.write_bytes(b"foo\nbar\n")
67+
readline_wrapper.read_history_file(str(histfile))
68+
assert readline_wrapper.reader.history == ["foo", "bar"]

0 commit comments

Comments
 (0)