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.

Author brett.cannon
Recipients alex, brett.cannon, ncoghlan, rhettinger
Date 2012-08-29.17:16:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1346260599.65.0.190822585501.issue15805@psf.upfronthosting.co.za>
In-reply-to
Content
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('sys.stdout', replacement if replacement is not None else io.StringIO())

Now that being said, this is extremely common, so Nick's point of just going ahead and providing the helper makes sense. But I wouldn't put it in contextlib but instead in unittest.mock since it is mocking out sys.stdout.
History
Date User Action Args
2012-08-29 17:16:39brett.cannonsetrecipients: + brett.cannon, rhettinger, ncoghlan, alex
2012-08-29 17:16:39brett.cannonsetmessageid: <1346260599.65.0.190822585501.issue15805@psf.upfronthosting.co.za>
2012-08-29 17:16:39brett.cannonlinkissue15805 messages
2012-08-29 17:16:38brett.cannoncreate