Exemplo n.º 1
0
def safe_dupfile(f):
    if pytest.__version__ < "6.0.0":
        from _pytest.capture import safe_text_dupfile
        return safe_text_dupfile(f, mode=f.mode)
    else:
        from _pytest.capture import EncodedFile
        default_encoding = "UTF8"

        encoding = getattr(f, "encoding", None)
        try:
            fd = f.fileno()
        except OSError:
            if "b" not in getattr(f, "mode", "") and hasattr(f, "encoding"):
                # we seem to have a text stream, let's just use it
                return f

        newfd = os.dup(fd)
        mode = f.mode
        if "b" not in mode:
            mode += "b"
        f = os.fdopen(newfd, mode, 0)  # no buffering

        return EncodedFile(f,
                           encoding or default_encoding,
                           errors="replace",
                           write_through=True)
Exemplo n.º 2
0
 def __init__(self, terminalreporter, *, rewrite=False):
     self.tr = terminalreporter
     # copy the original stdout for use with CaptureFixture
     self.tr._tw._file = safe_text_dupfile(self.tr._tw._file, mode=self.tr._tw._file.mode)
     self.rewrite = rewrite
     self.__reset()
     steps.subscribe(self.notify)
Exemplo n.º 3
0
def test_dupfile(tmpfile):
    flist = []
    for i in range(5):
        nf = capture.safe_text_dupfile(tmpfile, "wb")
        assert nf != tmpfile
        assert nf.fileno() != tmpfile.fileno()
        assert nf not in flist
        print(i, end="", file=nf)
        flist.append(nf)
    for i in range(5):
        f = flist[i]
        f.close()
    tmpfile.seek(0)
    s = tmpfile.read()
    assert "01234" in repr(s)
    tmpfile.close()
Exemplo n.º 4
0
def test_dupfile(tmpfile):
    flist = []
    for i in range(5):
        nf = capture.safe_text_dupfile(tmpfile, "wb")
        assert nf != tmpfile
        assert nf.fileno() != tmpfile.fileno()
        assert nf not in flist
        print_(i, end="", file=nf)
        flist.append(nf)
    for i in range(5):
        f = flist[i]
        f.close()
    tmpfile.seek(0)
    s = tmpfile.read()
    assert "01234" in repr(s)
    tmpfile.close()
Exemplo n.º 5
0
def test_dupfile(tmpfile):
    flist = []
    for i in range(5):
        nf = capture.safe_text_dupfile(tmpfile, "wb")
        assert nf != tmpfile
        assert nf.fileno() != tmpfile.fileno()
        assert nf not in flist
        print(i, end="", file=nf)
        flist.append(nf)

    fname_open = flist[0].name
    assert fname_open == repr(flist[0].buffer)

    for i in range(5):
        f = flist[i]
        f.close()
    fname_closed = flist[0].name
    assert fname_closed == repr(flist[0].buffer)
    assert fname_closed != fname_open
    tmpfile.seek(0)
    s = tmpfile.read()
    assert "01234" in repr(s)
    tmpfile.close()
    assert fname_closed == repr(flist[0].buffer)
Exemplo n.º 6
0
def test_dupfile_on_textio():
    io = py.io.TextIO()
    f = capture.safe_text_dupfile(io, "wb")
    f.write("hello")
    assert io.getvalue() == "hello"
    assert not hasattr(f, 'name')
Exemplo n.º 7
0
def test_dupfile_on_bytesio():
    io = py.io.BytesIO()
    f = capture.safe_text_dupfile(io, "wb")
    f.write("hello")
    assert io.getvalue() == b"hello"
    assert 'BytesIO object' in f.name
Exemplo n.º 8
0
def test_dupfile_on_textio():
    io = py.io.TextIO()
    f = capture.safe_text_dupfile(io, "wb")
    f.write("hello")
    assert io.getvalue() == "hello"
    assert not hasattr(f, 'name')
Exemplo n.º 9
0
def test_dupfile_on_bytesio():
    io = py.io.BytesIO()
    f = capture.safe_text_dupfile(io, "wb")
    f.write("hello")
    assert io.getvalue() == b"hello"
    assert 'BytesIO object' in f.name
Exemplo n.º 10
0
def test_dupfile_on_bytesio():
    bio = io.BytesIO()
    f = capture.safe_text_dupfile(bio, "wb")
    f.write("hello")
    assert bio.getvalue() == b"hello"
    assert "BytesIO object" in f.name
Exemplo n.º 11
0
def test_dupfile_on_textio():
    sio = StringIO()
    f = capture.safe_text_dupfile(sio, "wb")
    f.write("hello")
    assert sio.getvalue() == "hello"
    assert not hasattr(f, "name")
Exemplo n.º 12
0
def test_dupfile_on_bytesio():
    bio = io.BytesIO()
    f = capture.safe_text_dupfile(bio, "wb")
    f.write("hello")
    assert bio.getvalue() == b"hello"
    assert "BytesIO object" in f.name