diff --git a/python/test/test_api.py b/python/test/test_api.py index ae1a1ab..f6b151d 100644 --- a/python/test/test_api.py +++ b/python/test/test_api.py @@ -49,7 +49,7 @@ nucleus_num = 12 try: trexio.write_nucleus_num(test_file, -100) 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 try: @@ -60,7 +60,7 @@ except: try: trexio.write_nucleus_num(test_file, nucleus_num*2) 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 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): rmo_num = trexio.read_mo_num(test_file2) else: - print("Not reading the non-existing variable mo_num.") + print("Pass on reading the non-existing variable mo_num: checked") # close TREXIO file #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") + +#==========================================================# diff --git a/src/pytrexio.i b/src/pytrexio.i index 1665eb9..87da064 100644 --- a/src/pytrexio.i +++ b/src/pytrexio.i @@ -37,6 +37,7 @@ %apply int *OUTPUT { int64_t* const num}; %apply float *OUTPUT { float* const num}; %apply float *OUTPUT { double* const num}; +%apply int *OUTPUT { trexio_exit_code* const rc_open}; /* Does not work for arrays (SIGSEGV) */ diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 44728df..10629ec 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -911,11 +911,22 @@ def open(file_name: str, mode: str, back_end: int): >>> 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: - trexio_file = pytr.trexio_open(file_name, mode, back_end) - assert trexio_file is not None + return_obj = pytr.trexio_open(file_name, mode, back_end) + 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: - 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 #+end_src