File tree 2 files changed +50
-2
lines changed
2 files changed +50
-2
lines changed Original file line number Diff line number Diff line change @@ -594,7 +594,10 @@ def _done(self):
594
594
os .dup2 (targetfd_save , self .targetfd )
595
595
os .close (targetfd_save )
596
596
self .syscapture .done ()
597
- self .tmpfile .close ()
597
+ # Redirect any remaining output.
598
+ os .dup2 (self .targetfd , self .tmpfile .buffer .fileno ())
599
+ # TODO: atexit?
600
+ # self.tmpfile.close()
598
601
self ._state = "done"
599
602
600
603
def suspend (self ):
@@ -665,7 +668,8 @@ def snap(self):
665
668
def done (self ):
666
669
setattr (sys , self .name , self ._old )
667
670
del self ._old
668
- self .tmpfile .close ()
671
+ # TODO: atexit?
672
+ # self.tmpfile.close()
669
673
self ._state = "done"
670
674
671
675
def suspend (self ):
Original file line number Diff line number Diff line change @@ -1510,3 +1510,47 @@ def test_fails():
1510
1510
result_with_capture .stdout .fnmatch_lines (
1511
1511
["E TypeError: write() argument must be str, not bytes" ]
1512
1512
)
1513
+
1514
+
1515
+ def test_logging_in_atexit (testdir ):
1516
+ p = testdir .makepyfile (
1517
+ """
1518
+ import atexit
1519
+ import logging
1520
+ import sys
1521
+
1522
+ cur_stdout = sys.stdout
1523
+ LOGGER = logging.getLogger(__name__)
1524
+
1525
+ def test_fail():
1526
+ assert 0
1527
+
1528
+ def _atexit():
1529
+ print("test-print in atexit", cur_stdout)
1530
+ LOGGER.error("test-log in atexit")
1531
+
1532
+ print()
1533
+ print("test-register")
1534
+ print()
1535
+ atexit.register(_atexit)
1536
+ logging.basicConfig()
1537
+
1538
+ LOGGER.error("log_setup_not_shown_from_collection")
1539
+
1540
+ print(sys.stderr, id(sys.stderr))
1541
+ """
1542
+ )
1543
+ result = testdir .runpytest_subprocess (str (p ))
1544
+ result .stdout .fnmatch_lines (
1545
+ [
1546
+ "*= 1 failed in *" ,
1547
+ "test-print in atexit <_pytest.capture.EncodedFile object *" ,
1548
+ ]
1549
+ )
1550
+ assert result .stderr .lines == ["ERROR:test_logging_in_atexit:test-log in atexit" ]
1551
+ assert result .ret == 1
1552
+
1553
+ output = str (result .stdout ) + str (result .stderr )
1554
+ assert "test-register" not in output
1555
+ assert "*- Captured stderr call -*" not in output
1556
+ assert "log_setup_not_shown_from_collection" not in output
You can’t perform that action at this time.
0 commit comments