1010
1111from example_fgen_basic .pyfgen_runtime .base_finalisable import (
1212 FinalisableWrapperBase ,
13- FinalisableWrapperBasePtrBased ,
1413 check_initialised ,
15- check_initialised_ptr_based ,
16- execute_finalise_on_fail_ptr_based ,
14+ execute_finalise_on_fail ,
1715)
1816from example_fgen_basic .pyfgen_runtime .exceptions import CompiledExtensionNotFoundError
1917
2018try :
2119 from example_fgen_basic ._lib import ( # type: ignore
22- m_error_v_ptr_based_w ,
2320 m_error_v_w ,
2421 )
2522except (ModuleNotFoundError , ImportError ) as exc : # pragma: no cover
@@ -48,77 +45,20 @@ def exposed_attributes(self) -> tuple[str, ...]:
4845 """
4946 return ("code" , "message" )
5047
51- # TODO: from_build_args, from_new_connection, context manager, finalise
52-
53- @property
54- @check_initialised
55- def code (self ) -> int :
56- """
57- Error code
58-
59- Returns
60- -------
61- :
62- Error code, retrieved from Fortran
63- """
64- code : int = m_error_v_w .iget_code (instance_index = self .instance_index )
65-
66- return code
67-
68- @property
69- @check_initialised
70- def message (self ) -> str :
71- """
72- Error message
73-
74- Returns
75- -------
76- :
77- Error message, retrieved from Fortran
78- """
79- message : str = m_error_v_w .iget_message (
80- instance_index = self .instance_index
81- ).decode ()
82-
83- return message
84-
85-
86- @define
87- class ErrorVPtrBased (FinalisableWrapperBasePtrBased ):
88- """
89- TODO: auto docstring e.g. "Wrapper around the Fortran :class:`ErrorV`"
90-
91- Uses the pointer-based passing logic
92- """
93-
94- # Bug in Ipython pretty hence have to put this on every object?
95- def _repr_pretty_ (self , p : Any , cycle : bool ) -> None :
96- """
97- Get pretty representation of self
98-
99- Used by IPython notebooks and other tools
100- """
101- super ()._repr_pretty_ (p = p , cycle = cycle )
102-
103- @property
104- def exposed_attributes (self ) -> tuple [str , ...]:
105- """
106- Attributes exposed by this wrapper
107- """
108- return ("code" , "message" )
109-
48+ # TODO: context manager
11049 @classmethod
111- def from_new_connection (cls ) -> ErrorVPtrBased :
50+ def from_new_connection (cls ) -> ErrorV :
11251 """
11352 Initialise from a new connection
11453
11554 The user is responsible for releasing this connection
11655 using :attr:`~finalise` when it is no longer needed.
117- Alternatively a :obj:`~AtmosphereToOceanCarbonFluxCalculatorContext`
56+ Alternatively an [ErrorVContext][]
11857 can be used to handle the finalisation using a context manager.
11958
12059 Returns
12160 -------
61+ :
12262 A new instance with a unique instance index
12363
12464 Raises
@@ -128,26 +68,24 @@ def from_new_connection(cls) -> ErrorVPtrBased:
12868
12969 This could occur if too many instances are allocated at any one time
13070 """
131- instance_ptr = m_error_v_ptr_based_w .get_instance_ptr ()
132- # TODO: result type handling here
71+ instance_ptr : int = m_error_v_w .get_free_instance_number ()
13372
13473 return cls (instance_ptr )
13574
136- # TODO: from_build_args, from_new_connection, context manager, finalise
13775 @classmethod
13876 def from_build_args (
13977 cls ,
14078 code : int ,
14179 message : str = "" ,
142- ) -> ErrorVPtrBased :
80+ ) -> ErrorV :
14381 """
14482 Build the class (including connecting to Fortran)
14583 """
14684 out = cls .from_new_connection ()
14785 # TODO: remove or update this construct when we have result types
148- execute_finalise_on_fail_ptr_based (
86+ execute_finalise_on_fail (
14987 out ,
150- m_error_v_ptr_based_w .instance_build ,
88+ m_error_v_w .instance_build ,
15189 code = code ,
15290 message = message ,
15391 )
@@ -159,28 +97,11 @@ def finalise(self) -> None:
15997 """
16098 Close the connection with the Fortran module
16199 """
162- m_error_v_ptr_based_w .instance_finalise (self .instance_ptr )
163- self ._uninitialise_instance_ptr ()
164-
165- @property
166- def is_associated (self ) -> bool :
167- """
168- Check whether `self`'s pointer is associated on the Fortran side
169-
170- Returns
171- -------
172- :
173- Whether `self`'s pointer is associated on the Fortran side
174- """
175- if self .instance_ptr is None :
176- return False
177-
178- res : bool = bool (m_error_v_ptr_based_w .is_associated (self .instance_ptr )) # type: ignore
179-
180- return res
100+ m_error_v_w .instance_finalise (self .instance_index )
101+ self ._uninitialise_instance_index ()
181102
182103 @property
183- @check_initialised_ptr_based
104+ @check_initialised
184105 def code (self ) -> int :
185106 """
186107 Error code
@@ -190,12 +111,12 @@ def code(self) -> int:
190111 :
191112 Error code, retrieved from Fortran
192113 """
193- code : int = m_error_v_ptr_based_w .iget_code (instance_ptr = self .instance_ptr )
114+ code : int = m_error_v_w .iget_code (instance_index = self .instance_index )
194115
195116 return code
196117
197118 @property
198- @check_initialised_ptr_based
119+ @check_initialised
199120 def message (self ) -> str :
200121 """
201122 Error message
@@ -205,8 +126,8 @@ def message(self) -> str:
205126 :
206127 Error message, retrieved from Fortran
207128 """
208- message : str = m_error_v_ptr_based_w .iget_message (
209- instance_ptr = self .instance_ptr
129+ message : str = m_error_v_w .iget_message (
130+ instance_index = self .instance_index
210131 ).decode ()
211132
212133 return message
0 commit comments