Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clib: Switch the order of if-conditions to improve the Session.call_module performance #3502

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,31 +629,29 @@ def call_module(self, module: str, args: str | list[str]):

# 'args' can be (1) a single string or (2) a list of strings.
argv: bytes | ctp.Array[ctp.c_char_p] | None
if isinstance(args, str):
# 'args' is a single string that contains whitespace-separated arguments.
# In this way, we need to correctly handle option arguments that contain
# whitespaces or quotation marks. It's used in PyGMT <= v0.11.0 but is no
# longer recommended.
mode = self["GMT_MODULE_CMD"]
argv = args.encode()
elif isinstance(args, list):
if isinstance(args, list):
# 'args' is a list of strings and each string contains a module argument.
# In this way, GMT can correctly handle option arguments with whitespaces or
# quotation marks. This is the preferred way to pass arguments to the GMT
# API and is used for PyGMT >= v0.12.0.
mode = len(args) # 'mode' is the number of arguments.
# Pass a null pointer if no arguments are specified.
argv = strings_to_ctypes_array(args) if mode != 0 else None
elif isinstance(args, str):
# 'args' is a single string that contains whitespace-separated arguments.
# In this way, we need to correctly handle option arguments that contain
# whitespaces or quotation marks. It's used in PyGMT <= v0.11.0 but is no
# longer recommended.
mode = self["GMT_MODULE_CMD"]
argv = args.encode()
else:
raise GMTInvalidInput(
"'args' must be either a string or a list of strings."
)
msg = "'args' must either be a list of strings (recommended) or a string."
raise GMTInvalidInput(msg)

status = c_call_module(self.session_pointer, module.encode(), mode, argv)
if status != 0:
raise GMTCLibError(
f"Module '{module}' failed with status code {status}:\n{self._error_message}"
)
msg = f"Module '{module}' failed with status code {status}:\n{self._error_message}"
raise GMTCLibError(msg)

def create_data(
self,
Expand Down