Skip to content

"The handle is invalid" error when trying to use a std stream saved before teh call to pytest.main #12876

Open
@ikappaki

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:])
  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 list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    plugin: capturerelated to the capture builtin plugintype: docsdocumentation improvement, missing or needing clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions