mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-12-22 20:35:44 +01:00
handle return error of trexio_open in the Python API
This commit is contained in:
parent
9457a826d2
commit
67b00efa9c
@ -49,7 +49,7 @@ nucleus_num = 12
|
|||||||
try:
|
try:
|
||||||
trexio.write_nucleus_num(test_file, -100)
|
trexio.write_nucleus_num(test_file, -100)
|
||||||
except trexio.Error:
|
except trexio.Error:
|
||||||
print("Writing negative nucleus_num: checked.")
|
print("Raise error for an attempt to write negative nucleus_num: checked.")
|
||||||
|
|
||||||
# write nucleus_num in the file
|
# write nucleus_num in the file
|
||||||
try:
|
try:
|
||||||
@ -60,7 +60,7 @@ except:
|
|||||||
try:
|
try:
|
||||||
trexio.write_nucleus_num(test_file, nucleus_num*2)
|
trexio.write_nucleus_num(test_file, nucleus_num*2)
|
||||||
except trexio.Error:
|
except trexio.Error:
|
||||||
print("Attempt to overwrite nucleus_num: checked.")
|
print("Raise error for an attempt to overwrite nucleus_num: checked.")
|
||||||
|
|
||||||
# initialize charge arrays as a list and convert it to numpy array
|
# initialize charge arrays as a list and convert it to numpy array
|
||||||
charges = [6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.]
|
charges = [6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.]
|
||||||
@ -199,7 +199,7 @@ assert rpoint_group==point_group
|
|||||||
if trexio.has_mo_num(test_file2):
|
if trexio.has_mo_num(test_file2):
|
||||||
rmo_num = trexio.read_mo_num(test_file2)
|
rmo_num = trexio.read_mo_num(test_file2)
|
||||||
else:
|
else:
|
||||||
print("Not reading the non-existing variable mo_num.")
|
print("Pass on reading the non-existing variable mo_num: checked")
|
||||||
|
|
||||||
# close TREXIO file
|
# close TREXIO file
|
||||||
#trexio.close(test_file2)
|
#trexio.close(test_file2)
|
||||||
@ -215,3 +215,16 @@ except:
|
|||||||
|
|
||||||
#==========================================================#
|
#==========================================================#
|
||||||
|
|
||||||
|
#==========================================================#
|
||||||
|
#======= OPEN NON-EXISTING FILE TO TEST TREXIO.OPEN =======#
|
||||||
|
#==========================================================#
|
||||||
|
|
||||||
|
try:
|
||||||
|
void_file = trexio.File('non_existing.file', 'r', TEST_TREXIO_BACKEND)
|
||||||
|
except trexio.Error as e:
|
||||||
|
if e.error == trexio.TREXIO_OPEN_ERROR:
|
||||||
|
print("Opening non-existing file returns TREXIO_OPEN_ERROR: checked")
|
||||||
|
else:
|
||||||
|
raise ValueError("[DEV]: error handling of trexio_open function has changed; check the consistency")
|
||||||
|
|
||||||
|
#==========================================================#
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
%apply int *OUTPUT { int64_t* const num};
|
%apply int *OUTPUT { int64_t* const num};
|
||||||
%apply float *OUTPUT { float* const num};
|
%apply float *OUTPUT { float* const num};
|
||||||
%apply float *OUTPUT { double* const num};
|
%apply float *OUTPUT { double* const num};
|
||||||
|
%apply int *OUTPUT { trexio_exit_code* const rc_open};
|
||||||
|
|
||||||
/* Does not work for arrays (SIGSEGV) */
|
/* Does not work for arrays (SIGSEGV) */
|
||||||
|
|
||||||
|
@ -911,11 +911,22 @@ def open(file_name: str, mode: str, back_end: int):
|
|||||||
>>> trex_file = tr_open("example.h5", "w", TREXIO_HDF5)
|
>>> trex_file = tr_open("example.h5", "w", TREXIO_HDF5)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# The new trexio_open function is capable of returning error code which SWIG can append to the output trexio_s file struct
|
||||||
|
# However, if trexio_s* == NULL, then SWIG returns only an error code rc_open instead of a list [trexio_s, rc_open]
|
||||||
|
# Thus, the following try/except sequence is needed
|
||||||
try:
|
try:
|
||||||
trexio_file = pytr.trexio_open(file_name, mode, back_end)
|
return_obj = pytr.trexio_open(file_name, mode, back_end)
|
||||||
assert trexio_file is not None
|
assert return_obj is not None
|
||||||
|
if isinstance(return_obj, int):
|
||||||
|
raise Error(return_obj)
|
||||||
|
else:
|
||||||
|
rc_open = return_obj[1]
|
||||||
|
# this is a sanity check in case the code evolves and SWIG issue is patched
|
||||||
|
if rc_open == TREXIO_SUCCESS:
|
||||||
|
trexio_file = return_obj[0]
|
||||||
|
assert trexio_file is not None
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
raise Exception(f"Could not open TREXIO file {file_name} using trexio_open function. Please make sure that there are no typos in the file name.")
|
raise Exception(f"Could not open TREXIO file {file_name} using trexio_open function. The return value is None (NULL pointer).")
|
||||||
|
|
||||||
return trexio_file
|
return trexio_file
|
||||||
#+end_src
|
#+end_src
|
||||||
|
Loading…
Reference in New Issue
Block a user