changeset 380:de2306847251

replacing temporaryfile with an object seems good
author cat@eee
date Mon, 22 Feb 2010 21:26:40 -0500
parents 1babd3df22d1
children 04897151927b
files cmd2.py
diffstat 1 files changed, 3 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Mon Feb 22 19:24:50 2010 -0500
+++ b/cmd2.py	Mon Feb 22 21:26:40 2010 -0500
@@ -1428,24 +1428,6 @@
         return obj
     
 class OutputTrap(Borg):
-    '''Instantiate an OutputTrap to divert/capture ALL stdout output.  For use in unit testing.
-    Call `tearDown()` to return to normal output.'''
-    def __init__(self):
-        self.old_stdout = sys.stdout
-        self.trap = tempfile.TemporaryFile()
-        sys.stdout = self.trap
-    def read(self):
-        self.trap.seek(0)
-        result = self.trap.read().decode()  # Py3 sends stdout trap as bytes, not strings
-        self.trap.truncate(0)
-        try:
-            return result.strip('\x00') #TODO: understand this
-        except TypeError:
-            return result
-    def tearDown(self):
-        sys.stdout = self.old_stdout
-
-class OutputTrap(Borg):
     '''Instantiate  an OutputTrap to divert/capture ALL stdout output.  For use in unit testing.
     Call `tearDown()` to return to normal output.'''
     def __init__(self):
@@ -1455,7 +1437,9 @@
     def write(self, txt):
         self.contents += txt
     def read(self):
-        return self.contents
+        result = self.contents
+        self.contents = ''
+        return result
     def tearDown(self):
         sys.stdout = self.old_stdout
         self.contents = ''