From: Nicolas Patry Date: Thu, 19 Jan 2023 13:30:56 +0000 (+0100) Subject: Fixing the fact that the context manager wasn't properly cleaning up after itself... X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=a4042652348df70ddac5ade96698296365a42073;p=safetensors.git Fixing the fact that the context manager wasn't properly cleaning up after itself. (#166) * Adding failing test for windows. * This should fail. * Fixing Windows issue by actually dropping the rust resources. * Making the rust module private. --- diff --git a/bindings/python/py_src/safetensors/__init__.py b/bindings/python/py_src/safetensors/__init__.py index 3615460..6b98397 100644 --- a/bindings/python/py_src/safetensors/__init__.py +++ b/bindings/python/py_src/safetensors/__init__.py @@ -1,4 +1,20 @@ __version__ = "0.2.9" # Re-export this -from .safetensors_rust import safe_open # noqa: F401 +from ._safetensors_rust import safe_open as rust_open, serialize, serialize_file, deserialize, SafetensorError + + +class safe_open: + def __init__(self, *args, **kwargs): + self.args = args + self.kwargs = kwargs + + def __getattr__(self, __name: str): + return getattr(self.f, __name) + + def __enter__(self): + self.f = rust_open(*self.args, **self.kwargs) + return self + + def __exit__(self, type, value, traceback): + del self.f diff --git a/bindings/python/py_src/safetensors/numpy.py b/bindings/python/py_src/safetensors/numpy.py index 8a4c661..bae82b8 100644 --- a/bindings/python/py_src/safetensors/numpy.py +++ b/bindings/python/py_src/safetensors/numpy.py @@ -4,7 +4,7 @@ from typing import Dict, Optional, Union import numpy as np -from .safetensors_rust import deserialize, safe_open, serialize, serialize_file +from safetensors import deserialize, safe_open, serialize, serialize_file def save(tensor_dict: Dict[str, np.ndarray], metadata: Optional[Dict[str, str]] = None) -> bytes: diff --git a/bindings/python/py_src/safetensors/torch.py b/bindings/python/py_src/safetensors/torch.py index 110f326..1df67f3 100644 --- a/bindings/python/py_src/safetensors/torch.py +++ b/bindings/python/py_src/safetensors/torch.py @@ -5,7 +5,7 @@ from typing import Any, Dict, Optional, Union import torch -from .safetensors_rust import deserialize, safe_open, serialize, serialize_file +from safetensors import deserialize, safe_open, serialize, serialize_file def save(tensors: Dict[str, torch.Tensor], metadata: Optional[Dict[str, str]] = None) -> bytes: diff --git a/bindings/python/setup.py b/bindings/python/setup.py index eed0e2c..ea2c6c3 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -59,7 +59,7 @@ setup( author_email="", url="https://github.com/huggingface/safetensors", license="Apache License 2.0", - rust_extensions=[RustExtension("safetensors.safetensors_rust", binding=Binding.PyO3, debug=False)], + rust_extensions=[RustExtension("safetensors._safetensors_rust", binding=Binding.PyO3, debug=False)], extras_require=extras, classifiers=[ "Development Status :: 5 - Production/Stable", diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 19a885a..58a6e3d 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -1021,7 +1021,7 @@ pyo3::create_exception!( /// A Python module implemented in Rust. #[pymodule] -fn safetensors_rust(py: Python, m: &PyModule) -> PyResult<()> { +fn _safetensors_rust(py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(serialize, m)?)?; m.add_function(wrap_pyfunction!(serialize_file, m)?)?; m.add_function(wrap_pyfunction!(deserialize, m)?)?; diff --git a/bindings/python/tests/test_pt_comparison.py b/bindings/python/tests/test_pt_comparison.py index 8c5c3ce..38f4f38 100644 --- a/bindings/python/tests/test_pt_comparison.py +++ b/bindings/python/tests/test_pt_comparison.py @@ -2,7 +2,7 @@ import unittest import torch -from safetensors.safetensors_rust import safe_open +from safetensors import safe_open from safetensors.torch import load, load_file, save, save_file diff --git a/bindings/python/tests/test_simple.py b/bindings/python/tests/test_simple.py index 2b6497e..e05552b 100644 --- a/bindings/python/tests/test_simple.py +++ b/bindings/python/tests/test_simple.py @@ -6,7 +6,7 @@ import numpy as np import torch from safetensors.numpy import load, load_file, save, save_file -from safetensors.safetensors_rust import SafetensorError, serialize +from safetensors import safe_open, SafetensorError, serialize from safetensors.torch import load_file as load_file_pt from safetensors.torch import save_file as save_file_pt @@ -59,6 +59,20 @@ class TestCase(unittest.TestCase): os.remove(Path("./out.safetensors")) +class WindowsTestCase(unittest.TestCase): + def test_get_correctly_dropped(self): + tensors = { + "a": torch.zeros((2, 2)), + "b": torch.zeros((2, 3), dtype=torch.uint8), + } + save_file_pt(tensors, "./out.safetensors") + with safe_open("./out.safetensors", framework="pt") as f: + pass + + with open("./out.safetensors", "w") as g: + g.write("something") + + class ReadmeTestCase(unittest.TestCase): def assertTensorEqual(self, tensors1, tensors2, equality_fn): self.assertEqual(tensors1.keys(), tensors2.keys(), "tensor keys don't match")