[issue15805] Add stdout redirection tool to contextlib

2013-10-12 Thread Georg Brandl
Georg Brandl added the comment: Whatsnew: yes please. As for your second point, I assume Raymond wanted to exemplify usage with an "unfortunate" API that prints to stderr with no option to change it. It just turned out that dis() is not one of those APIs. For purposes of print(), you're almo

[issue15805] Add stdout redirection tool to contextlib

2013-10-10 Thread Ezio Melotti
Ezio Melotti added the comment: I think this should also be added to the whatsnew. Regarding the examples, isn't it easier to say that: with redirect_stdout(sys.stderr): print('error') is equivalent to print('error', file=sys.stderr) ? I think that in most of the cases users are redir

[issue15805] Add stdout redirection tool to contextlib

2013-10-10 Thread Vajrasky Kok
Vajrasky Kok added the comment: Nice. My only complain is the dis.dis example. We don't have to use redirect_stdout context manager. +# How to capture disassembly to a string + +import dis +import io + +f = io.StringIO() +with redirect_stdout(f): +

[issue15805] Add stdout redirection tool to contextlib

2013-10-10 Thread Roundup Robot
Roundup Robot added the comment: New changeset 63a1ee94b3ed by Raymond Hettinger in branch 'default': Issue #15805: Add contextlib.redirect_stdout() http://hg.python.org/cpython/rev/63a1ee94b3ed -- nosy: +python-dev ___ Python tracker

[issue15805] Add stdout redirection tool to contextlib

2013-10-10 Thread Raymond Hettinger
Changes by Raymond Hettinger : -- resolution: -> fixed status: open -> closed ___ Python tracker ___ ___ Python-bugs-list mailing lis

[issue15805] Add stdout redirection tool to contextlib

2013-07-23 Thread Barry A. Warsaw
Barry A. Warsaw added the comment: On Jul 23, 2013, at 04:24 AM, Alexander Belopolsky wrote: >@contextlib.contextmanager >def redirect_stdout(stream): >old_stdout = sys.stdout >sys.stdout = stream >yield >sys.stdout = old_stdout Make that: @contextlib.contextmanager def redirec

[issue15805] Add stdout redirection tool to contextlib

2013-07-22 Thread Nick Coghlan
Nick Coghlan added the comment: Can we go paint bikesheds somewhere else now, please? Raymond has persuaded me as contextlib maintainer that this small addition is worth making as a way of factoring out repeated calls to print with a file redirection in simple user scripts where thread safety

[issue15805] Add stdout redirection tool to contextlib

2013-07-22 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: Yes, I did miss Victor's dup2() comment. (It looks like I did not subscribe to this issue from the start and missed early discussion - sorry.) The simple feature is not very useful for me. I have to deal with too many cases of misguided code like this:

[issue15805] Add stdout redirection tool to contextlib

2013-07-22 Thread Nick Coghlan
Nick Coghlan added the comment: Alexander, please read the earlier comments on the issue: we're deliberately *not* doing that. Such functionality is more advanced, and more appropriate for an API in the subprocess module. Anyone interested in exploring that option further should create a separ

[issue15805] Add stdout redirection tool to contextlib

2013-07-22 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: In my post "fd 0" should have been "fd 1", of course. (Proving that it is not trivial to get it right:-) -- ___ Python tracker ___ _

[issue15805] Add stdout redirection tool to contextlib

2013-07-22 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: It would be nice if this context manager had an option to redirect the file descriptor 0 rather than just sys.stdout. (For those familiar with py.test, I am asking for an equivalent of --capture=fd functionality.) Unlike patching sys.stdout, which is wi

[issue15805] Add stdout redirection tool to contextlib

2013-07-22 Thread Nick Coghlan
Nick Coghlan added the comment: I'd prefer to keep the separate stream argument rather than duplicating the signature of open. Separation of concerns and all that :) -- ___ Python tracker __

[issue15805] Add stdout redirection tool to contextlib

2013-07-22 Thread Brett Cannon
Changes by Brett Cannon : -- nosy: -brett.cannon ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.p

