Patchwork [1,of,2] pycompat: change users of pycompat.{stdin,stdout,stderr} to use procutil.std*

login
register
mail settings
Submitter Manuel Jacob
Date July 8, 2020, 4:06 a.m.
Message ID <e06f86fc9e488722f691.1594181219@tmp>
Download mbox | patch
Permalink /patch/46652/
State Accepted
Headers show

Comments

Manuel Jacob - July 8, 2020, 4:06 a.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1594050265 -7200
#      Mon Jul 06 17:44:25 2020 +0200
# Node ID e06f86fc9e488722f691d4f9ac1d2d2e7d38bdd2
# Parent  0977f0be2b0d640088b1b645ac2397289bb18f6d
# EXP-Topic stdio
pycompat: change users of pycompat.{stdin,stdout,stderr} to use procutil.std*

On Python 3, pycompat.{stdin,stdout,stderr} are usually block-buffered even if
connected to a TTY. procutil.{stdin,stdout,stderr} provide consistent behavior
across platforms and Python versions.
Yuya Nishihara - July 8, 2020, 10:33 a.m.
On Wed, 08 Jul 2020 06:06:59 +0200, Manuel Jacob wrote:
> # HG changeset patch
> # User Manuel Jacob <me@manueljacob.de>
> # Date 1594050265 -7200
> #      Mon Jul 06 17:44:25 2020 +0200
> # Node ID e06f86fc9e488722f691d4f9ac1d2d2e7d38bdd2
> # Parent  0977f0be2b0d640088b1b645ac2397289bb18f6d
> # EXP-Topic stdio
> pycompat: change users of pycompat.{stdin,stdout,stderr} to use procutil.std*

Queued, thanks.

Patch

diff --git a/contrib/dumprevlog b/contrib/dumprevlog
--- a/contrib/dumprevlog
+++ b/contrib/dumprevlog
@@ -28,7 +28,7 @@ 
 
 def printb(data, end=b'\n'):
     sys.stdout.flush()
-    pycompat.stdout.write(data + end)
+    procutil.stdout.write(data + end)
 
 
 for f in sys.argv[1:]:
diff --git a/contrib/simplemerge b/contrib/simplemerge
--- a/contrib/simplemerge
+++ b/contrib/simplemerge
@@ -45,8 +45,8 @@ 
 
 
 def showhelp():
-    pycompat.stdout.write(usage)
-    pycompat.stdout.write(b'\noptions:\n')
+    procutil.stdout.write(usage)
+    procutil.stdout.write(b'\noptions:\n')
 
     out_opts = []
     for shortopt, longopt, default, desc in options:
@@ -62,11 +62,11 @@ 
         )
     opts_len = max([len(opt[0]) for opt in out_opts])
     for first, second in out_opts:
-        pycompat.stdout.write(b' %-*s  %s\n' % (opts_len, first, second))
+        procutil.stdout.write(b' %-*s  %s\n' % (opts_len, first, second))
 
 
 try:
-    for fp in (sys.stdin, pycompat.stdout, sys.stderr):
+    for fp in (sys.stdin, procutil.stdout, sys.stderr):
         procutil.setbinary(fp)
 
     opts = {}
@@ -92,11 +92,11 @@ 
     )
 except ParseError as e:
     e = stringutil.forcebytestr(e)
-    pycompat.stdout.write(b"%s: %s\n" % (sys.argv[0].encode('utf8'), e))
+    procutil.stdout.write(b"%s: %s\n" % (sys.argv[0].encode('utf8'), e))
     showhelp()
     sys.exit(1)
 except error.Abort as e:
-    pycompat.stderr.write(b"abort: %s\n" % e)
+    procutil.stderr.write(b"abort: %s\n" % e)
     sys.exit(255)
 except KeyboardInterrupt:
     sys.exit(255)
diff --git a/contrib/undumprevlog b/contrib/undumprevlog
--- a/contrib/undumprevlog
+++ b/contrib/undumprevlog
@@ -9,7 +9,6 @@ 
 from mercurial import (
     encoding,
     node,
-    pycompat,
     revlog,
     transaction,
     vfs as vfsmod,
@@ -30,7 +29,7 @@ 
     if l.startswith("file:"):
         f = encoding.strtolocal(l[6:-1])
         r = revlog.revlog(opener, f)
-        pycompat.stdout.write(b'%s\n' % f)
+        procutil.stdout.write(b'%s\n' % f)
     elif l.startswith("node:"):
         n = node.bin(l[6:-1])
     elif l.startswith("linkrev:"):
diff --git a/hgext/releasenotes.py b/hgext/releasenotes.py
--- a/hgext/releasenotes.py
+++ b/hgext/releasenotes.py
@@ -30,7 +30,10 @@ 
     scmutil,
     util,
 )
