[pypy-commit] pyrepl py3k-readline: fix reading/writing the pyrepl.readline history file, w.r.t. to unicode vs bytes

antocuni noreply at buildbot.pypy.org
Fri May 4 17:40:06 CEST 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k-readline
Changeset: r194:8d689f601f1a
Date: 2012-05-04 17:35 +0200
http://bitbucket.org/pypy/pyrepl/changeset/8d689f601f1a/

Log:	fix reading/writing the pyrepl.readline history file, w.r.t. to
	unicode vs bytes

diff --git a/pyrepl/readline.py b/pyrepl/readline.py
--- a/pyrepl/readline.py
+++ b/pyrepl/readline.py
@@ -32,6 +32,10 @@
 from pyrepl.completing_reader import CompletingReader
 from pyrepl.unix_console import UnixConsole, _error
 
+try:
+    unicode
+except NameError:
+    unicode = str
 
 ENCODING = sys.getfilesystemencoding() or 'latin1'     # XXX review
 
@@ -232,6 +236,8 @@
 
     def _histline(self, line):
         line = line.rstrip('\n')
+        if isinstance(line, unicode):
+            return line # on py3k
         try:
             return unicode(line, ENCODING)
         except UnicodeDecodeError:   # bah, silently fall back...
@@ -271,7 +277,9 @@
         history = self.get_reader().get_trimmed_history(maxlength)
         f = open(os.path.expanduser(filename), 'w')
         for entry in history:
-            if isinstance(entry, unicode):
+            # if we are on py3k, we don't need to encode strings before
+            # writing it to a file
+            if isinstance(entry, unicode) and sys.version_info < (3,):
                 try:
                     entry = entry.encode(ENCODING)
                 except UnicodeEncodeError:   # bah, silently fall back...


More information about the pypy-commit mailing list