Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResourceWarning from some tests in python<=3.9 #9707

Open
asottile opened this issue Feb 22, 2022 · 1 comment
Open

ResourceWarning from some tests in python<=3.9 #9707

asottile opened this issue Feb 22, 2022 · 1 comment
Labels
type: selftests a problem in the tests of pytest

Comments

@asottile
Copy link
Member

asottile commented Feb 22, 2022

a bit of a self-tests issue -- I noticed this while implementing a test bisector

I haven't quite figured out the "why" for this yet either -- for me it only reproduces in python<=3.9 and not in python3.10+

$ ./venv/bin/pytest testing/test_recwarn.py::TestWarns::test_can_capture_previously_warned 'testing/test_terminal.py::test_summary_stats[red-exp_line0-stats_arg0]'
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.1.0.dev230+g3b549642e, pluggy-1.0.0
rootdir: /tmp/pytest, configfile: pyproject.toml
plugins: hypothesis-6.37.2
collected 2 items                                                              

testing/test_recwarn.py .                                                [ 50%]
testing/test_terminal.py .                                               [100%]

============================== 2 passed in 0.15s ===============================
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name="<_io.FileIO name=14 mode='rb+' closefd=True>" mode='r+' encoding='utf-8'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name="<_io.FileIO name=16 mode='rb+' closefd=True>" mode='r+' encoding='utf-8'>

running either of those tests separately does not seem to cause the issue -- but running them together leads to these 3 leaked files

@pytest.mark.filterwarnings("ignore")
def test_can_capture_previously_warned(self) -> None:
def f() -> int:
warnings.warn(UserWarning("ohai"))
return 10
assert f() == 10
assert pytest.warns(UserWarning, f) == 10
assert pytest.warns(UserWarning, f) == 10
assert pytest.warns(UserWarning, f) != "10" # type: ignore[comparison-overlap]

def test_summary_stats(
tr: TerminalReporter,
exp_line: List[Tuple[str, Dict[str, bool]]],
exp_color: str,
stats_arg: Dict[str, List[object]],
) -> None:
tr.stats = stats_arg
# Fake "_is_last_item" to be True.
class fake_session:
testscollected = 0
tr._session = fake_session # type: ignore[assignment]
assert tr._is_last_item
# Reset cache.
tr._main_color = None
print("Based on stats: %s" % stats_arg)
print(f'Expect summary: "{exp_line}"; with color "{exp_color}"')
(line, color) = tr.build_summary_stats_line()
print(f'Actually got: "{line}"; with color "{color}"')
assert line == exp_line
assert color == exp_color

@asottile asottile added the type: selftests a problem in the tests of pytest label Feb 22, 2022
@nicoddemus
Copy link
Member

nicoddemus commented Feb 25, 2022

Thanks for looking into this!

I can reproduce the same warnings on Windows.

Using python -Xdev -Xtracemalloc=20 -m pytest... does not show any additional information.

Passing -s the warnings go away, suggesting it is related to capture.py. Also with --capture=sys the warnings don't appear, suggesting further it is related to FDCapture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: selftests a problem in the tests of pytest
Projects
None yet
Development

No branches or pull requests

2 participants