]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Fixing error message for FileNotFound. (#175)
authorNicolas Patry <patry.nicolas@protonmail.com>
Mon, 6 Feb 2023 11:05:40 +0000 (12:05 +0100)
committerGitHub <noreply@github.com>
Mon, 6 Feb 2023 11:05:40 +0000 (12:05 +0100)
* Fixing error message for FileNotFound.

* Old PR readme fix.

README.md
bindings/python/src/lib.rs
bindings/python/tests/test_simple.py

index 7d7725a6469eb5b6bef4f00a122797e85e38cb1b..f545dfd37a4550ba6f91bc266488e9cbe43e9734 100644 (file)
--- 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
index c1ab974757f0d03e683092365b44813f99698afd..e007abcab4dc5c0d294f7bd2aad0bb7850efd0cf 100644 (file)
@@ -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<Device>) -> PyResult<Self> {
-        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 {
index 1be7f3c5e15d062cfe773ec98447327b94edcee7..1f1122ecfeea3b3400a98b9d1485c3155095dbbb 100644 (file)
@@ -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")