From: Nicolas Patry Date: Mon, 6 Feb 2023 11:05:40 +0000 (+0100) Subject: Fixing error message for FileNotFound. (#175) X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=3dc566856400ad2f3b854573327816a3360fa7ac;p=safetensors.git Fixing error message for FileNotFound. (#175) * Fixing error message for FileNotFound. * Old PR readme fix. --- diff --git a/README.md b/README.md index 7d7725a..f545dfd 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ necessary)? This is becoming increasingly important in the ML world. ### Notes - Zero-copy: No format is really zero-copy in ML, it needs to go from disk to RAM/GPU RAM (that takes time). Also - In PyTorch/numpy, you need a mutable buffer, and we don't really want to mutate a mmaped file, so 1 copy will be necessary if you modify an array (copy-on-write). That being said, zero-copy is achievable in Rust if it's wanted and safety can be guaranteed by some other means. + In PyTorch/numpy, you need a mutable buffer, and we don't really want to mutate a mmaped file, so 1 copy is really necessary to use the thing freely in user code. That being said, zero-copy is achievable in Rust if it's wanted and safety can be guaranteed by some other means. SafeTensors is not zero-copy for the header. The choice of JSON is pretty arbitrary, but since deserialization is <<< of the time required to load the actual tensor data and is readable I went that way, (also space is <<< to the tensor data). - Endianness: Little-endian. This can be modified later, but it feels really unecessary at the diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index c1ab974..e007abc 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -2,7 +2,7 @@ //! Dummy doc use libloading::{Library, Symbol}; use memmap2::{Mmap, MmapOptions}; -use pyo3::exceptions::PyException; +use pyo3::exceptions::{PyException, PyFileNotFoundError}; use pyo3::once_cell::GILOnceCell; use pyo3::prelude::*; use pyo3::types::IntoPyDict; @@ -476,7 +476,9 @@ struct Open { impl Open { fn new(filename: PathBuf, framework: Framework, device: Option) -> PyResult { - let file = File::open(&filename)?; + let file = File::open(&filename).map_err(|_| { + PyFileNotFoundError::new_err(format!("No such file or directory: {filename:?}")) + })?; let device = device.unwrap_or(Device::Cpu); if device != Device::Cpu && framework != Framework::Pytorch { diff --git a/bindings/python/tests/test_simple.py b/bindings/python/tests/test_simple.py index 1be7f3c..1f1122e 100644 --- a/bindings/python/tests/test_simple.py +++ b/bindings/python/tests/test_simple.py @@ -76,6 +76,14 @@ class WindowsTestCase(unittest.TestCase): g.write("something") +class ErrorsTestCase(unittest.TestCase): + def test_file_not_found(self): + with self.assertRaises(FileNotFoundError) as ctx: + with safe_open("notafile", framework="pt"): + pass + self.assertEqual(str(ctx.exception), 'No such file or directory: "notafile"') + + class ReadmeTestCase(unittest.TestCase): def assertTensorEqual(self, tensors1, tensors2, equality_fn): self.assertEqual(tensors1.keys(), tensors2.keys(), "tensor keys don't match")