Opened 6 years ago

Closed 5 years ago

#7145 closed defect (wontfix)

Python: VSI functions never throw exceptions

Reported by: cdestigter Owned by: hobu
Priority: normal Milestone: closed_because_of_github_migration
Component: PythonBindings Version: svn-trunk
Severity: normal Keywords:
Cc:

Description

With UseExceptions enabled I expected these to throw exceptions:

>>> from osgeo import gdal
>>> gdal.UseExceptions()
>>> gdal.GetUseExceptions()
1

# Neither of these throw exceptions
>>> gdal.VSIFOpenL('/does-not-exist', 'r')
>>> gdal.VSIStatL('/does-not-exist')

Similarly, some operations that should throw exceptions instead cause segfaults:

>>> f = gdal.VSIFOpenL('/vsicurl/http://example.com/', 'r')
>>> f
<Swig Object of type 'void *' at 0x7f98c0022960>

>>> gdal.VSIFCloseL(f)
0
>>> gdal.VSIFCloseL(f)
Segmentation fault (core dumped)
# This one's especially gnarly since VSIFOpenL returns None instead of throwing an exception
>>> gdal.VSIFReadL(1, 1, None)
Segmentation fault (core dumped)

I also tried to trap 'no such file' problems with PushErrorHandler, thinking I could perhaps find an error code somewhere and raise an exception myself, but my error handler never gets called.

def _printy(*args, **kwargs):
    print("This doesn't get called")


@contextmanager
def error_handler():
    gdal.PushErrorHandler(_printy)
    try:
        yield
    finally:
        gdal.PopErrorHandler()


with error_handler():
    gdal.VSIFOpenL('/does-not-exist')
    

(I did note though that this approach was unlikely to work even if the error handler did get called, since exceptions raised from error handlers don't propagate: #5186 )

Change History (2)

comment:1 by cdestigter, 6 years ago

Further to this, I've just discovered gdal.VSIFOpenExL(fn, mode, 1) and gdal.VSIGetLastErrorMsg() which might be a suitable solution for my use-case.

I'm used to things in Python throwing exceptions, but feel free to close this if you don't think that's necessary. Cheers :)

comment:2 by Even Rouault, 5 years ago

Milestone: closed_because_of_github_migration
Resolution: wontfix
Status: newclosed

This ticket has been automatically closed because Trac is no longer used for GDAL bug tracking, since the project has migrated to GitHub. If you believe this ticket is still valid, you may file it to https://github.com/OSGeo/gdal/issues if it is not already reported there.

Note: See TracTickets for help on using tickets.