@@ -165,3 +165,41 @@ def vcell_infix_to_python_infix(
165165 except Exception as e :
166166 logging .exception ("Error in vcell_infix_to_python_infix()" , exc_info = e )
167167 raise
168+
169+ def vcell_infix_to_num_expr_infix (
170+ self , vcell_infix : str , target_num_expr_infix : MutableString , buffer_size : int | None = None
171+ ) -> ReturnValue :
172+ try :
173+ needed_buffer_size = int (1.5 * len (vcell_infix )) if buffer_size is None else buffer_size
174+ buff = ctypes .create_string_buffer (needed_buffer_size )
175+ with IsolateManager (self .lib ) as isolate_thread :
176+ json_ptr = self .lib .vcellInfixToNumExprInfix (
177+ isolate_thread , ctypes .c_char_p (vcell_infix .encode ("utf-8" )), buff , needed_buffer_size
178+ )
179+ value : bytes | None = ctypes .cast (json_ptr , ctypes .c_char_p ).value
180+ if value is None :
181+ logging .error ("Failed to regenerate vcml" )
182+ return ReturnValue (success = False , message = "Failed to generate NumExpr infix" )
183+ json_str = value .decode ("utf-8" )
184+ if "not enough room, need: `" in json_str :
185+ if buffer_size is not None :
186+ logging .error ("Failed to identify correct buffer size reported by previous error" )
187+ return ReturnValue (
188+ success = False , message = "Failed to identify correct buffer size reported by previous error"
189+ )
190+ # get the size from the error
191+ index = json_str .find ("not enough room, need: `" ) + len ("not enough room, need: `" )
192+ end_index = json_str .find ("`" , index )
193+ size_as_string : str = json_str [index :end_index ]
194+ if not size_as_string .isnumeric ():
195+ logging .error ("Buffer size reported by previous error is not an integer!" )
196+ return ReturnValue (
197+ success = False , message = "Buffer size reported by previous error is not an integer!"
198+ )
199+ return self .vcell_infix_to_num_expr_infix (vcell_infix , target_num_expr_infix , int (size_as_string ))
200+ # self.lib.freeString(json_ptr)
201+ target_num_expr_infix .value = buff .value .decode ("utf-8" )
202+ return ReturnValue .model_validate_json (json_data = json_str )
203+ except Exception as e :
204+ logging .exception ("Error in vcell_infix_to_num_expr_infix()" , exc_info = e )
205+ raise
0 commit comments