Skip to content

Commit 409f1c1

Browse files
committed
Small additions
1 parent 6b55f08 commit 409f1c1

7 files changed

Lines changed: 119 additions & 92 deletions

File tree

src/example_fgen_basic/error_v/error_v_manager.f90

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -309,34 +309,36 @@ subroutine ensure_array_capacity_for_instances(n)
309309

310310
end subroutine ensure_array_capacity_for_instances
311311

312-
pure recursive function get_error_message(err) result(full_msg)
312+
! pure recursive function get_error_message(err) result(full_msg)
313+
recursive function get_error_message(err) result(full_msg)
314+
type(ErrorV), intent(in) :: err
315+
character(len=:), allocatable :: full_msg
316+
character(len=:), allocatable :: cause_msg
313317

314-
type(ErrorV), intent(in) :: err
318+
full_msg = err%message
315319

316-
character(len=:), allocatable :: full_msg
317-
character(len=:), allocatable :: cause_msg
320+
if (err%cause/=0) then
318321

319-
full_msg = err%message
322+
cause_msg = get_error_message(instance_array(err%cause))
323+
full_msg = trim(full_msg) // NEW_LINE("A") // " Previous error --> " // trim(cause_msg)
324+
!MZ : free slot while passing by? If yes we loose the function "purity"
325+
instance_available(err%cause) = .true.
320326

321-
if (err%cause/=0) then
322-
!MZ : free slot while passing by?
323-
cause_msg = get_error_message(instance_array(err%cause))
324-
full_msg = trim(full_msg) // NEW_LINE("A") // " Previous error --> " // trim(cause_msg)
325-
end if
327+
end if
326328

327-
end function get_error_message
329+
end function get_error_message
328330

329-
subroutine deallocate_instance_arrays()
330-
!! Finalise an instance
331+
subroutine deallocate_instance_arrays()
332+
!! Finalise an instance
331333

332-
if (allocated(instance_available).and.allocated(instance_array)) then
333-
deallocate(instance_available,instance_array)
334-
else if(allocated(instance_available))then
335-
deallocate(instance_available)
336-
else if(allocated(instance_array)) then
337-
deallocate(instance_array)
338-
end if
334+
if (allocated(instance_available).and.allocated(instance_array)) then
335+
deallocate(instance_available,instance_array)
336+
else if(allocated(instance_available))then
337+
deallocate(instance_available)
338+
else if(allocated(instance_array)) then
339+
deallocate(instance_array)
340+
end if
339341

340-
end subroutine deallocate_instance_arrays
342+
end subroutine deallocate_instance_arrays
341343

342344
end module m_error_v_manager

src/example_fgen_basic/get_square_root.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ def get_square_root(inv: float) -> float:
6666
# I like the safety of finalising in `from_instance_index`.
6767
# if not finalised(result_instance_index):
6868
# finalise(result_instance_index)
69-
m_result_w.finalise_instance(result_instance_index)
69+
state = m_result_w.finalise_instance(result_instance_index)
70+
71+
if state != 0:
72+
error = ResultGen.from_instance_index(state)
73+
message = error.error_v.message.decode("utf-8")
74+
raise FortranError(error.error_v.code, message)
7075

7176
return res

src/example_fgen_basic/get_square_root_wrapper.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function get_square_root(inv) result(res_instance_index)
3131
!! Instance index of the result type
3232

3333
type(ResultGen) :: res
34-
type(ResultGen) :: res_get_available_instance_index
34+
! type(ResultGen) :: res_get_available_instance_index
3535
type(ResultGen) :: res_chk
3636

3737
res = o_get_square_root(inv)

src/example_fgen_basic/result/result_gen.f90

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module m_result_gen
22

33
use kind_parameters, only: dp,i8
44
use m_error_v, only: ErrorV
5+
use m_error_v_manager, only: error_v_manager_build_instance => build_instance
56

67
implicit none
78
private
@@ -42,13 +43,25 @@ function constructor(tag,data_int,data_dp,error_v) result(self)
4243
type(ErrorV), optional, intent(in) :: error_v
4344

