]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Python 2.0 added a warning on ByteStorage, so using UntypedStorage (#196)
authorNicolas Patry <patry.nicolas@protonmail.com>
Fri, 17 Mar 2023 14:56:06 +0000 (15:56 +0100)
committerGitHub <noreply@github.com>
Fri, 17 Mar 2023 14:56:06 +0000 (15:56 +0100)
* Python 2.0 added a warning on ByteStorage, so using UntypedStorage

directly for that version.

Small error in their docs, `size` got renamed `nbytes`.

* Remove unused work.

bindings/python/src/lib.rs

index 0e4624d5213bb096156e9d88e6b78dd0f14f1d4d..7c01cba65867bd69522a26be293d3396a79de3d9 100644 (file)
@@ -371,10 +371,15 @@ impl Open {
                     let py_filename: PyObject = filename.into_py(py);
                     let size: PyObject = buffer.len().into_py(py);
                     let shared: PyObject = false.into_py(py);
-                    let kwargs = [(intern!(py, "shared"), shared), (intern!(py, "size"), size)]
-                        .into_py_dict(py);
+                    let (size_name, storage_name) = if version >= Version::new(2, 0, 0) {
+                        (intern!(py, "nbytes"), intern!(py, "UntypedStorage"))
+                    } else {
+                        (intern!(py, "size"), intern!(py, "ByteStorage"))
+                    };
+                    let kwargs =
+                        [(intern!(py, "shared"), shared), (size_name, size)].into_py_dict(py);
                     let storage = module
-                        .getattr(intern!(py, "ByteStorage"))?
+                        .getattr(storage_name)?
                         .getattr(intern!(py, "from_file"))?
                         .call((py_filename,), Some(kwargs))?;