pyupgrade: drop redundant `open()` modes
On one hand, this gets rid of some py2 cruft (like `U` and `t` modes). OTOH, I
like being explict. But there's no way to disable individual transformations
(and this change is part of `_fix_tokens()`, which is even harder to selectively
use locally), and I'd rather have less noise when using `pyupgrade`.
Warning: `contrib/synthrepo.py` appears not to have been updated to py3, as it's
passing str to `error.Abort`. But since it was opening the file in unicode on
py2, this change is a no-op.
--- a/contrib/automation/hgautomation/aws.py Mon Jan 06 01:44:46 2025 -0500
+++ b/contrib/automation/hgautomation/aws.py Tue Jan 07 17:28:46 2025 -0500
@@ -332,7 +332,7 @@
pub_full = key_path / f
priv_full = key_path / ('keypair-%s' % name)
- with open(pub_full, 'r', encoding='ascii') as fh:
+ with open(pub_full, encoding='ascii') as fh:
data = fh.read()
if not data.startswith('ssh-rsa '):
--- a/contrib/import-checker.py Mon Jan 06 01:44:46 2025 -0500
+++ b/contrib/import-checker.py Tue Jan 07 17:28:46 2025 -0500
@@ -704,7 +704,7 @@
# Python source file encoding. But in reality we don't use anything
# other than ASCII (mainly) and UTF-8 (in a few exceptions), so
# simplicity is fine.
- with open(f, 'r', encoding='utf-8') as src:
+ with open(f, encoding='utf-8') as src:
for script, modname, t, line in embedded(f, modname, src):
yield script, modname.encode('utf8'), t, line
--- a/contrib/memory.py Mon Jan 06 01:44:46 2025 -0500
+++ b/contrib/memory.py Tue Jan 07 17:28:46 2025 -0500
@@ -15,7 +15,7 @@
def memusage(ui):
"""Report memory usage of the current process."""
result = {'peak': 0, 'rss': 0}
- with open('/proc/self/status', 'r') as status:
+ with open('/proc/self/status') as status:
# This will only work on systems with a /proc file system
# (like Linux).
for line in status:
--- a/contrib/perf-utils/perf-revlog-write-plot.py Mon Jan 06 01:44:46 2025 -0500
+++ b/contrib/perf-utils/perf-revlog-write-plot.py Tue Jan 07 17:28:46 2025 -0500
@@ -118,7 +118,7 @@
if len(sys.argv) > 1:
print('reading from %r' % sys.argv[1])
- with open(sys.argv[1], 'r') as fp:
+ with open(sys.argv[1]) as fp:
plot(json.load(fp), title=sys.argv[1])
else:
print('reading from stdin')
--- a/contrib/python-zstandard/setup.py Mon Jan 06 01:44:46 2025 -0500
+++ b/contrib/python-zstandard/setup.py Tue Jan 07 17:28:46 2025 -0500
@@ -77,7 +77,7 @@
version = None
-with open("c-ext/python-zstandard.h", "r") as fh:
+with open("c-ext/python-zstandard.h") as fh:
for line in fh:
if not line.startswith("#define PYTHON_ZSTANDARD_VERSION"):
continue
@@ -94,7 +94,7 @@
name="zstandard",
version=version,
description="Zstandard bindings for Python",
- long_description=open("README.rst", "r").read(),
+ long_description=open("README.rst").read(),
url="https://github.com/indygreg/python-zstandard",
author="Gregory Szorc",
author_email="gregory.szorc@gmail.com",
--- a/contrib/synthrepo.py Mon Jan 06 01:44:46 2025 -0500
+++ b/contrib/synthrepo.py Tue Jan 07 17:28:46 2025 -0500
@@ -326,7 +326,7 @@
dictfile = opts.get('dict') or '/usr/share/dict/words'
try:
- fp = open(dictfile, 'rU')
+ fp = open(dictfile)
except OSError as err:
raise error.Abort('%s: %s' % (dictfile, err.strerror))
words = fp.read().splitlines()
--- a/contrib/testparseutil.py Mon Jan 06 01:44:46 2025 -0500
+++ b/contrib/testparseutil.py Tue Jan 07 17:28:46 2025 -0500
@@ -48,7 +48,7 @@
def opentext(f):
- return open(f, 'r')
+ return open(f)
def b2s(x):
--- a/doc/gendoc.py Mon Jan 06 01:44:46 2025 -0500
+++ b/doc/gendoc.py Tue Jan 07 17:28:46 2025 -0500
@@ -361,7 +361,7 @@
def _rendertpl(tplname, data):
tplpath = os.path.join(os.path.dirname(__file__), 'templates', tplname)
- with open(tplpath, 'r') as f:
+ with open(tplpath) as f:
tpl = f.read()
if isinstance(tpl, bytes):
--- a/hgext/convert/common.py Mon Jan 06 01:44:46 2025 -0500
+++ b/hgext/convert/common.py Tue Jan 07 17:28:46 2025 -0500
@@ -90,7 +90,7 @@
whitespace: Optional[bytes] = None,
):
if data is None:
- data = open(filepath, 'r', encoding='latin1')
+ data = open(filepath, encoding='latin1')
else:
if filepath is not None:
raise error.ProgrammingError(
--- a/i18n/polib.py Mon Jan 06 01:44:46 2025 -0500
+++ b/i18n/polib.py Tue Jan 07 17:28:46 2025 -0500
@@ -1252,10 +1252,10 @@
enc = kwargs.get('encoding', default_encoding)
if _is_file(pofile):
try:
- self.fhandle = open(pofile, 'rt', encoding=enc)
+ self.fhandle = open(pofile, encoding=enc)
except LookupError:
enc = default_encoding
- self.fhandle = open(pofile, 'rt', encoding=enc)
+ self.fhandle = open(pofile, encoding=enc)
else:
self.fhandle = pofile.splitlines()
--- a/mercurial/crecord.py Mon Jan 06 01:44:46 2025 -0500
+++ b/mercurial/crecord.py Tue Jan 07 17:28:46 2025 -0500
@@ -623,7 +623,7 @@
chunkselector.stdscr = dummystdscr()
if testfn and os.path.exists(testfn):
# This file is purposely NOT opened in binary mode
- with open(testfn, 'r') as testf:
+ with open(testfn) as testf:
testcommands = [x.rstrip('\n') for x in testf.readlines()]
while True:
--- a/tests/logexceptions.py Mon Jan 06 01:44:46 2025 -0500
+++ b/tests/logexceptions.py Tue Jan 07 17:28:46 2025 -0500
@@ -49,7 +49,7 @@
frame = tb.tb_frame
try:
- with open(inspect.getsourcefile(tb), 'r') as fh:
+ with open(inspect.getsourcefile(tb)) as fh:
hgline = fh.readlines()[frame.f_lineno - 1].strip()
except (IndexError, OSError):
pass
--- a/tests/test-verify-repo-operations.py Mon Jan 06 01:44:46 2025 -0500
+++ b/tests/test-verify-repo-operations.py Tue Jan 07 17:28:46 2025 -0500
@@ -210,7 +210,7 @@
stderr=devnull,
)
rewriter.communicate("yes")
- with open(path, 'r') as i:
+ with open(path) as i:
ttest = i.read()
e = None
@@ -233,7 +233,7 @@
"",
)
o.write(l + os.linesep)
- with open(tf, 'r') as r:
+ with open(tf) as r:
t = r.read()
assert ext not in t, t
output = subprocess.check_output(