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
@@ -865,49 +861,6 @@ def tmpfile(testdir) -> Generator[BinaryIO, None, None]:
865
861
f .close ()
866
862
867
863
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
-
911
864
@contextlib .contextmanager
912
865
def lsof_check ():
913
866
pid = os .getpid ()
@@ -1355,8 +1308,8 @@ def test_error_attribute_issue555(testdir):
1355
1308
"""
1356
1309
import sys
1357
1310
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 "
1360
1313
"""
1361
1314
)
1362
1315
reprec = testdir .inline_run ()
@@ -1431,15 +1384,6 @@ def test_spam_in_thread():
1431
1384
result .stdout .no_fnmatch_line ("*IOError*" )
1432
1385
1433
1386
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
-
1443
1387
def test_global_capture_with_live_logging (testdir ):
1444
1388
# Issue 3819
1445
1389
# capture should work with live cli logging
@@ -1545,8 +1489,9 @@ def test_fails():
1545
1489
result_with_capture = testdir .runpytest (str (p ))
1546
1490
1547
1491
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
1550
1495
)
1551
1496
1552
1497
@@ -1556,12 +1501,13 @@ def test_stderr_write_returns_len(capsys):
1556
1501
1557
1502
1558
1503
def 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 ()
1563
1509
tmpfile .seek (0 )
1564
- assert tmpfile .read () == b"line1line2 "
1510
+ assert tmpfile .read () == b"line3line4 "
1565
1511
tmpfile .close ()
1566
1512
with pytest .raises (ValueError ):
1567
1513
ef .read ()
0 commit comments