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

capfd in binary mode? #2923

Closed
asottile opened this issue Nov 14, 2017 · 4 comments
Closed

capfd in binary mode? #2923

asottile opened this issue Nov 14, 2017 · 4 comments
Labels
plugin: capture related to the capture builtin plugin type: enhancement new feature or API change, should be merged into features branch

Comments

@asottile
Copy link
Member

asottile commented Nov 14, 2017

I'd like something like this to work:

def test_thing(capfd_bytes):
    subprocess.check_call(('echo', '-e', '\\x98\\xfe'))
    out, err = capfd_bytes.readouterr()
    assert out == b'\x98\xfe'

My actual usecase is running tar as stdout and then inspecting the output

I'm hacking around with this (which works, but is undeniably fragile):

def test(capfd):
    ...
    capfd._capture.out.tmpfile.encoding = None
    capfd._capture.err.tmpfile.encoding = None
    out, err = capfd.readouterr()
    capfd._capture.out.tmpfile.encoding = 'UTF-8'
    capfd._capture.err.tmpfile.encoding = 'UTF-8'
    # ...

would pytest be amenable to a PR which enables something like this without hax?

@nicoddemus nicoddemus added plugin: capture related to the capture builtin plugin type: enhancement new feature or API change, should be merged into features branch labels Nov 14, 2017
@nicoddemus
Copy link
Member

Hi @asottile!

I think so, yes, thanks for the offer. Do you have an API in mind?

(I could swear that this has been brought up before, but I can't find the issue...)

@asottile
Copy link
Member Author

It was probably me before 😆

I'm not sure I necessarily like the api I wrote in #2279 (too easy to mix and match bytes / text and get unicode errors when mixing and matching). I think it would be better to add new separate capfdbinary and capsysbinary (feel free to improve the names here) which have the same interface as capfd and capsys?

@nicoddemus
Copy link
Member

It was probably me before

Rá I knew this has been discussed before. 😆

I think it would be better to add new separate capfdbinary and capsysbinary (feel free to improve the names here) which have the same interface as capfd and capsys?

I think this might be the way to go, it might not be easy to change the stream types after it has been created, and having separate methods is clumsy as you noted.

From my POV please go ahead. 👍

@asottile
Copy link
Member Author

via #2925 (I'll open another issue for capsysbinary so I can changelog)

This was referenced Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: capture related to the capture builtin plugin type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

2 participants