From b3f799622dec19a4de4a08fdde8269cc07eb91a4 Mon Sep 17 00:00:00 2001 From: "r.jaepel" Date: Tue, 20 Aug 2024 17:35:32 +0200 Subject: [PATCH] Disable DLL interface for CADET < v5. This will break tests with CADET < v5. --- cadet/cadet.py | 20 ++++++++++++++++++++ cadet/cadet_dll.py | 13 +++++++++++++ 2 files changed, 33 insertions(+) diff --git a/cadet/cadet.py b/cadet/cadet.py index 7644d88..b7bc050 100644 --- a/cadet/cadet.py +++ b/cadet/cadet.py @@ -219,6 +219,7 @@ def __init__(self, install_path: Optional[Path] = None, use_dll: bool = False, * self._cadet_dll_runner: Optional[CadetDLLRunner] = CadetDLLRunner( self.cadet_dll_path ) + self._ensure_compatible_dll_version() else: self._cadet_dll_runner: Optional[CadetCLIRunner] = None self.use_dll = use_dll @@ -271,7 +272,26 @@ def install_path(self, install_path: Optional[os.PathLike]) -> None: self._cadet_cli_runner = CadetCLIRunner(self.cadet_cli_path) if self.cadet_dll_path is not None: self._cadet_dll_runner = CadetDLLRunner(self.cadet_dll_path) + self._ensure_compatible_dll_version() + def _ensure_compatible_dll_version(self): + """ + Ensure that the CADET DLL version is compatible with the required interface. + + Raises + ------ + RuntimeError + If `use_dll` is `True` and the CADET version is outdated (less than 5), indicating that + the DLL interface is not supported. + """ + if int(self._cadet_dll_runner.cadet_version[0]) < 5: + if self.use_dll: + raise RuntimeError( + f"CADET was set to `use_dll` but found outdated CADET " + f"version {self._cadet_dll_runner.cadet_version} " + f"which does not support the DLL interface." + ) + self._cadet_dll_runner = None @property def cadet_path(self) -> Optional[Path]: diff --git a/cadet/cadet_dll.py b/cadet/cadet_dll.py index 8b10e81..d4b6b85 100644 --- a/cadet/cadet_dll.py +++ b/cadet/cadet_dll.py @@ -1,6 +1,7 @@ import ctypes import io import os +import warnings from pathlib import Path from typing import Any, Optional @@ -1645,6 +1646,13 @@ def __init__(self, dll_path: os.PathLike | str) -> None: cdtGetLibraryBuildType.restype = ctypes.c_char_p self._cadet_build_type = cdtGetLibraryBuildType().decode('utf-8') + if int(self._cadet_version[0]) < 5: + warnings.warn( + f"The detected CADET installation with version {self._cadet_version}" + " is below version 5.0 and does not support the DLL interface." + ) + return + # Define the log handler callback type self.LOG_HANDLER_CLBK = ctypes.CFUNCTYPE( None, @@ -1684,6 +1692,9 @@ def clear(self) -> None: This method deletes the current simulation results and resets the driver. """ + if int(self._cadet_version[0]) < 5: + return + if hasattr(self, "res"): del self.res @@ -1694,6 +1705,8 @@ def __del__(self) -> None: """ Clean up the CADET driver on object deletion. """ + if int(self._cadet_version[0]) < 5: + return self._api.deleteDriver(self._driver) def setup_log_buffer(self, log_level: int = None) -> io.StringIO: