From 09c77562d2efdbe0b10ef050fccdf45ad805d091 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 10 Oct 2024 13:14:15 +0800 Subject: [PATCH 1/2] clib: Switch the order of if-conditions to improve the Session.call_module performance --- pygmt/clib/session.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 0effa04ae8e..9b12529503c 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -629,14 +629,7 @@ 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 @@ -644,16 +637,21 @@ def call_module(self, module: str, args: str | list[str]): 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 be either a string or a list of strings." + 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, From 40bc0ed8ce464ae0c174fc12552ac3597c20377e Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 11 Oct 2024 07:58:25 +0800 Subject: [PATCH 2/2] Update pygmt/clib/session.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/clib/session.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 9b12529503c..d3f38e44976 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -645,7 +645,7 @@ def call_module(self, module: str, args: str | list[str]): mode = self["GMT_MODULE_CMD"] argv = args.encode() else: - msg = "'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)