11import contextlib
22import io
33import os
4- import pickle
54import subprocess
65import sys
76import textwrap
8- from io import StringIO
97from io import UnsupportedOperation
108from typing import BinaryIO
119from typing import Generator
12- from typing import List
13- from typing import TextIO
1410
1511import pytest
1612from _pytest import capture
@@ -865,49 +861,6 @@ def tmpfile(testdir) -> Generator[BinaryIO, None, None]:
865861 f .close ()
866862
867863
868- @needsosdup
869- def test_dupfile (tmpfile ) -> None :
870- flist = [] # type: List[TextIO]
871- for i in range (5 ):
872- nf = capture .safe_text_dupfile (tmpfile , "wb" )
873- assert nf != tmpfile
874- assert nf .fileno () != tmpfile .fileno ()
875- assert nf not in flist
876- print (i , end = "" , file = nf )
877- flist .append (nf )
878-
879- fname_open = flist [0 ].name
880- assert fname_open == repr (flist [0 ].buffer )
881-
882- for i in range (5 ):
883- f = flist [i ]
884- f .close ()
885- fname_closed = flist [0 ].name
886- assert fname_closed == repr (flist [0 ].buffer )
887- assert fname_closed != fname_open
888- tmpfile .seek (0 )
889- s = tmpfile .read ()
890- assert "01234" in repr (s )
891- tmpfile .close ()
892- assert fname_closed == repr (flist [0 ].buffer )
893-
894-
895- def test_dupfile_on_bytesio ():
896- bio = io .BytesIO ()
897- f = capture .safe_text_dupfile (bio , "wb" )
898- f .write ("hello" )
899- assert bio .getvalue () == b"hello"
900- assert "BytesIO object" in f .name
901-
902-
903- def test_dupfile_on_textio ():
904- sio = StringIO ()
905- f = capture .safe_text_dupfile (sio , "wb" )
906- f .write ("hello" )
907- assert sio .getvalue () == "hello"
908- assert not hasattr (f , "name" )
909-
910-
911864@contextlib .contextmanager
912865def lsof_check ():
913866 pid = os .getpid ()
@@ -1355,8 +1308,8 @@ def test_error_attribute_issue555(testdir):
13551308 """
13561309 import sys
13571310 def test_capattr():
1358- assert sys.stdout.errors == "strict "
1359- assert sys.stderr.errors == "strict "
1311+ assert sys.stdout.errors == "replace "
1312+ assert sys.stderr.errors == "replace "
13601313 """
13611314 )
13621315 reprec = testdir .inline_run ()
@@ -1431,15 +1384,6 @@ def test_spam_in_thread():
14311384 result .stdout .no_fnmatch_line ("*IOError*" )
14321385
14331386
1434- def test_pickling_and_unpickling_encoded_file ():
1435- # See https://bitbucket.org/pytest-dev/pytest/pull-request/194
1436- # pickle.loads() raises infinite recursion if
1437- # EncodedFile.__getattr__ is not implemented properly
1438- ef = capture .EncodedFile (None , None )
1439- ef_as_str = pickle .dumps (ef )
1440- pickle .loads (ef_as_str )
1441-
1442-
14431387def test_global_capture_with_live_logging (testdir ):
14441388 # Issue 3819
14451389 # capture should work with live cli logging
@@ -1545,8 +1489,9 @@ def test_fails():
15451489 result_with_capture = testdir .runpytest (str (p ))
15461490
15471491 assert result_with_capture .ret == result_without_capture .ret
1548- result_with_capture .stdout .fnmatch_lines (
1549- ["E * TypeError: write() argument must be str, not bytes" ]
1492+ out = result_with_capture .stdout .str ()
1493+ assert ("TypeError: write() argument must be str, not bytes" in out ) or (
1494+ "TypeError: unicode argument expected, got 'bytes'" in out
15501495 )
15511496
15521497
@@ -1556,12 +1501,13 @@ def test_stderr_write_returns_len(capsys):
15561501
15571502
15581503def test_encodedfile_writelines (tmpfile : BinaryIO ) -> None :
1559- ef = capture .EncodedFile (tmpfile , "utf-8" )
1560- with pytest .raises (AttributeError ):
1561- ef .writelines ([b"line1" , b"line2" ]) # type: ignore[list-item] # noqa: F821
1562- assert ef .writelines (["line1" , "line2" ]) is None # type: ignore[func-returns-value] # noqa: F821
1504+ ef = capture .EncodedFile (tmpfile , encoding = "utf-8" )
1505+ with pytest .raises (TypeError ):
1506+ ef .writelines ([b"line1" , b"line2" ])
1507+ assert ef .writelines (["line3" , "line4" ]) is None # type: ignore[func-returns-value] # noqa: F821
1508+ ef .flush ()
15631509 tmpfile .seek (0 )
1564- assert tmpfile .read () == b"line1line2 "
1510+ assert tmpfile .read () == b"line3line4 "
15651511 tmpfile .close ()
15661512 with pytest .raises (ValueError ):
15671513 ef .read ()
0 commit comments