[issue15805] Add stdout redirection tool to contextlib

2013-07-22 Thread Barry A. Warsaw
Barry A. Warsaw added the comment: In general, I like where this is going. I agree that a stdout redirector is probably the most common case, and for that, it almost always (for me) redirects to an open file. The use case for stderr redirection is usually, but not always, to redirect stderr to

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread STINNER Victor
STINNER Victor added the comment: Can someone propose a patch instead of inline code? -- ___ Python tracker ___ ___ Python-bugs-list m

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Nick Coghlan
Nick Coghlan added the comment: Yeah, the docs will need to note that it isn't thread safe. However, non thread-safe constructs are often still incredibly useful for scripting use cases. -- ___ Python tracker ___

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Nikolaus Rath
Nikolaus Rath added the comment: I think stdout redirection is very useful, and I'm actually have a very similar context manager in my own code. However, I'd like to raise the question if using a context manager for this purpose should really be "officially blessed" in this way. To me, the wi

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Ezio Melotti
Changes by Ezio Melotti : -- nosy: +ezio.melotti ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.py

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Nick Coghlan
Nick Coghlan added the comment: OK, Raymond and I had a chat on IRC about this, and I've come back around to the idea that the simple "contextlib.redirect_stdout" model as Raymond originally proposed is the way to go. There are a few pieces to that rationale: 1. Why in contextlib? The io modu

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Marc Abramowitz
Marc Abramowitz added the comment: I like Nick's version. I don't know if __exit__ really needs error checking, but I like the API. For me, it strikes a good balance between being intuitive and being general enough to do all the stuff I'd like to do. Should the docstrings mention that it only

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Raymond Hettinger
Raymond Hettinger added the comment: Nick, it seems to me that you're going way over-board with an otherwise simple idea. I'm aiming for something that looks much like my original posting. That has been tried out on my students with great success. --

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Nick Coghlan
Nick Coghlan added the comment: And reviewing my own draft - the convenience functions would need docstrings too, and the docstrings should mention that a new StringIO instance is created by default and that "as name" can be used to get access to that object. -- __

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Nick Coghlan
Nick Coghlan added the comment: As Raymond noted, we should resist the temptation to generalise this too much - generalisation almost always comes at the cost of making any *specific* case harder (consider the difference between the complexity of the "support any URL scheme" model in urllib an

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Raymond Hettinger
Raymond Hettinger added the comment: I'm thinking that PrintRedirect is a better name because it coincides with the intent of the primary use case. The more computer-sciency you make the name, the more it becomes opaque to everyday users. -- ___ Py

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Marc Abramowitz
Marc Abramowitz added the comment: Thanks Nick! I'll work on applying your suggestions a little later. And I'll add a note about it not working with subprocesses because I realized that I forgot to do that. Regarding redirect_stdfile, which is presumably what you meant by "name-based API", I

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Nick Coghlan
Nick Coghlan added the comment: A good start, but: 1. io is too low level to depend on unittest (or even contextlib), as anything it imports will be imported automatically at interpreter startup. The context manager will need to be written out directly as a class with the appropriate methods. 2

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Marc Abramowitz
Marc Abramowitz added the comment: Oops, Nick => Brett. -- ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http

[issue15805] Add stdout redirection tool to contextlib

2013-07-21 Thread Marc Abramowitz
Marc Abramowitz added the comment: I agree also that io is a good place for the basic version that doesn't do file descriptor stuff and maybe the fancy file descriptor stuff should be a separate issue and should go in subprocess. To move this along and generate more discussion, I took code fro

[issue15805] Add stdout redirection tool to contextlib

2013-07-19 Thread Nick Coghlan
Nick Coghlan added the comment: +1 for io as an intuitive home for a basic version that redirects the current process output only (and is documented as doing so). While I like the idea of offering a more sophisticated version that affects subprocesses as well, I think that would be a very surpris

[issue15805] Add stdout redirection tool to contextlib

2013-07-19 Thread Marc Abramowitz
Marc Abramowitz added the comment: As it happens, I wrote a similar context manager to Victor's recently for a setup.py because I wanted to suppress compiler errors that are output to the console by distutils.ccompiler.CCompiler.has_function. As Victor mentioned, for this to work with subproce

[issue15805] Add stdout redirection tool to contextlib

2013-07-19 Thread Antoine Pitrou
Antoine Pitrou added the comment: I don't think this has anything to do in contextlib (which isn't a library of context managers, but a library of tools to work with context managers), it probably belongs in the io module instead. -- nosy: +pitrou _

