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

Comments

@asottile
Copy link
Member

@asottile 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
Copy link
Member

@nicoddemus nicoddemus commented Nov 14, 2017

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

@asottile asottile commented Nov 14, 2017

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

@nicoddemus nicoddemus commented Nov 14, 2017

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

@asottile asottile commented Nov 17, 2017

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

@asottile asottile closed this Nov 17, 2017
This was referenced Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants