diff --git a/qt/python/mantidqt/utils/test/test_writetosignal.py b/qt/python/mantidqt/utils/test/test_writetosignal.py index 61d121e483a3196e8df98b98458798ec1f6d4707..a597d3f7b22f08474366790c5cd7c8c56ddb2040 100644 --- a/qt/python/mantidqt/utils/test/test_writetosignal.py +++ b/qt/python/mantidqt/utils/test/test_writetosignal.py @@ -27,6 +27,24 @@ class Receiver(QObject): class WriteToSignalTest(GuiTest): + @classmethod + def setUpClass(cls): + if not hasattr(sys.stdout, "fileno"): + # if not present in the test stdout, then add it as an + # attribute so that mock can replace it later. + sys.stdout.fileno = None + + def test_run_with_output_present(self): + with patch("sys.stdout.fileno", return_value=10) as mock_fileno: + writer = WriteToSignal(sys.stdout) + mock_fileno.assert_called_once_with() + self.assertEqual(writer._original_out, sys.stdout) + + def test_run_without_output_present(self): + with patch("sys.stdout.fileno", return_value=-1) as mock_fileno: + writer = WriteToSignal(sys.stdout) + mock_fileno.assert_called_once_with() + self.assertEqual(writer._original_out, None) def test_connected_receiver_receives_text(self): with patch("sys.stdout.fileno") as mock_fileno: diff --git a/qt/python/mantidqt/utils/writetosignal.py b/qt/python/mantidqt/utils/writetosignal.py index 6af5aba9835e58c3e10319a3d16a86aa8101997a..3cca96e46952daac4b7b2aab8d8f971c0378e773 100644 --- a/qt/python/mantidqt/utils/writetosignal.py +++ b/qt/python/mantidqt/utils/writetosignal.py @@ -18,15 +18,15 @@ class WriteToSignal(QObject): Qt-signals. Mainly used to communicate stdout/stderr across threads""" + sig_write_received = Signal(str) + def __init__(self, original_out): QObject.__init__(self) # If the file descriptor of the stream is < 0 then we are running in a no-external-console mode if original_out.fileno() < 0: - self.__original_out = None + self._original_out = None else: - self.__original_out = original_out - - sig_write_received = Signal(str) + self._original_out = original_out def closed(self): return False @@ -38,9 +38,9 @@ class WriteToSignal(QObject): return False def write(self, txt): - if self.__original_out: + if self._original_out: try: - self.__original_out.write(txt) + self._original_out.write(txt) except IOError as e: self.sig_write_received.emit("Error: Unable to write to the console of the process.\n" "This error is not related to your script's execution.\n"