GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pytest doesn't display ResourceWarnings that unittest does · Issue #5676 · pytest-dev/pytest

pytest doesn't display ResourceWarnings that unittest does #5676

New issue
Copy link
New issue
Copy link
Closed
Closed
pytest doesn't display ResourceWarnings that unittest does#5676
Copy link
Labels
plugin: warningsrelated to the warnings builtin plugintype: bugproblem that needs to be addressed

Description

@mraspberry
mraspberry
opened on Jul 30, 2019
Issue body actions

pytest doesn't seem to be displaying ResourceWarnings that unittest does display.
I'm using pytest 5.0.1 on python 3.6.8 on Ubuntu 18.04.2 LTS running in WSL on Windows 10.

Function being tested that triggers ResourceWarning in unittest but not pytest:

def trigger_resourcewarning(filename):
    return open(filename).read()

pytest test code:

import test_module

def test_module_trigger_resource_warning(tmp_path):
    contents = 'TEST\n'
    filename = tmp_path / 'test.txt'
    filename.write_text(contents)
    assert test_module.trigger_resourcewarning(filename) == contents

unittest test code:

import os
import tempfile
import unittest
import test_module

class TestModuleTriggerResourceWarning(unittest.TestCase):

    def test_module_trigger_resource_warning(self):
        contents = 'TEST\n'
        with tempfile.NamedTemporaryFile(delete=False) as fd:
            filename = fd.name
            fd.write(contents.encode())
        self.addCleanup(os.unlink, filename)
        self.assertEqual(test_module.trigger_resourcewarning(filename), contents)

pytest output:

platform linux -- Python 3.6.8, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /home/matt/projects/pytest-issue
collected 1 item

tests/test_resource_warning_proof.py .                                   [100%]

unittest output:

/home/matt/projects/pytest-issue/test_module/__init__.py:4: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/tmp1qw9u_dn' mode='r' encoding='UTF-8'>
  return open(filename).read()
.
----------------------------------------------------------------------
Ran 1 test in 0.006s

OK
Reacted by Sofie Van Landeghem

Activity

  1. RonnyPfannschmidt commented on Jul 30, 2019

    @RonnyPfannschmidt
    RonnyPfannschmidt
    on Jul 30, 2019
    Member
    More actions

    Pathlib closes the fd on the method used in the domonstratiob

  2. RonnyPfannschmidt commented on Jul 30, 2019

    @RonnyPfannschmidt
    RonnyPfannschmidt
    on Jul 30, 2019
    Member
    More actions

    Oh wait, I did miss tread, please check if it happens with disabled assertion rewriting

  3. mraspberry commented on Jul 31, 2019

    @mraspberry
    mraspberry
    on Jul 31, 2019
    Author
    More actions

    Seems to have the same behavior

    $ pytest --assert=plain
    ============================= test session starts ==============================
    platform linux -- Python 3.6.8, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
    rootdir: /home/matt/projects/pytest-issue
    collected 1 item
    
    tests/test_resource_warning_proof.py .                                   [100%]
    
    =========================== 1 passed in 0.05 seconds ===========================
    
  4. asottile commented on Jul 31, 2019

    @asottile
    asottile
    on Jul 31, 2019
    Member
    More actions

    interesting:

    def test():
        open(__file__).read()
    
    
    if __name__ == '__main__':
        exit(test())
    $ python3 -Wonce t.py
    t.py:2: ResourceWarning: unclosed file <_io.TextIOWrapper name='t.py' mode='r' encoding='UTF-8'>
      open(__file__).read()
    $ pytest -qq t.py
    .                                                                        [100%]
    $ pytest -Wonce -qq t.py
    .                                                                        [100%]
    =============================== warnings summary ===============================
    t.py::test
      /tmp/x/t.py:2: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/x/t.py' mode='r' encoding='UTF-8'>
        open(__file__).read()
    
    -- Docs: https://docs.pytest.org/en/latest/warnings.html
    $ pytest -Werror -qq t.py
    .                            
  5. asottile
    added
    plugin: warningsrelated to the warnings builtin plugin
    type: bugproblem that needs to be addressed
    on Jul 31, 2019
  6. nicoddemus commented on Jul 31, 2019

    @nicoddemus
    nicoddemus
    on Jul 31, 2019
    Member
    More actions

    We register/unregister handlers at various stages, such as test setup/call/teardown, collection, etc. I think the last hook called where we still capture warnings is pytest_terminal_summary:

    pytest/src/_pytest/warnings.py

    Lines 131 to 137 in cb15e7c

    @pytest.hookimpl(hookwrapper=True)
    def pytest_terminal_summary(terminalreporter):
    config = terminalreporter.config
    with catch_warnings_for_item(
    config=config, ihook=config.hook, when="config", item=None
    ):
    yield

    What I want to say is that we are not capturing warnings during the entire session, but only at some points.

    I wonder if we could start warning capture during pytest_configure and end during pytest_unconfigure... 🤔

  7. asottile commented on Jul 31, 2019

    @asottile
    asottile
    on Jul 31, 2019
    Member
    More actions

    I'm more surprised that when I elevate it to -Werror that it is nowhere to be seen 🤔

  8. mraspberry commented on Aug 3, 2019

    @mraspberry
    mraspberry
    on Aug 3, 2019
    Author
    More actions

    I'd like to take a stab at fixing this. How should I go about that?

  9. nicoddemus commented on Aug 3, 2019

    @nicoddemus
    nicoddemus
    on Aug 3, 2019
    Member
    More actions

    I think I would follow these steps:

    1. Try to pinpont at which point exactly the warning is happening. I would start with -Werror, but as @asottile demonstrates that is not reliable as we expected.
    2. After that, we should find the adequate pytest hook to implement and catch warnings around it.

    I've investigated a bit the pytest_configure approach I mentioned in #5676 (comment), but it is not that simple, because we can't reuse catch_warnings, we would need to implement what catch_warnings does manually (and in fact it is what pytest itself used to do), because we need more fine grained control of what happens when a warning is issued, not just log it away for future use.

  10. asottile commented on Nov 11, 2020

    @asottile
    asottile
    on Nov 11, 2020
    Member
    More actions

    there's a hint in #8021 -- pytest should probably implement sys.unraisablehook for py3.8+ to help with this

    Reacted by Sorin Sbarnea and John Belmonte
  11. dstansby commented on Nov 30, 2020

    @dstansby
    dstansby
    on Nov 30, 2020
    More actions

    This Stack Overflow post might provide some clues:

    Unfortunately, it doesn't appear to be possible. That "Exception ignored in:" message is produced by the CPython function PyErr_WriteUnraisable in Python/errors.c. The comment before that function states:

    /* Call when an exception has occurred but there is no way for Python
    to handle it. Examples: exception in del or during GC. */

    The ResourceWarning is indeed being generated during garbage collection, and Python prints a message because it doesn't have a way to raise an exception at that point. This has to do with the core CPython implementation and there is nothing that unittest can do to override it.

  12. The-Compiler commented on Nov 30, 2020

    @The-Compiler
    The-Compiler
    on Nov 30, 2020
    Member
    More actions

    Indeed setting sys.unraisablehook helps here - closing this as duplicate of #5299 which will be fixed via #8055 soon.

    Reacted by David Stansby
  13. The-Compiler
    closed this as completedon Nov 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: warningsrelated to the warnings builtin plugintype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.