# HG changeset patch # User catherine@dellzilla # Date 1266426522 18000 # Node ID 7cd04727f7f76eeaf9557419cf432c50541c8065 # Parent 5e3f918c41d8e95a9d2a1a666c0a390b5fa44a2d redirect works even with print diff -r 5e3f918c41d8 -r 7cd04727f7f7 cmd2.py --- a/cmd2.py Wed Feb 17 00:01:55 2010 -0500 +++ b/cmd2.py Wed Feb 17 12:08:42 2010 -0500 @@ -183,7 +183,7 @@ except ImportError: def get_paste_buffer(*args): raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/') - setPasteBuffer = get_paste_buffer + write_to_paste_buffer = get_paste_buffer else: can_clip = False try: @@ -214,8 +214,7 @@ else: def get_paste_buffer(*args): raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') - setPasteBuffer = get_paste_buffer - writeToPasteBuffer = get_paste_buffer + write_to_paste_buffer = get_paste_buffer pyparsing.ParserElement.setDefaultWhitespaceChars(' \t') @@ -787,18 +786,20 @@ def redirect_output(self, statement): if statement.parsed.pipeTo: - self.kept_state = Statekeeper(self, ('stdout',)) + self.kept_state = Statekeeper(self, ('stdout',)) + self.kept_sys = Statekeeper(sys, ('stdout',)) self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) - self.stdout = self.redirect.stdin + sys.stdout = self.stdout = self.redirect.stdin elif statement.parsed.output: self.kept_state = Statekeeper(self, ('stdout',)) + self.kept_sys = Statekeeper(sys, ('stdout',)) if statement.parsed.outputTo: mode = 'w' if statement.parsed.output == '>>': mode = 'a' - self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) + sys.stdout = self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) else: - self.stdout = tempfile.TemporaryFile() + sys.stdout = self.stdout = tempfile.TemporaryFile() if statement.parsed.output == '>>': self.stdout.write(get_paste_buffer()) @@ -812,7 +813,8 @@ for result in self.redirect.communicate(): self.kept_state.stdout.write(result or '') self.stdout.close() - self.kept_state.restore() + self.kept_state.restore() + self.kept_sys.restore() self.kept_state = None def onecmd(self, line): diff -r 5e3f918c41d8 -r 7cd04727f7f7 docs/pycon2010/pirate6.py --- a/docs/pycon2010/pirate6.py Wed Feb 17 00:01:55 2010 -0500 +++ b/docs/pycon2010/pirate6.py Wed Feb 17 12:08:42 2010 -0500 @@ -1,4 +1,4 @@ -from cmd import Cmd +from cmd2 import Cmd # prompts and defaults class Pirate(Cmd):