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

Contributing partial work towards pytest branch coverage #1798

Conversation

jaltmayerpizzorno
Copy link

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.

@nedbat nedbat force-pushed the nedbat/sysmon-fake-lines-for-branches branch 9 times, most recently from 65fe025 to 3e8e923 Compare July 20, 2024 22:46
@nedbat nedbat force-pushed the nedbat/sysmon-fake-lines-for-branches branch from 3e8e923 to 9676aae Compare July 21, 2024 18:44
@nedbat nedbat force-pushed the nedbat/sysmon-fake-lines-for-branches branch from 037b0c5 to 77d1316 Compare August 10, 2024 23:20
Comment on lines +386 to +389
import contextlib
with open("/tmp/foo.out", "a") as f:
with contextlib.redirect_stdout(f):
print(f"sysmon adding {arc = } (return)")
Copy link

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")

?

Copy link
Owner

@nedbat nedbat Aug 23, 2024

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.

@nedbat nedbat deleted the branch nedbat:nedbat/sysmon-fake-lines-for-branches August 31, 2024 20:39
@nedbat nedbat closed this Aug 31, 2024
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

Successfully merging this pull request may close these issues.

3 participants