Revision: 3654
Author: pekka.klarck
Date: Fri May 28 16:18:21 2010
Log: Now that we don't have PARALLEL, output capturing could be simplified significantly. This changes could even help with the problems related to using signals with timeouts. Also moved outputcapture to correct place.
http://code.google.com/p/robotframework/source/detail?r=3654

Added:
 /trunk/src/robot/running/outputcapture.py
Deleted:
 /trunk/src/robot/utils/outputcapture.py
Modified:
 /trunk/src/robot/running/handlers.py
 /trunk/src/robot/utils/__init__.py
 /trunk/utest/output/test_listeners.py

=======================================
--- /dev/null
+++ /trunk/src/robot/running/outputcapture.py   Fri May 28 16:18:21 2010
@@ -0,0 +1,73 @@
+#  Copyright 2008-2009 Nokia Siemens Networks Oyj
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+import sys
+from StringIO import StringIO
+
+
+class OutputCapturer:
+
+    def __init__(self):
+        self._python_output = _PythonOutput()
+        self._java_output = _JavaOutput()
+
+    def release(self):
+        pyout, pyerr = self._python_output.release()
+        jaout, jaerr = self._java_output.release()
+        return (pyout, pyerr) if pyout or pyerr else (jaout, jaerr)
+
+
+class _PythonOutput(object):
+
+    def __init__(self):
+        self._orig_out = sys.stdout
+        self._orig_err = sys.stderr
+        sys.stderr = StringIO()
+        sys.stdout = StringIO()
+
+    def release(self):
+        sys.stdout.flush()
+        sys.stderr.flush()
+        out = sys.stdout.getvalue()
+        err = sys.stderr.getvalue()
+        sys.stdout = self._orig_out
+        sys.stderr = self._orig_err
+        return out, err
+
+
+if not sys.platform.startswith('java'):
+    class _JavaOutput(object):
+        def release(self):
+            return '', ''
+
+else:
+    from java.io import PrintStream, ByteArrayOutputStream
+    from java.lang import System
+
+    class _JavaOutput(object):
+
+        def __init__(self):
+            self._orig_out = System.out
+            self._orig_err = System.err
+            self._out = ByteArrayOutputStream()
+            self._err = ByteArrayOutputStream()
+            System.setOut(PrintStream(self._out, False, 'UTF-8'))
+            System.setErr(PrintStream(self._err, False, 'UTF-8'))
+
+        def release(self):
+            System.out.close()
+            System.err.close()
+            System.setOut(self._orig_out)
+            System.setErr(self._orig_err)
+            return self._out.toString('UTF-8'), self._err.toString('UTF-8')
=======================================
--- /trunk/src/robot/utils/outputcapture.py     Fri Apr 16 07:46:13 2010
+++ /dev/null
@@ -1,148 +0,0 @@
-#  Copyright 2008-2009 Nokia Siemens Networks Oyj
-#
-#  Licensed under the Apache License, Version 2.0 (the "License");
-#  you may not use this file except in compliance with the License.
-#  You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software
-#  distributed under the License is distributed on an "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#  See the License for the specific language governing permissions and
-#  limitations under the License.
-
-
-import sys
-import os
-from StringIO import StringIO
-
-from robotthread import Semaphore, current_thread
-
-if os.name == 'java':
-    from java.io import OutputStream, ByteArrayOutputStream, PrintStream
-    from java.lang import System
-
-
-def capture_output():
-    _OUTPUT_CAPTURE.capture_output()
-
-def release_output():
-    return _OUTPUT_CAPTURE.release_output()
-
-
-class _Output:
-
-    def __init__(self):
-        self._outs = {}
-        self._sema = Semaphore()
-
-    def write(self, msg):
-        self._sema.acquire()
-        thrd = current_thread()
-        key = thrd.getName() == 'TIMED_RUN' and 'TIMED' or thrd
-        if not self._outs.has_key(key):
-            self._outs[key] = self._get_stream()
-        self._outs[key].write(msg)
-        self._sema.release()
-
-    def get_value(self):
-        self._sema.acquire()
-        thrd = current_thread()
-        key = self._outs.has_key('TIMED') and 'TIMED' or thrd
-        if self._outs.has_key(key):
-            self._outs[key].flush()
-            msg = self._get_msg(key)
-            del(self._outs[key])
-            if key is not thrd and self._outs.has_key(thrd):
-                del(self._outs[thrd])
-        else:
-            msg = ''
-        self._sema.release()
-        return msg
-
-
-class _PythonOutput(_Output):
-
-    def _get_stream(self):
-        return StringIO()
-
-    def _get_msg(self, key):
-        return self._outs[key].getvalue()
-
-    def flush(self):
-        self._sema.acquire()
-        thrd = current_thread()
-        key = self._outs.has_key('TIMED') and 'TIMED' or thrd
-        if self._outs.has_key(key):
-            self._outs[key].flush()
-        self._sema.release()
-
-
-if os.name == 'java':
-
-    class _JavaOutput(_Output, OutputStream):
-
-        def __init__(self):
-            OutputStream.__init__(self)
-            _Output.__init__(self)
-
-        def _get_stream(self):
-            return ByteArrayOutputStream()
-
-        def _get_msg(self, key):
-            output = self._outs[key]
-            output.flush()
-            return output.toString('UTF-8')
-
-
-class _OutputCapture:
-
-    def __init__(self):
-        self._count = 0
-        self._sema = Semaphore()
-
-    def capture_output(self):
-        self._sema.acquire()
-        if self._count == 0:
-            self._capture_output()
-        self._count += 1
-        self._sema.release()
-
-    def _capture_output(self):
-        sys.stdout = _PythonOutput()
-        sys.stderr = _PythonOutput()
-        if os.name == 'java':
-            self._orig_java_out = System.out
-            self._orig_java_err = System.err
-            self._capt_java_out = _JavaOutput()
-            self._capt_java_err = _JavaOutput()
-            System.setOut(PrintStream(self._capt_java_out, False, 'UTF-8'))
-            System.setErr(PrintStream(self._capt_java_err, False, 'UTF-8'))
-
-    def release_output(self):
-        self._sema.acquire()
-        out, err = self._get_output()
-        self._count -= 1
-        if self._count == 0:
-            self._release_output()
-        self._sema.release()
-        return out, err
-
-    def _get_output(self):
-        if os.name == 'java':
-            out = self._capt_java_out.get_value()
-            err = self._capt_java_err.get_value()
-            if out or err:
-                return out, err
-        return sys.stdout.get_value(), sys.stderr.get_value()
-
-    def _release_output(self):
-        sys.stdout = sys.__stdout__
-        sys.stderr = sys.__stderr__
-        if os.name == 'java':
-            System.setOut(self._orig_java_out)
-            System.setErr(self._orig_java_err)
-
-
-_OUTPUT_CAPTURE = _OutputCapture()
=======================================
--- /trunk/src/robot/running/handlers.py        Thu May 27 07:11:21 2010
+++ /trunk/src/robot/running/handlers.py        Fri May 28 16:18:21 2010
@@ -17,10 +17,11 @@

 from robot import utils

