1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-12-22 20:36:01 +01:00

More Python-ic error handling

This commit is contained in:
q-posev 2022-05-04 11:47:30 +02:00
parent abc7e8e11d
commit 04367c1824
2 changed files with 29 additions and 17 deletions

View File

@ -45,21 +45,34 @@ import_array();
/* exception.i is a generic (language-independent) module */ /* exception.i is a generic (language-independent) module */
%include "exception.i" %include "exception.i"
/* Error handling
/* Error handling */
%typemap(out) qmckl_exit_code %{
if ($1 != QMCKL_SUCCESS) {
SWIG_exception(SWIG_RuntimeError, qmckl_string_of_error($1));
}
$result = Py_None;
Py_INCREF(Py_None); /* Py_None is a singleton so increment its reference if used. */
%}
/* More swig-y solution (e.g. compatible beyond Python) BUT it does not consume the qmckl_exit_code output as the solution above
TODO: the sizeof() check below if a dummy workaround TODO: the sizeof() check below if a dummy workaround
It is good to skip exception raise for functions like context_create and others, but might fail It is good to skip exception raise for functions like context_create and others, but might fail
if sizeof(result) == sizeof(qmckl_exit_code), e.g. for functions that return non-zero integers or floats if sizeof(result) == sizeof(qmckl_exit_code), e.g. for functions that return non-zero integers or floats
*/ */
/*
%exception { %exception {
$action $action
if (result != QMCKL_SUCCESS && sizeof(result) == sizeof(qmckl_exit_code)) { if (result != QMCKL_SUCCESS && sizeof(result) == sizeof(qmckl_exit_code)) {
SWIG_exception_fail(SWIG_RuntimeError, qmckl_string_of_error(result)); SWIG_exception_fail(SWIG_RuntimeError, qmckl_string_of_error(result));
} }
} }
*/
/* The exception handling above does not work for void functions like lock/unlock so exclude them for now */ /* The exception handling above does not work for void functions like lock/unlock so exclude them for now */
/*
%ignore qmckl_lock; %ignore qmckl_lock;
%ignore qmckl_unlock; %ignore qmckl_unlock;
*/
/* Parse the header files to generate wrappers */ /* Parse the header files to generate wrappers */
%include "qmckl.h" %include "qmckl.h"

View File

@ -18,33 +18,32 @@ ITERMAX = 10
ctx = pq.qmckl_context_create() ctx = pq.qmckl_context_create()
try:
pq.qmckl_trexio_read(ctx, 'fake.h5')
except RuntimeError:
print('Error handling check: passed')
fname = join('data', 'Alz_small.h5') fname = join('data', 'Alz_small.h5')
rc = pq.qmckl_trexio_read(ctx, fname) pq.qmckl_trexio_read(ctx, fname)
assert rc==pq.QMCKL_SUCCESS print('trexio_read: passed')
print(pq.qmckl_string_of_error(rc))
rc = pq.qmckl_set_electron_walk_num(ctx, walk_num) pq.qmckl_set_electron_walk_num(ctx, walk_num)
assert rc==pq.QMCKL_SUCCESS
rc, mo_num = pq.qmckl_get_mo_basis_mo_num(ctx) mo_num = pq.qmckl_get_mo_basis_mo_num(ctx)
assert rc==pq.QMCKL_SUCCESS assert mo_num == 404
rc = pq.qmckl_set_electron_coord(ctx, 'T', coord) pq.qmckl_set_electron_coord(ctx, 'T', coord)
assert rc==pq.QMCKL_SUCCESS
size_max = 5*walk_num*elec_num*mo_num size_max = 5*walk_num*elec_num*mo_num
mo_vgl = pq.qmckl_get_mo_basis_mo_vgl(ctx, size_max)
assert mo_vgl.size == size_max
rc, mo_vgl = pq.qmckl_get_mo_basis_mo_vgl(ctx, size_max)
assert rc==pq.QMCKL_SUCCESS
start = time.clock_gettime_ns(time.CLOCK_REALTIME) start = time.clock_gettime_ns(time.CLOCK_REALTIME)
for _ in range(ITERMAX): for _ in range(ITERMAX):
rc, mo_vgl_in = pq.qmckl_get_mo_basis_mo_vgl_inplace(ctx, size_max) mo_vgl_in = pq.qmckl_get_mo_basis_mo_vgl_inplace(ctx, size_max)
assert rc==pq.QMCKL_SUCCESS
end = time.clock_gettime_ns(time.CLOCK_REALTIME) end = time.clock_gettime_ns(time.CLOCK_REALTIME)