Skip to content

Exception when using capturer inside pytest with capsys #7

@clara-avnet

Description

@clara-avnet

Using capturer inside a pytest test that uses the pytest fixture capsys leads to the following error in the call to CaptureOutput():

=================================== FAILURES ===================================
________________________________ test_capturer _________________________________

capsys = <_pytest.capture.CaptureFixture object at 0x7f07bed1f4a8>

    def test_capturer(capsys):
>       with capturemod.CaptureOutput() as capturer:

test_file.py:5: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/home/scherercl/msc/0000/libMscBoostPython.git/venv/lib/python3.6/site-packages/capturer/__init__.py:238: in __init__
    self.stdout_stream = self.initialize_stream(sys.stdout, STDOUT_FD)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <capturer.CaptureOutput object at 0x7f07bed1f080>
file_obj = <_io.TextIOWrapper encoding='UTF-8'>, expected_fd = 1

    def initialize_stream(self, file_obj, expected_fd):
        """
        Initialize one or more :class:`Stream` objects to capture a standard stream.
    
        :param file_obj: A file-like object with a ``fileno()`` method.
        :param expected_fd: The expected file descriptor of the file-like object.
        :returns: The :class:`Stream` connected to the file descriptor of the
                  file-like object.
    
        By default this method just initializes a :class:`Stream` object
        connected to the given file-like object and its underlying file
        descriptor (a simple one-liner).
    
        If however the file descriptor of the file-like object doesn't have the
        expected value (``expected_fd``) two :class:`Stream` objects will be
        created instead: One of the stream objects will be connected to the
        file descriptor of the file-like object and the other stream object
        will be connected to the file descriptor that was expected
        (``expected_fd``).
    
        This approach is intended to make sure that "nested" output capturing
        works as expected: Output from the current Python process is captured
        from the file descriptor of the file-like object while output from
        subprocesses is captured from the file descriptor given by
        ``expected_fd`` (because the operating system defines special semantics
        for the file descriptors with the numbers one and two that we can't
        just ignore).
    
        For more details refer to `issue 2 on GitHub
        <https://github.com/xolox/python-capturer/issues/2>`_.
        """
>       real_fd = file_obj.fileno()
E       io.UnsupportedOperation: fileno

I suspect, this has to do with nested capturing, as the function's documentation helpfully states (#2).

Reproduction

python 3.7.3
pytest 5.3.5
capturer 2.4

File test_file.py, called with pytest test_file.py:

import subprocess
from capturer import CaptureOutput

def test_capturer(capsys):
    with CaptureOutput() as capturer:
        # Generate some output from Python.
        print("Output from Python")
        # Generate output from a subprocess.
        subprocess.run(["echo", "Output from a subprocess"])
        # Get the output in each of the supported formats.
        assert capturer.get_bytes() == b'Output from Python\r\nOutput from a subprocess\r\n'
        assert capturer.get_lines() == [u'Output from Python', u'Output from a subprocess']
        assert capturer.get_text() == u'Output from Python\nOutput from a subprocess'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions