Python contextlib.redirect_stderr() Examples

The following are 30 code examples of contextlib.redirect_stderr(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module contextlib , or try the search function .
Example #1
Source File: test_training.py    From vecto with Mozilla Public License 2.0 8 votes vote down vote up
def test_train_word2vec_subword_jap(self):
        path_corpus = "./tests/data/corpora/jap/tokenized/"
        path_word2chars = "./tests/data/corpora/jap/char2radical/char2radical.txt"
        sio = io.StringIO()
        with contextlib.redirect_stderr(sio):
            run_module('vecto.embeddings.train_word2vec',
                       ['--path_corpus', path_corpus, '--path_out', '/tmp/vecto/embeddings/', '--dimension', '5',
                        '--subword', 'sum', '--language', 'jap', '--min_gram', '1', '--max_gram', '1'])
            run_module('vecto.embeddings.train_word2vec',
                       ['--path_corpus', path_corpus, '--path_out', '/tmp/vecto/embeddings/', '--dimension', '5',
                        '--subword', 'sum', '--language', 'jap', '--min_gram', '1', '--max_gram', '1',
                        '--path_word2chars', path_word2chars])

            with self.assertRaises(RuntimeError):
                run_module('vecto.embeddings.train_word2vec',
                           ['--path_corpus', path_corpus + "NONEXISTING", '--path_out', '/tmp/vecto/embeddings/',
                            '--dimension', '5',
                            '--subword', 'sum', '--language', 'jap', '--min_gram', '1', '--max_gram', '1']) 
Example #2
Source File: pyscriptexecutor.py    From grinder with GNU General Public License v2.0 6 votes vote down vote up
def _exec_script(self, host_info, py_script) -> any:
        """
        Import an additional script and run it

        :param host_info: host information
        :param py_script: python script filename
        :return: script execution result
        """
        if isinstance(py_script, str) and py_script.endswith(".py"):
            full_path = self.base_path.joinpath(py_script)
            py_script_wo_extension = py_script.replace(".py", "")
            loader = SourceFileLoader("main", str(full_path))
            module = ModuleType(loader.name)
            loader.exec_module(module)
            script_result = {py_script_wo_extension: module.main(host_info)}
            if not self.mute:
                return script_result
            with redirect_stderr(None), redirect_stdout(None):
                return script_result 
Example #3
Source File: command_test.py    From stakkr with Apache License 2.0 6 votes vote down vote up
def test_command_without_stderr_but_stdout_err(self):
        # TODO make it work under windows
        if os.name == 'nt':
            return

        f = io.StringIO()
        with redirect_stdout(f):
            launch_cmd_displays_output(self.cmd_nook, True, False)
        res = f.getvalue()
        self.assertEqual('\n', res)

        try:
            from contextlib import redirect_stderr
        except Exception:
            return

        f = io.StringIO()
        with redirect_stderr(f):
            launch_cmd_displays_output(self.cmd_nook, True, False)
        res = f.getvalue()
        self.assertEqual('', res) 
Example #4
Source File: test_project.py    From signac-flow with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_run_fork(self):
        project = self.mock_project()
        output = StringIO()
        for job in project:
            job.doc.fork = True
            break

        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                with redirect_stderr(output):
                    project.run()

        for job in project:
            if job.doc.get('fork'):
                assert os.getpid() != job.doc.test
            else:
                assert os.getpid() == job.doc.test 
Example #5
Source File: runner.py    From TestSlide with MIT License 6 votes vote down vote up
def _run_example(self, example):
        if example.focus and self.fail_if_focused:
            raise AssertionError(
                "Focused example not allowed with --fail-if-focused"
                ". Please remove the focus to allow the test to run."
            )
        if self.quiet:
            stdout = io.StringIO()
            stderr = io.StringIO()
            example_exception = None
            with redirect_stdout(stdout), redirect_stderr(stderr):
                try:
                    _ExampleRunner(example, self.formatter).run()
                except BaseException as ex:
                    example_exception = ex
            if example_exception:
                if not isinstance(example_exception, Skip):
                    if stdout.getvalue():
                        print("stdout:\n{}".format(stdout.getvalue()))
                    if stderr.getvalue():
                        print("stderr:\n{}".format(stderr.getvalue()))
                raise example_exception
        else:
            _ExampleRunner(example, self.formatter).run() 
Example #6
Source File: command_test.py    From stakkr with Apache License 2.0 6 votes vote down vote up
def test_command_with_stderr_no_stdout_err_loop(self):
        # TODO make it work under windows
        if os.name == 'nt':
            return

        f = io.StringIO()
        with redirect_stdout(f):
            cmd = ['cat', 'w', 'r', 'o', 'n', 'g', 'f', 'i', 'l', 'e']
            launch_cmd_displays_output(cmd, False, True)
        res = f.getvalue()
        expected = re.compile(r'.*\.\.\. and more.*', re.MULTILINE)
        self.assertRegex(res, expected)

        try:
            from contextlib import redirect_stderr
        except Exception:
            return

        f = io.StringIO()
        with redirect_stderr(f):
            launch_cmd_displays_output(self.cmd_nook, False, True)
        res = f.getvalue()
        self.assertEqual('', res) 
Example #7
Source File: command_test.py    From stakkr with Apache License 2.0 6 votes vote down vote up
def test_command_with_stderr_no_stdout_ok(self):
        # TODO make it work under windows
        if os.name == 'nt':
            return

        f = io.StringIO()
        with redirect_stdout(f):
            launch_cmd_displays_output(self.cmd_ok, False, True)
        res = f.getvalue()
        self.assertEqual('.', res[:1])

        try:
            from contextlib import redirect_stderr
        except Exception:
            return

        f = io.StringIO()
        with redirect_stderr(f):
            launch_cmd_displays_output(self.cmd_ok, False, True)
        res = f.getvalue()
        self.assertEqual('', res) 
Example #8
Source File: test_project.py    From signac-flow with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_cmd_with_job_error_handling(self):

        class A(FlowProject):
            pass

        @A.operation
        @with_job
        @cmd
        def test_context(job):
            return "exit 1"

        project = self.mock_project(A)
        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                starting_dir = os.getcwd()
                with pytest.raises(subprocess.CalledProcessError):
                    with redirect_stderr(StringIO()):
                        project.run()
                assert os.getcwd() == starting_dir 
Example #9
Source File: command_test.py    From stakkr with Apache License 2.0 6 votes vote down vote up
def test_command_with_stdout_ok(self):
        # TODO make it work under windows
        if os.name == 'nt':
            return

        f = io.StringIO()
        with redirect_stdout(f):
            launch_cmd_displays_output(self.cmd_ok, True, False)
        res = f.getvalue()
        self.assertEqual('coucou\n\n', res)

        try:
            from contextlib import redirect_stderr
        except Exception:
            return

        f = io.StringIO()
        with redirect_stderr(f):
            launch_cmd_displays_output(self.cmd_ok, True, False)
        res = f.getvalue()
        self.assertEqual('', res) 
Example #10
Source File: command_test.py    From stakkr with Apache License 2.0 6 votes vote down vote up
def test_command_without_stdout_ok(self):
        # TODO make it work under windows
        if os.name == 'nt':
            return

        f = io.StringIO()
        with redirect_stdout(f):
            launch_cmd_displays_output(self.cmd_ok, False, False)
        res = f.getvalue()
        self.assertEqual('.', res[:1])

        try:
            from contextlib import redirect_stderr
        except Exception:
            return

        f = io.StringIO()
        with redirect_stderr(f):
            launch_cmd_displays_output(self.cmd_ok, False, False)
        res = f.getvalue()
        self.assertEqual('', res) 
Example #11
Source File: command_test.py    From stakkr with Apache License 2.0 6 votes vote down vote up
def test_command_without_stderr_and_stdout_err(self):
        # TODO make it work under windows
        if os.name == 'nt':
            return

        f = io.StringIO()
        with redirect_stdout(f):
            launch_cmd_displays_output(self.cmd_nook, False, False)
        res = f.getvalue()
        self.assertEqual('\n', res)

        try:
            from contextlib import redirect_stderr
        except Exception:
            return

        f = io.StringIO()
        with redirect_stderr(f):
            launch_cmd_displays_output(self.cmd_nook, False, False)
        res = f.getvalue()
        self.assertEqual('', res) 
Example #12
Source File: test_project.py    From signac-flow with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_with_job_error_handling(self):

        class A(FlowProject):
            pass

        @A.operation
        @with_job
        def test_context(job):
            raise Exception

        project = self.mock_project(A)
        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                starting_dir = os.getcwd()
                with pytest.raises(Exception):
                    with redirect_stderr(StringIO()):
                        project.run()
                assert os.getcwd() == starting_dir 
Example #13
Source File: mainWindow.py    From fontgoggles with Apache License 2.0 6 votes vote down vote up
def setFontItemText(self, fontItemInfo, fontItem):
        font = fontItemInfo.font
        if font is None:
            return
        stderr = io.StringIO()
        with contextlib.redirect_stderr(stderr):
            glyphs = font.getGlyphRunFromTextInfo(self.textInfo,
                                                  features=self.project.textSettings.features,
                                                  varLocation=self.project.textSettings.varLocation,
                                                  colorLayers=self.project.textSettings.enableColor)
        stderr = stderr.getvalue()
        if stderr:
            fontItem.writeCompileOutput(stderr)

        addBoundingBoxes(glyphs)
        fontItem.glyphs = glyphs
        charSelection = self.characterList.getSelection()
        if charSelection:
            with self.blockCallbackRecursion():
                fontItem.selection = fontItem.glyphs.mapCharsToGlyphs(charSelection) 
Example #14
Source File: cloud_logging.py    From training_results_v0.5 with Apache License 2.0 6 votes vote down vote up
def configure(project=LOGGING_PROJECT):
    """Configures cloud logging

    This is called for all main calls. If a $LOGGING_PROJECT is environment
    variable configured, then STDERR and STDOUT are redirected to cloud
    logging.
    """
    if not project:
        sys.stderr.write('!! Error: The $LOGGING_PROJECT enviroment '
                         'variable is required in order to set up cloud logging. '
                         'Cloud logging is disabled.\n')
        return

    logging.basicConfig(level=logging.INFO)
    try:
        # if this fails, redirect stderr to /dev/null so no startup spam.
        with contextlib.redirect_stderr(io.StringIO()):
            client = glog.Client(project)
            client.setup_logging(logging.INFO)
    except:
        sys.stderr.write('!! Cloud logging disabled\n') 
Example #15
Source File: cloud_logging.py    From training_results_v0.5 with Apache License 2.0 6 votes vote down vote up
def configure(project=LOGGING_PROJECT):
    """Configures cloud logging

    This is called for all main calls. If a $LOGGING_PROJECT is environment
    variable configured, then STDERR and STDOUT are redirected to cloud
    logging.
    """
    if not project:
        sys.stderr.write('!! Error: The $LOGGING_PROJECT enviroment '
                         'variable is required in order to set up cloud logging. '
                         'Cloud logging is disabled.\n')
        return

    logging.basicConfig(level=logging.INFO)
    try:
        # if this fails, redirect stderr to /dev/null so no startup spam.
        with contextlib.redirect_stderr(io.StringIO()):
            client = glog.Client(project)
            client.setup_logging(logging.INFO)
    except:
        sys.stderr.write('!! Cloud logging disabled\n') 
Example #16
Source File: test_project.py    From signac-flow with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_with_job_works_with_cmd(self):

        class A(FlowProject):
            pass

        @A.operation
        @with_job
        @cmd
        def test_context(job):
            return "echo 'hello' > world.txt"

        project = self.mock_project(A)
        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                starting_dir = os.getcwd()
                with redirect_stderr(StringIO()):
                    project.run()
                assert os.getcwd() == starting_dir
                for job in project:
                    assert os.path.isfile(job.fn("world.txt")) 
Example #17
Source File: test_project.py    From signac-flow with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_with_job_decorator(self):

        class A(FlowProject):
            pass

        @A.operation
        @with_job
        def test_context(job):
            assert os.path.realpath(os.getcwd()) == os.path.realpath(job.ws)

        project = self.mock_project(A)
        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                starting_dir = os.getcwd()
                with redirect_stderr(StringIO()):
                    project.run()
                assert os.getcwd() == starting_dir 
Example #18
Source File: testing_utils.py    From neural_chat with MIT License 6 votes vote down vote up
def capture_output():
    """
    Suppress all stdout and stderr into a single buffer.

    Use as a context manager.

    :returns: the output
    :rtype: io.StringIO

    >>> with capture_output() as output:
    ...     print('hello')
    >>> output.getvalue()
    'hello'
    """
    sio = TeeStringIO()
    with contextlib.redirect_stdout(sio), contextlib.redirect_stderr(sio):
        yield sio 
Example #19
Source File: cloud_logging.py    From training with Apache License 2.0 6 votes vote down vote up
def configure(project=LOGGING_PROJECT):
    """Configures cloud logging

    This is called for all main calls. If a $LOGGING_PROJECT is environment
    variable configured, then STDERR and STDOUT are redirected to cloud
    logging.
    """
    if not project:
        sys.stderr.write('!! Error: The $LOGGING_PROJECT enviroment '
                         'variable is required in order to set up cloud logging. '
                         'Cloud logging is disabled.\n')
        return

    try:
        # if this fails, redirect stderr to /dev/null so no startup spam.
        with contextlib.redirect_stderr(io.StringIO()):
            client = glog.Client(project)
            client.setup_logging(logging.INFO)
    except:
        logging.basicConfig(level=logging.INFO)
        sys.stderr.write('!! Cloud logging disabled\n') 
Example #20
Source File: testing_utils.py    From KBRD with MIT License 6 votes vote down vote up
def capture_output():
    """
    Context manager which suppresses all stdout and stderr, and combines them
    into a single io.StringIO.

    :returns: the output
    :rtype: io.StringIO

    >>> with capture_output() as output:
    ...     print('hello')
    >>> output.getvalue()
    'hello'
    """
    sio = TeeStringIO()
    with contextlib.redirect_stdout(sio), contextlib.redirect_stderr(sio):
        yield sio 
Example #21
Source File: contexttools.py    From kur with Apache License 2.0 6 votes vote down vote up
def redirect_stderr(x):
	""" Redirects stderr to another file-like object.

		This is some compatibility code to support Python 3.4.
	"""
	if hasattr(contextlib, 'redirect_stderr'):
		result = contextlib.redirect_stderr
	else:
		@contextlib.contextmanager
		def result(x):
			""" Stand-in for Python 3.5's `redirect_stderr`.

				Notes: Non-reentrant, non-threadsafe
			"""
			old_stderr = sys.stderr
			sys.stderr = x
			yield
			sys.stder = old_stderr

	return result(x)

############################################################################### 
Example #22
Source File: test_cli.py    From stm32pio with MIT License 6 votes vote down vote up
def test_status(self):
        """
        Test the app output returned as a response to the 'status' command
        """

        buffer_stdout = io.StringIO()
        with contextlib.redirect_stdout(buffer_stdout), contextlib.redirect_stderr(None):
            return_code = stm32pio.app.main(sys_argv=['status', '-d', str(FIXTURE_PATH)], should_setup_logging=False)

        self.assertEqual(return_code, 0, msg="Non-zero return code")

        matches_counter = 0
        last_stage_pos = -1
        for stage in stm32pio.lib.ProjectStage:
            if stage != stm32pio.lib.ProjectStage.UNDEFINED:
                match = re.search(r"^((\[ \])|(\[\*\])) {2}" + str(stage) + '$', buffer_stdout.getvalue(), re.MULTILINE)
                self.assertTrue(match, msg="Status information was not found on STDOUT")
                if match:
                    matches_counter += 1
                    self.assertGreater(match.start(), last_stage_pos, msg="The order of stages is messed up")
                    last_stage_pos = match.start()

        self.assertEqual(matches_counter, len(stm32pio.lib.ProjectStage) - 1)  # UNDEFINED stage should not be printed 
Example #23
Source File: test_iostream.py    From pyslam with GNU General Public License v3.0 6 votes vote down vote up
def test_err(capfd):
    msg = "Something that should not show up in log"
    stream = StringIO()
    with redirect_stderr(stream):
        m.raw_err(msg)
    stdout, stderr = capfd.readouterr()
    assert stdout == ''
    assert stderr == msg
    assert stream.getvalue() == ''

    stream = StringIO()
    with redirect_stderr(stream):
        m.captured_err(msg)
    stdout, stderr = capfd.readouterr()
    assert stdout == ''
    assert stderr == ''
    assert stream.getvalue() == msg 
Example #24
Source File: stdlib.py    From tox with MIT License 6 votes vote down vote up
def suppress_output():
    """suppress both stdout and stderr outputs"""
    if sys.version_info >= (3, 5):
        from contextlib import redirect_stdout, redirect_stderr
    else:

        class _RedirectStream(object):

            _stream = None

            def __init__(self, new_target):
                self._new_target = new_target
                self._old_targets = []

            def __enter__(self):
                self._old_targets.append(getattr(sys, self._stream))
                setattr(sys, self._stream, self._new_target)
                return self._new_target

            def __exit__(self, exctype, excinst, exctb):
                setattr(sys, self._stream, self._old_targets.pop())

        class redirect_stdout(_RedirectStream):
            _stream = "stdout"

        class redirect_stderr(_RedirectStream):
            _stream = "stderr"

    with TemporaryFile("wt") as file:
        with redirect_stdout(file):
            with redirect_stderr(file):
                yield 
Example #25
Source File: test_iostream.py    From pyslam with GNU General Public License v3.0 6 votes vote down vote up
def test_redirect_both(capfd):
    msg = "StdOut"
    msg2 = "StdErr"

    stream = StringIO()
    stream2 = StringIO()
    with redirect_stdout(stream):
        with redirect_stderr(stream2):
            with m.ostream_redirect():
                m.raw_output(msg)
                m.raw_err(msg2)
    stdout, stderr = capfd.readouterr()
    assert stdout == ''
    assert stderr == ''
    assert stream.getvalue() == msg
    assert stream2.getvalue() == msg2 
Example #26
Source File: conftest.py    From cmd2 with MIT License 6 votes vote down vote up
def run_cmd(app, cmd):
    """ Clear out and err StdSim buffers, run the command, and return out and err """
    saved_sysout = sys.stdout
    sys.stdout = app.stdout

    # This will be used to capture app.stdout and sys.stdout
    copy_cmd_stdout = StdSim(app.stdout)

    # This will be used to capture sys.stderr
    copy_stderr = StdSim(sys.stderr)

    try:
        app.stdout = copy_cmd_stdout
        with redirect_stdout(copy_cmd_stdout):
            with redirect_stderr(copy_stderr):
                app.onecmd_plus_hooks(cmd)
    finally:
        app.stdout = copy_cmd_stdout.inner_stream
        sys.stdout = saved_sysout

    out = copy_cmd_stdout.getvalue()
    err = copy_stderr.getvalue()
    return normalize(out), normalize(err) 
Example #27
Source File: test_iostream.py    From pyslam with GNU General Public License v3.0 6 votes vote down vote up
def test_redirect_both(capfd):
    msg = "StdOut"
    msg2 = "StdErr"

    stream = StringIO()
    stream2 = StringIO()
    with redirect_stdout(stream):
        with redirect_stderr(stream2):
            with m.ostream_redirect():
                m.raw_output(msg)
                m.raw_err(msg2)
    stdout, stderr = capfd.readouterr()
    assert stdout == ''
    assert stderr == ''
    assert stream.getvalue() == msg
    assert stream2.getvalue() == msg2 
Example #28
Source File: test_cli.py    From reframe with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run_command_inline(argv, funct, *args, **kwargs):
    # Save current execution context
    argv_save = sys.argv
    environ_save = env.snapshot()
    sys.argv = argv
    exitcode = None

    captured_stdout = StringIO()
    captured_stderr = StringIO()
    print(*sys.argv)
    with redirect_stdout(captured_stdout):
        with redirect_stderr(captured_stderr):
            try:
                with rt.temp_runtime(None):
                    exitcode = funct(*args, **kwargs)
            except SystemExit as e:
                exitcode = e.code
            finally:
                # Restore execution context
                environ_save.restore()
                sys.argv = argv_save

    return (exitcode,
            captured_stdout.getvalue(),
            captured_stderr.getvalue()) 
Example #29
Source File: test_iostream.py    From pyslam with GNU General Public License v3.0 6 votes vote down vote up
def test_err(capfd):
    msg = "Something that should not show up in log"
    stream = StringIO()
    with redirect_stderr(stream):
        m.raw_err(msg)
    stdout, stderr = capfd.readouterr()
    assert stdout == ''
    assert stderr == msg
    assert stream.getvalue() == ''

    stream = StringIO()
    with redirect_stderr(stream):
        m.captured_err(msg)
    stdout, stderr = capfd.readouterr()
    assert stdout == ''
    assert stderr == ''
    assert stream.getvalue() == msg 
Example #30
Source File: external.py    From trafilatura with GNU General Public License v3.0 6 votes vote down vote up
def try_readability(htmlinput, url):
    '''Safety net: try with the generic algorithm readability'''
    # defaults: min_text_length=25, retry_length=250
    try:
        doc = LXMLDocument(htmlinput, url=url, min_text_length=25, retry_length=250)
        if MUFFLE_FLAG is False:
            resultstring = doc.summary(html_partial=True)
        else:
            with open(os.devnull, 'w') as devnull:
                with redirect_stderr(devnull):
                    resultstring = doc.summary(html_partial=True)
        newtree = html.fromstring(resultstring, parser=HTML_PARSER)
        return newtree
    except (etree.SerialisationError, Unparseable):
        return etree.Element('div')


# bypass parsing
#def my_bypass(html_tree, default_encoding, encoding, enc_errors):
#    return html_tree
#if justext:
#    justext.html_to_dom = my_bypass