Skip to content

pytest 4.5 floods the output with logging errors when logging from atexit handlers #5282

Open
@hynek

Description

This seems to be related to #4942 however I'd prefer this to be fixed rather than masked away. :)

Consider the following example:

import atexit
import io
import logging
import logging.config

import pytest


LOG_PROD = """\
[loggers]
keys = root

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console

[handler_console]
class = StreamHandler
args = (sys.stdout,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(message)s
"""


logger = logging.getLogger()

logging.config.fileConfig(
    io.StringIO(LOG_PROD), disable_existing_loggers=False
)


@pytest.fixture
def fix():
    logger.error("start")

    @atexit.register
    def boom():
        logger.error("end")

    return True


def test_boom(fix):
    assert fix

It will output the following:

==================================================================== test session starts =====================================================================
platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: /Users/hynek/tmp, inifile: setup.cfg
collected 1 item

t.py .                                                                                                                                                 [100%]

================================================================== 1 passed in 0.01 seconds ==================================================================
--- Logging error ---
Traceback (most recent call last):
  File "/Users/hynek/.pyenv/versions/3.7.3/lib/python3.7/logging/__init__.py", line 1037, in emit
    stream.write(msg + self.terminator)
  File "/Users/hynek/.local/venvs/tempenv-4f92186385ef7/lib/python3.7/site-packages/_pytest/capture.py", line 439, in write
    self.buffer.write(obj)
ValueError: I/O operation on closed file
Call stack:
  File "/Users/hynek/tmp/t.py", line 47, in boom
    logger.error("end")
Message: 'end'
Arguments: ()

When running functional tests, this output gets very long.


I guess the proper fix would be to monkeypatch atexit and run the hooks when a test ends?

cc @blueyed @The-Compiler

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    plugin: capturerelated to the capture 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