Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when printing while capsysbinary is active #6926

Merged
merged 1 commit into from Mar 17, 2020

Commits on Mar 16, 2020

  1. Fix crash when printing while capsysbinary is active

    Previously, writing to sys.stdout/stderr in text-mode (e.g.
    `print('foo')`) while a `capsysbinary` fixture is active, would crash
    with:
    
        /usr/lib/python3.7/contextlib.py:119: in __exit__
            next(self.gen)
        E   TypeError: write() argument must be str, not bytes
    
    This is due to some confusion in the types. The relevant functions are
    `snap()` and `writeorg()`. The function `snap()` returns what was
    captured, and the return type should be `bytes` for the binary captures
    and `str` for the regular ones. The `snap()` return value is eventually
    passed to `writeorg()` to be written to the original file, so it's input
    type should correspond to `snap()`. But this was incorrect for
    `SysCaptureBinary`, which handled it like `str`.
    
    To fix this, be explicit in the `snap()` and `writeorg()`
    implementations, also of the other Capture types.
    
    We can't add type annotations yet, because the current inheritance
    scheme breaks Liskov Substitution and mypy would complain. To be
    refactored later.
    
    Fixes: pytest-dev#6871
    Co-authored-by: Ran Benita (some modifications & commit message)
    blueyed authored and bluetech committed Mar 16, 2020
    Configuration menu
    Copy the full SHA
    1fda861 View commit details
    Browse the repository at this point in the history