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 module isn't something people normally need to think about - we mostly 
hide it from them since they can just call the open builtin. Similarly, most 
new users don't need to touch the sys module - it's all hidden behind higher 
level abstractions (like input, print and argparse).

However, contextlib is something everyone will be exposed to, both to turn 
generators into context managers, but also for the utility context managers 
closing() and ignored().

It also comes down to trusting Raymond's opinion as an experienced Python 
teacher that contextlib is a more discoverable location than io for this 
particular piece of functionality.

2. Why only stdout? Why not stdin and stderr?

Raymond pointed out that the case for this kind of redirection helper is much 
weaker for stdin and stderr. Wanting to control where "print" and similar 
operations write to is quite common, but writing to stderr is often 
specifically about bypassing typical redirections and faking input through 
stdin is typically only done for testing purposes.

Adding stdlib APIs without compelling use cases isn't a good idea, even when 
they seem like an "obvious" generalisation.

In this case, outside the CPython test suite, I couldn't think of any situation 
where the limited versions of these helpers would have actually been useful to 
me, and in the test suite case, the typical answer would be "use a mock" (that 
just wasn't an option for CPython until recently when unittest.mock was added 
to the standard library).

Instead, I now think both of those cases would be better handled by the more 
sophisticated API discussed above that would deal with these things at the file 
descriptor level. So I suggest we split that API out as a new issue targetting 
the subprocess API.

3. Why not include automatic creation of StringIO objects?

Including such a default actually makes the API harder to document and explain. 
Composing a "you must supply a stream object" API with io.StringIO is easy to 
get local redirection is easy. If we support implicit creation, then we need to 
explain that it happens, and that the "as" clause can be used to retrieve the 
implicitly created object.

Raymond plans to work on a patch for this simple version of the API.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15805>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to