Skip to content

Commit

Permalink
SIM105: Replace try-except-pass blocks with the contextlib.suppress c…
Browse files Browse the repository at this point in the history
…ontext manager
  • Loading branch information
seisman committed Sep 23, 2024
1 parent 2454aa6 commit 1eb4bbc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
6 changes: 3 additions & 3 deletions pygmt/clib/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
environment variable :term:`GMT_LIBRARY_PATH`.
"""

import contextlib
import ctypes
import os
import shutil
Expand Down Expand Up @@ -160,14 +161,13 @@ def clib_full_names(env: Mapping | None = None) -> Iterator[str]:
# 2. Search for the library returned by command "gmt --show-library".
# Use `str(Path(realpath))` to avoid mixture of separators "\\" and "/".
if gmtbin := shutil.which("gmt"):
try:
# Suppress the CalledProcessError when the 'gmt' executable is broken
with contextlib.suppress(sp.CalledProcessError):
libfullpath = Path(
sp.check_output([gmtbin, "--show-library"], encoding="utf-8").rstrip()
)
if libfullpath.exists():
yield str(libfullpath)
except sp.CalledProcessError: # the 'gmt' executable is broken
pass

# 3. Search for DLLs in PATH by calling find_library() (Windows only)
if sys.platform == "win32":
Expand Down
5 changes: 2 additions & 3 deletions pygmt/src/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
plot - Plot in two dimensions.
"""

import contextlib
from pathlib import Path

from pygmt.clib import Session
Expand Down Expand Up @@ -246,13 +247,11 @@ def plot( # noqa: PLR0912
if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all():
kwargs["S"] = "s0.2c"
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
try:
with contextlib.suppress(FileNotFoundError):
with Path(which(data)).open(encoding="utf-8") as file:
line = file.readline()
if "@GMULTIPOINT" in line or "@GPOINT" in line:
kwargs["S"] = "s0.2c"
except FileNotFoundError:
pass

with Session() as lib:
with lib.virtualfile_in(
Expand Down
5 changes: 2 additions & 3 deletions pygmt/src/plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
plot3d - Plot in three dimensions.
"""

import contextlib
from pathlib import Path

from pygmt.clib import Session
Expand Down Expand Up @@ -222,13 +223,11 @@ def plot3d( # noqa: PLR0912
if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all():
kwargs["S"] = "u0.2c"
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
try:
with contextlib.suppress(FileNotFoundError):
with Path(which(data)).open(encoding="utf-8") as file:
line = file.readline()
if "@GMULTIPOINT" in line or "@GPOINT" in line:
kwargs["S"] = "u0.2c"
except FileNotFoundError:
pass

with Session() as lib:
with lib.virtualfile_in(
Expand Down
5 changes: 2 additions & 3 deletions pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Doesn't include the plotting commands which have their own test files.
"""

import contextlib
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -112,7 +113,7 @@ def test_figure_savefig_geotiff():
assert fname.exists()

# Check if a TIFF is georeferenced or not
try:
with contextlib.suppress(ImportError): # Skip if failed to import
import rioxarray
from rasterio.errors import NotGeoreferencedWarning
from rasterio.transform import Affine
Expand Down Expand Up @@ -150,8 +151,6 @@ def test_figure_savefig_geotiff():
a=1.0, b=0.0, c=0.0, d=0.0, e=1.0, f=0.0
)
assert len(record) == 1
except ImportError:
pass
geofname.unlink()
fname.unlink()

Expand Down

0 comments on commit 1eb4bbc

Please sign in to comment.