Skip to content

Commit e1fd1f3

Browse files
[gamma.error] handle signal kills
1 parent cce863e commit e1fd1f3

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

docs/source/api/gamma/error.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Error handling
2+
--------------
3+
4+
.. automodule:: pyroSAR.gamma.error
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+
9+
.. autosummary::
10+
:nosignatures:
11+
12+
gammaErrorHandler
13+
GammaUnknownError

docs/source/api/gamma/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ GAMMA
77
util
88
auxil
99
dem
10-
api
10+
api
11+
error

pyroSAR/gamma/auxil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def process(cmd, outdir=None, logfile=None, logpath=None,
466466
# execute the command
467467
returncode, out, err = run(cmd, outdir=outdir, logfile=log, inlist=inlist,
468468
void=False, errorpass=True, env=gammaenv)
469-
gammaErrorHandler(out, err)
469+
gammaErrorHandler(returncode, out, err)
470470
if not void:
471471
return out, err
472472

pyroSAR/gamma/error.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
###############################################################################
22
# interface for translating GAMMA errors messages into Python error types
33

4-
# Copyright (c) 2015-2019, the pyroSAR Developers.
4+
# Copyright (c) 2015-2026, the pyroSAR Developers.
55

66
# This file is part of the pyroSAR Project. It is subject to the
77
# license terms in the LICENSE.txt file found in the top-level
@@ -13,16 +13,24 @@
1313
###############################################################################
1414

1515
import re
16+
import signal
1617

1718

18-
def gammaErrorHandler(out, err):
19+
def gammaErrorHandler(returncode: int, out: str, err: str) -> None:
1920
"""
20-
Function to raise errors in Python. This function is not intended for direct use, but as part of function gamma.util.process
21-
Args:
22-
out: the stdout message returned by a subprocess call of a gamma command
23-
err: the stderr message returned by a subprocess call of a gamma command
21+
Function to raise errors in Python. This function is not intended
22+
for direct use but as part of function :func:`pyroSAR.gamma.auxil.process`.
23+
24+
Parameters
25+
----------
26+
returncode:
27+
the subprocess return code
28+
out:
29+
the stdout message returned by a subprocess call of a gamma command
30+
err:
31+
the stderr message returned by a subprocess call of a gamma command
2432
25-
Raises: IOError | ValueError | RuntimeError | None
33+
Raises: IOError | ValueError | RuntimeError
2634
2735
"""
2836

@@ -59,26 +67,33 @@ def gammaErrorHandler(out, err):
5967
'cannot create ISP image parameter file': OSError}
6068

6169
# check if the error message is known and throw the mapped error from knownErrors accordingly.
62-
# Otherwise throw an GammaUnknownError.
63-
# The actual message is passed to the error and thus visible for backtracing
64-
if len(errormessages) > 0:
65-
errormessage = errormessages[-1]
66-
err_out = '\n\n'.join([re.sub('ERROR[: ]*', '', x) for x in errormessages])
67-
for error in knownErrors:
68-
if re.search(error, errormessage):
69-
errortype = knownErrors[error]
70-
if errortype:
71-
raise errortype(err_out)
72-
else:
73-
return
70+
# Otherwise raise a RuntimeError if killed by a signal and a GammaUnknownError in all other cases.
71+
# The actual message is passed to the error and thus visible for backtracing.
72+
if returncode != 0:
73+
if len(errormessages) > 0:
74+
errormessage = errormessages[-1]
75+
err_out = '\n\n'.join([re.sub('ERROR[: ]*', '', x) for x in errormessages])
76+
for error in knownErrors:
77+
if re.search(error, errormessage):
78+
errortype = knownErrors[error]
79+
if errortype:
80+
raise errortype(err_out)
81+
else:
82+
return
83+
else:
84+
err_out = f'failed with return code {returncode}'
85+
if returncode < 0:
86+
# handle signal kills like SIGSEGV (segmentation fault)
87+
sig = signal.Signals(-returncode)
88+
raise RuntimeError(err_out + f' ({sig.name})')
7489
raise GammaUnknownError(err_out)
7590

7691

7792
class GammaUnknownError(Exception):
7893
"""
7994
This is a general error, which is raised if the error message is not yet integrated
80-
into the known errors of function gammaErrorHandler.
81-
If this error occurs the message should be included in function gammaErrorHandler.
95+
into the known errors of function :func:`gammaErrorHandler`.
96+
If this error occurs, the message should be included in this function.
8297
"""
8398

8499
def __init__(self, errormessage):

0 commit comments

Comments
 (0)