def test_run_multiple_times_with_different_stdout_and_stderr(self): stdout, stderr = StringIO(), StringIO() self._run(stdout, stderr) self._assert_normal_stdout_stderr_are_empty() self._assert_output(stdout, [('My Suite', 2), ('My Test', 1)]) self._assert_output(stderr, [('Hello, world!', 1)]) stdout.close(); stderr.close() output = StringIO() self._run(output, output, variable='MESSAGE:Hi, again!') self._assert_normal_stdout_stderr_are_empty() self._assert_output(output, [('My Suite', 2), ('My Test', 1), ('Hi, again!', 1), ('Hello, world!', 0)]) output.close() self._run(variable='MESSAGE:Last hi!') self._assert_output(sys.__stdout__, [('My Suite', 2), ('My Test', 1)]) self._assert_output(sys.__stderr__, [('Last hi!', 1), ('Hello, world!', 0)])
def test_run_multiple_times_with_different_stdout_and_stderr(self): stdout, stderr = StringIO(), StringIO() self._run(stdout, stderr) self._assert_normal_stdout_stderr_are_empty() self._assert_output(stdout, [('My Suite', 2), ('My Test', 1)]) self._assert_output(stderr, [('Hello, world!', 1)]) stdout.close(); stderr.close() output = StringIO() self._run(output, output, variable='MESSAGE:Hi, again!') self._assert_normal_stdout_stderr_are_empty() self._assert_output(output, [('My Suite', 2), ('My Test', 1), ('Hi, again!', 1), ('Hello, world!', 0)]) output.close() self._run(variable='MESSAGE:Last hi!') self._assert_output(sys.__stdout__, [('My Suite', 2), ('My Test', 1)]) self._assert_output(sys.__stderr__, [('Last hi!', 1), ('Hello, world!', 0)])
class PythonCapturer(object): def __init__(self, stdout=True): if stdout: self._original = sys.stdout self._set_stream = self._set_stdout else: self._original = sys.stderr self._set_stream = self._set_stderr self._stream = StringIO() self._set_stream(self._stream) def _set_stdout(self, stream): sys.stdout = stream def _set_stderr(self, stream): sys.stderr = stream def release(self): # Original stream must be restored before closing the current self._set_stream(self._original) try: return self._get_value(self._stream) finally: self._stream.close() self._avoid_at_exit_errors(self._stream) def _get_value(self, stream): try: return decode_output(stream.getvalue()) except UnicodeError: # Error occurs if non-ASCII chars logged both as str and unicode. stream.buf = decode_output(stream.buf) stream.buflist = [decode_output(item) for item in stream.buflist] return stream.getvalue() def _avoid_at_exit_errors(self, stream): # Avoid ValueError at program exit when logging module tries to call # methods of streams it has intercepted that are already closed. # Which methods are called, and does logging silence possible errors, # depends on Python/Jython version. For related discussion see # http://bugs.python.org/issue6333 stream.write = lambda s: None stream.flush = lambda: None
class PythonCapturer(object): def __init__(self, stdout=True): if stdout: self._original = sys.stdout self._set_stream = self._set_stdout else: self._original = sys.stderr self._set_stream = self._set_stderr self._stream = StringIO() self._set_stream(self._stream) def _set_stdout(self, stream): sys.stdout = stream def _set_stderr(self, stream): sys.stderr = stream def release(self): # Original stream must be restored before closing the current self._set_stream(self._original) try: return self._get_value(self._stream) finally: self._stream.close() self._avoid_at_exit_errors(self._stream) def _get_value(self, stream): try: return decode_output(stream.getvalue()) except UnicodeError: # Error occurs if non-ASCII chars logged both as str and unicode. stream.buf = decode_output(stream.buf) stream.buflist = [decode_output(item) for item in stream.buflist] return stream.getvalue() def _avoid_at_exit_errors(self, stream): # Avoid ValueError at program exit when logging module tries to call # methods of streams it has intercepted that are already closed. # Which methods are called, and does logging silence possible errors, # depends on Python/Jython version. For related discussion see # http://bugs.python.org/issue6333 stream.write = lambda s: None stream.flush = lambda: None
class ClosableOutput(object): def __init__(self, path): self._output = StringIO() self._path = path def __enter__(self): return self def __exit__(self, *args): self.close() def write(self, data): self._output.write(data) def close(self): self.value = self._output.getvalue() self._output.close() def __str__(self): return self._path
class ClosableOutput(object): def __init__(self, path): self._output = StringIO() self._path = path def __enter__(self): return self def __exit__(self, *args): self.close() def write(self, data): self._output.write(data) def close(self): self.value = self._output.getvalue() self._output.close() def __str__(self): return self._path