Page MenuHomePhabricator

pywikibot.output seems not to use either sys.stdout nor sys.stderr
Open, LowPublic

Description

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

Event Timeline

Change 743725 had a related patch set uploaded (by Chris Maynor; author: Chris Maynor):

[pywikibot/core@master] logging: Fix redirection for terminal interfaces

https://gerrit.wikimedia.org/r/743725

Xqt triaged this task as Low priority.Dec 6 2021, 5:32 AM

Change 743725 merged by jenkins-bot:

[pywikibot/core@master] logging: Fix redirection for terminal interfaces

https://gerrit.wikimedia.org/r/743725

Does not work as expected, some output is suppressed. Description of this malfunction will follow.

Old behaviour of output:

image.png (884×909 px, 143 KB)

New and wrong behaviour after the patch above:

image.png (885×905 px, 131 KB)

The diff isn't shown and the output from input choice (last line) is missing and is printed after the input was given.

Change 748274 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] Revert \"logging: Fix redirection for terminal interfaces\"

https://gerrit.wikimedia.org/r/748274

Change 748274 merged by jenkins-bot:

[pywikibot/core@master] Revert \"logging: Fix redirection for terminal interfaces\"

https://gerrit.wikimedia.org/r/748274

@Xqt, that is very interesting. I checked out both my commit, and the commit before your revert, and see this when performing your command above, which has all of the coloring and the correct printing

image.png (1×1 px, 868 KB)

I notice you're on a Windows machine, while I'm on Unix, so probably why I can't reproduce. I will look more into this

I notice you're on a Windows machine

Yes, I am using windows. Obviously there are some differences in the UI implementation of Win32UI and UnixUI. I hadn't the time to investigate deeper into this matter but the breakage was so important that I have reverted your patch for now. Thank you for your help.