-
-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grass.script.setup: Add return context manager from init (#1912)
The grass.script.setup.init function now returns a SessionHandle handle object which is a context manager (a class with __enter__ and __exit__ methods). Works as the Python open function which can be used without a context manager. This also makes it compatible with the current usage of init. Supports cases when session object is used to do finish and when reference to the session object is not assigned to variable. Use of global finish is still supported. Multiple calls of finish on session raises exception. Multiple sessions in parallel are not supported (the underlying global finish function does not currently support that). Cleaning is not enforced. However, the context manager makes it easier to do that right. A simple test using pytest of context manager API uses private (protected) function from grass.script.core (for now). Co-authored-by: Vaclav Petras <wenzeslaus@gmail.com>
- Loading branch information
1 parent
dbd51e0
commit 94b8ec6
Showing
2 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""Test functions in grass.script.setup""" | ||
|
||
import grass.script as gs | ||
import grass.script.setup as grass_setup | ||
|
||
|
||
def test_init_as_context_manager(tmp_path): | ||
"""Check that init function return value works as a context manager""" | ||
location = "test" | ||
gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access | ||
with grass_setup.init(tmp_path / location): | ||
gs.run_command("g.region", flags="p") |