-from mercurial.utils import stringutil
+from mercurial.utils import (
+    procutil,
+    stringutil,
+)
 
 cmdtable = {}
 command = registrar.command(cmdtable)
@@ -689,7 +692,7 @@ 
 def debugparsereleasenotes(ui, path, repo=None):
     """parse release notes and print resulting data structure"""
     if path == b'-':
-        text = pycompat.stdin.read()
+        text = procutil.stdin.read()
     else:
         with open(path, b'rb') as fh:
             text = fh.read()
diff --git a/tests/test-bundle2-format.t b/tests/test-bundle2-format.t
--- a/tests/test-bundle2-format.t
+++ b/tests/test-bundle2-format.t
@@ -22,8 +22,8 @@ 
   > from mercurial import changegroup
   > from mercurial import error
   > from mercurial import obsolete
-  > from mercurial import pycompat
   > from mercurial import registrar
+  > from mercurial.utils import procutil
   > 
   > 
   > try:
@@ -148,7 +148,7 @@ 
   >        bundler.newpart(b'output', data=genraise(), mandatory=False)
   > 
   >     if path is None:
-  >         file = pycompat.stdout
+  >         file = procutil.stdout
   >     else:
   >         file = open(path, 'wb')
   > 
@@ -181,7 +181,7 @@ 
   >         lock = repo.lock()
   >         tr = repo.transaction(b'processbundle')
   >         try:
-  >             unbundler = bundle2.getunbundler(ui, pycompat.stdin)
+  >             unbundler = bundle2.getunbundler(ui, procutil.stdin)
   >             op = bundle2.processbundle(repo, unbundler, lambda: tr)
   >             tr.close()
   >         except error.BundleValueError as exc:
@@ -192,7 +192,7 @@ 
   >         if tr is not None:
   >             tr.release()
   >         lock.release()
-  >         remains = pycompat.stdin.read()
+  >         remains = procutil.stdin.read()
   >         ui.write(b'%i unread bytes\n' % len(remains))
   >     if op.records[b'song']:
   >         totalverses = sum(r[b'verses'] for r in op.records[b'song'])
@@ -207,7 +207,7 @@ 
   > @command(b'statbundle2', [], b'')
   > def cmdstatbundle2(ui, repo):
   >     """print statistic on the bundle2 container read from stdin"""
-  >     unbundler = bundle2.getunbundler(ui, pycompat.stdin)
+  >     unbundler = bundle2.getunbundler(ui, procutil.stdin)
   >     try:
   >         params = unbundler.params
   >     except error.BundleValueError as exc:
diff --git a/tests/test-debugcommands.t b/tests/test-debugcommands.t
--- a/tests/test-debugcommands.t
+++ b/tests/test-debugcommands.t
@@ -573,11 +573,13 @@ 
   $ cat > debugstacktrace.py << EOF
   > from __future__ import absolute_import
   > from mercurial import (
-  >     pycompat,
   >     util,
   > )
+  > from mercurial.utils import (
+  >     procutil,
+  > )
   > def f():
-  >     util.debugstacktrace(f=pycompat.stdout)
+  >     util.debugstacktrace(f=procutil.stdout)
   >     g()
   > def g():
   >     util.dst(b'hello from g\\n', skip=1)
diff --git a/tests/test-help.t b/tests/test-help.t
--- a/tests/test-help.t
+++ b/tests/test-help.t
@@ -1917,9 +1917,9 @@ 
   > EOF
 
   $ "$PYTHON" <<EOF | sh
-  > from mercurial import pycompat
+  > from mercurial.utils import procutil
   > upper = b"\x8bL\x98^"
-  > pycompat.stdout.write(b"hg --encoding cp932 help -e ambiguous.%s\n" % upper)
+  > procutil.stdout.write(b"hg --encoding cp932 help -e ambiguous.%s\n" % upper)
   > EOF
   \x8bL\x98^ (esc)
   ----
@@ -1928,9 +1928,9 @@ 
   
 
   $ "$PYTHON" <<EOF | sh
-  > from mercurial import pycompat
+  > from mercurial.utils import procutil
   > lower = b"\x8bl\x98^"
-  > pycompat.stdout.write(b"hg --encoding cp932 help -e ambiguous.%s\n" % lower)
+  > procutil.stdout.write(b"hg --encoding cp932 help -e ambiguous.%s\n" % lower)
   > EOF
   \x8bl\x98^ (esc)
   ----
