예제 #1
0
def test_real_fh_nosetests():
    # sys.stdout and sys.stderr have been monkey-patched by nosetests
    # with a custom class (not io.StringIO!)
    with sh.real_fh(sys.stdout) as rfh:
        assert rfh is not sys.stdout
        assert rfh.fileno() > 2
        rfh.write("Hello world")
    with sh.real_fh(sys.stderr) as rfh:
        assert rfh is not sys.stderr
        assert rfh.fileno() > 2
        rfh.write("Hello world")
예제 #2
0
def test_real_fh_fullpipe():
    # Exceed the typical size of a pipe (64 kbytes on Linux)
    # in an attempt to trigger a deadlock if the pipe isn't
    # continuously flushed.
    fh = io.StringIO()
    payload = "x" * int(2**20)  # 1MB payload
    with sh.real_fh(fh) as rfh:
        rfh.write(payload)
    assert fh.getvalue() == payload
예제 #3
0
def test_real_fh_crash():
    # Test that the output copy is wrapped by a `finally` clause,
    # so that it is not lost if the wrapped code raises an Exception
    fh = io.StringIO()
    with pytest.raises(StubError):
        with sh.real_fh(fh) as rfh:
            rfh.write("Hello world")
            raise StubError()

    assert fh.getvalue() == "Hello world"
예제 #4
0
def test_real_fh_crash():
    # Test that the output copy is wrapped by a `finally` clause,
    # so that it is not lost if the wrapped code raises an Exception
    fh = io.StringIO()
    try:
        with sh.real_fh(fh) as rfh:
            rfh.write("Hello world")
            raise StubError()
    except StubError:
        pass
    else:
        # Exception isn't masked
        assert False

    assert fh.getvalue() == "Hello world"
예제 #5
0
def test_real_fh_bytesio():
    fh = io.BytesIO()
    with sh.real_fh(fh) as rfh:
        assert rfh.fileno() > 2
        rfh.write(b"Hello world")
    assert fh.getvalue() == b"Hello world"
예제 #6
0
def test_real_fh_stringio():
    fh = io.StringIO()
    with sh.real_fh(fh) as rfh:
        assert rfh.fileno() > 2
        rfh.write("Hello world")
    assert fh.getvalue() == "Hello world"
예제 #7
0
def test_real_fh_trivial():
    # Real POSIX-backed file handle
    with tempfile.TemporaryFile() as fh:
        with sh.real_fh(fh) as rfh:
            assert rfh is fh
예제 #8
0
def test_real_fh_none():
    # Required by call, check_call, check_output
    with sh.real_fh(None) as rfh:
        assert rfh is None