From 38bc8e451a949fd04905212fbf3116eb4782211f Mon Sep 17 00:00:00 2001 From: dkunhamb Date: Thu, 5 Sep 2024 09:30:39 -0500 Subject: [PATCH 1/4] update execute script with return value --- src/ansys/mechanical/core/embedding/app.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ansys/mechanical/core/embedding/app.py b/src/ansys/mechanical/core/embedding/app.py index fedf6ca03..b40a697d0 100644 --- a/src/ansys/mechanical/core/embedding/app.py +++ b/src/ansys/mechanical/core/embedding/app.py @@ -227,7 +227,13 @@ 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) + if script_result is None: + error_msg = f"Failed to execute the script" + if script_result.Error is not None: + error_msg += f": {script_result.Error.Message}" + raise Exception(error_msg) + return str(script_result.Value) def plotter(self) -> None: """Return ``ansys.tools.visualization_interface.Plotter`` object.""" From 7c3079b1b75869dc9efe26483f9cbc1bb669eb88 Mon Sep 17 00:00:00 2001 From: dkunhamb Date: Thu, 5 Sep 2024 10:05:31 -0500 Subject: [PATCH 2/4] update execute script with return value --- src/ansys/mechanical/core/embedding/app.py | 7 ++++--- tests/embedding/test_app.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/ansys/mechanical/core/embedding/app.py b/src/ansys/mechanical/core/embedding/app.py index b40a697d0..d31ca2cc3 100644 --- a/src/ansys/mechanical/core/embedding/app.py +++ b/src/ansys/mechanical/core/embedding/app.py @@ -228,10 +228,11 @@ def execute_script(self, script: str) -> typing.Any: args = None rets = None 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: - error_msg = f"Failed to execute the script" - if script_result.Error is not None: - error_msg += f": {script_result.Error.Message}" + raise Exception(error_msg) + if script_result.Error is not None: + error_msg += f": {script_result.Error.Message}" raise Exception(error_msg) return str(script_result.Value) diff --git a/tests/embedding/test_app.py b/tests/embedding/test_app.py index 3cfda59bd..80d992b85 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") From a59831304f1a10b04b1f5f54945a17a5d67c1ff3 Mon Sep 17 00:00:00 2001 From: dkunhamb Date: Thu, 5 Sep 2024 10:14:42 -0500 Subject: [PATCH 3/4] review change --- src/ansys/mechanical/core/embedding/app.py | 2 +- tests/embedding/test_app.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/mechanical/core/embedding/app.py b/src/ansys/mechanical/core/embedding/app.py index d31ca2cc3..83367ad3e 100644 --- a/src/ansys/mechanical/core/embedding/app.py +++ b/src/ansys/mechanical/core/embedding/app.py @@ -234,7 +234,7 @@ def execute_script(self, script: str) -> typing.Any: if script_result.Error is not None: error_msg += f": {script_result.Error.Message}" raise Exception(error_msg) - return str(script_result.Value) + 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 80d992b85..8f88253f8 100644 --- a/tests/embedding/test_app.py +++ b/tests/embedding/test_app.py @@ -319,7 +319,7 @@ 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" + assert result == 5 with pytest.raises(Exception): # This will throw an exception since no module named test available embedded_app.execute_script("import test") From 6456dbfe3dc95f447c637b57ac81a2ce354ec933 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:16:06 +0000 Subject: [PATCH 4/4] chore: adding changelog file 894.fixed.md --- doc/changelog.d/894.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/894.fixed.md 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