Skip to content

Use general OSError and more Python3 defaults from pyupgrade #19241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/matplotlib/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False):
stderr=(subprocess.PIPE if hide_stderr
else None))
break
except EnvironmentError:
except OSError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not bother changing this file either as it will be deleted by #18971 .

e = sys.exc_info()[1]
if e.errno == errno.ENOENT:
continue
Expand Down Expand Up @@ -115,7 +115,7 @@ def git_get_keywords(versionfile_abs):
# _version.py.
keywords = {}
try:
f = open(versionfile_abs, "r")
f = open(versionfile_abs)
for line in f.readlines():
if line.strip().startswith("git_refnames ="):
mo = re.search(r'=\s*"(.*)"', line)
Expand All @@ -126,7 +126,7 @@ def git_get_keywords(versionfile_abs):
if mo:
keywords["full"] = mo.group(1)
f.close()
except EnvironmentError:
except OSError:
pass
return keywords

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ def __init__(self, filename, metadata=None):
if not opened:
try:
self.tell_base = filename.tell()
except IOError:
except OSError:
fh = BytesIO()
self.original_file_like = filename
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _move_path_to_path_or_stream(src, dst):
If *dst* is a path, the metadata of *src* are *not* copied.
"""
if is_writable_file_like(dst):
fh = (open(src, 'r', encoding='latin-1')
fh = (open(src, encoding='latin-1')
if file_requires_unicode(dst)
else open(src, 'rb'))
with fh:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_webagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def random_ports(port, n):
mpl.rcParams['webagg.port_retries']):
try:
app.listen(port, cls.address)
except socket.error as e:
except OSError as e:
if e.errno != errno.EADDRINUSE:
raise
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ def json_load(filename):
--------
json_dump
"""
with open(filename, 'r') as fh:
with open(filename) as fh:
return json.load(fh, object_hook=_json_decode)


Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ def validate_webagg_address(s):
import socket
try:
socket.inet_aton(s)
except socket.error as e:
except OSError as e:
raise ValueError(
"'webagg.address' is not a valid IP address") from e
return s
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/style/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def use(style):
try:
rc = rc_params_from_file(style, use_default_template=False)
_apply_style(rc)
except IOError as err:
raise IOError(
except OSError as err:
raise OSError(
"{!r} not found in the style library and input is not a "
"valid URL or path; see `style.available` for list of "
"available styles".format(style)) from err
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def convert(old, new):
msg += "Standard output:\n%s\n" % stdout
if stderr:
msg += "Standard error:\n%s\n" % stderr
raise IOError(msg)
raise OSError(msg)

return convert

Expand Down Expand Up @@ -282,7 +282,7 @@ def convert(filename, cache):
"""
path = Path(filename)
if not path.exists():
raise IOError(f"{path} does not exist")
raise OSError(f"{path} does not exist")
if path.suffix[1:] not in converter:
import pytest
pytest.skip(f"Don't know how to convert {path.suffix} files to png")
Expand Down Expand Up @@ -425,7 +425,7 @@ def compare_images(expected, actual, tol, in_decorator=False):
# Convert the image to png
expected = os.fspath(expected)
if not os.path.exists(expected):
raise IOError('Baseline image %r does not exist.' % expected)
raise OSError('Baseline image %r does not exist.' % expected)
extension = expected.split('.')[-1]
if extension != 'png':
actual = convert(actual, cache=True)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Check that the test directories exist.
if not (Path(__file__).parent / 'baseline_images').exists():
raise IOError(
raise OSError(
'The baseline image directory does not exist. '
'This is most likely because the test data is not installed. '
'You may need to install matplotlib from source to get the '
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ def __array_finalize__(self, obj):

def __getitem__(self, item):
units = getattr(self, "units", None)
ret = super(QuantityND, self).__getitem__(item)
ret = super().__getitem__(item)
if isinstance(ret, QuantityND) or units is not None:
ret = QuantityND(ret, units)
return ret
Expand Down
2 changes: 1 addition & 1 deletion lib/mpl_toolkits/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Check that the test directories exist
if not (Path(__file__).parent / "baseline_images").exists():
raise IOError(
raise OSError(
'The baseline image directory does not exist. '
'This is most likely because the test data is not installed. '
'You may need to install matplotlib from source to get the '
Expand Down
6 changes: 3 additions & 3 deletions setupext.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def download_or_cache(url, sha):
if cache_dir is not None: # Try to read from cache.
try:
data = (cache_dir / sha).read_bytes()
except IOError:
except OSError:
pass
else:
if _get_hash(data) == sha:
Expand All @@ -95,7 +95,7 @@ def download_or_cache(url, sha):
cache_dir.mkdir(parents=True, exist_ok=True)
with open(cache_dir / sha, "xb") as fout:
fout.write(data)
except IOError:
except OSError:
pass

return BytesIO(data)
Expand Down Expand Up @@ -590,7 +590,7 @@ def do_custom_build(self, env):
except Exception:
pass
else:
raise IOError(
raise OSError(
f"Failed to download FreeType. Please download one of "
f"{target_urls} and extract it into {src_path} at the "
f"top-level of the source repository.")
Expand Down
6 changes: 3 additions & 3 deletions tools/cache_zenodo_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def download_or_cache(url, version):
if cache_dir is not None: # Try to read from cache.
try:
data = (cache_dir / version).read_bytes()
except IOError:
except OSError:
pass
else:
return BytesIO(data)
Expand All @@ -40,7 +40,7 @@ def download_or_cache(url, version):
cache_dir.mkdir(parents=True, exist_ok=True)
with open(cache_dir / version, "xb") as fout:
fout.write(data)
except IOError:
except OSError:
pass

return BytesIO(data)
Expand Down Expand Up @@ -103,7 +103,7 @@ def _get_xdg_cache_dir():
target_dir.mkdir(exist_ok=True, parents=True)
header = []
footer = []
with open(citing, "r") as fin:
with open(citing) as fin:
target = header
for ln in fin:
if target is not None:
Expand Down