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

Comments

Projects
None yet
2 participants
@asottile
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

This comment has been minimized.

Member

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

This comment has been minimized.

Member

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

This comment has been minimized.

Member

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

This comment has been minimized.

Member

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