Skip to content

Commit bbce801

Browse files
authored
Apply assorted Pyugrade suggestions (#4056)
2 parents a2d4a4e + 3aec0a3 commit bbce801

File tree

15 files changed

+35
-45
lines changed

15 files changed

+35
-45
lines changed

pkg_resources/__init__.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,7 @@ def _extract_resource(self, manager, zip_path): # noqa: C901
18741874
timestamp, size = self._get_date_and_size(self.zipinfo[zip_path])
18751875

18761876
if not WRITE_SUPPORT:
1877-
raise IOError(
1877+
raise OSError(
18781878
'"os.rename" and "os.unlink" are not supported ' 'on this platform'
18791879
)
18801880
try:
@@ -2001,7 +2001,7 @@ def get_metadata(self, name):
20012001
if name != 'PKG-INFO':
20022002
raise KeyError("No metadata except PKG-INFO is available")
20032003

2004-
with io.open(self.path, encoding='utf-8', errors="replace") as f:
2004+
with open(self.path, encoding='utf-8', errors="replace") as f:
20052005
metadata = f.read()
20062006
self._warn_on_replacement(metadata)
20072007
return metadata
@@ -2095,8 +2095,7 @@ def find_eggs_in_zip(importer, path_item, only=False):
20952095
if _is_egg_path(subitem):
20962096
subpath = os.path.join(path_item, subitem)
20972097
dists = find_eggs_in_zip(zipimport.zipimporter(subpath), subpath)
2098-
for dist in dists:
2099-
yield dist
2098+
yield from dists
21002099
elif subitem.lower().endswith(('.dist-info', '.egg-info')):
21012100
subpath = os.path.join(path_item, subitem)
21022101
submeta = EggMetadata(zipimport.zipimporter(subpath))
@@ -2131,8 +2130,7 @@ def find_on_path(importer, path_item, only=False):
21312130
for entry in sorted(entries):
21322131
fullpath = os.path.join(path_item, entry)
21332132
factory = dist_factory(path_item, entry, only)
2134-
for dist in factory(fullpath):
2135-
yield dist
2133+
yield from factory(fullpath)
21362134

21372135

21382136
def dist_factory(path_item, entry, only):
@@ -2850,8 +2848,7 @@ def _get_metadata_path_for_display(self, name):
28502848

28512849
def _get_metadata(self, name):
28522850
if self.has_metadata(name):
2853-
for line in self.get_metadata_lines(name):
2854-
yield line
2851+
yield from self.get_metadata_lines(name)
28552852

28562853
def _get_version(self):
28572854
lines = self._get_metadata(self.PKG_INFO)
@@ -3243,7 +3240,7 @@ def ensure_directory(path):
32433240
def _bypass_ensure_directory(path):
32443241
"""Sandbox-bypassing version of ensure_directory()"""
32453242
if not WRITE_SUPPORT:
3246-
raise IOError('"os.mkdir" not supported on this platform.')
3243+
raise OSError('"os.mkdir" not supported on this platform.')
32473244
dirname, filename = split(path)
32483245
if dirname and filename and not isdir(dirname):
32493246
_bypass_ensure_directory(dirname)

setuptools/command/build_py.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
import fnmatch
77
import textwrap
8-
import io
98
import distutils.errors
109
import itertools
1110
import stat
@@ -249,7 +248,7 @@ def check_package(self, package, package_dir):
249248
else:
250249
return init_py
251250

252-
with io.open(init_py, 'rb') as f:
251+
with open(init_py, 'rb') as f:
253252
contents = f.read()
254253
if b'declare_namespace' not in contents:
255254
raise distutils.errors.DistutilsError(

setuptools/command/develop.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from distutils.errors import DistutilsOptionError
44
import os
55
import glob
6-
import io
76

87
from setuptools.command.easy_install import easy_install
98
from setuptools import _path
@@ -154,7 +153,7 @@ def install_egg_scripts(self, dist):
154153
for script_name in self.distribution.scripts or []:
155154
script_path = os.path.abspath(convert_path(script_name))
156155
script_name = os.path.basename(script_path)
157-
with io.open(script_path) as strm:
156+
with open(script_path) as strm:
158157
script_text = strm.read()
159158
self.install_script(dist, script_name, script_text, script_path)
160159

setuptools/command/easy_install.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def check_site_dir(self): # noqa: C901 # is too complex (12) # FIXME
480480
if not os.path.exists(instdir):
481481
try:
482482
os.makedirs(instdir)
483-
except (OSError, IOError):
483+
except OSError:
484484
self.cant_write_to_target()
485485

486486
# Is it a configured, PYTHONPATH, implicit, or explicit site dir?
@@ -498,7 +498,7 @@ def check_site_dir(self): # noqa: C901 # is too complex (12) # FIXME
498498
os.unlink(testfile)
499499
open(testfile, 'w').close()
500500
os.unlink(testfile)
501-
except (OSError, IOError):
501+
except OSError:
502502
self.cant_write_to_target()
503503

504504
if not is_site_dir and not self.multi_version:
@@ -594,7 +594,7 @@ def check_pth_processing(self):
594594
dirname = os.path.dirname(ok_file)
595595
os.makedirs(dirname, exist_ok=True)
596596
f = open(pth_file, 'w')
597-
except (OSError, IOError):
597+
except OSError:
598598
self.cant_write_to_target()
599599
else:
600600
try:
@@ -1995,9 +1995,9 @@ def is_python(text, filename='<string>'):
19951995
def is_sh(executable):
19961996
"""Determine if the specified executable is a .sh (contains a #! line)"""
19971997
try:
1998-
with io.open(executable, encoding='latin-1') as fp:
1998+
with open(executable, encoding='latin-1') as fp:
19991999
magic = fp.read(2)
2000-
except (OSError, IOError):
2000+
except OSError:
20012001
return executable
20022002
return magic == '#!'
20032003

setuptools/command/sdist.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import distutils.command.sdist as orig
33
import os
44
import sys
5-
import io
65
import contextlib
76
from itertools import chain
87

@@ -189,7 +188,7 @@ def _manifest_is_not_generated(self):
189188
if not os.path.isfile(self.manifest):
190189
return False
191190

192-
with io.open(self.manifest, 'rb') as fp:
191+
with open(self.manifest, 'rb') as fp:
193192
first_line = fp.readline()
194193
return first_line != '# file GENERATED by distutils, do NOT edit\n'.encode()
195194

setuptools/command/upload_docs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from distutils import log
99
from distutils.errors import DistutilsOptionError
1010
import os
11-
import socket
1211
import zipfile
1312
import tempfile
1413
import shutil
@@ -201,7 +200,7 @@ def upload_file(self, filename):
201200
conn.putheader('Authorization', auth)
202201
conn.endheaders()
203202
conn.send(body)
204-
except socket.error as e:
203+
except OSError as e:
205204
self.announce(str(e), log.ERROR)
206205
return
207206

setuptools/config/expand.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"""
2020
import ast
2121
import importlib
22-
import io
2322
import os
2423
import pathlib
2524
import sys
@@ -147,7 +146,7 @@ def _filter_existing_files(filepaths: Iterable[_Path]) -> Iterator[_Path]:
147146

148147

149148
def _read_file(filepath: Union[bytes, _Path]) -> str:
150-
with io.open(filepath, encoding='utf-8') as f:
149+
with open(filepath, encoding='utf-8') as f:
151150
return f.read()
152151

153152

setuptools/dist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ def _parse_config_files(self, filenames=None): # noqa: C901
482482
parser = ConfigParser()
483483
parser.optionxform = str
484484
for filename in filenames:
485-
with io.open(filename, encoding='utf-8') as reader:
485+
with open(filename, encoding='utf-8') as reader:
486486
if DEBUG:
487487
self.announce(" reading {filename}".format(**locals()))
488488
parser.read_file(reader)

setuptools/msvc.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -567,17 +567,17 @@ def lookup(self, key, name):
567567
bkey = None
568568
try:
569569
bkey = openkey(hkey, ms(key), 0, key_read)
570-
except (OSError, IOError):
570+
except OSError:
571571
if not self.pi.current_is_x86():
572572
try:
573573
bkey = openkey(hkey, ms(key, True), 0, key_read)
574-
except (OSError, IOError):
574+
except OSError:
575575
continue
576576
else:
577577
continue
578578
try:
579579
return winreg.QueryValueEx(bkey, name)[0]
580-
except (OSError, IOError):
580+
except OSError:
581581
pass
582582
finally:
583583
if bkey:
@@ -646,7 +646,7 @@ def find_reg_vs_vers(self):
646646
for hkey, key in itertools.product(self.ri.HKEYS, vckeys):
647647
try:
648648
bkey = winreg.OpenKey(hkey, ms(key), 0, winreg.KEY_READ)
649-
except (OSError, IOError):
649+
except OSError:
650650
continue
651651
with bkey:
652652
subkeys, values, _ = winreg.QueryInfoKey(bkey)
@@ -678,7 +678,7 @@ def find_programdata_vs_vers(self):
678678
try:
679679
hashed_names = listdir(instances_dir)
680680

681-
except (OSError, IOError):
681+
except OSError:
682682
# Directory not exists with all Visual Studio versions
683683
return vs_versions
684684

@@ -698,7 +698,7 @@ def find_programdata_vs_vers(self):
698698
self._as_float_version(state['installationVersion'])
699699
] = vs_path
700700

701-
except (OSError, IOError, KeyError):
701+
except (OSError, KeyError):
702702
# Skip if "state.json" file is missing or bad format
703703
continue
704704

@@ -784,7 +784,7 @@ def _guess_vc(self):
784784
vc_ver = listdir(guess_vc)[-1]
785785
self.vc_ver = self._as_float_version(vc_ver)
786786
return join(guess_vc, vc_ver)
787-
except (OSError, IOError, IndexError):
787+
except (OSError, IndexError):
788788
return ''
789789

790790
def _guess_vc_legacy(self):

setuptools/package_index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ def open_url(self, url, warning=None): # noqa: C901 # is too complex (12)
806806
'%s returned a bad status line. The server might be '
807807
'down, %s' % (url, v.line)
808808
) from v
809-
except (http.client.HTTPException, socket.error) as v:
809+
except (http.client.HTTPException, OSError) as v:
810810
if warning:
811811
self.warn(warning, v)
812812
else:

setuptools/tests/test_core_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def test_maintainer_author(name, attrs, tmpdir):
286286

287287
dist.metadata.write_pkg_info(fn_s)
288288

289-
with io.open(str(fn.join('PKG-INFO')), 'r', encoding='utf-8') as f:
289+
with open(str(fn.join('PKG-INFO')), 'r', encoding='utf-8') as f:
290290
pkg_info = f.read()
291291

292292
assert _valid_metadata(pkg_info)

setuptools/tests/test_install_scripts.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""install_scripts tests
22
"""
33

4-
import io
54
import sys
65

76
import pytest
@@ -43,7 +42,7 @@ def test_sys_executable_escaping_unix(self, tmpdir, monkeypatch):
4342
monkeypatch.setattr('sys.executable', self.unix_exe)
4443
with tmpdir.as_cwd():
4544
self._run_install_scripts(str(tmpdir))
46-
with io.open(str(tmpdir.join('foo')), 'r') as f:
45+
with open(str(tmpdir.join('foo')), 'r') as f:
4746
actual = f.readline()
4847
assert actual == expected
4948

@@ -57,7 +56,7 @@ def test_sys_executable_escaping_win32(self, tmpdir, monkeypatch):
5756
monkeypatch.setattr('sys.executable', self.win32_exe)
5857
with tmpdir.as_cwd():
5958
self._run_install_scripts(str(tmpdir))
60-
with io.open(str(tmpdir.join('foo-script.py')), 'r') as f:
59+
with open(str(tmpdir.join('foo-script.py')), 'r') as f:
6160
actual = f.readline()
6261
assert actual == expected
6362

@@ -71,7 +70,7 @@ def test_executable_with_spaces_escaping_unix(self, tmpdir):
7170
expected = '#!%s\n' % self.unix_spaces_exe
7271
with tmpdir.as_cwd():
7372
self._run_install_scripts(str(tmpdir), self.unix_spaces_exe)
74-
with io.open(str(tmpdir.join('foo')), 'r') as f:
73+
with open(str(tmpdir.join('foo')), 'r') as f:
7574
actual = f.readline()
7675
assert actual == expected
7776

@@ -85,6 +84,6 @@ def test_executable_arg_escaping_win32(self, tmpdir):
8584
expected = '#!"%s"\n' % self.win32_exe
8685
with tmpdir.as_cwd():
8786
self._run_install_scripts(str(tmpdir), '"' + self.win32_exe + '"')
88-
with io.open(str(tmpdir.join('foo-script.py')), 'r') as f:
87+
with open(str(tmpdir.join('foo-script.py')), 'r') as f:
8988
actual = f.readline()
9089
assert actual == expected

setuptools/tests/test_sdist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def decompose(path):
8686

8787

8888
def read_all_bytes(filename):
89-
with io.open(filename, 'rb') as fp:
89+
with open(filename, 'rb') as fp:
9090
return fp.read()
9191

9292

setuptools/tests/test_setopt.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
import configparser
32

43
from setuptools.command import setopt
@@ -8,13 +7,13 @@ class TestEdit:
87
@staticmethod
98
def parse_config(filename):
109
parser = configparser.ConfigParser()
11-
with io.open(filename, encoding='utf-8') as reader:
10+
with open(filename, encoding='utf-8') as reader:
1211
parser.read_file(reader)
1312
return parser
1413

1514
@staticmethod
1615
def write_text(file, content):
17-
with io.open(file, 'wb') as strm:
16+
with open(file, 'wb') as strm:
1817
strm.write(content.encode('utf-8'))
1918

2019
def test_utf8_encoding_retained(self, tmpdir):

tools/build_launchers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def build_cmake_project_with_msbuild(build_arena, msbuild_parameters):
8686
subprocess.check_call(cmd, cwd=build_arena)
8787

8888

89-
@functools.lru_cache()
89+
@functools.lru_cache
9090
def get_cmake():
9191
"""Find CMake using registry."""
9292
import winreg
@@ -96,7 +96,7 @@ def get_cmake():
9696
return root / 'bin\\CMake.exe'
9797

9898

99-
@functools.lru_cache()
99+
@functools.lru_cache
100100
def get_msbuild():
101101
"""Use VSWhere to find MSBuild."""
102102
vswhere = pathlib.Path(

0 commit comments

Comments
 (0)