Using contextlib.redirect_stderr or redirect_stdout does not work with pywikibot.output():
>>> import contextlib >>> import io >>> import pywikibot >>> with contextlib.redirect_stderr(io.StringIO()) as f: pywikibot.output('foo') foo >>> s = f.getvalue() >>> s '' >>> with contextlib.redirect_stdout(io.StringIO()) as f: pywikibot.output('foo') foo >>> s = f.getvalue() >>> s '' >>>
but it works for Pythons help() function as expected:
>>> with contextlib.redirect_stdout(io.StringIO()) as f: help('foo') >>> s = f.getvalue() >>> s "No Python documentation found for 'foo'.\nUse help() to get the interactive help utility.\nUse help(str) for help on the str class.\n\n" >>>
The behaviour fails in 6.2 as well as in a very early release 3.0.20180108