-
-
Notifications
You must be signed in to change notification settings - Fork 429
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
Contributing partial work towards pytest branch coverage #1798
Contributing partial work towards pytest branch coverage #1798
Conversation
65fe025
to
3e8e923
Compare
3e8e923
to
9676aae
Compare
037b0c5
to
77d1316
Compare
import contextlib | ||
with open("/tmp/foo.out", "a") as f: | ||
with contextlib.redirect_stdout(f): | ||
print(f"sysmon adding {arc = } (return)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to understand the purpose of this code.
I get why contextlib.redirect_stdout
is useful for capturing the output of other code that directly print()
s data we'd prefer to send somewhere else. Its docs give the example of capturing a help()
call in the REPL. Smart use of redirection, very clever.
But what's the point of using it just to surround an explicit print()
? print
already has support for redirecting its output somewhere other than sys.stdout
.
What's the advantage of doing this, over...
with open("/tmp/foo.out", "a") as f:
print(f"sysmon adding {arc=} (return)", file=f)
or even...
with open("/tmp/foo.out", "a") as f:
f.write(f"sysmon adding {arc=} (return)\n")
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is temporary debugging code. It's written to be quick to insert, and then will be removed later. I have a snippet in my editor to add this:
import contextlib
with open("/tmp/foo.out", "a") as f:
with contextlib.redirect_stdout(f):
print(f"")
This makes it easy for me to add print statements without having to remember file=f
, or that f.write
only accepts strings.
Hi Ned, I started working on this test to ensure that Coverage.py would provide coverage within pytest modules, but ran out of time before my trip tomorrow. I am also including a
wrap_pytest
function that I think should be sufficient to restore AST-based instrumentation on pytest modules despite assertion rewriting... but I couldn't get the tests to work, so never integrated it.