The restoring of sys.stdout should happen right after:

    self.runCmd("script my.date()")

In case the self.expect() fails, the restore would not be executed.

llvm-svn: 124500
diff --git a/lldb/test/command_source/TestCommandSource.py b/lldb/test/command_source/TestCommandSource.py
index 0f34eb46..006967d 100644
--- a/lldb/test/command_source/TestCommandSource.py
+++ b/lldb/test/command_source/TestCommandSource.py
@@ -29,14 +29,14 @@
         # Python should evaluate "my.date()" successfully.
         self.runCmd("script my.date()")
 
+        # Now restore stdout to the way we were. :-)
+        sys.stdout = old_stdout
+
         import datetime
         self.expect(session.getvalue(), "script my.date() runs successfully",
                     exe=False,
             substrs = [str(datetime.date.today())])
 
-        # Now restore stdout to the way we were. :-)
-        sys.stdout = old_stdout
-
 
 if __name__ == '__main__':
     import atexit