diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index bd0fb87f0..4bf979efc 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -14,8 +14,7 @@ from tempfile import TemporaryFile import six import pytest -from _pytest.compat import CaptureIO - +from _pytest.compat import CaptureIO, dummy_context_manager patchsysdict = {0: "stdin", 1: "stdout", 2: "stderr"} @@ -122,16 +121,12 @@ class CaptureManager(object): cap.suspend_capturing(in_=in_) return outerr - @contextlib.contextmanager - def _dummy_context_manager(self): - yield - @contextlib.contextmanager def disabled(self): """Context manager to temporarily disables capture.""" # Need to undo local capsys-et-al if exists before disabling global capture fixture = getattr(self._current_item, "_capture_fixture", None) - ctx_manager = fixture.suspend() if fixture else self._dummy_context_manager() + ctx_manager = fixture.suspend() if fixture else dummy_context_manager() with ctx_manager: self.suspend_global_capture(item=None, in_=False) try: diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index c3ecaf912..ea369ccf2 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -8,6 +8,7 @@ import functools import inspect import re import sys +from contextlib import contextmanager import py @@ -151,6 +152,13 @@ def getfuncargnames(function, is_method=False, cls=None): return arg_names +@contextmanager +def dummy_context_manager(): + """Context manager that does nothing, useful in situations where you might need an actual context manager or not + depending on some condition. Using this allow to keep the same code""" + yield + + def get_default_arg_names(function): # Note: this code intentionally mirrors the code at the beginning of getfuncargnames, # to get the arguments which were excluded from its result because they had default values diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 395dc19e9..5b0fcd693 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -6,6 +6,7 @@ from contextlib import closing, contextmanager import re import six +from _pytest.compat import dummy_context_manager from _pytest.config import create_terminal_writer import pytest import py @@ -369,11 +370,6 @@ def pytest_configure(config): config.pluginmanager.register(LoggingPlugin(config), "logging-plugin") -@contextmanager -def _dummy_context_manager(): - yield - - class LoggingPlugin(object): """Attaches to the logging module and captures log messages for each test. """ @@ -537,7 +533,7 @@ class LoggingPlugin(object): log_cli_handler, formatter=log_cli_formatter, level=log_cli_level ) else: - self.live_logs_context = _dummy_context_manager() + self.live_logs_context = dummy_context_manager() class _LiveLoggingStreamHandler(logging.StreamHandler): @@ -575,7 +571,7 @@ class _LiveLoggingStreamHandler(logging.StreamHandler): ctx_manager = ( self.capture_manager.disabled() if self.capture_manager - else _dummy_context_manager() + else dummy_context_manager() ) with ctx_manager: if not self._first_record_emitted: