Skip to content

Commit b960654

Browse files
committed
clib.session: Refactor to simplify the checking of currently open session
1 parent 9c47b38 commit b960654

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

pygmt/clib/session.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def session_pointer(self) -> ctp.c_void_p:
160160
If trying to access without a currently open GMT session (i.e., outside of
161161
the context manager).
162162
"""
163-
if not hasattr(self, "_session_pointer") or self._session_pointer is None:
163+
if getattr(self, "_session_pointer", None) is None:
164164
raise GMTCLibNoSessionError("No currently open GMT API session.")
165165
return self._session_pointer
166166

@@ -338,19 +338,12 @@ def create(self, name: str):
338338
name
339339
A name for this session. Doesn't really affect the outcome.
340340
"""
341-
try:
342-
# Won't raise an exception if there is a currently open session.
343-
_ = self.session_pointer
344-
# In this case, fail to create a new session until the old one is destroyed.
341+
if getattr(self, "session_pointer", None) is not None:
345342
msg = (
346343
"Failed to create a GMT API session: There is a currently open session."
347344
" Must destroy it first."
348345
)
349346
raise GMTCLibError(msg)
350-
# If the exception is raised, this means that there is no open session and we're
351-
# free to create a new one.
352-
except GMTCLibNoSessionError:
353-
pass
354347

355348
c_create_session = self.get_libgmt_func(
356349
"GMT_Create_Session",

0 commit comments

Comments
 (0)