[pypy-commit] pypy default: Fix: call str() only once per prompt, not twice, even though the

arigo noreply at buildbot.pypy.org
Thu Sep 1 11:13:56 CEST 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r46975:414bb2d98b0c
Date: 2011-09-01 11:11 +0200
http://bitbucket.org/pypy/pypy/changeset/414bb2d98b0c/

Log:	Fix: call str() only once per prompt, not twice, even though the
	same object is in reader.ps1 and reader.ps2.

diff --git a/lib_pypy/pyrepl/reader.py b/lib_pypy/pyrepl/reader.py
--- a/lib_pypy/pyrepl/reader.py
+++ b/lib_pypy/pyrepl/reader.py
@@ -401,13 +401,19 @@
             return "(arg: %s) "%self.arg
         if "\n" in self.buffer:
             if lineno == 0:
-                return self._ps2
+                res = self.ps2
             elif lineno == self.buffer.count("\n"):
-                return self._ps4
+                res = self.ps4
             else:
-                return self._ps3
+                res = self.ps3
         else:
-            return self._ps1
+            res = self.ps1
+        # Lazily call str() on self.psN, and cache the results using as key
+        # the object on which str() was called.  This ensures that even if the
+        # same object is used e.g. for ps1 and ps2, str() is called only once.
+        if res not in self._pscache:
+            self._pscache[res] = str(res)
+        return self._pscache[res]
 
     def push_input_trans(self, itrans):
         self.input_trans_stack.append(self.input_trans)
@@ -473,8 +479,7 @@
             self.pos = 0
             self.dirty = 1
             self.last_command = None
-            self._ps1, self._ps2, self._ps3, self._ps4 = \
-                           map(str, [self.ps1, self.ps2, self.ps3, self.ps4])
+            self._pscache = {}
         except:
             self.restore()
             raise


More information about the pypy-commit mailing list