From 64541a7438ba9c6ff54cd957ac437808028c28f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Tue, 5 Jan 2021 07:16:17 +0100 Subject: [PATCH 1/3] Use OSError as the general system-related exception According to PEP 3151, OSError can be used for all system-related exceptions and thus replace IOError, EnvironmentError, socket.error, etc. --- lib/matplotlib/_version.py | 4 ++-- lib/matplotlib/backends/backend_pdf.py | 2 +- lib/matplotlib/backends/backend_webagg.py | 2 +- lib/matplotlib/rcsetup.py | 2 +- lib/matplotlib/style/core.py | 4 ++-- lib/matplotlib/testing/compare.py | 6 +++--- lib/matplotlib/tests/__init__.py | 2 +- lib/mpl_toolkits/tests/__init__.py | 2 +- setupext.py | 6 +++--- tools/cache_zenodo_svg.py | 4 ++-- 10 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/_version.py b/lib/matplotlib/_version.py index 08dc9b6117f2..38b5859a1c3a 100644 --- a/lib/matplotlib/_version.py +++ b/lib/matplotlib/_version.py @@ -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: e = sys.exc_info()[1] if e.errno == errno.ENOENT: continue @@ -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 diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 636f87c3d97b..ee08ac642214 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -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: diff --git a/lib/matplotlib/backends/backend_webagg.py b/lib/matplotlib/backends/backend_webagg.py index 7f43187ac65f..a6736164daad 100644 --- a/lib/matplotlib/backends/backend_webagg.py +++ b/lib/matplotlib/backends/backend_webagg.py @@ -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: diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 3aa6e898c271..12746166bfd5 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -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 diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index 0c2f632e016a..c39d1996d4cb 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -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 diff --git a/lib/matplotlib/testing/compare.py b/lib/matplotlib/testing/compare.py index fb2e55880cde..c576c394b2ec 100644 --- a/lib/matplotlib/testing/compare.py +++ b/lib/matplotlib/testing/compare.py @@ -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 @@ -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") @@ -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) diff --git a/lib/matplotlib/tests/__init__.py b/lib/matplotlib/tests/__init__.py index 7c4c5e946b6b..8cce4fe4558d 100644 --- a/lib/matplotlib/tests/__init__.py +++ b/lib/matplotlib/tests/__init__.py @@ -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 ' diff --git a/lib/mpl_toolkits/tests/__init__.py b/lib/mpl_toolkits/tests/__init__.py index 5b6390f4fe26..ea4d8ed16a6a 100644 --- a/lib/mpl_toolkits/tests/__init__.py +++ b/lib/mpl_toolkits/tests/__init__.py @@ -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 ' diff --git a/setupext.py b/setupext.py index 0742230844fc..6b9e6fdbc8c1 100644 --- a/setupext.py +++ b/setupext.py @@ -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: @@ -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) @@ -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.") diff --git a/tools/cache_zenodo_svg.py b/tools/cache_zenodo_svg.py index 27d707ae36b7..b37cb70de8b6 100644 --- a/tools/cache_zenodo_svg.py +++ b/tools/cache_zenodo_svg.py @@ -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) @@ -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) From 9d571b9b333341f0ba36805d38042da9c5032596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Tue, 5 Jan 2021 07:22:54 +0100 Subject: [PATCH 2/3] Use Python3 defaults for opening file mode "r" --- lib/matplotlib/_version.py | 2 +- lib/matplotlib/backends/backend_ps.py | 2 +- lib/matplotlib/font_manager.py | 2 +- tools/cache_zenodo_svg.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/_version.py b/lib/matplotlib/_version.py index 38b5859a1c3a..4de04ebb72cb 100644 --- a/lib/matplotlib/_version.py +++ b/lib/matplotlib/_version.py @@ -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) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index ecd6383fe31a..dabb895cbbe7 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -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: diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index eede88590b7a..4e1e79888455 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -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) diff --git a/tools/cache_zenodo_svg.py b/tools/cache_zenodo_svg.py index b37cb70de8b6..19e9d19815d4 100644 --- a/tools/cache_zenodo_svg.py +++ b/tools/cache_zenodo_svg.py @@ -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: From 42214bb4a11c42c70568519bc37892a4861327a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Tue, 5 Jan 2021 07:26:29 +0100 Subject: [PATCH 3/3] Use Python3 default for super() --- lib/matplotlib/tests/test_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index ba8c8580d4bc..7d4d6a2e560b 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -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