Issue 22389: Add contextlib.redirect_stderr() - Python tracker (original) (raw)

Created on 2014-09-11 14:25 by barry, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue22389.diff berker.peksag,2014-09-12 15:32 review
issue22389_v2.diff berker.peksag,2014-10-09 08:35 review
issue22389_v3.diff berker.peksag,2014-10-11 06:59 review
Messages (17)
msg226774 - (view) Author: Barry A. Warsaw (barry) * (Python committer) 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) * (Python committer) Date: 2014-09-11 14:25
Why not adding a new redirect_stderr() function?
msg226776 - (view) Author: Barry A. Warsaw (barry) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) Date: 2014-09-26 21:48
Good point. Will update the patch. Thanks!
msg227658 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) 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) * (Python committer) Date: 2014-10-09 08:35
Here's an updated patch.
msg228840 - (view) Author: STINNER Victor (vstinner) * (Python committer) 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) * (Python committer) 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) * (Python committer) Date: 2014-10-11 06:59
Good point. Patch updated. Thanks for the reviews!
msg229109 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) 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) (Python triager) 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
History
Date User Action Args
2022-04-11 14:58:07 admin set github: 66583
2014-11-28 21:29:02 berker.peksag set status: open -> closedassignee: rhettinger -> berker.peksagresolution: fixedstage: patch review -> resolved
2014-11-28 21:27:35 python-dev set nosy: + python-devmessages: +
2014-10-12 00:26:48 ncoghlan set messages: +
2014-10-11 06:59:41 berker.peksag set files: + issue22389_v3.diffmessages: +
2014-10-11 06:35:38 rhettinger set assignee: ncoghlan -> rhettinger
2014-10-09 16:31:41 yselivanov set messages: +
2014-10-09 08:41:19 vstinner set messages: +
2014-10-09 08:39:44 vstinner set title: Generalize contextlib.redirect_stdout -> Add contextlib.redirect_stderr()
2014-10-09 08:35:10 berker.peksag set files: + issue22389_v2.diffmessages: +
2014-09-29 10:40:04 John Isidore set nosy: + John Isidoremessages: +
2014-09-26 21:48:38 yselivanov set messages: +
2014-09-26 21:48:07 berker.peksag set type: enhancementmessages: +
2014-09-26 21:45:24 yselivanov set nosy: + yselivanovmessages: +
2014-09-12 15:32:41 berker.peksag set files: + issue22389.diffnosy: + berker.peksagmessages: + keywords: + patchstage: patch review
2014-09-12 08:07:05 ncoghlan set messages: +
2014-09-12 07:41:11 vstinner set messages: +
2014-09-12 07:38:51 rhettinger set assignee: ncoghlannosy: + ncoghlan
2014-09-12 07:38:34 rhettinger set nosy: + rhettingermessages: +
2014-09-11 14:28:52 barry set messages: +
2014-09-11 14:25:52 vstinner set nosy: + vstinnermessages: +
2014-09-11 14:25:13 barry create