changeset 45055:4c1b4805db57

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.
author Manuel Jacob <me@manueljacob.de>
date Mon, 06 Jul 2020 17:44:25 +0200
parents 922dbf0df4f1
children 9694895749ad
files contrib/dumprevlog contrib/simplemerge contrib/undumprevlog hgext/releasenotes.py tests/test-bundle2-format.t tests/test-debugcommands.t tests/test-help.t tests/test-hgweb-non-interactive.t tests/test-import-context.t tests/test-lfs.t tests/test-template-functions.t tests/test-template-map.t
diffstat 12 files changed, 47 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/dumprevlog	Mon Jul 06 10:30:55 2020 +0200
+++ b/contrib/dumprevlog	Mon Jul 06 17:44:25 2020 +0200
@@ -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:]:
--- a/contrib/simplemerge	Mon Jul 06 10:30:55 2020 +0200
+++ b/contrib/simplemerge	Mon Jul 06 17:44:25 2020 +0200
@@ -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)
--- a/contrib/undumprevlog	Mon Jul 06 10:30:55 2020 +0200
+++ b/contrib/undumprevlog	Mon Jul 06 17:44:25 2020 +0200
@@ -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:"):
--- a/hgext/releasenotes.py	Mon Jul 06 10:30:55 2020 +0200
+++ b/hgext/releasenotes.py	Mon Jul 06 17:44:25 2020 +0200
@@ -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()
--- a/tests/test-bundle2-format.t	Mon Jul 06 10:30:55 2020 +0200
+++ b/tests/test-bundle2-format.t	Mon Jul 06 17:44:25 2020 +0200
@@ -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:
--- a/tests/test-debugcommands.t	Mon Jul 06 10:30:55 2020 +0200
+++ b/tests/test-debugcommands.t	Mon Jul 06 17:44:25 2020 +0200
@@ -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)
@@ -588,15 +590,15 @@
   > EOF
   $ "$PYTHON" debugstacktrace.py
   stacktrace at:
-   *debugstacktrace.py:14 in * (glob)
-   *debugstacktrace.py:7  in f (glob)
+   *debugstacktrace.py:16 in * (glob)
+   *debugstacktrace.py:9  in f (glob)
   hello from g at:
-   *debugstacktrace.py:14 in * (glob)
-   *debugstacktrace.py:8  in f (glob)
+   *debugstacktrace.py:16 in * (glob)
+   *debugstacktrace.py:10 in f (glob)
   hi ...
   from h hidden in g at:
-   *debugstacktrace.py:8  in f (glob)
-   *debugstacktrace.py:11 in g (glob)
+   *debugstacktrace.py:10 in f (glob)
+   *debugstacktrace.py:13 in g (glob)
 
 Test debugcapabilities command:
 
--- a/tests/test-help.t	Mon Jul 06 10:30:55 2020 +0200
+++ b/tests/test-help.t	Mon Jul 06 17:44:25 2020 +0200
@@ -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)
   ----
--- a/tests/test-hgweb-non-interactive.t	Mon Jul 06 10:30:55 2020 +0200
+++ b/tests/test-hgweb-non-interactive.t	Mon Jul 06 17:44:25 2020 +0200
@@ -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')
--- a/tests/test-import-context.t	Mon Jul 06 10:30:55 2020 +0200
+++ b/tests/test-import-context.t	Mon Jul 06 17:44:25 2020 +0200
@@ -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
 
--- a/tests/test-lfs.t	Mon Jul 06 10:30:55 2020 +0200
+++ b/tests/test-lfs.t	Mon Jul 06 17:44:25 2020 +0200
@@ -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
 
--- a/tests/test-template-functions.t	Mon Jul 06 10:30:55 2020 +0200
+++ b/tests/test-template-functions.t	Mon Jul 06 17:44:25 2020 +0200
@@ -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"
--- a/tests/test-template-map.t	Mon Jul 06 10:30:55 2020 +0200
+++ b/tests/test-template-map.t	Mon Jul 06 17:44:25 2020 +0200
@@ -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"