Skip to content

Commit 5cd2214

Browse files
committed
Factor out _capturing_for_request into a function
1 parent f3b0150 commit 5cd2214

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

src/_pytest/capture.py

+34-36
Original file line numberDiff line numberDiff line change
@@ -148,34 +148,6 @@ def read_global_capture(self):
148148

149149
# Fixture Control (it's just forwarding, think about removing this later)
150150

151-
@contextlib.contextmanager
152-
def _capturing_for_request(
153-
self, request: FixtureRequest
154-
) -> Generator["CaptureFixture", None, None]:
155-
"""
156-
Context manager that creates a ``CaptureFixture`` instance for the
157-
given ``request``, ensuring there is only a single one being requested.
158-
159-
This is used has a helper with ``capsys``, ``capfd`` etc.
160-
"""
161-
if self._capture_fixture:
162-
other_name = next(
163-
k
164-
for k, v in map_fixname_class.items()
165-
if v is self._capture_fixture.captureclass
166-
)
167-
raise request.raiseerror(
168-
"cannot use {} and {} at the same time".format(
169-
request.fixturename, other_name
170-
)
171-
)
172-
capture_class = map_fixname_class[request.fixturename]
173-
self._capture_fixture = CaptureFixture(capture_class, request)
174-
self.activate_fixture()
175-
yield self._capture_fixture
176-
self._capture_fixture.close()
177-
self._capture_fixture = None
178-
179151
def activate_fixture(self):
180152
"""If the current item is using ``capsys`` or ``capfd``, activate them so they take precedence over
181153
the global capture.
@@ -262,6 +234,36 @@ def pytest_internalerror(self, excinfo):
262234
self.stop_global_capturing()
263235

264236

237+
@contextlib.contextmanager
238+
def _capturing_for_request(
239+
request: FixtureRequest,
240+
) -> Generator["CaptureFixture", None, None]:
241+
"""
242+
Context manager that creates a ``CaptureFixture`` instance for the
243+
given ``request``, ensuring there is only a single one being requested.
244+
245+
This is used has a helper with ``capsys``, ``capfd`` etc.
246+
"""
247+
capman = request.config.pluginmanager.getplugin("capturemanager")
248+
if capman._capture_fixture:
249+
other_name = next(
250+
k
251+
for k, v in map_fixname_class.items()
252+
if v is capman._capture_fixture.captureclass
253+
)
254+
raise request.raiseerror(
255+
"cannot use {} and {} at the same time".format(
256+
request.fixturename, other_name
257+
)
258+
)
259+
capture_class = map_fixname_class[request.fixturename]
260+
capman._capture_fixture = CaptureFixture(capture_class, request)
261+
capman.activate_fixture()
262+
yield capman._capture_fixture
263+
capman._capture_fixture.close()
264+
capman._capture_fixture = None
265+
266+
265267
@pytest.fixture
266268
def capsys(request):
267269
"""Enable text capturing of writes to ``sys.stdout`` and ``sys.stderr``.
@@ -270,8 +272,7 @@ def capsys(request):
270272
calls, which return a ``(out, err)`` namedtuple.
271273
``out`` and ``err`` will be ``text`` objects.
272274
"""
273-
capman = request.config.pluginmanager.getplugin("capturemanager")
274-
with capman._capturing_for_request(request) as fixture:
275+
with _capturing_for_request(request) as fixture:
275276
yield fixture
276277

277278

@@ -283,8 +284,7 @@ def capsysbinary(request):
283284
method calls, which return a ``(out, err)`` namedtuple.
284285
``out`` and ``err`` will be ``bytes`` objects.
285286
"""
286-
capman = request.config.pluginmanager.getplugin("capturemanager")
287-
with capman._capturing_for_request(request) as fixture:
287+
with _capturing_for_request(request) as fixture:
288288
yield fixture
289289

290290

@@ -300,8 +300,7 @@ def capfd(request):
300300
pytest.skip(
301301
"capfd fixture needs os.dup function which is not available in this system"
302302
)
303-
capman = request.config.pluginmanager.getplugin("capturemanager")
304-
with capman._capturing_for_request(request) as fixture:
303+
with _capturing_for_request(request) as fixture:
305304
yield fixture
306305

307306

@@ -317,8 +316,7 @@ def capfdbinary(request):
317316
pytest.skip(
318317
"capfdbinary fixture needs os.dup function which is not available in this system"
319318
)
320-
capman = request.config.pluginmanager.getplugin("capturemanager")
321-
with capman._capturing_for_request(request) as fixture:
319+
with _capturing_for_request(request) as fixture:
322320
yield fixture
323321

324322

0 commit comments

Comments
 (0)