Skip to content

Commit

Permalink
Merge pull request #10280 from rouault/swig_python_GDALAccess
Browse files Browse the repository at this point in the history
Python bindings: check validity of GDALAccess flag passed to gdal.Open()
  • Loading branch information
rouault committed Jul 6, 2024
2 parents b08dfac + 58dfa4a commit b3de5b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions autotest/gcore/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def test_basic_test_1():
pytest.fail("did not get expected error message, got %s" % gdal.GetLastErrorMsg())


def test_basic_test_invalid_open_flag():
with pytest.raises(Exception, match="invalid value for GDALAccess"):
gdal.Open("data/byte.tif", "invalid")

assert gdal.OF_RASTER not in (gdal.GA_ReadOnly, gdal.GA_Update)
with pytest.raises(Exception, match="invalid value for GDALAccess"):
gdal.Open("data/byte.tif", gdal.OF_RASTER)


@pytest.mark.skipif(sys.platform != "linux", reason="Incorrect platform")
def test_basic_test_strace_non_existing_file():

Expand Down
14 changes: 14 additions & 0 deletions swig/include/python/typemaps_python.i
Original file line number Diff line number Diff line change
Expand Up @@ -3065,6 +3065,20 @@ OBJECT_LIST_INPUT(GDALEDTComponentHS)
%#endif
}

%typemap(in) GDALAccess
{
// %typemap(in) GDALAccess
int val = 0;
int ecode = SWIG_AsVal_int($input, &val);
if (!SWIG_IsOK(ecode)) {
SWIG_exception_fail(SWIG_ArgError(ecode), "invalid value for GDALAccess");
}
if( val != GA_ReadOnly && val != GA_Update )
{
SWIG_exception_fail(SWIG_ValueError, "invalid value for GDALAccess");
}
$1 = static_cast<GDALAccess>(val);
}

%typemap(in) GDALRIOResampleAlg
{
Expand Down

0 comments on commit b3de5b7

Please sign in to comment.