From 8636de227e7b4aa552f7fc19215e5870f22f3d62 Mon Sep 17 00:00:00 2001 From: Tpt Date: Mon, 29 Aug 2022 16:20:15 +0200 Subject: [PATCH] Upgrades to PyO3 0.17 --- Cargo.lock | 21 +++++++++++---------- python/Cargo.toml | 2 +- python/src/io.rs | 46 ++++++++++++++++++++++----------------------- python/src/model.rs | 17 ++--------------- 4 files changed, 37 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed0a9bf8..2775856f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1004,13 +1004,14 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.16.6" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0220c44442c9b239dd4357aa856ac468a4f5e1f0df19ddb89b2522952eb4c6ca" +checksum = "12f72538a0230791398a0986a6518ebd88abc3fded89007b506ed072acc831e1" dependencies = [ "cfg-if", "indoc", "libc", + "memoffset", "parking_lot", "pyo3-build-config", "pyo3-ffi", @@ -1020,9 +1021,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.16.6" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c819d397859445928609d0ec5afc2da5204e0d0f73d6bf9e153b04e83c9cdc2" +checksum = "fc4cf18c20f4f09995f3554e6bcf9b09bd5e4d6b67c562fdfaafa644526ba479" dependencies = [ "once_cell", "target-lexicon", @@ -1030,9 +1031,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.16.6" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca882703ab55f54702d7bfe1189b41b0af10272389f04cae38fe4cd56c65f75f" +checksum = "a41877f28d8ebd600b6aa21a17b40c3b0fc4dfe73a27b6e81ab3d895e401b0e9" dependencies = [ "libc", "pyo3-build-config", @@ -1040,9 +1041,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.16.6" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "568749402955ad7be7bad9a09b8593851cd36e549ac90bfd44079cea500f3f21" +checksum = "2e81c8d4bcc2f216dc1b665412df35e46d12ee8d3d046b381aad05f1fcf30547" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -1052,9 +1053,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.16.6" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "611f64e82d98f447787e82b8e7b0ebc681e1eb78fc1252668b2c605ffb4e1eb8" +checksum = "85752a767ee19399a78272cc2ab625cd7d373b2e112b4b13db28de71fa892784" dependencies = [ "proc-macro2", "quote", diff --git a/python/Cargo.toml b/python/Cargo.toml index e0bbaadd..a9178356 100644 --- a/python/Cargo.toml +++ b/python/Cargo.toml @@ -17,4 +17,4 @@ doctest = false [dependencies] oxigraph = { version = "0.3.6", path="../lib", features = ["http_client"] } -pyo3 = { version = "0.16", features = ["extension-module", "abi3-py37"] } +pyo3 = { version = "0.17", features = ["extension-module", "abi3-py37"] } diff --git a/python/src/io.rs b/python/src/io.rs index da1c288f..645f94b5 100644 --- a/python/src/io.rs +++ b/python/src/io.rs @@ -286,37 +286,37 @@ pub(crate) struct PyIo(PyObject); impl Read for PyIo { fn read(&mut self, mut buf: &mut [u8]) -> io::Result { - let gil = Python::acquire_gil(); - let py = gil.python(); - let read = self - .0 - .call_method(py, "read", (buf.len(),), None) - .map_err(to_io_err)?; - let bytes = read - .extract::<&[u8]>(py) - .or_else(|e| read.extract::<&str>(py).map(|s| s.as_bytes()).or(Err(e))) - .map_err(to_io_err)?; - buf.write_all(bytes)?; - Ok(bytes.len()) + Python::with_gil(|py| { + let read = self + .0 + .call_method(py, "read", (buf.len(),), None) + .map_err(to_io_err)?; + let bytes = read + .extract::<&[u8]>(py) + .or_else(|e| read.extract::<&str>(py).map(|s| s.as_bytes()).or(Err(e))) + .map_err(to_io_err)?; + buf.write_all(bytes)?; + Ok(bytes.len()) + }) } } impl Write for PyIo { fn write(&mut self, buf: &[u8]) -> io::Result { - let gil = Python::acquire_gil(); - let py = gil.python(); - self.0 - .call_method(py, "write", (PyBytes::new(py, buf),), None) - .map_err(to_io_err)? - .extract::(py) - .map_err(to_io_err) + Python::with_gil(|py| { + self.0 + .call_method(py, "write", (PyBytes::new(py, buf),), None) + .map_err(to_io_err)? + .extract::(py) + .map_err(to_io_err) + }) } fn flush(&mut self) -> io::Result<()> { - let gil = Python::acquire_gil(); - let py = gil.python(); - self.0.call_method(py, "flush", (), None)?; - Ok(()) + Python::with_gil(|py| { + self.0.call_method(py, "flush", (), None)?; + Ok(()) + }) } } diff --git a/python/src/model.rs b/python/src/model.rs index 5bc38d10..89f8c8af 100644 --- a/python/src/model.rs +++ b/python/src/model.rs @@ -97,7 +97,7 @@ impl PyNamedNode { fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult { if let Ok(other) = other.downcast::>() { - Ok(eq_ord_compare(self, &other.borrow(), op)) + Ok(op.matches(self.cmp(&other.borrow()))) } else if PyBlankNode::is_type_of(other) || PyLiteral::is_type_of(other) || PyDefaultGraph::is_type_of(other) @@ -831,9 +831,7 @@ impl PyQuad { 4 } - fn __getitem__(&self, input: usize) -> PyResult { - let gil = Python::acquire_gil(); - let py = gil.python(); + fn __getitem__(&self, input: usize, py: Python<'_>) -> PyResult { match input { 0 => Ok(PySubject::from(self.inner.subject.clone()).into_py(py)), 1 => Ok(PyNamedNode::from(self.inner.predicate.clone()).into_py(py)), @@ -1126,17 +1124,6 @@ fn eq_compare_other_type(op: CompareOp) -> PyResult { } } -fn eq_ord_compare(a: &T, b: &T, op: CompareOp) -> bool { - match op { - CompareOp::Lt => a < b, - CompareOp::Le => a <= b, - CompareOp::Eq => a == b, - CompareOp::Ne => a != b, - CompareOp::Gt => a > b, - CompareOp::Ge => a >= b, - } -} - fn hash(t: &impl Hash) -> u64 { let mut s = DefaultHasher::new(); t.hash(&mut s);