Skip to content

Commit

Permalink
clib.session: Refactor to simplify the checking of currently open ses…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
seisman committed Oct 16, 2024
1 parent 9c47b38 commit b960654
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def session_pointer(self) -> ctp.c_void_p:
If trying to access without a currently open GMT session (i.e., outside of
the context manager).
"""
if not hasattr(self, "_session_pointer") or self._session_pointer is None:
if getattr(self, "_session_pointer", None) is None:
raise GMTCLibNoSessionError("No currently open GMT API session.")
return self._session_pointer

Expand Down Expand Up @@ -338,19 +338,12 @@ def create(self, name: str):
name
A name for this session. Doesn't really affect the outcome.
"""
try:
# Won't raise an exception if there is a currently open session.
_ = self.session_pointer
# In this case, fail to create a new session until the old one is destroyed.
if getattr(self, "session_pointer", None) is not None:
msg = (
"Failed to create a GMT API session: There is a currently open session."
" Must destroy it first."
)
raise GMTCLibError(msg)
# If the exception is raised, this means that there is no open session and we're
# free to create a new one.
except GMTCLibNoSessionError:
pass

c_create_session = self.get_libgmt_func(
"GMT_Create_Session",
Expand Down

0 comments on commit b960654

Please sign in to comment.