|
4 | 4 |
|
5 | 5 | Uses ctypes to wrap most of the core functions from the C API.
|
6 | 6 | """
|
| 7 | +import contextlib |
7 | 8 | import ctypes as ctp
|
8 | 9 | import pathlib
|
9 | 10 | import sys
|
10 | 11 | import warnings
|
11 |
| -from contextlib import contextmanager, nullcontext |
12 | 12 |
|
13 | 13 | import numpy as np
|
14 | 14 | import pandas as pd
|
@@ -190,12 +190,10 @@ def info(self):
|
190 | 190 | }
|
191 | 191 | # For GMT<6.4.0, API_IMAGE_LAYOUT is not defined if GMT is not
|
192 | 192 | # compiled with GDAL. Since GMT 6.4.0, GDAL is a required GMT
|
193 |
| - # dependency. The try-except block can be refactored after we bump |
| 193 | + # dependency. The code block can be refactored after we bump |
194 | 194 | # the minimum required GMT version to 6.4.0.
|
195 |
| - try: |
| 195 | + with contextlib.suppress(GMTCLibError): |
196 | 196 | self._info["image layout"] = self.get_default("API_IMAGE_LAYOUT")
|
197 |
| - except GMTCLibError: |
198 |
| - pass |
199 | 197 | # API_BIN_VERSION is new in GMT 6.4.0.
|
200 | 198 | if Version(self._info["version"]) >= Version("6.4.0"):
|
201 | 199 | self._info["binary version"] = self.get_default("API_BIN_VERSION")
|
@@ -732,10 +730,7 @@ def _parse_pad(self, family, kwargs):
|
732 | 730 | """
|
733 | 731 | pad = kwargs.get("pad", None)
|
734 | 732 | if pad is None:
|
735 |
| - if "MATRIX" in family: |
736 |
| - pad = 0 |
737 |
| - else: |
738 |
| - pad = self["GMT_PAD_DEFAULT"] |
| 733 | + pad = 0 if "MATRIX" in family else self["GMT_PAD_DEFAULT"] |
739 | 734 | return pad
|
740 | 735 |
|
741 | 736 | def _parse_constant(self, constant, valid, valid_modifiers=None):
|
@@ -1089,7 +1084,7 @@ def write_data(self, family, geometry, mode, wesn, output, data):
|
1089 | 1084 | if status != 0:
|
1090 | 1085 | raise GMTCLibError(f"Failed to write dataset to '{output}'")
|
1091 | 1086 |
|
1092 |
| - @contextmanager |
| 1087 | + @contextlib.contextmanager |
1093 | 1088 | def open_virtual_file(self, family, geometry, direction, data):
|
1094 | 1089 | """
|
1095 | 1090 | Open a GMT virtual file to pass data to and from a module.
|
@@ -1196,7 +1191,7 @@ def open_virtual_file(self, family, geometry, direction, data):
|
1196 | 1191 | if status != 0:
|
1197 | 1192 | raise GMTCLibError(f"Failed to close virtual file '{vfname}'.")
|
1198 | 1193 |
|
1199 |
| - @contextmanager |
| 1194 | + @contextlib.contextmanager |
1200 | 1195 | def virtualfile_from_vectors(self, *vectors):
|
1201 | 1196 | """
|
1202 | 1197 | Store 1-D arrays as columns of a table inside a virtual file.
|
@@ -1298,7 +1293,7 @@ def virtualfile_from_vectors(self, *vectors):
|
1298 | 1293 | ) as vfile:
|
1299 | 1294 | yield vfile
|
1300 | 1295 |
|
1301 |
| - @contextmanager |
| 1296 | + @contextlib.contextmanager |
1302 | 1297 | def virtualfile_from_matrix(self, matrix):
|
1303 | 1298 | """
|
1304 | 1299 | Store a 2-D array as a table inside a virtual file.
|
@@ -1379,7 +1374,7 @@ def virtualfile_from_matrix(self, matrix):
|
1379 | 1374 | ) as vfile:
|
1380 | 1375 | yield vfile
|
1381 | 1376 |
|
1382 |
| - @contextmanager |
| 1377 | + @contextlib.contextmanager |
1383 | 1378 | def virtualfile_from_grid(self, grid):
|
1384 | 1379 | """
|
1385 | 1380 | Store a grid in a virtual file.
|
@@ -1550,8 +1545,8 @@ def virtualfile_from_data( # noqa: PLR0912
|
1550 | 1545 |
|
1551 | 1546 | # Decide which virtualfile_from_ function to use
|
1552 | 1547 | _virtualfile_from = {
|
1553 |
| - "file": nullcontext, |
1554 |
| - "arg": nullcontext, |
| 1548 | + "file": contextlib.nullcontext, |
| 1549 | + "arg": contextlib.nullcontext, |
1555 | 1550 | "geojson": tempfile_from_geojson,
|
1556 | 1551 | "grid": self.virtualfile_from_grid,
|
1557 | 1552 | "image": tempfile_from_image,
|
|
0 commit comments