Skip to content

Commit 1eab386

Browse files
committed
Port 5e062fe507c3 from pypy.
1 parent cb9f8ba commit 1eab386

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

pyrepl/readline.py

Lines changed: 9 additions & 2 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()

0 commit comments

Comments
 (0)