Patchwork [6,of,7] get-with-headers: handle python3 bytes in stdout

login
register
mail settings
Submitter timeless
Date May 11, 2016, 5:20 a.m.
Message ID <6849a6b26ea35775c142.1462944028@gcc2-power8.osuosl.org>
Download mbox | patch
Permalink /patch/14997/
State Changes Requested
Delegated to: Yuya Nishihara
Headers show

Comments

timeless - May 11, 2016, 5:20 a.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1462561643 0
#      Fri May 06 19:07:23 2016 +0000
# Node ID 6849a6b26ea35775c142b78802b6b943dd2adf68
# Parent  67542cf802e8669a1c954c3066f468fe3ae6ba0e
# EXP-Topic runtests
# Available At bb://timeless/mercurial-crew
#              hg pull bb://timeless/mercurial-crew -r 6849a6b26ea3
get-with-headers: handle python3 bytes in stdout
Yuya Nishihara - May 16, 2016, 2:33 p.m.
On Wed, 11 May 2016 05:20:28 +0000, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1462561643 0
> #      Fri May 06 19:07:23 2016 +0000
> # Node ID 6849a6b26ea35775c142b78802b6b943dd2adf68
> # Parent  67542cf802e8669a1c954c3066f468fe3ae6ba0e
> # EXP-Topic runtests
> # Available At bb://timeless/mercurial-crew
> #              hg pull bb://timeless/mercurial-crew -r 6849a6b26ea3
> get-with-headers: handle python3 bytes in stdout
> 
> diff -r 67542cf802e8 -r 6849a6b26ea3 tests/get-with-headers.py
> --- a/tests/get-with-headers.py	Fri May 06 19:06:05 2016 +0000
> +++ b/tests/get-with-headers.py	Fri May 06 19:07:23 2016 +0000
> @@ -17,6 +17,17 @@
>  except ImportError:
>      pass
>  
> +try:
> +    stdout = sys.stdout.buffer
> +except AttributeError:
> +    stdout = sys.stdout
> +
> +if sys.version_info[0] < 3:
> +    sprint = print
> +else:
> +    def sprint(*s):
> +        print(*s, file=sys.stdout, flush=True)

Why do flush() only on Python 3 ?

The commit message explains why we need "stdout = sys.stdout.buffer", but
it says nothing about sprint().

Patch

diff -r 67542cf802e8 -r 6849a6b26ea3 tests/get-with-headers.py
--- a/tests/get-with-headers.py	Fri May 06 19:06:05 2016 +0000
+++ b/tests/get-with-headers.py	Fri May 06 19:07:23 2016 +0000
@@ -17,6 +17,17 @@ 
 except ImportError:
     pass
 
+try:
+    stdout = sys.stdout.buffer
+except AttributeError:
+    stdout = sys.stdout
+
+if sys.version_info[0] < 3:
+    sprint = print
+else:
+    def sprint(*s):
+        print(*s, file=sys.stdout, flush=True)
+
 twice = False
 if '--twice' in sys.argv:
     sys.argv.remove('--twice')
@@ -41,15 +52,15 @@ 
     conn = httplib.HTTPConnection(host)
     conn.request("GET", '/' + path, None, headers)
     response = conn.getresponse()
-    print(response.status, response.reason)
+    sprint(response.status, response.reason)
     if show[:1] == ['-']:
         show = sorted(h for h, v in response.getheaders()
                       if h.lower() not in show)
     for h in [h.lower() for h in show]:
         if response.getheader(h, None) is not None:
-            print("%s: %s" % (h, response.getheader(h)))
+            sprint("%s: %s" % (h, response.getheader(h)))
     if not headeronly:
-        print()
+        sprint()
         data = response.read()
 
         # Pretty print JSON. This also has the beneficial side-effect
@@ -60,9 +71,9 @@ 
             data = json.loads(data)
             lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
             for line in lines:
-                print(line.rstrip())
+                sprint(line.rstrip())
         else:
-            sys.stdout.write(data)
+            stdout.write(data)
 
         if twice and response.getheader('ETag', None):
             tag = response.getheader('ETag')