Skip to content

Commit

Permalink
python scripting lib: fix run_command(error='status') not returning 0 (
Browse files Browse the repository at this point in the history
…OSGeo#1839)

With this fix run_command when called with errors='status' will always return the return code (even if completed successfully).
Also fixes write_command error handling behavior.
  • Loading branch information
petrasovaa authored and ninsbl committed Oct 26, 2022
1 parent f63f8e0 commit bdd6fa7
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,16 @@ def make_command(
def handle_errors(returncode, result, args, kwargs):
"""Error handler for :func:`run_command()` and similar functions
The function returns *result* if *returncode* is equal to 0,
otherwise it reports errors based on the current settings.
The functions which are using this function to handle errors,
can be typically called with an *errors* parameter.
This function can handle one of the following values: raise,
fatal, status, exit, and ignore. The value raise is a default.
If returncode is 0, *result* is returned, unless
``errors="status"`` is set.
If *kwargs* dictionary contains key ``errors``, the value is used
to determine the behavior on error.
to determine the return value and the behavior on error.
The value ``errors="raise"`` is a default in which case a
``CalledModuleError`` exception is raised.
Expand Down Expand Up @@ -406,13 +406,13 @@ def get_module_and_code(args, kwargs):
code = " ".join(args)
return module, code

handler = kwargs.get("errors", "raise")
if handler.lower() == "status":
return returncode
if returncode == 0:
return result
handler = kwargs.get("errors", "raise")
if handler.lower() == "ignore":
return result
elif handler.lower() == "status":
return returncode
elif handler.lower() == "fatal":
module, code = get_module_and_code(args, kwargs)
fatal(
Expand Down Expand Up @@ -695,7 +695,7 @@ def write_command(*args, **kwargs):
returncode = process.poll()
if _capture_stderr and returncode:
sys.stderr.write(stderr)
return handle_errors(returncode, returncode, args, kwargs)
return handle_errors(returncode, None, args, kwargs)


def exec_command(
Expand Down

0 comments on commit bdd6fa7

Please sign in to comment.