jenkins-bot has submitted this change and it was merged.
Change subject: win32_unicode: always use byte std streams ......................................................................
win32_unicode: always use byte std streams
On Python 3, sys.std* expect text data, while on Python 2, sys.std* expect bytes. The interface we provide (win32_unicode.std*) is bytes-based (specifically, utf-8 encoded text). Python 3 provides sys.std*.buffer as bytes interface, so we use that instead of sys.std*.
Bug: T147654 Change-Id: Ida7dba2def00d60160054919b3084945f629571b --- M pywikibot/userinterfaces/win32_unicode.py 1 file changed, 32 insertions(+), 17 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/userinterfaces/win32_unicode.py b/pywikibot/userinterfaces/win32_unicode.py index 2425fc9..8295c51 100755 --- a/pywikibot/userinterfaces/win32_unicode.py +++ b/pywikibot/userinterfaces/win32_unicode.py @@ -35,18 +35,42 @@
OSWIN32 = (sys.platform == "win32")
+ +# If any exception occurs in this code, we'll probably try to print it on stderr, +# which makes for frustrating debugging if stderr is directed to our wrapper. +# So be paranoid about catching errors and reporting them to original_stderr, +# so that we can at least see them. +def _complain(message): + print(isinstance(message, str) and message or repr(message), + file=sys.stderr) # noqa: print + + if sys.version_info[0] > 2: unicode = str PY3 = True + try: + stdout = sys.stdout.buffer + stderr = sys.stderr.buffer + stdin = sys.stdin.buffer + except AttributeError as e: + _complain("Cannot find buffer interface for stdout, stderr or stdin.\n" + "Using (incorrect) text interfaces instead. This is likely to\n" + "break on non-ascii text.") + stdout = sys.stdout + stderr = sys.stderr + stdin = sys.stdin + else: PY3 = False + stdout = sys.stdout + stderr = sys.stderr + stdin = sys.stdin
-stdin = sys.stdin -stdout = sys.stdout -stderr = sys.stderr argv = sys.argv
-original_stderr = sys.stderr +original_stdout = stdout +original_stderr = stderr +original_stdin = stdin
if OSWIN32: from ctypes import WINFUNCTYPE, windll, POINTER, WinError @@ -167,15 +191,6 @@ return std.fileno() except UnsupportedOperation: pass - - -# If any exception occurs in this code, we'll probably try to print it on stderr, -# which makes for frustrating debugging if stderr is directed to our wrapper. -# So be paranoid about catching errors and reporting them to original_stderr, -# so that we can at least see them. -def _complain(message): - print(isinstance(message, str) and message or repr(message), - file=original_stderr) # noqa: print
def register_cp65001(): @@ -316,17 +331,17 @@ stdin = UnicodeInput(hStdin, name='<Unicode console stdin>')
if real_stdout: - stdout = UnicodeOutput(hStdout, sys.stdout, STDOUT_FILENO, + stdout = UnicodeOutput(hStdout, original_stdout, STDOUT_FILENO, '<Unicode console stdout>') else: - stdout = UnicodeOutput(None, sys.stdout, old_stdout_fileno, + stdout = UnicodeOutput(None, original_stdout, old_stdout_fileno, '<Unicode redirected stdout>')
if real_stderr: - stderr = UnicodeOutput(hStderr, sys.stderr, STDERR_FILENO, + stderr = UnicodeOutput(hStderr, original_stderr, STDERR_FILENO, '<Unicode console stderr>') else: - stderr = UnicodeOutput(None, sys.stderr, old_stderr_fileno, + stderr = UnicodeOutput(None, original_stderr, old_stderr_fileno, '<Unicode redirected stderr>') except Exception as e: _complain("exception %r while fixing up sys.stdout and sys.stderr" % (e,))
pywikibot-commits@lists.wikimedia.org