-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Add contextlib.redirect_stderr() #66583
Comments
redirect_stdout is almost exactly what I want, except I want to redirect stderr! redirect_stdout.__init__() should take a 'stream_name' argument (possibly keyword-only) which could be set to 'stderr'. I propose it's implemented as setattr(sys, stream_name, new_target) |
Why not adding a new redirect_stderr() function? |
On Sep 11, 2014, at 02:25 PM, STINNER Victor wrote:
With a little refactoring redirect_stdout into a subclass, that would work |
+1 on Victor's suggestion. I don't think hypergeneralizing it is the way to go. That adds too much complexity for too little benefit. |
redirect_stdout("stderr", stream) looks wrong to be: you want to redirect "stdout" or "stderr"? If you want to redirect something else (ex: stdin), you can still implement the very simple pattern: old_stdin = sys.stdin
try:
sys.stdin = mock_input
...
finally:
sys.stdin = old_stdin By the way, I'm not convinced that we should add redirect_stderr. @barry: How many usage of this new functions do you see in the standard library? |
I'm fine with adding "redirect_stderr" - better to have the obvious (Tangentially related, I should do a contextlib2 release at some point, but |
Here's a simple implementation. I will add tests and update the documentation. |
@berker Peksag: The patch looks fine, although I would rename 'redirect_stream' -> '_redirect_stream' or '_RedirectStream' |
Good point. Will update the patch. Thanks! |
@berker Peksag: Also, please update the docs. |
There is stdout_redirected() function [1] that allows to redirect a file object given as [1] http://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python/22434262#22434262 |
Here's an updated patch. |
issue22389_v2.diff: .. function:: redirect_stdout(new_target) I would prefer to have two distinct entries in the documentation. The redirect_stderr() doc can be after redirect_stdout() and just say "Similar to redirect_stdout() but redirects sys.stderr". |
I think that Victor is right, it would be better to have two distinct entries in the docs. Besides that - LGTM. |
Good point. Patch updated. Thanks for the reviews! |
I just pushed the docs fix for issue bpo-21061, so the docs part of the patch may need tweaking in order to apply automatically. |
New changeset 7f12c9c09fb6 by Berker Peksag in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: