Skip to content

Commit 7f4f6b1

Browse files
authored
Merge branch 'main' into clib/return_table
2 parents 43732a7 + 99774d4 commit 7f4f6b1

9 files changed

+43
-38
lines changed

.github/workflows/benchmarks.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
6565
# Download cached remote files (artifacts) from GitHub
6666
- name: Download remote data from GitHub
67-
uses: dawidd6/[email protected].1
67+
uses: dawidd6/[email protected].2
6868
with:
6969
workflow: cache_data.yaml
7070
workflow_conclusion: success

.github/workflows/cache_data.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838

3939
# Install Micromamba with conda-forge dependencies
4040
- name: Setup Micromamba
41-
uses: mamba-org/[email protected].0
41+
uses: mamba-org/[email protected].1
4242
with:
4343
environment-name: pygmt
4444
condarc: |

.github/workflows/ci_docs.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575

7676
# Install Micromamba with conda-forge dependencies
7777
- name: Setup Micromamba
78-
uses: mamba-org/[email protected].0
78+
uses: mamba-org/[email protected].1
7979
with:
8080
environment-name: pygmt
8181
condarc: |
@@ -111,7 +111,7 @@ jobs:
111111
112112
# Download cached remote files (artifacts) from GitHub
113113
- name: Download remote data from GitHub
114-
uses: dawidd6/[email protected].1
114+
uses: dawidd6/[email protected].2
115115
with:
116116
workflow: cache_data.yaml
117117
workflow_conclusion: success

.github/workflows/ci_doctests.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141

4242
# Install Micromamba with conda-forge dependencies
4343
- name: Setup Micromamba
44-
uses: mamba-org/[email protected].0
44+
uses: mamba-org/[email protected].1
4545
with:
4646
environment-name: pygmt
4747
condarc: |
@@ -70,7 +70,7 @@ jobs:
7070
7171
# Download cached remote files (artifacts) from GitHub
7272
- name: Download remote data from GitHub
73-
uses: dawidd6/[email protected].1
73+
uses: dawidd6/[email protected].2
7474
with:
7575
workflow: cache_data.yaml
7676
workflow_conclusion: success

.github/workflows/ci_tests.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ jobs:
9898

9999
# Install Micromamba with conda-forge dependencies
100100
- name: Setup Micromamba
101-
uses: mamba-org/[email protected].0
101+
uses: mamba-org/[email protected].1
102102
with:
103103
environment-name: pygmt
104104
condarc: |
@@ -127,7 +127,7 @@ jobs:
127127
128128
# Download cached remote files (artifacts) from GitHub
129129
- name: Download remote data from GitHub
130-
uses: dawidd6/[email protected].1
130+
uses: dawidd6/[email protected].2
131131
with:
132132
workflow: cache_data.yaml
133133
workflow_conclusion: success

.github/workflows/ci_tests_dev.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353

5454
# Install Micromamba with conda-forge dependencies
5555
- name: Setup Micromamba
56-
uses: mamba-org/[email protected].0
56+
uses: mamba-org/[email protected].1
5757
with:
5858
environment-name: pygmt
5959
condarc: |
@@ -134,7 +134,7 @@ jobs:
134134

135135
# Download cached remote files (artifacts) from GitHub
136136
- name: Download remote data from GitHub
137-
uses: dawidd6/[email protected].1
137+
uses: dawidd6/[email protected].2
138138
with:
139139
workflow: cache_data.yaml
140140
workflow_conclusion: success

.github/workflows/ci_tests_legacy.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050

5151
# Install Micromamba with conda-forge dependencies
5252
- name: Setup Micromamba
53-
uses: mamba-org/[email protected].0
53+
uses: mamba-org/[email protected].1
5454
with:
5555
environment-name: pygmt
5656
condarc: |
@@ -82,7 +82,7 @@ jobs:
8282
8383
# Download cached remote files (artifacts) from GitHub
8484
- name: Download remote data from GitHub
85-
uses: dawidd6/[email protected].1
85+
uses: dawidd6/[email protected].2
8686
with:
8787
workflow: cache_data.yaml
8888
workflow_conclusion: success

pygmt/clib/loading.py

+29-25
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,36 @@
1010
import shutil
1111
import subprocess as sp
1212
import sys
13+
from collections.abc import Iterator, Mapping
1314
from ctypes.util import find_library
1415
from pathlib import Path
1516

1617
from pygmt.exceptions import GMTCLibError, GMTCLibNotFoundError, GMTOSError
1718

1819

