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

pytest suppresses object allocation info in ResourceWarnings when PYTHONTRACEMALLOC is enabled #7731

Closed
mgedmin opened this issue Sep 7, 2020 · 4 comments
Labels
plugin: warnings related to the warnings builtin plugin type: enhancement new feature or API change, should be merged into features branch

Comments

@mgedmin
Copy link
Contributor

mgedmin commented Sep 7, 2020

Recent Python 3 versions (I think 3.7+?) show additional information in ResourceWarnings when you enable PYTHONTRACEMALLOC:

$ cat demo.py
def leak():
    x = open('/dev/null')


if __name__ == "__main__":
    leak()

$ export PYTHONWARNINGS=all
$ export PYTHONTRACEMALLOC=10
$ python3 demo.py
demo.py:6: ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
  leak()
Object allocated at (most recent call last):
  File "demo.py", lineno 6
    leak()
  File "demo.py", lineno 2
    x = open('/dev/null')

However pytest suppresses this somehow:

$ cat test.py
import demo


def test_leak():
    demo.leak()

$ export PYTHONWARNINGS=all
$ export PYTHONTRACEMALLOC=10
$ pytest test.py
/home/mg/.local/pipx/venvs/pytest/lib/python3.8/site-packages/_pytest/compat.py:340: PytestDeprecationWarning: The TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.
See https://docs.pytest.org/en/stable/deprecations.html#terminalreporter-writer for more information.
  return getattr(object, name, default)
============================= test session starts ==============================
platform linux -- Python 3.8.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/mg/tmp
collected 1 item                                                               

test.py .                                                                [100%]

=============================== warnings summary ===============================
test.py::test_leak
  /home/mg/tmp/test.py:5: ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
    demo.leak()

-- Docs: https://docs.pytest.org/en/stable/warnings.html
========================= 1 passed, 1 warning in 0.01s =========================

It's pretty much impossible to debug ResourceWarnings without seeing where the objects were created, so I'd love any suggestions.

(I have tried playing with pytest's command line options like -Wnone, --disable-warnings or -p no:warnings, but either I get the same uninformative ResourceWarning, or I don't get any warning at all. In none of the cases I managed to see the object allocation stack trace.)

@graingert
Copy link
Member

Related: #5299

@Zac-HD Zac-HD added plugin: warnings related to the warnings builtin plugin type: enhancement new feature or API change, should be merged into features branch labels Sep 8, 2020
@asottile
Copy link
Member

asottile commented Sep 8, 2020

dupe #5676

@asottile asottile closed this as completed Sep 8, 2020
@mgedmin
Copy link
Contributor Author

mgedmin commented Sep 9, 2020

That looks like a different bug to me? For me the ResourceWarning is captured and displayed, just without the full information that Python itself provides.

@asottile
Copy link
Member

asottile commented Sep 9, 2020

yeah, same as here: #5676 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: warnings related to the warnings builtin plugin type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

4 participants