[PATCH] get-with-headers: use bytes stdout thoroughly

Gregory Szorc gregory.szorc at gmail.com
Fri Mar 2 14:38:08 EST 2018


On Fri, Mar 2, 2018 at 2:07 PM, Yuya Nishihara <yuya at tcha.org> wrote:

> # HG changeset patch
> # User Yuya Nishihara <yuya at tcha.org>
> # Date 1520016631 18000
> #      Fri Mar 02 13:50:31 2018 -0500
> # Node ID b71dad3502643ade9b01166ab6657d216c46c221
> # Parent  45f149bf08d12750cc4993fd3d4e6f1bb999e8d5
> get-with-headers: use bytes stdout thoroughly
>

Queued, thanks.


>
> On Python 3, sys.stdout.buffer is backed by a separate buffer from
> sys.stdout.
> We should choose one.
>
> diff --git a/tests/get-with-headers.py b/tests/get-with-headers.py
> --- a/tests/get-with-headers.py
> +++ b/tests/get-with-headers.py
> @@ -3,7 +3,7 @@
>  """This does HTTP GET requests given a host:port and path and returns
>  a subset of the headers plus the body of the result."""
>
> -from __future__ import absolute_import, print_function
> +from __future__ import absolute_import
>
>  import argparse
>  import json
> @@ -23,6 +23,8 @@ try:
>  except ImportError:
>      pass
>
> +stdout = getattr(sys.stdout, 'buffer', sys.stdout)
> +
>  parser = argparse.ArgumentParser()
>  parser.add_argument('--twice', action='store_true')
>  parser.add_argument('--headeronly', action='store_true')
> @@ -62,21 +64,23 @@ def request(host, path, show):
>      conn = httplib.HTTPConnection(host)
>      conn.request("GET", '/' + path, None, headers)
>      response = conn.getresponse()
> -    print(response.status, response.reason)
> +    stdout.write(b'%d %s\n' % (response.status,
> +                               response.reason.encode('ascii')))
>      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)))
> +            stdout.write(b"%s: %s\n" % (h.encode('ascii'),
> +                                        response.getheader(h).encode('
> ascii')))
>      if not headeronly:
> -        print()
> +        stdout.write(b'\n')
>          data = response.read()
>
>          if args.bodyfile:
>              bodyfh = open(args.bodyfile, 'wb')
>          else:
> -            bodyfh = getattr(sys.stdout, 'buffer', sys.stdout)
> +            bodyfh = stdout
>
>          # Pretty print JSON. This also has the beneficial side-effect
>          # of verifying emitted JSON is well-formed.
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.mercurial-scm.org/pipermail/mercurial-devel/attachments/20180302/8f63b894/attachment.html>


More information about the Mercurial-devel mailing list