Skip to content

grass.pygrass: Raise GrassError and TypeError in RasterAbstractBase - #7994

Merged
echoix merged 1 commit into
OSGeo:mainfrom
bhuvan-somisetty:fix-7993-pygrass-raster-abstract-exceptions
Oct 18, 2026
Merged

echoix merged 1 commit into
OSGeo:mainfrom
bhuvan-somisetty:fix-7993-pygrass-raster-abstract-exceptions

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown
Contributor

Description

This PR fixes exception handling in RasterAbstractBase in python/grass/pygrass/raster/abstract.py:

  1. In RasterAbstractBase.set_region() and set_region_from_rast(), replaces fatal() and bare raise calls with raise GrassError(...) when region modification is attempted on an open raster map.
  2. In RasterAbstractBase.__getitem__(), replaces fatal("Invalid argument type.") with raise TypeError(...) when an unsupported index type is provided.
  3. Removes the unused fatal import from grass.script.
  4. Adds unit tests in python/grass/pygrass/raster/testsuite/test_pygrass_raster.py.

Motivation and context

As a library module, pygrass classes should raise standard Python exceptions rather than calling grass.script.fatal() (which abruptly terminates the Python session/process via sys.exit(1)) or executing bare raise statements outside of active exception handlers.

Fixes #7993.

How has this been tested?

Added unit tests in python/grass/pygrass/raster/testsuite/test_pygrass_raster.py verifying:

  • RasterRow.set_region() and RasterRow.set_region_from_rast() raise GrassError when called on an open raster map.
  • RasterRow.__getitem__() raises TypeError when indexed with an invalid key type.

Screenshots (if appropriate)

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as before)

Checklist

  • PR title provides summary of the changes and starts with one of the pre-defined prefixes
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

@github-actions github-actions Bot added Python Related code is in Python libraries tests Related to Test Suite labels Oct 1, 2026
In `python/grass/pygrass/raster/abstract.py`, `RasterAbstractBase.set_region()`
and `set_region_from_rast()` previously called `fatal()` and a bare `raise`
when invoked on an open raster map, causing a process exit via `sys.exit()`
or a `RuntimeError` due to missing active exception context.

Additionally, `RasterAbstractBase.__getitem__()` called `fatal()` when passed
an invalid key type instead of raising `TypeError`.

Fix this by:
- Raising `GrassError` when region modifications are attempted on an open raster map.
- Raising `TypeError` on unsupported index key types in `__getitem__()`.
- Removing the unused `fatal` import.
- Adding unit tests in `test_pygrass_raster.py`.

Fixes OSGeo#7993.

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
@bhuvan-somisetty
bhuvan-somisetty force-pushed the fix-7993-pygrass-raster-abstract-exceptions branch from e40221a to c90d4c5 Compare October 3, 2026 12:01
@bhuvan-somisetty

Copy link
Copy Markdown
Contributor Author

Hi @echoix @ninsbl, this one's ready for a review whenever you have time. Checks are passing, and I'm happy to change anything. Thanks!

@echoix
echoix merged commit c1a946d into OSGeo:main Oct 18, 2026
26 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries Python Related code is in Python tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] grass.pygrass: RasterAbstractBase uses fatal() and bare raise instead of raising GrassError / TypeError

2 participants