Skip to content

"ValueError: I/O operation on closed file." with click's CliRunner (capturing) #3344

Closed
@blueyed

Description

@blueyed

With click's CliRunner I've noticed that it throws a ValueError when pytest's
pdb gets used in the CliRunner.isolation context:

test_pytest_capture.py:

def test_click_isolation():
    from click.testing import CliRunner

    runner = CliRunner()
    with runner.isolation() as out:
        print('isolation')
        __import__('pdb').set_trace()
        print('END')
        print(out, out.getvalue())

Test run:

% pytest test_pytest_capture.py
==================================== test session starts ====================================
platform linux -- Python 3.6.4, pytest-3.5.1.dev7+ged118d7f, py-1.5.3, pluggy-0.6.0
rootdir: …/Vcs/click, inifile:
collected 1 item                                                                            

test_pytest_capture.py 
>>>>>>>>>>>>>>>>>>>>>>>>>> PDB set_trace (IO-capturing turned off) >>>>>>>>>>>>>>>>>>>>>>>>>>
> …/Vcs/click/test_pytest_capture.py(8)test_click_isolation()
-> print('END')
(Pdb) c
END
F                                                              [100%]

========================================= FAILURES ==========================================
___________________________________ test_click_isolation ____________________________________

    def test_click_isolation():
        from click.testing import CliRunner
    
        runner = CliRunner()
        with runner.isolation() as out:
            print('isolation')
            __import__('pdb').set_trace()
>           print('END')
E           ValueError: I/O operation on closed file.

test_pytest_capture.py:8: ValueError
================================= 1 failed in 0.77 seconds ==================================

When using -s bdb.BdbQuit is raised:

% pytest test_pytest_capture.py -s
==================================== test session starts ====================================
platform linux -- Python 3.6.4, pytest-3.5.1.dev7+ged118d7f, py-1.5.3, pluggy-0.6.0
rootdir: …/Vcs/click, inifile:
collected 1 item                                                                            

test_pytest_capture.py F

========================================= FAILURES ==========================================
___________________________________ test_click_isolation ____________________________________

    def test_click_isolation():
        from click.testing import CliRunner
    
        runner = CliRunner()
        with runner.isolation() as out:
            print('isolation')
            __import__('pdb').set_trace()
>           print('END')

test_pytest_capture.py:8: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
test_pytest_capture.py:8: in test_click_isolation
    print('END')
/usr/lib/python3.6/bdb.py:48: in trace_dispatch
    return self.dispatch_line(frame)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pdb.Pdb object at 0x7f35b42e8710>, frame = <frame object at 0x7f35b429f848>

    def dispatch_line(self, frame):
        if self.stop_here(frame) or self.break_here(frame):
            self.user_line(frame)
>           if self.quitting: raise BdbQuit
E           bdb.BdbQuit

/usr/lib/python3.6/bdb.py:67: BdbQuit
================================= 1 failed in 0.06 seconds ==================================

The ValueError comes from out.getvalue really, the location is off due to the
pdb.set_trace (see #3237).

pytest.testing.isolation: https://github.com/pallets/click/blob/55682f6f5348f5220a557f89c3a796321a52aebf/click/testing.py#L139

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: capturerelated to the capture builtin pluginplugin: debuggingrelated to the debugging 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