This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add contextlib.redirect_stderr()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: berker.peksag Nosy List: John Isidore, barry, berker.peksag, ncoghlan, python-dev, rhettinger, vstinner, yselivanov
Priority: normal Keywords: patch

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

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
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
  sys.stdin = mock_input
  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: Nick 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).

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

 .. 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: Nick 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().
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66583
2014-11-28 21:29:02berker.peksagsetstatus: open -> closed
assignee: rhettinger -> berker.peksag
resolution: fixed
stage: patch review -> resolved
2014-11-28 21:27:35python-devsetnosy: + python-dev
messages: + msg231830
2014-10-12 00:26:48ncoghlansetmessages: + msg229109
2014-10-11 06:59:41berker.peksagsetfiles: + issue22389_v3.diff

messages: + msg229062
2014-10-11 06:35:38rhettingersetassignee: ncoghlan -> rhettinger
2014-10-09 16:31:41yselivanovsetmessages: + msg228878
2014-10-09 08:41:19vstinnersetmessages: + msg228840
2014-10-09 08:39:44vstinnersettitle: Generalize contextlib.redirect_stdout -> Add contextlib.redirect_stderr()
2014-10-09 08:35:10berker.peksagsetfiles: + issue22389_v2.diff

messages: + msg228839
2014-09-29 10:40:04John Isidoresetnosy: + John Isidore
messages: + msg227792
2014-09-26 21:48:38yselivanovsetmessages: + msg227658
2014-09-26 21:48:07berker.peksagsettype: enhancement
messages: + msg227657
2014-09-26 21:45:24yselivanovsetnosy: + yselivanov
messages: + msg227656
2014-09-12 15:32:41berker.peksagsetfiles: + issue22389.diff

nosy: + berker.peksag
messages: + msg226825

keywords: + patch
stage: patch review
2014-09-12 08:07:05ncoghlansetmessages: + msg226814
2014-09-12 07:41:11vstinnersetmessages: + msg226811
2014-09-12 07:38:51rhettingersetassignee: ncoghlan

nosy: + ncoghlan
2014-09-12 07:38:34rhettingersetnosy: + rhettinger
messages: + msg226810
2014-09-11 14:28:52barrysetmessages: + msg226776
2014-09-11 14:25:52vstinnersetnosy: + vstinner
messages: + msg226775
2014-09-11 14:25:13barrycreate