Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Update execute_script method #894

Merged
merged 5 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)

Check warning on line 233 in src/ansys/mechanical/core/embedding/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/app.py#L233

Added line #L233 was not covered by tests
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")
Loading