From 76846573e0da9204bcaf06c590dc8f65d348ad73 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Thu, 1 May 2025 16:52:09 +1200 Subject: [PATCH 1/2] Raise FileNotFoundError instead of panic when opening non-existent files --- python/src/tiff.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/src/tiff.rs b/python/src/tiff.rs index 82f1351..d173aa8 100644 --- a/python/src/tiff.rs +++ b/python/src/tiff.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use async_tiff::metadata::{PrefetchBuffer, TiffMetadataReader}; use async_tiff::reader::AsyncFileReader; use async_tiff::TIFF; -use pyo3::exceptions::PyIndexError; +use pyo3::exceptions::{PyFileNotFoundError, PyIndexError}; use pyo3::prelude::*; use pyo3::types::PyType; use pyo3_async_runtimes::tokio::future_into_py; @@ -32,7 +32,9 @@ impl PyTIFF { let reader = store.into_async_file_reader(path); let cog_reader = future_into_py(py, async move { - let metadata_fetch = PrefetchBuffer::new(reader.clone(), prefetch).await.unwrap(); + let metadata_fetch = PrefetchBuffer::new(reader.clone(), prefetch) + .await + .map_err(|err| PyFileNotFoundError::new_err(err.to_string()))?; let mut metadata_reader = TiffMetadataReader::try_open(&metadata_fetch).await.unwrap(); let ifds = metadata_reader .read_all_ifds(&metadata_fetch) From 38b5bdb1a1fcd4a33f754f671b053974f64993ce Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Thu, 1 May 2025 16:54:35 +1200 Subject: [PATCH 2/2] Add test to check that FileNotFoundError is raised when file is missing --- python/tests/test_cog.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/python/tests/test_cog.py b/python/tests/test_cog.py index 577a0bb..afe9660 100644 --- a/python/tests/test_cog.py +++ b/python/tests/test_cog.py @@ -1,7 +1,10 @@ import async_tiff from time import time + +import pytest + from async_tiff import TIFF -from async_tiff.store import S3Store +from async_tiff.store import LocalStore, S3Store store = S3Store("sentinel-cogs", region="us-west-2", skip_signature=True) path = "sentinel-s2-l2a-cogs/12/S/UF/2022/6/S2B_12SUF_20220609_0_L2A/B04.tif" @@ -25,3 +28,12 @@ gkd.citation dir(gkd) + + +async def test_cog_missing_file(): + """ + Ensure that a FileNotFoundError is raised when passing in a missing file. + """ + store = LocalStore() + with pytest.raises(FileNotFoundError): + await TIFF.open(path="imaginary_file.tif", store=store)