Patchwork [2,of,5] py3: use bytes stdout

login
register
mail settings
Submitter Matt Harbison
Date Dec. 19, 2018, 10:39 p.m.
Message ID <c96002c3968f7f74bfe6.1545259157@Envy>
Download mbox | patch
Permalink /patch/37250/
State Accepted
Headers show

Comments

Matt Harbison - Dec. 19, 2018, 10:39 p.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1545249081 18000
#      Wed Dec 19 14:51:21 2018 -0500
# Node ID c96002c3968f7f74bfe68c5bacb1ba717a58e290
# Parent  155fcb60c7ec28729e50790c398a256498731d0e
py3: use bytes stdout

This fixes a failure in test-run-tests.t around notarealhghavefeature.  It seems
crazy to me that all of this needs to be adjusted in all of these tests, but the
line as run-tests.py sees it in _processoutput() before doing anything is
already mangled with a trailing '\r'.  Switching to normalizenewlines=True for
TTest works, but I'm sure that breaks other stuff.

Patch

diff --git a/tests/hghave.py b/tests/hghave.py
--- a/tests/hghave.py
+++ b/tests/hghave.py
@@ -16,6 +16,16 @@  checks = {
     "false": (lambda: False, "nail clipper"),
 }
 
+try:
+    import msvcrt
+    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
+    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
+except ImportError:
+    pass
+
+stdout = getattr(sys.stdout, 'buffer', sys.stdout)
+stderr = getattr(sys.stderr, 'buffer', sys.stderr)
+
 if sys.version_info[0] >= 3:
     def _bytespath(p):
         if p is None:
@@ -90,11 +100,12 @@  def require(features):
     result = checkfeatures(features)
 
     for missing in result['missing']:
-        sys.stderr.write('skipped: unknown feature: %s\n' % missing)
+        stderr.write(('skipped: unknown feature: %s\n'
+                      % missing).encode('utf-8'))
     for msg in result['skipped']:
-        sys.stderr.write('skipped: %s\n' % msg)
+        stderr.write(('skipped: %s\n' % msg).encode('utf-8'))
     for msg in result['error']:
-        sys.stderr.write('%s\n' % msg)
+        stderr.write(('%s\n' % msg).encode('utf-8'))
 
     if result['missing']:
         sys.exit(2)