+from outputcapture import OutputCapturer
 from runkwregister import RUN_KW_REGISTER
-from arguments import PythonKeywordArguments, JavaKeywordArguments, \
-    DynamicKeywordArguments, PythonInitArguments, JavaInitArguments, \
-    RunKeywordArguments
+from arguments import (PythonKeywordArguments, JavaKeywordArguments,
+                       DynamicKeywordArguments, RunKeywordArguments,
+                       PythonInitArguments, JavaInitArguments)
 from signalhandler import STOP_SIGNAL_MONITOR


@@ -113,11 +114,15 @@
         return lambda: handler(*positional, **named)

     def _run_with_output_captured_and_signal_monitor(self, runner, output):
-        utils.capture_output()
+        capturer = OutputCapturer()
         try:
             return self._run_with_signal_monitoring(runner)
         finally:
-            self._release_and_log_output(output)
+            stdout, stderr = capturer.release()
+            output.log_output(stdout)
+            output.log_output(stderr)
+            if stderr.strip() != '':
+                sys.__stderr__.write(stderr+'\n')

     def _run_with_signal_monitoring(self, runner):
         try:
@@ -126,13 +131,6 @@
         finally:
             STOP_SIGNAL_MONITOR.stop_running_keyword()

-    def _release_and_log_output(self, logger):
-        stdout, stderr = utils.release_output()
-        logger.log_output(stdout)
-        logger.log_output(stderr)
-        if stderr.strip() != '':
-            sys.stderr.write(stderr+'\n')
-
     def _current_handler(self):
         if self._method:
             return self._method
=======================================
--- /trunk/src/robot/utils/__init__.py  Fri May 28 04:44:00 2010
+++ /trunk/src/robot/utils/__init__.py  Fri May 28 16:18:21 2010
@@ -26,7 +26,6 @@
 from match import eq, eq_any, matches, matches_any
from misc import plural_or_not, get_link_path, printable_name, seq2str, seq2str2
 from normalizing import normalize, normalize_tags, normpath, NormalizedDict
-from outputcapture import capture_output, release_output
 from robottime import (get_timestamp, get_start_timestamp, format_time,
                        get_time, get_elapsed_time, elapsed_time_to_string,
                        timestr_to_secs, secs_to_timestr, secs_to_timestamp,
=======================================
--- /trunk/utest/output/test_listeners.py       Mon Mar 29 00:08:29 2010
+++ /trunk/utest/output/test_listeners.py       Fri May 28 16:18:21 2010
@@ -4,6 +4,7 @@
 from robot.output import LOGGER
 from robot.utils.asserts import *
 from robot import utils
+from robot.running.outputcapture import OutputCapturer


 class _Mock:
@@ -125,7 +126,7 @@
     def setUp(self):
         self.listeners = Listeners([(self.listener_name, [])])
         self.listener = self.listeners._listeners[0]
-        utils.capture_output()
+        self.capturer = OutputCapturer()

     def test_start_suite(self):
         self.listeners.start_suite(SuiteMock())
@@ -176,7 +177,7 @@
         self._assert_output('Closing...')

     def _assert_output(self, expected):
-        stdout, stderr = utils.release_output()
+        stdout, stderr = self.capturer.release()
         assert_equals(stderr, '')
         assert_equals(stdout.rstrip(), expected)

Reply via email to