diff --git a/tests/test-hgweb-non-interactive.t b/tests/test-hgweb-non-interactive.t
--- a/tests/test-hgweb-non-interactive.t
+++ b/tests/test-hgweb-non-interactive.t
@@ -14,10 +14,12 @@ 
   >     dispatch,
   >     encoding,
   >     hg,
-  >     pycompat,
   >     ui as uimod,
   >     util,
   > )
+  > from mercurial.utils import (
+  >     procutil,
+  > )
   > ui = uimod.ui
   > from mercurial.hgweb import hgweb_mod
   > stringio = util.stringio
@@ -69,8 +71,8 @@ 
   > for c in i(env, startrsp):
   >     pass
   > sys.stdout.flush()
-  > pycompat.stdout.write(b'---- ERRORS\n')
-  > pycompat.stdout.write(b'%s\n' % errors.getvalue())
+  > procutil.stdout.write(b'---- ERRORS\n')
+  > procutil.stdout.write(b'%s\n' % errors.getvalue())
   > print('---- OS.ENVIRON wsgi variables')
   > print(sorted([x for x in os.environ if x.startswith('wsgi')]))
   > print('---- request.ENVIRON wsgi variables')
diff --git a/tests/test-import-context.t b/tests/test-import-context.t
--- a/tests/test-import-context.t
+++ b/tests/test-import-context.t
@@ -19,9 +19,8 @@ 
   > EOF
   $ cat > cat.py <<EOF
   > import sys
-  > from mercurial import pycompat
-  > from mercurial.utils import stringutil
-  > pycompat.stdout.write(b'%s\n'
+  > from mercurial.utils import procutil, stringutil
+  > procutil.stdout.write(b'%s\n'
   >                       % stringutil.pprint(open(sys.argv[1], 'rb').read()))
   > EOF
 
diff --git a/tests/test-lfs.t b/tests/test-lfs.t
--- a/tests/test-lfs.t
+++ b/tests/test-lfs.t
@@ -697,6 +697,7 @@ 
   >     revlog,
   > )
   > from mercurial.utils import (
+  >     procutil,
   >     stringutil,
   > )
   > def hash(rawtext):
@@ -713,7 +714,7 @@ 
   >         texts = [fl.rawdata(i) for i in fl]
   >         flags = [int(fl._revlog.flags(i)) for i in fl]
   >         hashes = [hash(t) for t in texts]
-  >         pycompat.stdout.write(b'  %s: rawsizes=%r flags=%r hashes=%s\n'
+  >         procutil.stdout.write(b'  %s: rawsizes=%r flags=%r hashes=%s\n'
   >                               % (name, sizes, flags, stringutil.pprint(hashes)))
   > EOF
 
diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t
--- a/tests/test-template-functions.t
+++ b/tests/test-template-functions.t
@@ -1575,15 +1575,15 @@ 
   > from __future__ import absolute_import
   > from mercurial import (
   >     dispatch,
-  >     pycompat,
   > )
   > from mercurial.utils import (
   >     cborutil,
+  >     procutil,
   >     stringutil,
   > )
   > dispatch.initstdio()
-  > items = cborutil.decodeall(pycompat.stdin.read())
-  > pycompat.stdout.write(stringutil.pprint(items, indent=1) + b'\n')
+  > items = cborutil.decodeall(procutil.stdin.read())
+  > procutil.stdout.write(stringutil.pprint(items, indent=1) + b'\n')
   > EOF
 
   $ hg log -T "{rev|cbor}" -R a -l2 | "$PYTHON" "$TESTTMP/decodecbor.py"
diff --git a/tests/test-template-map.t b/tests/test-template-map.t
--- a/tests/test-template-map.t
+++ b/tests/test-template-map.t
@@ -675,20 +675,20 @@ 
   > from __future__ import absolute_import
   > from mercurial import (
   >     dispatch,
-  >     pycompat,
   > )
   > from mercurial.utils import (
   >     cborutil,
+  >     procutil,
   >     stringutil,
   > )
   > dispatch.initstdio()
-  > data = pycompat.stdin.read()
+  > data = procutil.stdin.read()
   > # our CBOR decoder doesn't support parsing indefinite-length arrays,
   > # but the log output is indefinite stream by nature.
   > assert data[:1] == cborutil.BEGIN_INDEFINITE_ARRAY
   > assert data[-1:] == cborutil.BREAK
   > items = cborutil.decodeall(data[1:-1])
-  > pycompat.stdout.write(stringutil.pprint(items, indent=1) + b'\n')
+  > procutil.stdout.write(stringutil.pprint(items, indent=1) + b'\n')
   > EOF
 
   $ hg log -k nosuch -Tcbor | "$PYTHON" "$TESTTMP/decodecborarray.py"