19-
def load_libgmt(lib_fullnames=None):
20+
def load_libgmt(lib_fullnames: Iterator[str] | None = None) -> ctypes.CDLL:
2021
"""
2122
Find and load ``libgmt`` as a :py:class:`ctypes.CDLL`.
2223
2324
Will look for the GMT shared library in the directories determined by
24-
clib_full_names().
25+
``clib_full_names()``.
2526
2627
Parameters
2728
----------
28-
lib_fullnames : list of str or None
29-
List of possible full names of GMT's shared library. If ``None``, will
30-
default to ``clib_full_names()``.
29+
lib_fullnames
30+
List of possible full names of GMT's shared library. If ``None``, will default
31+
to ``clib_full_names()``.
3132
3233
Returns
3334
-------
34-
:py:class:`ctypes.CDLL` object
35+
libgmt
3536
The loaded shared library.
3637
3738
Raises
3839
------
3940
GMTCLibNotFoundError
40-
If there was any problem loading the library (couldn't find it or
41-
couldn't access the functions).
41+
If there was any problem loading the library (couldn't find it or couldn't
42+
access the functions).
4243
"""
4344
if lib_fullnames is None:
4445
lib_fullnames = clib_full_names()
@@ -96,20 +97,27 @@ def clib_names(os_name: str) -> list[str]:
9697
return libnames
9798

9899

99-
def clib_full_names(env=None):
100+
def clib_full_names(env: Mapping | None = None) -> Iterator[str]:
100101
"""
101-
Return the full path of GMT's shared library for the current OS.
102+
Return full path(s) of GMT shared library for the current operating system.
103+
104+
The GMT shared library is searched for in following ways, sorted by priority:
105+
106+
1. Path defined by environmental variable GMT_LIBRARY_PATH
107+
2. Path returned by command "gmt --show-library"
108+
3. Path defined by environmental variable PATH (Windows only)
109+
4. System default search path
102110
103111
Parameters
104112
----------
105-
env : dict or None
106-
A dictionary containing the environment variables. If ``None``, will
107-
default to ``os.environ``.
113+
env
114+
A dictionary containing the environment variables. If ``None``, will default to
115+
``os.environ``.
108116
109117
Yields
110118
------
111-
lib_fullnames: list of str
112-
List of possible full names of GMT's shared library.
119+
lib_fullnames
120+
List of possible full names of GMT shared library.
113121
"""
114122
if env is None:
115123
env = os.environ
@@ -118,21 +126,18 @@ def clib_full_names(env=None):
118126

119127
# Search for the library in different ways, sorted by priority.
120128
# 1. Search for the library in GMT_LIBRARY_PATH if defined.
121-
libpath = env.get("GMT_LIBRARY_PATH", "") # e.g. $HOME/miniconda/envs/pygmt/lib
122-
if libpath:
129+
if libpath := env.get("GMT_LIBRARY_PATH"): # e.g. $HOME/miniconda/envs/pygmt/lib
123130
for libname in libnames:
124131
libfullpath = Path(libpath) / libname
125132
if libfullpath.exists():
126133
yield str(libfullpath)
127134

128-
# 2. Search for the library returned by command "gmt --show-library"
129-
# Use `str(Path(realpath))` to avoid mixture of separators "\\" and "/"
130-
if (gmtbin := shutil.which("gmt")) is not None:
135+
# 2. Search for the library returned by command "gmt --show-library".
136+
# Use `str(Path(realpath))` to avoid mixture of separators "\\" and "/".
137+
if gmtbin := shutil.which("gmt"):
131138
try:
132139
libfullpath = Path(
133-
sp.check_output([gmtbin, "--show-library"], encoding="utf-8").rstrip(
134-
"\n"
135-
)
140+
sp.check_output([gmtbin, "--show-library"], encoding="utf-8").rstrip()
136141
)
137142
if libfullpath.exists():
138143
yield str(libfullpath)
@@ -142,8 +147,7 @@ def clib_full_names(env=None):
142147
# 3. Search for DLLs in PATH by calling find_library() (Windows only)
143148
if sys.platform == "win32":
144149
for libname in libnames:
145-
libfullpath = find_library(libname)
146-
if libfullpath:
150+
if libfullpath := find_library(libname):
147151
yield libfullpath
148152

149153
# 4. Search for library names in the system default path

pygmt/tests/test_clib_loading.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ def test_load_libgmt():
6464
check_libgmt(load_libgmt())
6565

6666

67-
@pytest.mark.skipif(sys.platform == "win32", reason="run on UNIX platforms only")
6867
def test_load_libgmt_fails(monkeypatch):
6968
"""
7069
Test that GMTCLibNotFoundError is raised when GMT's shared library cannot be found.
7170
"""
7271
with monkeypatch.context() as mpatch:
72+
if sys.platform == "win32":
73+
mpatch.setattr(ctypes.util, "find_library", lambda name: "fakegmt.dll") # noqa: ARG005
7374
mpatch.setattr(
7475
sys,
7576
"platform",

0 commit comments

Comments
 (0)