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
Conversation
Here's the output for the test in the suite:
|
db8dd8e
to
92da79a
Compare
92da79a
to
cdd827b
Compare
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.
Nice! Thanks for the PR, you beat me to it 😉
src/_pytest/warnings.py
Outdated
# Use a leading new line to better separate the (large) output | ||
# from the traceback to the previous warning text. | ||
msg += ( | ||
f"\nObject allocated at (most recent call first):\n{formatted_tb}" |
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.
I'm not sure most recent call first
is correct here, maybe just remove it?
@@ -81,6 +81,25 @@ def warning_record_to_str(warning_message: warnings.WarningMessage) -> str: | |||
warning_message.lineno, | |||
warning_message.line, | |||
) | |||
if warning_message.source is not None: | |||
try: | |||
import tracemalloc |
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.
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.
Looks great to me! I left one comment.
except ImportError: | ||
has_tracemalloc = False | ||
|
||
pytester.makepyfile( |
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.
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 comment
The 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:
python -X tracemalloc=20 -m pytest testing\test_warnings.py
And it passes.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I guess python -X tracemalloc=20
doesn't transfer, but an envvar does, so try this:
PYTHONTRACEMALLOC=100 pytest testing/test_warnings.py -k resource
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.
Ahh got it, good catch. 😁
Updated the test, thanks.
Fix #9644
cc @fschulze