Avoid silent crashing of TMatrixInterpolator.__cinit__()

Former-commit-id: fb6bfc9f52b8d0dadf89c93835360bca54cb66c9
This commit is contained in:
Marek Nečada 2019-07-16 14:06:55 +03:00
parent a315923750
commit a63348d414
4 changed files with 22 additions and 3 deletions

View File

@ -1134,6 +1134,8 @@ cdef class TMatrixInterpolator:
def __cinit__(self, filename, BaseSpec bspec, *args, **kwargs):
'''Creates a T-matrix interpolator object from a scuff-tmatrix output'''
global qpms_load_scuff_tmatrix_crash_on_failure
qpms_load_scuff_tmatrix_crash_on_failure = False
self.spec = bspec
cdef char * cpath = make_c_string(filename)
if QPMS_SUCCESS != qpms_load_scuff_tmatrix(cpath, self.spec.rawpointer(),

View File

@ -276,6 +276,7 @@ cdef extern from "gsl/gsl_interp.h":
# ^^^ These are probably the only relevant ones.
cdef extern from "tmatrices.h":
bint qpms_load_scuff_tmatrix_crash_on_failure
struct qpms_tmatrix_interpolator_t:
const qpms_vswf_set_spec_t *bspec
struct qpms_permittivity_interpolator_t:

View File

@ -18,6 +18,7 @@
#include "tmatrices.h"
#include "qpms_specfunc.h"
#include "normalisation.h"
#include <errno.h>
#define HBAR (1.05457162825e-34)
#define ELECTRONVOLT (1.602176487e-19)
@ -443,6 +444,8 @@ qpms_errno_t qpms_read_scuff_tmatrix(
return QPMS_SUCCESS;
}
bool qpms_load_scuff_tmatrix_crash_on_failure = true;
qpms_errno_t qpms_load_scuff_tmatrix(
const char *path, ///< file path
const qpms_vswf_set_spec_t * bs, ///< VSWF set spec
@ -454,8 +457,10 @@ qpms_errno_t qpms_load_scuff_tmatrix(
) {
FILE *f = fopen(path, "r");
if (!f)
if (qpms_load_scuff_tmatrix_crash_on_failure)
qpms_pr_error_at_line(__FILE__, __LINE__, __func__,
"Could not open T-matrix file %s", path);
else return errno;
qpms_errno_t retval =
qpms_read_scuff_tmatrix(f, bs, n, freqs, freqs_su, tmatrices_array, tmdata);

View File

@ -139,6 +139,17 @@ qpms_errno_t qpms_load_scuff_tmatrix(
complex double **tmdata ///< The T-matrices raw contents
);
/// Tells whether qpms_load_scuff_tmatrix should crash if fopen() fails.
/** If true (default), the function causes the program
* die e.g. when the tmatrix file
* does not exists.
*
* If false, it does nothing and returns an appropriate error value instead.
* This is desirable e.g. when used in Python (so that proper exception can
* be thrown).
*/
extern bool qpms_load_scuff_tmatrix_crash_on_failure;
/// Loads a scuff-tmatrix generated file.
/** A simple wrapper over qpms_read_scuff_tmatrix() that needs a
* path instead of open FILE.