# HG changeset patch # User cat@eee # Date 1266892000 18000 # Node ID de2306847251c28c826a1e622aa312419108e1d9 # Parent 1babd3df22d1a97b31cbacbd0d9b2877caaaabc3 replacing temporaryfile with an object seems good diff -r 1babd3df22d1 -r de2306847251 cmd2.py --- 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 = ''