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

TypeError is raised if no stderr is registered, but a file is passed as the stderr argument #144

Open
tbhartman opened this issue Feb 10, 2024 · 1 comment

Comments

@tbhartman
Copy link

tbhartman commented Feb 10, 2024

Description

An TypeError is raised at fake_popen.py:271 when calling with a file passed to argument stderr if no stderr was given during registration

>           buffer.write(data_type(data))
E           TypeError: encoding without a string argument

pyvenv\Lib\site-packages\pytest_subprocess\fake_popen.py:271: TypeError

Example

Below is an example demonstrating the error.

def test_fp_bug(fp):
    with NamedTemporaryFile("wb", delete=False) as f:
        fp.register(
            ["git", 'rev-parse', 'HEAD'],
            stdout=["47c77698dd8e0e35af00da56806fe98fcb9a1056"],
            )
        p = subprocess.Popen(('git', 'rev-parse', 'HEAD'), stdout=f.file, stderr=f.file)
        p.wait()
        fp.allow_unregistered(True)
        p = subprocess.Popen(('git', 'rev-parse', 'HEAD'), stdout=f.file, stderr=f.file)
        p.wait()

This raises the type error. I can correct this:

--- fake_popen.py   2024-02-10 06:09:01.897909800 -0500
+++ fake_popen.py       2024-02-10 06:08:49.610943500 -0500
@@ -273,7 +267,7 @@
         )
         if isinstance(data, (list, tuple)):
             buffer.writelines([data_type(line + "\n") for line in data])
-        elif data is not None:
+        else:
             buffer.write(data_type(data))

     def _convert(self, input: Union[str, bytes]) -> Union[str, bytes]:
@aklajnert
Copy link
Owner

Thanks for reporting an issue. I think I've never consider the stdout/stderr to be files, hence these cases were not tested and are buggy. I'll try to find some time to look into that, but I can't say exactly when it will happen. Feel free to submit a PR if you need it sooner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants