Repository navigation
grass.pygrass: Raise OpenError in RasterSegment.open when map exists - #8008
bhuvan-somisetty wants to merge 3 commits into
Conversation
In `python/grass/pygrass/raster/__init__.py`, `RasterSegment.open()` previously called `fatal(str_err.format(self))` when attempting to open an existing raster map in write (`"w"`) or read-write (`"rw"`) mode without `overwrite=True`, causing an immediate process exit via `sys.exit(1)`. Fix this by: - Raising `OpenError(str_err.format(self.name))` instead of calling `fatal()`. - Removing the unused `fatal` import from `grass.script`. - Adding unit tests for `RasterSegment` in `test_pygrass_raster.py`. Fixes OSGeo#8007. Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
| cls.use_temp_region() | ||
| cls.runModule("g.region", n=40, s=0, e=40, w=0, res=10) | ||
| cls.runModule( | ||
| "r.mapcalc", | ||
| expression="%s = row() + (10.0 * col())" % (cls.name), | ||
| overwrite=True, | ||
| ) |
There was a problem hiding this comment.
Try to use addClassCleanup and friends for handling incomplete setup teardown or errors in the setup class. I had already changed some gunittest test files with that (when it was causing problems on Windows), at least 3, maybe 5 or more.
I don't remember exactly what was the best way for the region, but I remember already handling that somewhere in the code.
There was a problem hiding this comment.
Done, switched to addClassCleanup for both the map and the region.
| if (self.mode in {"w", "rw"}) and not self.overwrite: | ||
| str_err = _("Raster map <{0}> already exists. Use overwrite.") | ||
| fatal(str_err.format(self)) | ||
| raise OpenError(str_err.format(self.name)) |
There was a problem hiding this comment.
Can you refresh me on what self.name resolves to? Will it always resolve?
There was a problem hiding this comment.
It's the plain name from the constructor, so it's always set, but it drops the mapset. Now it uses self (name@mapset) like RasterRow.
|
Also consider these potential problems (below), judging if they are justified and correct issues or not. If it is right about the part that RasterRow already has a check similar, then adapting the abstract base might be a better solution. Details
I checked out PR #8008 as branch 10 findings, most severe first:
|
Move the check for an existing map opened for writing into a base class method used by both RasterRow and RasterSegment, so they raise the same OpenError with the full map name. In RasterSegment, run the check before reading the map metadata. Callers who caught ScriptError from the previous gs.fatal() call in RasterSegment.open now need to catch OpenError. Share the test map setup between the test classes, use class cleanups so the map and region are removed even if setup fails, check that a failed open leaves the map untouched, and overwrite copies of the test map instead of the shared one.
|
Went through the list. The check is now a shared helper on the base class and runs before the metadata reads. The overwrite tests use their own copies, and the failure tests check that the map is left untouched. 1 and 2 don't apply because mtype defaults to CELL, so it's never None. |
The pygrass Module interface used by runModule rejects a comma-separated string for a key-description parameter such as g.copy raster, so copy_test_map failed with TypeError. Pass the from and to names as a list instead. Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
Description
In
python/grass/pygrass/raster/__init__.py,RasterSegment.open()previously calledfatal(str_err.format(self))fromgrass.scriptwhen attempting to open an existing raster map in write ("w") or read-write ("rw") mode withoutoverwrite=True.This change:
fatal()with raisingOpenError(str_err.format(self.name)).from grass.script import fatalimport.RasterSegmentTestCasewithinpython/grass/pygrass/raster/testsuite/test_pygrass_raster.pycoveringOpenErroron non-existent maps in read mode, existing maps in write/read-write modes without overwrite, and successful opens withoverwrite=True.Motivation and context
Calling
fatal()inside PyGRASS library classes immediately terminates the Python process (sys.exit(1)), preventing applications, notebooks, and test runners from catching and handling the exception viaexcept OpenError:. RaisingOpenErroralignsRasterSegmentwithRasterRow.open()and standard PyGRASS exception handling.Fixes #8007.
How has this been tested?
Added unit tests in
python/grass/pygrass/raster/testsuite/test_pygrass_raster.py(RasterSegmentTestCase) and verified withruff checkandruff format.Screenshots (if appropriate)
N/A
Types of changes
Checklist