22import os
33import sys
44from typing import Generator
5- from typing import TextIO
65
76import pytest
87from _pytest .config import Config
1110from _pytest .stash import StashKey
1211
1312
14- fault_handler_stderr_key = StashKey [TextIO ]()
13+ fault_handler_stderr_fd_key = StashKey [int ]()
1514fault_handler_originally_enabled_key = StashKey [bool ]()
1615
1716
@@ -26,22 +25,19 @@ def pytest_addoption(parser: Parser) -> None:
2625def pytest_configure (config : Config ) -> None :
2726 import faulthandler
2827
29- stderr_fd_copy = os .dup (get_stderr_fileno ())
30- config .stash [fault_handler_stderr_key ] = open (
31- stderr_fd_copy , "w" , encoding = sys .stderr .encoding
32- )
28+ config .stash [fault_handler_stderr_fd_key ] = os .dup (get_stderr_fileno ())
3329 config .stash [fault_handler_originally_enabled_key ] = faulthandler .is_enabled ()
34- faulthandler .enable (file = config .stash [fault_handler_stderr_key ])
30+ faulthandler .enable (file = config .stash [fault_handler_stderr_fd_key ])
3531
3632
3733def pytest_unconfigure (config : Config ) -> None :
3834 import faulthandler
3935
4036 faulthandler .disable ()
4137 # Close the dup file installed during pytest_configure.
42- if fault_handler_stderr_key in config .stash :
43- config .stash [fault_handler_stderr_key ]. close ( )
44- del config .stash [fault_handler_stderr_key ]
38+ if fault_handler_stderr_fd_key in config .stash :
39+ os . close ( config .stash [fault_handler_stderr_fd_key ] )
40+ del config .stash [fault_handler_stderr_fd_key ]
4541 if config .stash .get (fault_handler_originally_enabled_key , False ):
4642 # Re-enable the faulthandler if it was originally enabled.
4743 faulthandler .enable (file = get_stderr_fileno ())
@@ -69,10 +65,10 @@ def get_timeout_config_value(config: Config) -> float:
6965@pytest .hookimpl (hookwrapper = True , trylast = True )
7066def pytest_runtest_protocol (item : Item ) -> Generator [None , None , None ]:
7167 timeout = get_timeout_config_value (item .config )
72- stderr = item .config .stash [fault_handler_stderr_key ]
73- if timeout > 0 and stderr is not None :
68+ if timeout > 0 :
7469 import faulthandler
7570
71+ stderr = item .config .stash [fault_handler_stderr_fd_key ]
7672 faulthandler .dump_traceback_later (timeout , file = stderr )
7773 try :
7874 yield
0 commit comments