-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Expand warnings output for ResourceWarning #9682
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
More information about the location of resources that led Python to raise :class:`ResourceWarning` can now | ||
be obtained by enabling :mod:`tracemalloc`. | ||
|
||
See :ref:`resource-warnings` for more information. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import os | ||
import sys | ||
import warnings | ||
from typing import List | ||
from typing import Optional | ||
|
@@ -774,3 +775,53 @@ def test_it(): | |
"*Unknown pytest.mark.unknown*", | ||
] | ||
) | ||
|
||
|
||
def test_resource_warning(pytester: Pytester, monkeypatch: pytest.MonkeyPatch) -> None: | ||
# Some platforms (notably PyPy) don't have tracemalloc. | ||
# We choose to explicitly not skip this in case tracemalloc is not | ||
# available, using `importorskip("tracemalloc")` for example, | ||
# because we want to ensure the same code path does not break in those platforms. | ||
try: | ||
import tracemalloc # noqa | ||
|
||
has_tracemalloc = True | ||
except ImportError: | ||
has_tracemalloc = False | ||
|
||
pytester.makepyfile( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test (tracemalloc not enabled) will fail if the person running the pytest test suite enables tracemalloc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm why do you say that? And do you mean running in CPython? I tried it here:
And it passes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note the try/except is checking if tracemalloc is available at all, which is not on PyPy for example, not if tracemalloc is enabled or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh got it, good catch. 😁 Updated the test, thanks. |
||
""" | ||
def open_file(p): | ||
f = p.open("r") | ||
assert p.read_text() == "hello" | ||
|
||
def test_resource_warning(tmp_path): | ||
p = tmp_path.joinpath("foo.txt") | ||
p.write_text("hello") | ||
open_file(p) | ||
""" | ||
) | ||
result = pytester.run(sys.executable, "-Xdev", "-m", "pytest") | ||
expected_extra = ( | ||
[ | ||
"*ResourceWarning* unclosed file*", | ||
"*Enable tracemalloc to get traceback where the object was allocated*", | ||
"*See https* for more info.", | ||
] | ||
if has_tracemalloc | ||
else [] | ||
) | ||
result.stdout.fnmatch_lines([*expected_extra, "*1 passed*"]) | ||
|
||
monkeypatch.setenv("PYTHONTRACEMALLOC", "20") | ||
|
||
result = pytester.run(sys.executable, "-Xdev", "-m", "pytest") | ||
expected_extra = ( | ||
[ | ||
"*ResourceWarning* unclosed file*", | ||
"*Object allocated at*", | ||
] | ||
if has_tracemalloc | ||
else [] | ||
) | ||
result.stdout.fnmatch_lines([*expected_extra, "*1 passed*"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there platforms which don't have
tracemalloc
available?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pypy3 CI failed with
ImportError: cannot import _tracemalloc
, so I went ahead and assumed that was the case, but didn't look into official sources TBH.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is likely that PyPy doesn't support it at all due to differing memory management.