1
1
import contextlib
2
2
import io
3
3
import os
4
- import pickle
5
4
import subprocess
6
5
import sys
7
6
import textwrap
8
- from io import StringIO
9
7
from io import UnsupportedOperation
10
8
from typing import BinaryIO
11
9
from typing import Generator
12
- from typing import List
13
- from typing import TextIO
14
10
15
11
import pytest
16
12
from _pytest import capture
@@ -827,48 +823,6 @@ def tmpfile(testdir) -> Generator[BinaryIO, None, None]:
827
823
f .close ()
828
824
829
825
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
-
872
826
@contextlib .contextmanager
873
827
def lsof_check ():
874
828
pid = os .getpid ()
@@ -1307,8 +1261,8 @@ def test_error_attribute_issue555(testdir):
1307
1261
"""
1308
1262
import sys
1309
1263
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 "
1312
1266
"""
1313
1267
)
1314
1268
reprec = testdir .inline_run ()
@@ -1383,15 +1337,6 @@ def test_spam_in_thread():
1383
1337
result .stdout .no_fnmatch_line ("*IOError*" )
1384
1338
1385
1339
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
-
1395
1340
def test_global_capture_with_live_logging (testdir ):
1396
1341
# Issue 3819
1397
1342
# capture should work with live cli logging
@@ -1497,8 +1442,9 @@ def test_fails():
1497
1442
result_with_capture = testdir .runpytest (str (p ))
1498
1443
1499
1444
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
1502
1448
)
1503
1449
1504
1450
@@ -1508,12 +1454,13 @@ def test_stderr_write_returns_len(capsys):
1508
1454
1509
1455
1510
1456
def 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 ()
1515
1462
tmpfile .seek (0 )
1516
- assert tmpfile .read () == b"line1line2 "
1463
+ assert tmpfile .read () == b"line3line4 "
1517
1464
tmpfile .close ()
1518
1465
with pytest .raises (ValueError ):
1519
1466
ef .read ()
0 commit comments