Skip to content

Commit

Permalink
FIX: Update execute_script method (#894)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
dipinknair and pyansys-ci-bot authored Sep 5, 2024
1 parent cf3b0c5 commit 32344ff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/changelog.d/894.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update ``execute_script`` method
9 changes: 8 additions & 1 deletion src/ansys/mechanical/core/embedding/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
11 changes: 11 additions & 0 deletions tests/embedding/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 32344ff

Please sign in to comment.