[issue15805] Add stdout redirection tool to contextlib

2013-07-19 Thread Marc Abramowitz
Changes by Marc Abramowitz : -- nosy: +Marc.Abramowitz ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http://m

[issue15805] Add stdout redirection tool to contextlib

2013-03-16 Thread STINNER Victor
STINNER Victor added the comment: If such context manager is added, it should be documented that it does not work with subprocess or C functions writing directly into the file descriptor 1. For such issues, I'm using dup2(). Example from my pysandbox project: @contextlib.contextmanager def cap

[issue15805] Add stdout redirection tool to contextlib

2013-03-15 Thread Raymond Hettinger
Changes by Raymond Hettinger : -- assignee: ncoghlan -> rhettinger ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscrib

[issue15805] Add stdout redirection tool to contextlib

2013-03-15 Thread Barry A. Warsaw
Changes by Barry A. Warsaw : -- nosy: +barry ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python

[issue15805] Add stdout redirection tool to contextlib

2012-09-25 Thread Nick Coghlan
Nick Coghlan added the comment: I'd actually be inclined to make it the full trio: redirect_stdin, redirect_stdout, redirect_stderr. Mostly because I don't see an especially compelling reason to privilege redirecting stdout over the other two standard streams, and the "pass in the stream name

[issue15805] Add stdout redirection tool to contextlib

2012-09-25 Thread Raymond Hettinger
Raymond Hettinger added the comment: I like Nick's proposed variant and think it should go in contextlib, not the mocking library. Redirection has a lot of use cases that has nothing to do with mocking-up test suites. Contextlib is about general purpose context-managers that apply in many si

[issue15805] Add stdout redirection tool to contextlib

2012-08-29 Thread Alexander Belopolsky
Changes by Alexander Belopolsky : -- nosy: +belopolsky ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http://m

[issue15805] Add stdout redirection tool to contextlib

2012-08-29 Thread Brett Cannon
Brett Cannon added the comment: So Alex's point is valid: the examples in the unittest.mock.patch docs shows how to do this (http://docs.python.org/dev/py3k/library/unittest.mock.html#patch). So this could be simplified to: def redirect_stdout(replacement=None): return unittest.mock.patch('

[issue15805] Add stdout redirection tool to contextlib

2012-08-28 Thread Nick Coghlan
Nick Coghlan added the comment: Sure, but by targeting a specific use case you can make it really trivial to use. -- ___ Python tracker ___ _

[issue15805] Add stdout redirection tool to contextlib

2012-08-28 Thread Alex Gaynor
Alex Gaynor added the comment: Sounds like a special case of a small part of mock. Not sure that this observation is significant though. -- nosy: +alex ___ Python tracker ___ __

[issue15805] Add stdout redirection tool to contextlib

2012-08-28 Thread Nick Coghlan
Nick Coghlan added the comment: We actually use a variant of this idea in the test suite (http://docs.python.org/dev/library/test#test.support.captured_stdout) It would be pretty easy to combine the two concepts by defaulting the redirection to a new StringIO instance if no other destination i

[issue15805] Add stdout redirection tool to contextlib

2012-08-28 Thread Raymond Hettinger
Changes by Raymond Hettinger : -- assignee: -> ncoghlan nosy: +ncoghlan priority: normal -> low ___ Python tracker ___ ___ Python-bug

[issue15805] Add stdout redirection tool to contextlib

2012-08-28 Thread Raymond Hettinger
New submission from Raymond Hettinger: The technique of temporarily redirecting stdout could be encapsulated in a context manager. print('This goes to stdout') with RedirectStdout(sys.stderr): print('This goes to stderr') print('So does this') print('This goes to stdout') The print f