GeezerVison is showing github.com — clib: Refactor to check the GMT version only once · GenericMappingTools/pygmt@b42b758
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .github/ISSUE_TEMPLATE/5-bump_gmt_checklist.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -35,7 +35,7 @@ using the following command: | 35 | 35 | **To-Do for bumping the minimum required GMT version**: | 36 | 36 |
| 37 | 37 | - [ ] Bump the minimum required GMT version (1 PR) | 38 | | - - [ ] Update `required_version` in `pygmt/clib/session.py` | | 38 | + - [ ] Update `required_gmt_version` in `pygmt/clib/__init__.py` | 39 | 39 | - [ ] Update `test_get_default` in `pygmt/tests/test_clib.py` | 40 | 40 | - [ ] Update minimum required versions in `doc/minversions.md` | 41 | 41 | - [ ] Remove unsupported GMT version from `.github/workflows/ci_tests_legacy.yaml` | |
Collapse file Copy file name to clipboardExpand all lines: doc/conf.py +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | # ruff: isort: off | 9 | 9 | from sphinx_gallery.sorting import ExplicitOrder, ExampleTitleSortKey | 10 | | -import pygmt | | 10 | +from pygmt.clib import required_gmt_version | 11 | 11 | from pygmt import __commit__, __version__ | 12 | 12 | from pygmt.sphinx_gallery import PyGMTScraper | 13 | 13 |
| 14 | 14 | # ruff: isort: on | 15 | 15 |
| 16 | 16 | requires_python = metadata("pygmt")["Requires-Python"] | 17 | | -with pygmt.clib.Session() as lib: | 18 | | - requires_gmt = f">={lib.required_version}" | | 17 | +requires_gmt = f">={required_gmt_version}" | 19 | 18 |
| 20 | 19 | extensions = [ | 21 | 20 | "myst_parser", | |
Collapse file Copy file name to clipboardExpand all lines: pygmt/clib/__init__.py +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | interface. Access to the C library is done through ctypes. | 6 | 6 | """ | 7 | 7 |
| 8 | | -from pygmt.clib.session import Session | | 8 | +from packaging.version import Version | | 9 | +from pygmt.clib.session import Session, __gmt_version__ | | 10 | +from pygmt.exceptions import GMTVersionError | 9 | 11 |
| 10 | | -with Session() as lib: | 11 | | - __gmt_version__ = lib.info["version"] | | 12 | +required_gmt_version = "6.3.0" | | 13 | + | | 14 | +# Check if the GMT version is older than the required version. | | 15 | +if Version(__gmt_version__) < Version(required_gmt_version): | | 16 | + msg = ( | | 17 | + f"Using an incompatible GMT version {__gmt_version__}. " | | 18 | + f"Must be equal or newer than {required_gmt_version}." | | 19 | + ) | | 20 | + raise GMTVersionError(msg) |
Collapse file Copy file name to clipboardExpand all lines: pygmt/clib/loading.py +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,6 +64,33 @@ def load_libgmt(lib_fullnames: Iterator[str] | None = None) -> ctypes.CDLL: | 64 | 64 | return libgmt | 65 | 65 |
| 66 | 66 |
| | 67 | +def get_gmt_version(libgmt: ctypes.CDLL) -> str: | | 68 | + """ | | 69 | + Get the GMT version string of the GMT shared library. | | 70 | +
| | 71 | + Parameters | | 72 | + ---------- | | 73 | + libgmt | | 74 | + The GMT shared library. | | 75 | +
| | 76 | + Returns | | 77 | + ------- | | 78 | + The GMT version string in *major.minor.patch* format. | | 79 | + """ | | 80 | + func = libgmt.GMT_Get_Version | | 81 | + func.argtypes = ( | | 82 | + ctypes.c_void_p, # Unused parameter, so it can be None. | | 83 | + ctypes.POINTER(ctypes.c_uint), # major | | 84 | + ctypes.POINTER(ctypes.c_uint), # minor | | 85 | + ctypes.POINTER(ctypes.c_uint), # patch | | 86 | + ) | | 87 | + # The function return value is the current library verison as a float, e.g., 6.5. | | 88 | + func.restype = ctypes.c_float | | 89 | + major, minor, patch = ctypes.c_uint(0), ctypes.c_uint(0), ctypes.c_uint(0) | | 90 | + func(None, major, minor, patch) | | 91 | + return f"{major.value}.{minor.value}.{patch.value}" | | 92 | + | | 93 | + | 67 | 94 | def clib_names(os_name: str) -> list[str]: | 68 | 95 | """ | 69 | 96 | Return the name(s) of GMT's shared library for the current operating system. | |
Collapse file Copy file name to clipboardExpand all lines: pygmt/clib/session.py +5-32Lines changed: 5 additions & 32 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | strings_to_ctypes_array, | 25 | 25 | vectors_to_arrays, | 26 | 26 | ) | 27 | | -from pygmt.clib.loading import load_libgmt | | 27 | +from pygmt.clib.loading import get_gmt_version, load_libgmt | 28 | 28 | from pygmt.datatypes import _GMT_DATASET, _GMT_GRID | 29 | | -from pygmt.exceptions import ( | 30 | | - GMTCLibError, | 31 | | - GMTCLibNoSessionError, | 32 | | - GMTInvalidInput, | 33 | | - GMTVersionError, | 34 | | -) | 35 | | -from pygmt.helpers import ( | 36 | | - data_kind, | 37 | | - tempfile_from_geojson, | 38 | | - tempfile_from_image, | 39 | | -) | | 29 | +from pygmt.exceptions import GMTCLibError, GMTCLibNoSessionError, GMTInvalidInput | | 30 | +from pygmt.helpers import data_kind, tempfile_from_geojson, tempfile_from_image | 40 | 31 |
| 41 | 32 | FAMILIES = [ | 42 | 33 | "GMT_IS_DATASET", # Entity is a data table | | 96 | 87 |
| 97 | 88 | # Load the GMT library outside the Session class to avoid repeated loading. | 98 | 89 | _libgmt = load_libgmt() | | 90 | +__gmt_version__ = get_gmt_version(_libgmt) | 99 | 91 |
| 100 | 92 |
| 101 | 93 | class Session: | @@ -153,9 +145,6 @@ class Session: | 153 | 145 | -55 -47 -24 -10 190 981 1 1 8 14 1 1 | 154 | 146 | """ | 155 | 147 |
| 156 | | - # The minimum supported GMT version. | 157 | | - required_version = "6.3.0" | 158 | | - | 159 | 148 | @property | 160 | 149 | def session_pointer(self): | 161 | 150 | """ | @@ -210,27 +199,11 @@ def info(self): | 210 | 199 |
| 211 | 200 | def __enter__(self): | 212 | 201 | """ | 213 | | - Create a GMT API session and check the libgmt version. | | 202 | + Create a GMT API session. | 214 | 203 |
| 215 | 204 | Calls :meth:`pygmt.clib.Session.create`. | 216 | | -
| 217 | | - Raises | 218 | | - ------ | 219 | | - GMTVersionError | 220 | | - If the version reported by libgmt is less than | 221 | | - ``Session.required_version``. Will destroy the session before | 222 | | - raising the exception. | 223 | 205 | """ | 224 | 206 | self.create("pygmt-session") | 225 | | - # Need to store the version info because 'get_default' won't work after | 226 | | - # the session is destroyed. | 227 | | - version = self.info["version"] | 228 | | - if Version(version) < Version(self.required_version): | 229 | | - self.destroy() | 230 | | - raise GMTVersionError( | 231 | | - f"Using an incompatible GMT version {version}. " | 232 | | - f"Must be equal or newer than {self.required_version}." | 233 | | - ) | 234 | 207 | return self | 235 | 208 |
| 236 | 209 | def __exit__(self, exc_type, exc_value, traceback): | |
Collapse file Copy file name to clipboardExpand all lines: pygmt/tests/test_clib.py +11-20Lines changed: 11 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -577,27 +577,18 @@ def mock_defaults(api, name, value): # noqa: ARG001 | 577 | 577 | ses.destroy() | 578 | 578 |
| 579 | 579 |
| 580 | | -def test_fails_for_wrong_version(): | | 580 | +def test_fails_for_wrong_version(monkeypatch): | 581 | 581 | """ | 582 | | - Make sure the clib.Session raises an exception if GMT is too old. | | 582 | + Make sure that importing clib raise an exception if GMT is too old. | 583 | 583 | """ | | 584 | + import importlib | 584 | 585 |
| 585 | | - # Mock GMT_Get_Default to return an old version | 586 | | - def mock_defaults(api, name, value): # noqa: ARG001 | 587 | | - """ | 588 | | - Return an old version. | 589 | | - """ | 590 | | - if name == b"API_VERSION": | 591 | | - value.value = b"5.4.3" | 592 | | - else: | 593 | | - value.value = b"bla" | 594 | | - return 0 | 595 | | - | 596 | | - lib = clib.Session() | 597 | | - with mock(lib, "GMT_Get_Default", mock_func=mock_defaults): | | 586 | + with monkeypatch.context() as mpatch: | | 587 | + # Monkeypatch the version string returned by pygmt.clib.loading.get_gmt_version. | | 588 | + mpatch.setattr(clib.loading, "get_gmt_version", lambda libgmt: "5.4.3") # noqa: ARG005 | | 589 | + # Reload the clib.sesion module so that patched get_gmt_version is called. | | 590 | + importlib.reload(clib.session) | | 591 | + assert clib.__gmt_version__ == "5.4.3" # Old GMT version is returned. | 598 | 592 | with pytest.raises(GMTVersionError): | 599 | | - with lib: | 600 | | - assert lib.info["version"] != "5.4.3" | 601 | | - # Make sure the session is closed when the exception is raised. | 602 | | - with pytest.raises(GMTCLibNoSessionError): | 603 | | - assert lib.session_pointer | | 593 | + # Raise an exception if pygmt.clib is loaded/reloaded. | | 594 | + importlib.reload(clib) |
Collapse file Copy file name to clipboardExpand all lines: pygmt/tests/test_clib_loading.py +19-1Lines changed: 19 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | from pathlib import PurePath | 12 | 12 |
| 13 | 13 | import pytest | 14 | | -from pygmt.clib.loading import check_libgmt, clib_full_names, clib_names, load_libgmt | | 14 | +from pygmt.clib.loading import ( | | 15 | + check_libgmt, | | 16 | + clib_full_names, | | 17 | + clib_names, | | 18 | + get_gmt_version, | | 19 | + load_libgmt, | | 20 | +) | 15 | 21 | from pygmt.clib.session import Session | 16 | 22 | from pygmt.exceptions import GMTCLibError, GMTCLibNotFoundError, GMTOSError | 17 | 23 |
| @@ -360,3 +366,15 @@ def test_clib_full_names_gmt_library_path_incorrect_path_included( | 360 | 366 | # Windows: find_library() searches the library in PATH, so one more | 361 | 367 | npath = 2 if sys.platform == "win32" else 1 | 362 | 368 | assert list(lib_fullpaths) == [gmt_lib_realpath] * npath + gmt_lib_names | | 369 | + | | 370 | + | | 371 | +############################################################################### | | 372 | +# Test get_gmt_version | | 373 | +def test_get_gmt_version(): | | 374 | + """ | | 375 | + Test if get_gmt_version returns a version string in major.minor.patch format. | | 376 | + """ | | 377 | + version = get_gmt_version(load_libgmt()) | | 378 | + assert isinstance(version, str) | | 379 | + assert len(version.split(".")) == 3 # In major.minor.patch format | | 380 | + assert version.split(".")[0] == "6" # Is GMT 6.x.x |
|