[pypy-commit] pypy default: Don't crash on reading or writing stuff to the history file

arigo noreply at buildbot.pypy.org
Tue Nov 1 08:38:03 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r48639:5e062fe507c3
Date: 2011-11-01 07:37 +0000
http://bitbucket.org/pypy/pypy/changeset/5e062fe507c3/

Log:	Don't crash on reading or writing stuff to the history file if the
	encoding is wrong. Just fall back to utf-8, a kind of safe default.

diff --git a/lib_pypy/pyrepl/readline.py b/lib_pypy/pyrepl/readline.py
--- a/lib_pypy/pyrepl/readline.py
+++ b/lib_pypy/pyrepl/readline.py
@@ -231,7 +231,11 @@
         return ''.join(chars)
 
     def _histline(self, line):
-        return unicode(line.rstrip('\n'), ENCODING)
+        line = line.rstrip('\n')
+        try:
+            return unicode(line, ENCODING)
+        except UnicodeDecodeError:   # bah, silently fall back...
+            return unicode(line, 'utf-8')
 
     def get_history_length(self):
         return self.saved_history_length
@@ -268,7 +272,10 @@
         f = open(os.path.expanduser(filename), 'w')
         for entry in history:
             if isinstance(entry, unicode):
-                entry = entry.encode(ENCODING)
+                try:
+                    entry = entry.encode(ENCODING)
+                except UnicodeEncodeError:   # bah, silently fall back...
+                    entry = entry.encode('utf-8')
             entry = entry.replace('\n', '\r\n')   # multiline history support
             f.write(entry + '\n')
         f.close()


More information about the pypy-commit mailing list