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
@@ -827,48 +823,6 @@ def tmpfile(testdir) -> Generator[BinaryIO, None, None]:
827823 f .close ()
828824
829825
830- def test_dupfile (tmpfile ) -> None :
831- flist = [] # type: List[TextIO]
832- for i in range (5 ):
833- nf = capture .safe_text_dupfile (tmpfile , "wb" )
834- assert nf != tmpfile
835- assert nf .fileno () != tmpfile .fileno ()
836- assert nf not in flist
837- print (i , end = "" , file = nf )
838- flist .append (nf )
839-
840- fname_open = flist [0 ].name
841- assert fname_open == repr (flist [0 ].buffer )
842-
843- for i in range (5 ):
844- f = flist [i ]
845- f .close ()
846- fname_closed = flist [0 ].name
847- assert fname_closed == repr (flist [0 ].buffer )
848- assert fname_closed != fname_open
849- tmpfile .seek (0 )
850- s = tmpfile .read ()
851- assert "01234" in repr (s )
852- tmpfile .close ()
853- assert fname_closed == repr (flist [0 ].buffer )
854-
855-
856- def test_dupfile_on_bytesio ():
857- bio = io .BytesIO ()
858- f = capture .safe_text_dupfile (bio , "wb" )
859- f .write ("hello" )
860- assert bio .getvalue () == b"hello"
861- assert "BytesIO object" in f .name
862-
863-
864- def test_dupfile_on_textio ():
865- sio = StringIO ()
866- f = capture .safe_text_dupfile (sio , "wb" )
867- f .write ("hello" )
868- assert sio .getvalue () == "hello"
869- assert not hasattr (f , "name" )
870-
871-
872826@contextlib .contextmanager
873827def lsof_check ():
874828 pid = os .getpid ()
@@ -1307,8 +1261,8 @@ def test_error_attribute_issue555(testdir):
13071261 """
13081262 import sys
13091263 def test_capattr():
1310- assert sys.stdout.errors == "strict "
1311- assert sys.stderr.errors == "strict "
1264+ assert sys.stdout.errors == "replace "
1265+ assert sys.stderr.errors == "replace "
13121266 """
13131267 )
13141268 reprec = testdir .inline_run ()
@@ -1383,15 +1337,6 @@ def test_spam_in_thread():
13831337 result .stdout .no_fnmatch_line ("*IOError*" )
13841338
13851339
1386- def test_pickling_and_unpickling_encoded_file ():
1387- # See https://bitbucket.org/pytest-dev/pytest/pull-request/194
1388- # pickle.loads() raises infinite recursion if
1389- # EncodedFile.__getattr__ is not implemented properly
1390- ef = capture .EncodedFile (None , None )
1391- ef_as_str = pickle .dumps (ef )
1392- pickle .loads (ef_as_str )
1393-
1394-
13951340def test_global_capture_with_live_logging (testdir ):
13961341 # Issue 3819
13971342 # capture should work with live cli logging
@@ -1497,8 +1442,9 @@ def test_fails():
14971442 result_with_capture = testdir .runpytest (str (p ))
14981443
14991444 assert result_with_capture .ret == result_without_capture .ret
1500- result_with_capture .stdout .fnmatch_lines (
1501- ["E * TypeError: write() argument must be str, not bytes" ]
1445+ out = result_with_capture .stdout .str ()
1446+ assert ("TypeError: write() argument must be str, not bytes" in out ) or (
1447+ "TypeError: unicode argument expected, got 'bytes'" in out
15021448 )
15031449
15041450
@@ -1508,12 +1454,13 @@ def test_stderr_write_returns_len(capsys):
15081454
15091455
15101456def test_encodedfile_writelines (tmpfile : BinaryIO ) -> None :
1511- ef = capture .EncodedFile (tmpfile , "utf-8" )
1512- with pytest .raises (AttributeError ):
1513- ef .writelines ([b"line1" , b"line2" ]) # type: ignore[list-item] # noqa: F821
1514- assert ef .writelines (["line1" , "line2" ]) is None # type: ignore[func-returns-value] # noqa: F821
1457+ ef = capture .EncodedFile (tmpfile , encoding = "utf-8" )
1458+ with pytest .raises (TypeError ):
1459+ ef .writelines ([b"line1" , b"line2" ])
1460+ assert ef .writelines (["line3" , "line4" ]) is None # type: ignore[func-returns-value] # noqa: F821
1461+ ef .flush ()
15151462 tmpfile .seek (0 )
1516- assert tmpfile .read () == b"line1line2 "
1463+ assert tmpfile .read () == b"line3line4 "
15171464 tmpfile .close ()
15181465 with pytest .raises (ValueError ):
15191466 ef .read ()
0 commit comments