diff --git a/CHANGES.md b/CHANGES.md index 9af4fdf5..ee4a01f5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ - Fix decode error reading an sqlite file on windows (#568). - Fix wrong layername when creating .gpkg.zip file (#570). +- Fix segfault on providing an invalid value for `layer` in `read_info` (#564). ### Packaging diff --git a/pyogrio/_io.pyx b/pyogrio/_io.pyx index 7845310c..a4aed843 100644 --- a/pyogrio/_io.pyx +++ b/pyogrio/_io.pyx @@ -304,6 +304,10 @@ cdef OGRLayerH get_ogr_layer(GDALDatasetH ogr_dataset, layer) except NULL: elif isinstance(layer, int): ogr_layer = check_pointer(GDALDatasetGetLayer(ogr_dataset, layer)) + else: + raise ValueError( + f"'layer' parameter must be a str or int, got {type(layer)}" + ) # GDAL does not always raise exception messages in this case except NullPointerError: diff --git a/pyogrio/core.py b/pyogrio/core.py index 4ae00158..164c0029 100644 --- a/pyogrio/core.py +++ b/pyogrio/core.py @@ -237,9 +237,9 @@ def read_info( ---------- path_or_buffer : str, pathlib.Path, bytes, or file-like A dataset path or URI, raw buffer, or file-like object with a read method. - layer : [type], optional + layer : str or int, optional Name or index of layer in data source. Reads the first layer by default. - encoding : [type], optional (default: None) + encoding : str, optional (default: None) If present, will be used as the encoding for reading string values from the data source, unless encoding can be inferred directly from the data source. diff --git a/pyogrio/tests/test_core.py b/pyogrio/tests/test_core.py index 11af010e..3ac23e36 100644 --- a/pyogrio/tests/test_core.py +++ b/pyogrio/tests/test_core.py @@ -600,6 +600,11 @@ def test_read_info_unspecified_layer_warning(data_dir): read_info(data_dir / "sample.osm.pbf") +def test_read_info_invalid_layer(naturalearth_lowres): + with pytest.raises(ValueError, match="'layer' parameter must be a str or int"): + read_bounds(naturalearth_lowres, layer=["list_arg_is_invalid"]) + + def test_read_info_without_geometry(no_geometry_file): assert read_info(no_geometry_file)["total_bounds"] is None