pytest-dev / pytest-dev/pytest
"The handle is invalid" error when trying to use a std stream saved before teh call to `pytest.main`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
Hi,
it seems there's an issue when trying to use a standard system output stream that was saved before calling pytest.main on MS-Windows
E OSError: [WinError 6] The handle is invalid
To reproduce on MS-Windows 11:
1.Create a script that calls pytest.main but saves sys.stdout in another module before doing so. A test file will then attempt to write to this saved stdout stream
save.py
STDOUT = None
issue_test.py
import save
import sys
save.STDOUT.write(":save.STDOUT\n")
def test_addition():
assert 1 + 1 == 2
ptissue.py
import pytest
import save
import sys
if __name__ == "__main__":
save.STDOUT = sys.stdout
pytest.main(sys.argv[1:])
- Run the script, you will encounter the error
> python .\ptissue.py
=============================================================================================================== test session starts ================================================================================================================
platform win32 -- Python 3.11.4, pytest-8.3.3, pluggy-1.5.0
collected 0 items / 1 error
====================================================================================================================== ERRORS ======================================================================================================================
__________________________________________________________________________________________________________ ERROR collecting issue_test.py __________________________________________________________________________________________________________
issue_test.py:4: in <module>
save.STDOUT.write(":save.STDOUT\n")
E OSError: [WinError 6] The handle is invalid
============================================================================================================= short test summary info ==============================================================================================================
ERROR issue_test.py - OSError: [WinError 6] The handle is invalid
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================================================================= 1 error in 0.08s =================================================================================================================
:save.STDOUT
The error does not occur if you pass the -s option:
> python .\ptissue.py -s
=============================================================================================================== test session starts ================================================================================================================
platform win32 -- Python 3.11.4, pytest-8.3.3, pluggy-1.5.0
collecting ... :save.STDOUT
collected 1 item
issue_test.py .
================================================================================================================ 1 passed in 0.01s =================================================================================================================
Thanks
> pip list
Package Version
---------- -------
colorama 0.4.6
iniconfig 2.0.0
packaging 24.1
pip 23.1.2
pluggy 1.5.0
pytest 8.3.3
setuptools 65.5.0
> pytest --version
pytest 8.3.3
- a detailed description of the bug or problem you are having
- output of
pip listfrom the virtual environment you are using - pytest and operating system versions
- minimal example if possible
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Windows reproduction using ptissue.py, issue_test.py, and save.py, focusing on pytest.main with and without -s. Trace how pytest handles the saved sys.stdout during collection; done means the saved stream can be written without requiring -s and the regression is covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100