4445
integer, intent(in) :: tag
46+
integer :: cause
4547

4648
call self % build (tag = tag, data_int = data_int, data_dp = data_dp,&
4749
error_v = error_v, res=res_check)
4850

4951
if (res_check % is_error()) then
50-
print *, res_check % error_v % message
51-
error stop
52+
53+
cause = error_v_manager_build_instance( &
54+
code = res_check % error_v % code, &
55+
message = res_check % error_v % message &
56+
)
57+
58+
call self % build(tag = T_ERR, &
59+
error_v = ErrorV( &
60+
code=1, &
61+
message=("Build Instance error : "), &
62+
cause=cause &
63+
)&
64+
)
5265
end if
5366

5467
end function constructor

src/example_fgen_basic/result/result_gen.py

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class ResultGen:
2929
error_v: ErrorV | None
3030
"""Error"""
3131

32-
"""
33-
Parameters:
34-
"""
3532
__fs_int = m_result_w.s_int
3633
__fs_dp = m_result_w.s_dp
3734
__fs_err = m_result_w.s_err
@@ -44,56 +41,48 @@ def from_instance_index(cls, instance_index: int) -> ResultGen:
4441
Parameters
4542
----------
4643
instance_index
47-
Instance index received form Fortran
44+
Instance index received form Fortran
4845
4946
Returns
5047
-------
5148
:
52-
Initialised index
49+
Initialised index
5350
"""
5451
valid_instance = m_result_w.probe_instance(instance_index)
5552

56-
if valid_instance != instance_index:
53+
if valid_instance != 0:
5754
instance_index = valid_instance
5855

5956
tag = m_result_w.get_instance_tag(instance_index)
6057

6158
if tag == cls.__fs_int:
62-
data_v: int | None = m_result_w.get_data_int(instance_index)
59+
data_v_int: int | None = m_result_w.get_data_int(instance_index)
6360
error_v = None
61+
res = cls(data_v=data_v_int, error_v=error_v)
6462

6563
elif tag == cls.__fs_dp:
66-
data_v: float | None = m_result_w.get_data_dp(instance_index)
64+
data_v_float: float | None = m_result_w.get_data_dp(instance_index)
6765
error_v = None
66+
res = cls(data_v=data_v_float, error_v=error_v)
6867

6968
elif tag == cls.__fs_err:
70-
data_v = None
71-
error_tuple: tuple[int | None, str | None] = m_result_w.get_error(
72-
instance_index
73-
)
74-
code, message = error_tuple
69+
data_v_err = None
70+
code, message = m_result_w.get_error(instance_index)
71+
# if code is None or message is None:
72+
# raise ValueError("Fortran returned incomplete error information")
7573
error_v = ErrorV(code=code, message=message)
76-
else:
77-
print("ERRRORRR")
7874

79-
res = cls(data_v=data_v, error_v=error_v)
75+
res = cls(data_v=data_v_err, error_v=error_v)
76+
else:
77+
msg = f"Undefinded tag: {tag}"
78+
raise ValueError(msg)
8079

8180
return res
8281

8382
@classmethod
8483
def free_fortran_memory(cls) -> None:
8584
"""
86-
Initialise from an instance index received from Fortran
87-
88-
Parameters
89-
----------
90-
instance_index
91-
None
92-
93-
Returns
94-
-------
95-
:
96-
None: It just frees memory
85+
Free memory on the Fortran side.
9786
"""
9887
m_result_w.free_resources()
9988

@@ -109,14 +98,16 @@ def build_fortran_instance(self) -> int:
10998
:
11099
Instance index of the object which has been created on the Fortran side
111100
"""
112-
if (self.data_v is None) & (self.error_v is not None):
113-
instance_index: int = m_result_w.build_instance_err(
101+
instance_index: int
102+
103+
if self.data_v is None and self.error_v is not None:
104+
instance_index = m_result_w.build_instance_err(
114105
self.error_v.code, self.error_v.message
115106
)
116107
elif isinstance(self.data_v, int):
117-
instance_index: int = m_result_w.build_instance_int(self.data_v)
108+
instance_index = m_result_w.build_instance_int(self.data_v)
118109
elif isinstance(self.data_v, float):
119-
instance_index: int = m_result_w.build_instance_dp(self.data_v)
110+
instance_index = m_result_w.build_instance_dp(self.data_v)
120111
else:
121112
msg = f"data_v={self.data_v}, error_v={self.error_v}"
122113
raise KeyError(msg)

src/example_fgen_basic/result/result_manager.f90

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,37 @@ subroutine build_instance(tag, data_int, data_dp, error_v, instance_index, res_c
7171

7272
end subroutine build_instance
7373

74-
subroutine finalise_instance(instance_index)
74+
function finalise_instance(instance_index) result(state_index)
7575
!! Finalise an instance
7676

7777
integer, intent(in) :: instance_index
7878
!! Index of the instance to finalise
7979

80-
type(ResultGen) :: res_check_index_claimed
80+
type(ResultGen) :: res_check
81+
integer :: cause, state_index
82+
83+
res_check = check_index_claimed(instance_index)
8184

82-
res_check_index_claimed = check_index_claimed(instance_index)
8385
! MZ how do we handle unsuccefull finalisation?
84-
! if(res_check_index_claimed%is_error()) return
86+
if((res_check%is_error()) .and. (&
87+
res_check%error_v%code /= 33)) then
88+
89+
cause = error_v_manager_build_instance(code = res_check % error_v % code, &
90+
message = res_check % error_v % message)
91+
92+
call build_instance (tag = T_ERR,&
93+
error_v = ErrorV(code=1,message="Finalise Instance error : ",cause=cause),&
94+
instance_index = state_index, &
95+
res_check=res_check &
96+
)
97+
98+
return
99+
end if
100+
101+
state_index = 0
85102
call instance_array(instance_index) % finalise()
86103

87-
end subroutine finalise_instance
104+
end function finalise_instance
88105

89106
subroutine set_instance_index_to(instance_index, data_int, data_dp, error_v, res_check)
90107

@@ -102,12 +119,12 @@ subroutine set_instance_index_to(instance_index, data_int, data_dp, error_v, res
102119
if (input_check == 0) then
103120

104121
call res_check % build (tag = T_ERR,&
105-
error_v = ErrorV(code=1,message="Setting instance ERROR: Empty Input"))
122+
error_v = ErrorV(code=1,message="Setting instance ERROR: Empty Input"))
106123

107124
else if (input_check > 1) then
108125

109126
call res_check % build (tag = T_ERR,&
110-
error_v = ErrorV(code=1,message="Setting instance ERROR: Multiple Input"))
127+
error_v = ErrorV(code=1,message="Setting instance ERROR: Multiple Input"))
111128

112129
else
113130

@@ -150,7 +167,7 @@ function probe_instance(instance_index) result(res_instance_index)
150167

151168
end if
152169

153-
res_instance_index = instance_index
170+
res_instance_index = 0
154171

155172
end function probe_instance
156173

@@ -241,9 +258,9 @@ function check_index_claimed(instance_index) result(res_check_index_claimed)
241258
if (instance_array(instance_index)%tag==T_NONE) then
242259

243260
msg = "Index " // trim(adjustl(idx_str)) // " has not been claimed"
244-
call res_check_index_claimed % build(tag=T_ERR,error_v=ErrorV(code=3, message=msg))
245-
261+
call res_check_index_claimed % build(tag=T_ERR,error_v=ErrorV(code=33, message=msg))
246262
return
263+
247264
end if
248265

249266
call res_check_index_claimed % build(tag=T_CLAIM)
@@ -297,8 +314,6 @@ subroutine deallocate_instance_array()
297314

298315
if (allocated (instance_array))then
299316
deallocate(instance_array)
300-
else
301-
print *, "instance_array NOT allocated"
302317
end if
303318

304319
end subroutine deallocate_instance_array

0 commit comments

Comments
 (0)