-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
functools.partial(print, file=sys.stderr) stderr not captured via capsys/capfd #8900
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
Comments
When the function is defined without partial, it works. I.e. replacing def print_err(*args, **kwargs):
kwargs.setdefault("file", sys.stderr)
print(*args, **kwargs) Makes the tests pass. |
This is "just" a matter of when the lookup is resolved:
So I think your |
Thanks for the explanation. This makes sense when explained, but I would probably never figured that out. Is this worth documenting somehow? |
I was thinking about this more and don't understand why does this also affect |
It is that Line 383 in d0bd269
|
This also happens if you from sys import stderr
def test_err(capsys):
print("stuff", file=stderr)
out, err = capsys.readouterr()
assert "err" == "stuff" # Fails, err is "". import sys
def test_err(capsys):
print("stuff", file=sys.stderr)
out, err = capsys.readouterr()
assert "err" == "stuff" # Passes. |
I've been wrapping my head around this for a while and I was unable to figure out what's going on and why my
capsys
(orcapfd
) usage does not capture stderr at all. I've narrowed the case down to usingfunctools.partial(print, file=sys.stderr)
. When that function is used, its output is not captured incapsys.readouterr().err
(orcapsys.readouterr().err
).Here is a reproducer adapted from the docs:
I'd expect all tests here to pass. The ones using
print_err
don't pass.The
capsys
/capfd
captured standard error is empty. The printed "Captured stderr call" is populated instead.My operating system is Fedora Linux. This is pip list:
But this also happens at least with pytest 6.0, 5.4 and 4.6.
Possibly related to #5997 but not sure.
The text was updated successfully, but these errors were encountered: