diff --git a/doc/changelog.d/3606.miscellaneous.md b/doc/changelog.d/3606.miscellaneous.md new file mode 100644 index 0000000000..0828ccb275 --- /dev/null +++ b/doc/changelog.d/3606.miscellaneous.md @@ -0,0 +1 @@ +feat: avoid errors when retrieving invalid routine \ No newline at end of file diff --git a/src/ansys/mapdl/core/parameters.py b/src/ansys/mapdl/core/parameters.py index 32c96bbef8..8178e44a0b 100644 --- a/src/ansys/mapdl/core/parameters.py +++ b/src/ansys/mapdl/core/parameters.py @@ -171,7 +171,14 @@ def routine(self) -> str: >>> mapdl.parameters.routine 'PREP7' """ - value = self._mapdl.get_value("ACTIVE", item1="ROUT") + value = int(self._mapdl.get_value("ACTIVE", item1="ROUT")) + if value not in ROUTINE_MAP: + self._mapdl.logger.info( + f"Getting a valid routine number failed. Routine obtained is {value}. Executing 'FINISH'." + ) + self._mapdl.finish() + value = 0 + return ROUTINE_MAP[int(value)] @property diff --git a/tests/test_parameters.py b/tests/test_parameters.py index 417c6389cd..aec7512d31 100644 --- a/tests/test_parameters.py +++ b/tests/test_parameters.py @@ -20,7 +20,9 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import logging import re +from unittest.mock import patch import numpy as np import pytest @@ -470,3 +472,25 @@ def test_non_interactive(mapdl, cleared): mapdl.parameters["qwer"] = 3 assert mapdl.parameters["qwer"] == 3 + + +@pytest.mark.parametrize("value", [121, 299]) +def test_failing_get_routine(mapdl, caplog, value): + from ansys.mapdl.core.parameters import ROUTINE_MAP + + prev_level = mapdl.logger.logger.level + mapdl.logger.setLevel(logging.INFO) + + with patch("ansys.mapdl.core.mapdl_extended._MapdlExtended.get_value") as mck: + mck.return_value = value + with caplog.at_level(logging.INFO): + routine = mapdl.parameters.routine + + mck.assert_called_once() + + txt = str(caplog.text) + assert f"Getting a valid routine number failed." in txt + assert f"Routine obtained is {value}. Executing 'FINISH'." in txt + assert routine == ROUTINE_MAP[0] + + mapdl.logger.setLevel(prev_level)