Skip to content

Commit 3c94259

Browse files
merge from default
2 parents 2dc67a5 + 1eab386 commit 3c94259

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

pyrepl/readline.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ def get_completer_delims(self):
231231
return ''.join(chars)
232232

233233
def _histline(self, line):
234-
return unicode(line.rstrip('\n'), ENCODING)
234+
line = line.rstrip('\n')
235+
try:
236+
return unicode(line, ENCODING)
237+
except UnicodeDecodeError: # bah, silently fall back...
238+
return unicode(line, 'utf-8')
235239

236240
def get_history_length(self):
237241
return self.saved_history_length
@@ -268,7 +272,10 @@ def write_history_file(self, filename='~/.history'):
268272
f = open(os.path.expanduser(filename), 'w')
269273
for entry in history:
270274
if isinstance(entry, unicode):
271-
entry = entry.encode(ENCODING)
275+
try:
276+
entry = entry.encode(ENCODING)
277+
except UnicodeEncodeError: # bah, silently fall back...
278+
entry = entry.encode('utf-8')
272279
entry = entry.replace('\n', '\r\n') # multiline history support
273280
f.write(entry + '\n')
274281
f.close()
@@ -395,9 +402,21 @@ def _setup():
395402
_wrapper.f_in = f_in
396403
_wrapper.f_out = f_out
397404

398-
if hasattr(sys, '__raw_input__'): # PyPy
399-
_old_raw_input = sys.__raw_input__
405+
if '__pypy__' in sys.builtin_module_names: # PyPy
406+
407+
def _old_raw_input(prompt=''):
408+
# sys.__raw_input__() is only called when stdin and stdout are
409+
# as expected and are ttys. If it is the case, then get_reader()
410+
# should not really fail in _wrapper.raw_input(). If it still
411+
# does, then we will just cancel the redirection and call again
412+
# the built-in raw_input().
413+
try:
414+
del sys.__raw_input__
415+
except AttributeError:
416+
pass
417+
return raw_input(prompt)
400418
sys.__raw_input__ = _wrapper.raw_input
419+
401420
else:
402421
# this is not really what readline.c does. Better than nothing I guess
403422
import __builtin__

0 commit comments

Comments
 (0)