changeset 48873:5aafc3c5bdec

py3: use io.BytesIO directly Previously, pycompat.bytesio and pycompat.stringio referred to io.BytesIO. And util.bytesio and util.stringio aliased the pycompat symbols. This commit switches everything to use io.BytesIO directly. util.bytesio and util.stringio still exist to provide backwards compatibility, as they were the preferred symbols. Differential Revision: https://phab.mercurial-scm.org/D12252
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 20 Feb 2022 15:03:26 -0700
parents 968b29a5a7fc
children af0b21d5a930
files hgext/phabricator.py mercurial/pure/mpatch.py mercurial/pure/parsers.py mercurial/pycompat.py mercurial/util.py tests/test-basic.t tests/test-util.py
diffstat 7 files changed, 15 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/phabricator.py	Sun Feb 20 14:52:40 2022 -0700
+++ b/hgext/phabricator.py	Sun Feb 20 15:03:26 2022 -0700
@@ -62,6 +62,7 @@
 import base64
 import contextlib
 import hashlib
+import io
 import itertools
 import json
 import mimetypes
@@ -2200,7 +2201,7 @@
             for drev, contents in patches:
                 ui.status(_(b'applying patch from D%s\n') % drev)
 
-                with patch.extract(ui, pycompat.bytesio(contents)) as patchdata:
+                with patch.extract(ui, io.BytesIO(contents)) as patchdata:
                     msg, node, rej = cmdutil.tryimportone(
                         ui,
                         repo,
--- a/mercurial/pure/mpatch.py	Sun Feb 20 14:52:40 2022 -0700
+++ b/mercurial/pure/mpatch.py	Sun Feb 20 15:03:26 2022 -0700
@@ -7,11 +7,11 @@
 
 from __future__ import absolute_import
 
+import io
 import struct
 
-from .. import pycompat
 
-stringio = pycompat.bytesio
+stringio = io.BytesIO
 
 
 class mpatchError(Exception):
--- a/mercurial/pure/parsers.py	Sun Feb 20 14:52:40 2022 -0700
+++ b/mercurial/pure/parsers.py	Sun Feb 20 15:03:26 2022 -0700
@@ -7,6 +7,7 @@
 
 from __future__ import absolute_import
 
+import io
 import stat
 import struct
 import zlib
@@ -26,7 +27,7 @@
 from ..revlogutils import nodemap as nodemaputil
 from ..revlogutils import constants as revlog_constants
 
-stringio = pycompat.bytesio
+stringio = io.BytesIO
 
 
 _pack = struct.pack
--- a/mercurial/pycompat.py	Sun Feb 20 14:52:40 2022 -0700
+++ b/mercurial/pycompat.py	Sun Feb 20 15:03:26 2022 -0700
@@ -111,9 +111,6 @@
 sysexecutable = sys.executable
 if sysexecutable:
     sysexecutable = os.fsencode(sysexecutable)
-bytesio = io.BytesIO
-# TODO deprecate stringio name, as it is a lie on Python 3.
-stringio = bytesio
 
 
 def maplist(*args):
--- a/mercurial/util.py	Sun Feb 20 14:52:40 2022 -0700
+++ b/mercurial/util.py	Sun Feb 20 15:03:26 2022 -0700
@@ -21,6 +21,7 @@
 import errno
 import gc
 import hashlib
+import io
 import itertools
 import locale
 import mmap
@@ -78,7 +79,7 @@
 httplib = pycompat.httplib
 safehasattr = pycompat.safehasattr
 socketserver = pycompat.socketserver
-bytesio = pycompat.bytesio
+bytesio = io.BytesIO
 # TODO deprecate stringio name, as it is a lie on Python 3.
 stringio = bytesio
 xmlrpclib = pycompat.xmlrpclib
--- a/tests/test-basic.t	Sun Feb 20 14:52:40 2022 -0700
+++ b/tests/test-basic.t	Sun Feb 20 15:03:26 2022 -0700
@@ -240,15 +240,16 @@
 Underlying message streams should be updated when ui.fout/ferr are set:
 
   $ cat <<'EOF' > capui.py
-  > from mercurial import pycompat, registrar
+  > import io
+  > from mercurial import registrar
   > cmdtable = {}
   > command = registrar.command(cmdtable)
   > @command(b'capui', norepo=True)
   > def capui(ui):
   >     out = ui.fout
-  >     ui.fout = pycompat.bytesio()
+  >     ui.fout = io.BytesIO()
   >     ui.status(b'status\n')
-  >     ui.ferr = pycompat.bytesio()
+  >     ui.ferr = io.BytesIO()
   >     ui.warn(b'warn\n')
   >     out.write(b'stdout: %s' % ui.fout.getvalue())
   >     out.write(b'stderr: %s' % ui.ferr.getvalue())
--- a/tests/test-util.py	Sun Feb 20 14:52:40 2022 -0700
+++ b/tests/test-util.py	Sun Feb 20 15:03:26 2022 -0700
@@ -2,6 +2,7 @@
 from __future__ import absolute_import
 
 import contextlib
+import io
 import itertools
 import unittest
 
@@ -55,7 +56,7 @@
 
 @contextlib.contextmanager
 def capturestderr():
-    """Replace utils.procutil.stderr with a pycompat.bytesio instance
+    """Replace utils.procutil.stderr with an io.BytesIO instance
 
     The instance is made available as the return value of __enter__.
 
@@ -63,7 +64,7 @@
 
     """
     orig = utils.procutil.stderr
-    utils.procutil.stderr = pycompat.bytesio()
+    utils.procutil.stderr = io.BytesIO()
     try:
         yield utils.procutil.stderr
     finally: