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

Inconsistent use of gdal #1863

Open
schwehr opened this issue Dec 31, 2023 · 1 comment · Fixed by #1867
Open

Inconsistent use of gdal #1863

schwehr opened this issue Dec 31, 2023 · 1 comment · Fixed by #1867
Assignees
Labels
bug Something isn't working

Comments

@schwehr
Copy link
Collaborator

schwehr commented Dec 31, 2023

Here

gdal.PushErrorHandler("CPLQuietErrorHandler")

    from osgeo import gdal
    import rasterio

    # ... and suppress errors
    gdal.PushErrorHandler("CPLQuietErrorHandler")

Whereas here

try:

    try:
        from osgeo import gdal, osr
    except ImportError:
        raise ImportError("GDAL is not installed. Install it with pip install GDAL")

# [SNIP]

    gdal.UseExceptions()
  1. One of the importss is wrapped in a try, but the others are not. I suggest dropping the try/except wrapping.
  2. The UseExceptions is great as that will become the default behavior of GDAL in the future. I suggest always doing that right after the imports each time there is a from osgeo import
  3. The gdal.PushErrorHandler calls are missing the matching gdal.PopErrorHandler(). You are welcome to use this context manager if it makes things easier. https://github.com/schwehr/gdal-autotest2/blob/577974592837fdfc18fd90b338cb657f1f2332bd/python/gcore/gcore_util.py#L50
@contextlib.contextmanager
def ErrorHandler(error_name):
  handler = gdal.PushErrorHandler(error_name)
  try:
    yield handler
  finally:
    gdal.PopErrorHandler()
@schwehr schwehr added the bug Something isn't working label Dec 31, 2023
@schwehr
Copy link
Collaborator Author

schwehr commented Jan 7, 2024

#1867 wasn't quite right. I will try to make a PR this week that does what I think was intended. Sorry that my explanation above wasn't clear.

@schwehr schwehr reopened this Jan 7, 2024
@schwehr schwehr self-assigned this Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant