Skip to content

Commit f674217

Browse files
committed
Moved dummy_context_manager to compat module
1 parent 9f7345d commit f674217

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

src/_pytest/capture.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
import six
1616
import pytest
17-
from _pytest.compat import CaptureIO
18-
17+
from _pytest.compat import CaptureIO, dummy_context_manager
1918

2019
patchsysdict = {0: "stdin", 1: "stdout", 2: "stderr"}
2120

@@ -122,16 +121,12 @@ def suspend_global_capture(self, item=None, in_=False):
122121
cap.suspend_capturing(in_=in_)
123122
return outerr
124123

125-
@contextlib.contextmanager
126-
def _dummy_context_manager(self):
127-
yield
128-
129124
@contextlib.contextmanager
130125
def disabled(self):
131126
"""Context manager to temporarily disables capture."""
132127
# Need to undo local capsys-et-al if exists before disabling global capture
133128
fixture = getattr(self._current_item, "_capture_fixture", None)
134-
ctx_manager = fixture.suspend() if fixture else self._dummy_context_manager()
129+
ctx_manager = fixture.suspend() if fixture else dummy_context_manager()
135130
with ctx_manager:
136131
self.suspend_global_capture(item=None, in_=False)
137132
try:

src/_pytest/compat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import inspect
99
import re
1010
import sys
11+
from contextlib import contextmanager
1112

1213
import py
1314

@@ -151,6 +152,13 @@ def getfuncargnames(function, is_method=False, cls=None):
151152
return arg_names
152153

153154

155+
@contextmanager
156+
def dummy_context_manager():
157+
"""Context manager that does nothing, useful in situations where you might need an actual context manager or not
158+
depending on some condition. Using this allow to keep the same code"""
159+
yield
160+
161+
154162
def get_default_arg_names(function):
155163
# Note: this code intentionally mirrors the code at the beginning of getfuncargnames,
156164
# to get the arguments which were excluded from its result because they had default values

src/_pytest/logging.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
import six
88

9+
from _pytest.compat import dummy_context_manager
910
from _pytest.config import create_terminal_writer
1011
import pytest
1112
import py
@@ -369,11 +370,6 @@ def pytest_configure(config):
369370
config.pluginmanager.register(LoggingPlugin(config), "logging-plugin")
370371

371372

372-
@contextmanager
373-
def _dummy_context_manager():
374-
yield
375-
376-
377373
class LoggingPlugin(object):
378374
"""Attaches to the logging module and captures log messages for each test.
379375
"""
@@ -537,7 +533,7 @@ def _setup_cli_logging(self):
537533
log_cli_handler, formatter=log_cli_formatter, level=log_cli_level
538534
)
539535
else:
540-
self.live_logs_context = _dummy_context_manager()
536+
self.live_logs_context = dummy_context_manager()
541537

542538

543539
class _LiveLoggingStreamHandler(logging.StreamHandler):
@@ -575,7 +571,7 @@ def emit(self, record):
575571
ctx_manager = (
576572
self.capture_manager.disabled()
577573
if self.capture_manager
578-
else _dummy_context_manager()
574+
else dummy_context_manager()
579575
)
580576
with ctx_manager:
581577
if not self._first_record_emitted:

0 commit comments

Comments
 (0)