Skip to content

Commit 6b7df90

Browse files
authored
Raise TypeError instead of panic on doing fetch_tile from striped TIFFs (#99)
Avoid panic when running `.fetch_tile()` on a striped TIFF (that does not have tiles).
1 parent 0884082 commit 6b7df90

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

python/src/tiff.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use async_tiff::metadata::{PrefetchBuffer, TiffMetadataReader};
44
use async_tiff::reader::AsyncFileReader;
55
use async_tiff::TIFF;
6-
use pyo3::exceptions::{PyFileNotFoundError, PyIndexError};
6+
use pyo3::exceptions::{PyFileNotFoundError, PyIndexError, PyTypeError};
77
use pyo3::prelude::*;
88
use pyo3::types::PyType;
99
use pyo3_async_runtimes::tokio::future_into_py;
@@ -69,7 +69,11 @@ impl PyTIFF {
6969
// TODO: avoid this clone; add Arc to underlying rust code?
7070
.clone();
7171
future_into_py(py, async move {
72-
let tile = ifd.fetch_tile(x, y, reader.as_ref()).await.unwrap();
72+
let tile = ifd
73+
.fetch_tile(x, y, reader.as_ref())
74+
.await
75+
.map_err(|err| PyTypeError::new_err(err.to_string()))?;
76+
7377
Ok(PyTile::new(tile))
7478
})
7579
}
@@ -91,7 +95,10 @@ impl PyTIFF {
9195
// TODO: avoid this clone; add Arc to underlying rust code?
9296
.clone();
9397
future_into_py(py, async move {
94-
let tiles = ifd.fetch_tiles(&x, &y, reader.as_ref()).await.unwrap();
98+
let tiles = ifd
99+
.fetch_tiles(&x, &y, reader.as_ref())
100+
.await
101+
.map_err(|err| PyTypeError::new_err(err.to_string()))?;
95102
let py_tiles = tiles.into_iter().map(PyTile::new).collect::<Vec<_>>();
96103
Ok(py_tiles)
97104
})

python/tests/test_exceptions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Unit tests to ensure that proper errors are raised instead of a panic.
3+
"""
4+
5+
import pytest
6+
from async_tiff.store import HTTPStore
7+
8+
from async_tiff import TIFF
9+
10+
11+
async def test_raise_typeerror_fetch_tile_striped_tiff():
12+
"""
13+
Ensure that a TypeError is raised when trying to fetch a tile from a striped TIFF.
14+
"""
15+
store = HTTPStore(url="https://github.com/")
16+
path = "OSGeo/gdal/raw/refs/tags/v3.11.0/autotest/gdrivers/data/gtiff/int8.tif"
17+
18+
tiff = await TIFF.open(path=path, store=store)
19+
assert len(tiff.ifds) >= 1
20+
21+
with pytest.raises(TypeError):
22+
await tiff.fetch_tile(0, 0, 0)

0 commit comments

Comments
 (0)