diff --git a/doc/changelog.d/894.fixed.md b/doc/changelog.d/894.fixed.md new file mode 100644 index 000000000..9f199006d --- /dev/null +++ b/doc/changelog.d/894.fixed.md @@ -0,0 +1 @@ +Update ``execute_script`` method \ No newline at end of file diff --git a/src/ansys/mechanical/core/embedding/app.py b/src/ansys/mechanical/core/embedding/app.py index fedf6ca03..83367ad3e 100644 --- a/src/ansys/mechanical/core/embedding/app.py +++ b/src/ansys/mechanical/core/embedding/app.py @@ -227,7 +227,14 @@ def execute_script(self, script: str) -> typing.Any: light_mode = True args = None rets = None - return self.script_engine.ExecuteCode(script, SCRIPT_SCOPE, light_mode, args, rets) + script_result = self.script_engine.ExecuteCode(script, SCRIPT_SCOPE, light_mode, args, rets) + error_msg = f"Failed to execute the script" + if script_result is None: + raise Exception(error_msg) + if script_result.Error is not None: + error_msg += f": {script_result.Error.Message}" + raise Exception(error_msg) + return script_result.Value def plotter(self) -> None: """Return ``ansys.tools.visualization_interface.Plotter`` object.""" diff --git a/tests/embedding/test_app.py b/tests/embedding/test_app.py index 3cfda59bd..8f88253f8 100644 --- a/tests/embedding/test_app.py +++ b/tests/embedding/test_app.py @@ -312,3 +312,14 @@ def test_rm_lockfile(embedded_app, tmp_path: pytest.TempPathFactory): lockfile_path = os.path.join(embedded_app.DataModel.Project.ProjectDirectory, ".mech_lock") # Assert lock file path does not exist assert not os.path.exists(lockfile_path) + + +@pytest.mark.embedding +def test_app_execute_script(embedded_app): + """Test execute_script method.""" + embedded_app.update_globals(globals()) + result = embedded_app.execute_script("2+3") + assert result == 5 + with pytest.raises(Exception): + # This will throw an exception since no module named test available + embedded_app.execute_script("import test")