Page MenuHomePhabricator

pywikibot.output seems not to use either sys.stdout nor sys.stderr
Closed, ResolvedPublic

Assigned To
Authored By
Xqt
May 27 2021, 12:38 PM
Referenced Files
F34889680: image.png
Dec 19 2021, 5:12 PM
F34889614: image.png
Dec 19 2021, 3:44 PM
F34889611: image.png
Dec 19 2021, 3:44 PM

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.

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

[pywikibot/core@master] [cleanup] no longer use win32_unicode for Python 3.6+

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

@maynorc: I think your patch will work after this my last patch above because I introduce two flush() calls in UI._print().

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

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

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

@maynorc: I think your patch will work after this my last patch above because I introduce two flush() calls in UI._print().

Yes, colors are working with flushing. I've restored you patch. But redirection fails:

import contextlib
import io
mport pywikibot
SyntaxError: invalid syntax
import pywikibot
with contextlib.redirect_stderr(io.StringIO()) as f:
	pywikibot.output('foo')

	
s = f.getvalue()
s
''
with contextlib.redirect_stdout(io.StringIO()) as f:
	pywikibot.stdout('foo')

s = f.getvalue()

s
''

The reason is that __stdin__, __stdout__ and __stderr__ may be None:
https://docs.python.org/3.11/library/sys.html?highlight=__stdin__#sys.__stdin__

@Xqt does redirection work now that you have the non-dunderscored streams available as a backup in the patch now?

@Xqt does redirection work now that you have the non-dunderscored streams available as a backup in the patch now?

Yes is works fine. The problem occurred if I made tests with Python IDLE where underscored IOs were None.

Change 769693 merged by jenkins-bot:

[pywikibot/core@master] [cleanup] no longer use win32_unicode for Python 3.6+

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

Change 769911 merged by jenkins-bot:

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

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