Skip to content

Commit a502f35

Browse files
committed
Use generator yield in clib_full_names function
1 parent 7108301 commit a502f35

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

pygmt/clib/loading.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"""
77
import ctypes
88
import os
9+
import subprocess as sp
910
import sys
1011
from ctypes.util import find_library
11-
import subprocess as sp
1212

1313
from pygmt.exceptions import GMTCLibError, GMTCLibNotFoundError, GMTOSError
1414

@@ -33,9 +33,8 @@ def load_libgmt():
3333
couldn't access the functions).
3434
3535
"""
36-
lib_fullnames = clib_full_names()
3736
error = True
38-
for libname in lib_fullnames:
37+
for libname in clib_full_names():
3938
try:
4039
libgmt = ctypes.CDLL(libname)
4140
check_libgmt(libgmt)
@@ -100,22 +99,20 @@ def clib_full_names(env=None):
10099
env = os.environ
101100

102101
libnames = clib_names(os_name=sys.platform) # e.g. libgmt.so, libgmt.dylib, gmt.dll
102+
libpath = env.get("GMT_LIBRARY_PATH", "") # e.g. $HOME/miniconda/envs/pygmt/lib
103103

104104
# list of libraries paths to search, sort by priority from high to low
105-
lib_fullnames = []
106-
107105
# Search for libraries in GMT_LIBRARY_PATH if defined.
108-
libpath = env.get("GMT_LIBRARY_PATH", "") # e.g. $HOME/miniconda/envs/pygmt/lib
109106
if libpath:
110107
for libname in libnames:
111-
lib_fullnames.append(os.path.join(libpath, libname))
108+
yield os.path.join(libpath, libname)
112109

113110
# Search for the library returned by command "gmt --show-library"
114111
try:
115112
lib_fullpath = sp.check_output(
116113
["gmt", "--show-library"], encoding="utf-8"
117114
).rstrip("\n")
118-
lib_fullnames.append(lib_fullpath)
115+
yield lib_fullpath
119116
except FileNotFoundError: # command not found
120117
pass
121118

@@ -124,12 +121,11 @@ def clib_full_names(env=None):
124121
for libname in libnames:
125122
libfullpath = find_library(libname)
126123
if libfullpath:
127-
lib_fullnames.append(libfullpath)
124+
yield libfullpath
128125

129126
# Search for library names in the system default path [the lowest priority]
130-
lib_fullnames.extend(libnames)
131-
132-
return lib_fullnames
127+
for libname in libnames:
128+
yield libname
133129

134130

135131
def check_libgmt(libgmt):

0 commit comments

Comments
 (0)