msg226774 - (view) |
Author: Barry A. Warsaw (barry) *  |
Date: 2014-09-11 14:25 |
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) |
|
|
msg226775 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2014-09-11 14:25 |
Why not adding a new redirect_stderr() function? |
|
|
msg226776 - (view) |
Author: Barry A. Warsaw (barry) *  |
Date: 2014-09-11 14:28 |
On Sep 11, 2014, at 02:25 PM, STINNER Victor wrote: >Why not adding a new redirect_stderr() function? With a little refactoring redirect_stdout into a subclass, that would work too. |
|
|
msg226810 - (view) |
Author: Raymond Hettinger (rhettinger) *  |
Date: 2014-09-12 07:38 |
+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. |
|
|
msg226811 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2014-09-12 07:41 |
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? |
|
|
msg226814 - (view) |
Author: Alyssa Coghlan (ncoghlan) *  |
Date: 2014-09-12 08:07 |
I'm fine with adding "redirect_stderr" - better to have the obvious counterpart, rather than hypergeneralising, or having to explain why it's missing. It's *currently* missing largely on a "wait for someone to ask" basis, and Barry asked. (Tangentially related, I should do a contextlib2 release at some point, but my previous CI provider shut down...) |
|
|
msg226825 - (view) |
Author: Berker Peksag (berker.peksag) *  |
Date: 2014-09-12 15:32 |
Here's a simple implementation. I will add tests and update the documentation. |
|
|
msg227656 - (view) |
Author: Yury Selivanov (yselivanov) *  |
Date: 2014-09-26 21:45 |
@Berker Peksag: The patch looks fine, although I would rename 'redirect_stream' -> '_redirect_stream' or '_RedirectStream' |
|
|
msg227657 - (view) |
Author: Berker Peksag (berker.peksag) *  |
Date: 2014-09-26 21:48 |
Good point. Will update the patch. Thanks! |
|
|
msg227658 - (view) |
Author: Yury Selivanov (yselivanov) *  |
Date: 2014-09-26 21:48 |
@Berker Peksag: Also, please update the docs. |
|
|
msg227792 - (view) |
Author: (John Isidore) |
Date: 2014-09-29 10:40 |
There is stdout_redirected() function [1] that allows to redirect a file object given as `stdout` patameter including `sys.stderr`. It works at a file descriptor level i.e. it supports redirecting subprocess' output too but it doesn't work for StringIO (no fd). [1] http://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python/22434262#22434262 |
|
|
msg228839 - (view) |
Author: Berker Peksag (berker.peksag) *  |
Date: 2014-10-09 08:35 |
Here's an updated patch. |
|
|
msg228840 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2014-10-09 08:41 |
issue22389_v2.diff: .. function:: redirect_stdout(new_target) + redirect_stderr(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". |
|
|
msg228878 - (view) |
Author: Yury Selivanov (yselivanov) *  |
Date: 2014-10-09 16:31 |
I think that Victor is right, it would be better to have two distinct entries in the docs. Besides that - LGTM. |
|
|
msg229062 - (view) |
Author: Berker Peksag (berker.peksag) *  |
Date: 2014-10-11 06:59 |
Good point. Patch updated. Thanks for the reviews! |
|
|
msg229109 - (view) |
Author: Alyssa Coghlan (ncoghlan) *  |
Date: 2014-10-12 00:26 |
I just pushed the docs fix for issue #21061, so the docs part of the patch may need tweaking in order to apply automatically. |
|
|
msg231830 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2014-11-28 21:27 |
New changeset 7f12c9c09fb6 by Berker Peksag in branch 'default': Issue #22389: Add contextlib.redirect_stderr(). https://hg.python.org/cpython/rev/7f12c9c09fb6 |
|
|