Skip to content

Commit

Permalink
Avoiding running get_array and non_interactive raising exceptions (
Browse files Browse the repository at this point in the history
…#2484)

* * Not running `get_array` in `non_interactive`
* Clarifying exceptions messages
* Early exit of `non_interactive` cont manager when an exception is raised inside.

* avoiding raising exception in main thread

* Fixing ealry exit of `non_interactive` cont man.

* removing checking main thread
  • Loading branch information
germa89 authored Nov 8, 2023
1 parent 7674756 commit 428fba2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
43 changes: 40 additions & 3 deletions src/ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,20 @@ def __enter__(self):
self._parent()._store_commands = True

def __exit__(self, *args):
self._parent()._log.debug("Exiting non-interactive mode")
self._parent()._flush_stored()
self._parent()._store_commands = False

if args[0] is not None:
# An exception was raised, let's exit now without flushing
self._parent()._log.debug(
"An exception was found in the `non_interactive` environment. "
"Hence the commands are not flushed."
)
return None
else:
# No exception so let's flush.
self._parent()._log.debug("Exiting non-interactive mode")
self._parent()._flush_stored()

class _save_selection:
"""Save the selection and returns to it when exiting"""

Expand Down Expand Up @@ -3827,7 +3837,20 @@ def get_array(
-0.00178402, -0.01234851, 0.01234851, -0.01234851])
"""
arr = self._get_array(entity, entnum, item1, it1num, item2, it2num, kloop)
parm_name = kwargs.get("parm", None)

if self._store_commands:
raise MapdlRuntimeError(
"Cannot use `mapdl.get_array` when in `non_interactive` mode, "
"since it does not return anything until the `non_interactive` context "
"manager is finished.\n"
"Exit `non_interactive` mode before using this method.\n\n"
"Alternatively you can use `mapdl.vget` to specify the name of the MAPDL parameter where to store the retrieved value."
)

arr = self._get_array(
entity, entnum, item1, it1num, item2, it2num, kloop, **kwargs
)

# edge case where corba refuses to return the array
ntry = 0
Expand All @@ -3853,6 +3876,16 @@ def _get_array(
"""Uses the VGET command to get an array from ANSYS"""
parm_name = kwargs.pop("parm", None)

if self._store_commands and not parm_name:
raise MapdlRuntimeError(
"Cannot use `mapdl._get_array` when in `non_interactive` mode, "
"since it does not return anything until the `non_interactive` context "
"manager is finished.\n"
"Exit `non_interactive` mode before using this method.\n\n"
"Alternatively you can use `mapdl.vget` or use the `parm` kwarg in "
"`mapdl._get_array` to specify the name of the MAPDL parameter where to store the retrieved value. In any case, this function will return `None`"
)

if parm_name is None:
parm_name = "__vget_tmp_%d__" % self._vget_arr_counter
self._vget_arr_counter += 1
Expand All @@ -3869,6 +3902,10 @@ def _get_array(
mute=False,
)

if self._store_commands:
# Return early
return None

# check if empty array
if "the dimension number 1 is 0" in out:
return np.empty(0)
Expand Down
6 changes: 4 additions & 2 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2040,8 +2040,10 @@ def _get(

if self._store_commands:
raise MapdlRuntimeError(
"Cannot use gRPC enabled ``GET`` when in non_interactive mode. "
"Exit non_interactive mode before using this method."
"Cannot use `mapdl.get_value` when in `non_interactive` mode. "
"Exit non_interactive mode before using this method.\n\n"
"Alternatively you can use `mapdl.get` to specify the name of "
"the MAPDL parameter where to store the retrieved value.\n"
)

cmd = f"{entity},{entnum},{item1},{it1num},{item2},{it2num},{item3}, {it3num}, {item4}, {it4num}"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2117,3 +2117,10 @@ def test_saving_selection_context(mapdl, cube_solve):

assert "nod_selection_4".upper() not in mapdl.cmlist()
assert "nod_selection_4" not in mapdl.components


def test_get_array_non_interactive(mapdl, solved_box):
mapdl.allsel()
with pytest.raises(MapdlRuntimeError):
with mapdl.non_interactive:
mapdl.get_array("asdf", "2")

0 comments on commit 428fba2

Please sign in to comment.