From ad4dd2832e26284bec659ea6ae906045a492cd4a Mon Sep 17 00:00:00 2001 From: Tpt Date: Sat, 4 Dec 2021 08:35:19 +0100 Subject: [PATCH] Upgrades to latest pyo3 version --- lib/Cargo.toml | 2 +- lib/benches/store.rs | 1 + python/Cargo.toml | 2 +- python/src/io.rs | 26 ++++++------ python/src/model.rs | 95 +++++++++++++++----------------------------- python/src/sparql.rs | 50 +++++++++-------------- python/src/store.rs | 49 ++++++++++------------- 7 files changed, 86 insertions(+), 139 deletions(-) diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 053c9110..ae6147d3 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -37,7 +37,7 @@ rio_xml = "0.6" hex = "0.4" nom = "7" siphasher = "0.3" -lasso = "0.5" +lasso = "0.6" lazy_static = "1" sophia_api = { version = "0.7", optional = true } json-event-parser = "0.1" diff --git a/lib/benches/store.rs b/lib/benches/store.rs index a91f705a..e82ace72 100644 --- a/lib/benches/store.rs +++ b/lib/benches/store.rs @@ -185,6 +185,7 @@ fn read_data(file: &str) -> impl BufRead { BufReader::new(zstd::Decoder::new(File::open(file).unwrap()).unwrap()) } +#[allow(clippy::large_enum_variant)] #[derive(Clone)] enum Operation { Query(Query), diff --git a/python/Cargo.toml b/python/Cargo.toml index 951e8b27..aa57d34d 100644 --- a/python/Cargo.toml +++ b/python/Cargo.toml @@ -17,7 +17,7 @@ doctest = false [dependencies] oxigraph = {version = "0.3.0-dev", path="../lib", features = ["http_client"]} -pyo3 = {version = "0.14", features = ["extension-module", "abi3-py36"]} +pyo3 = {version = "0.15", features = ["extension-module", "abi3-py36"]} native-tls = {version = "0.2", features = ["vendored"]} [package.metadata.maturin] diff --git a/python/src/io.rs b/python/src/io.rs index b2e8ae5d..ee1ae909 100644 --- a/python/src/io.rs +++ b/python/src/io.rs @@ -1,3 +1,5 @@ +#![allow(clippy::needless_option_as_deref)] + use crate::model::{PyQuad, PyTriple}; use oxigraph::io::read::{QuadReader, TripleReader}; use oxigraph::io::{ @@ -7,9 +9,7 @@ use pyo3::exceptions::{PyIOError, PySyntaxError, PyValueError}; use pyo3::prelude::*; use pyo3::types::PyBytes; use pyo3::wrap_pyfunction; -use pyo3::PyIterProtocol; -use std::io; -use std::io::{BufReader, Read, Write}; +use std::io::{self, BufReader, Read, Write}; pub fn add_to_module(module: &PyModule) -> PyResult<()> { module.add_wrapped(wrap_pyfunction!(parse))?; @@ -149,14 +149,14 @@ pub struct PyTripleReader { inner: TripleReader>, } -#[pyproto] -impl PyIterProtocol for PyTripleReader { - fn __iter__(slf: PyRefMut) -> Py { +#[pymethods] +impl PyTripleReader { + fn __iter__(slf: PyRef<'_, Self>) -> Py { slf.into() } - fn __next__(mut slf: PyRefMut) -> PyResult> { - slf.inner + fn __next__(&mut self) -> PyResult> { + self.inner .next() .map(|q| Ok(q.map_err(map_io_err)?.into())) .transpose() @@ -168,14 +168,14 @@ pub struct PyQuadReader { inner: QuadReader>, } -#[pyproto] -impl PyIterProtocol for PyQuadReader { - fn __iter__(slf: PyRefMut) -> Py { +#[pymethods] +impl PyQuadReader { + fn __iter__(slf: PyRef<'_, Self>) -> Py { slf.into() } - fn __next__(mut slf: PyRefMut) -> PyResult> { - slf.inner + fn __next__(&mut self) -> PyResult> { + self.inner .next() .map(|q| Ok(q.map_err(map_io_err)?.into())) .transpose() diff --git a/python/src/model.rs b/python/src/model.rs index 5f5960d5..18fa08c3 100644 --- a/python/src/model.rs +++ b/python/src/model.rs @@ -3,7 +3,7 @@ use oxigraph::sparql::Variable; use pyo3::basic::CompareOp; use pyo3::exceptions::{PyIndexError, PyNotImplementedError, PyTypeError, PyValueError}; use pyo3::prelude::*; -use pyo3::{PyIterProtocol, PyMappingProtocol, PyObjectProtocol, PyTypeInfo}; +use pyo3::PyTypeInfo; use std::collections::hash_map::DefaultHasher; use std::hash::Hash; use std::hash::Hasher; @@ -80,10 +80,7 @@ impl PyNamedNode { fn value(&self) -> &str { self.inner.as_str() } -} -#[pyproto] -impl PyObjectProtocol for PyNamedNode { fn __str__(&self) -> String { self.inner.to_string() } @@ -99,7 +96,7 @@ impl PyObjectProtocol for PyNamedNode { } fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult { - if let Ok(other) = other.downcast::>() { + if let Ok(other) = other.downcast::>() { Ok(eq_ord_compare(self, &other.borrow(), op)) } else if PyBlankNode::is_type_of(other) || PyLiteral::is_type_of(other) @@ -188,10 +185,7 @@ impl PyBlankNode { fn value(&self) -> &str { self.inner.as_str() } -} -#[pyproto] -impl PyObjectProtocol for PyBlankNode { fn __str__(&self) -> String { self.inner.to_string() } @@ -207,7 +201,7 @@ impl PyObjectProtocol for PyBlankNode { } fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult { - if let Ok(other) = other.downcast::>() { + if let Ok(other) = other.downcast::>() { eq_compare(self, &other.borrow(), op) } else if PyNamedNode::is_type_of(other) || PyLiteral::is_type_of(other) @@ -327,10 +321,7 @@ impl PyLiteral { fn datatype(&self) -> PyNamedNode { self.inner.datatype().into_owned().into() } -} -#[pyproto] -impl PyObjectProtocol for PyLiteral { fn __str__(&self) -> String { self.inner.to_string() } @@ -346,7 +337,7 @@ impl PyObjectProtocol for PyLiteral { } fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult { - if let Ok(other) = other.downcast::>() { + if let Ok(other) = other.downcast::>() { eq_compare(self, &other.borrow(), op) } else if PyNamedNode::is_type_of(other) || PyBlankNode::is_type_of(other) @@ -376,22 +367,19 @@ impl From for GraphName { impl PyDefaultGraph { #[new] fn new() -> Self { - PyDefaultGraph {} + Self {} } #[getter] fn value(&self) -> &str { "" } -} -#[pyproto] -impl PyObjectProtocol for PyDefaultGraph { - fn __str__(&self) -> &'p str { + fn __str__(&self) -> &str { "DEFAULT" } - fn __repr__(&self) -> &'p str { + fn __repr__(&self) -> &str { "" } @@ -400,7 +388,7 @@ impl PyObjectProtocol for PyDefaultGraph { } fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult { - if let Ok(other) = other.downcast::>() { + if let Ok(other) = other.downcast::>() { eq_compare(self, &other.borrow(), op) } else if PyNamedNode::is_type_of(other) || PyBlankNode::is_type_of(other) @@ -616,10 +604,7 @@ impl PyTriple { fn object(&self) -> PyTerm { self.inner.object.clone().into() } -} -#[pyproto] -impl PyObjectProtocol for PyTriple { fn __str__(&self) -> String { self.inner.to_string() } @@ -634,13 +619,10 @@ impl PyObjectProtocol for PyTriple { hash(&self.inner) } - fn __richcmp__(&self, other: &PyCell, op: CompareOp) -> PyResult { - eq_compare(self, &other.borrow(), op) + fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult { + eq_compare(self, other, op) } -} -#[pyproto] -impl PyMappingProtocol<'p> for PyTriple { fn __len__(&self) -> usize { 3 } @@ -653,16 +635,13 @@ impl PyMappingProtocol<'p> for PyTriple { _ => Err(PyIndexError::new_err("A triple has only 3 elements")), } } -} -#[pyproto] -impl PyIterProtocol for PyTriple { - fn __iter__(slf: PyRef) -> TripleComponentsIter { + fn __iter__(&self) -> TripleComponentsIter { TripleComponentsIter { inner: vec![ - slf.inner.subject.clone().into(), - slf.inner.predicate.clone().into(), - slf.inner.object.clone(), + self.inner.subject.clone().into(), + self.inner.predicate.clone().into(), + self.inner.object.clone(), ] .into_iter(), } @@ -821,10 +800,7 @@ impl PyQuad { fn triple(&self) -> PyTriple { Triple::from(self.inner.clone()).into() } -} -#[pyproto] -impl PyObjectProtocol for PyQuad { fn __str__(&self) -> String { self.inner.to_string() } @@ -847,13 +823,10 @@ impl PyObjectProtocol for PyQuad { hash(&self.inner) } - fn __richcmp__(&self, other: &PyCell, op: CompareOp) -> PyResult { - eq_compare(self, &other.borrow(), op) + fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult { + eq_compare(self, other, op) } -} -#[pyproto] -impl PyMappingProtocol<'p> for PyQuad { fn __len__(&self) -> usize { 4 } @@ -869,11 +842,8 @@ impl PyMappingProtocol<'p> for PyQuad { _ => Err(PyIndexError::new_err("A quad has only 4 elements")), } } -} -#[pyproto] -impl PyIterProtocol for PyQuad { - fn __iter__(slf: PyRef) -> QuadComponentsIter { + fn __iter__(slf: PyRef<'_, Self>) -> QuadComponentsIter { QuadComponentsIter { inner: vec![ Some(slf.inner.subject.clone().into()), @@ -943,10 +913,7 @@ impl PyVariable { fn value(&self) -> &str { self.inner.as_str() } -} -#[pyproto] -impl PyObjectProtocol for PyVariable { fn __str__(&self) -> String { self.inner.to_string() } @@ -959,8 +926,8 @@ impl PyObjectProtocol for PyVariable { hash(&self.inner) } - fn __richcmp__(&self, other: &PyCell, op: CompareOp) -> PyResult { - eq_compare(self, &other.borrow(), op) + fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult { + eq_compare(self, other, op) } } @@ -1233,14 +1200,14 @@ pub struct TripleComponentsIter { inner: IntoIter, } -#[pyproto] -impl PyIterProtocol for TripleComponentsIter { - fn __iter__(slf: PyRefMut) -> Py { +#[pymethods] +impl TripleComponentsIter { + fn __iter__(slf: PyRef<'_, Self>) -> Py { slf.into() } - fn __next__(mut slf: PyRefMut) -> Option { - slf.inner.next().map(PyTerm::from) + fn __next__(&mut self) -> Option { + self.inner.next().map(PyTerm::from) } } @@ -1249,18 +1216,18 @@ pub struct QuadComponentsIter { inner: IntoIter>, } -#[pyproto] -impl PyIterProtocol for QuadComponentsIter { - fn __iter__(slf: PyRefMut) -> Py { +#[pymethods] +impl QuadComponentsIter { + fn __iter__(slf: PyRef<'_, Self>) -> Py { slf.into() } - fn __next__(mut slf: PyRefMut) -> Option { - slf.inner.next().map(move |t| { + fn __next__(&mut self, py: Python<'_>) -> Option { + self.inner.next().map(move |t| { if let Some(t) = t { - PyTerm::from(t).into_py(slf.py()) + PyTerm::from(t).into_py(py) } else { - PyDefaultGraph {}.into_py(slf.py()) + PyDefaultGraph {}.into_py(py) } }) } diff --git a/python/src/sparql.rs b/python/src/sparql.rs index b79fb756..abf98ce2 100644 --- a/python/src/sparql.rs +++ b/python/src/sparql.rs @@ -3,11 +3,8 @@ use crate::model::*; use oxigraph::model::Term; use oxigraph::sparql::*; use pyo3::exceptions::{PyRuntimeError, PySyntaxError, PyTypeError, PyValueError}; -use pyo3::prelude::{ - pyclass, pymethods, pyproto, FromPyObject, IntoPy, Py, PyAny, PyCell, PyErr, PyObject, PyRef, - PyRefMut, PyResult, Python, -}; -use pyo3::{PyIterProtocol, PyMappingProtocol, PyObjectProtocol}; +use pyo3::prelude::*; +use pyo3::{Py, PyRef}; use std::vec::IntoIter; pub fn parse_query( @@ -90,8 +87,8 @@ pub struct PyQuerySolution { inner: QuerySolution, } -#[pyproto] -impl PyObjectProtocol for PyQuerySolution { +#[pymethods] +impl PyQuerySolution { fn __repr__(&self) -> String { let mut buffer = String::new(); buffer.push_str("'); buffer } -} -#[pyproto] -impl PyMappingProtocol for PyQuerySolution { fn __len__(&self) -> usize { self.inner.len() } @@ -130,13 +124,10 @@ impl PyMappingProtocol for PyQuerySolution { ))) } } -} -#[pyproto] -impl PyIterProtocol for PyQuerySolution { - fn __iter__(slf: PyRef) -> SolutionValueIter { + fn __iter__(&self) -> SolutionValueIter { SolutionValueIter { - inner: slf + inner: self .inner .values() .map(|v| v.cloned()) @@ -151,14 +142,14 @@ pub struct SolutionValueIter { inner: IntoIter>, } -#[pyproto] -impl PyIterProtocol for SolutionValueIter { - fn __iter__(slf: PyRefMut) -> Py { +#[pymethods] +impl SolutionValueIter { + fn __iter__(slf: PyRef<'_, Self>) -> Py { slf.into() } - fn __next__(mut slf: PyRefMut) -> Option> { - slf.inner.next().map(|v| v.map(PyTerm::from)) + fn __next__(&mut self) -> Option> { + self.inner.next().map(|v| v.map(PyTerm::from)) } } @@ -189,16 +180,13 @@ impl PyQuerySolutions { .map(|v| v.clone().into()) .collect() } -} -#[pyproto] -impl PyIterProtocol for PyQuerySolutions { - fn __iter__(slf: PyRefMut) -> Py { + fn __iter__(slf: PyRef<'_, Self>) -> Py { slf.into() } - fn __next__(mut slf: PyRefMut) -> PyResult> { - Ok(slf + fn __next__(&mut self) -> PyResult> { + Ok(self .inner .next() .transpose() @@ -218,14 +206,14 @@ pub struct PyQueryTriples { inner: QueryTripleIter, } -#[pyproto] -impl PyIterProtocol for PyQueryTriples { - fn __iter__(slf: PyRefMut) -> Py { +#[pymethods] +impl PyQueryTriples { + fn __iter__(slf: PyRef<'_, Self>) -> Py { slf.into() } - fn __next__(mut slf: PyRefMut) -> PyResult> { - Ok(slf + fn __next__(&mut self) -> PyResult> { + Ok(self .inner .next() .transpose() diff --git a/python/src/store.rs b/python/src/store.rs index 5c7f5ddb..56e0aa92 100644 --- a/python/src/store.rs +++ b/python/src/store.rs @@ -1,3 +1,5 @@ +#![allow(clippy::needless_option_as_deref)] + use crate::io::{map_io_err, PyFileLike}; use crate::model::*; use crate::sparql::*; @@ -5,10 +7,8 @@ use oxigraph::io::{DatasetFormat, GraphFormat}; use oxigraph::model::GraphNameRef; use oxigraph::store::{self, Store}; use pyo3::exceptions::PyValueError; -use pyo3::prelude::{ - pyclass, pymethods, pyproto, Py, PyAny, PyObject, PyRef, PyRefMut, PyResult, Python, -}; -use pyo3::{PyIterProtocol, PyObjectProtocol, PySequenceProtocol}; +use pyo3::prelude::*; +use pyo3::{Py, PyRef}; use std::io::BufReader; /// Disk-based RDF store. @@ -425,10 +425,7 @@ impl PyStore { .map_err(map_io_err)?; Ok(()) } -} -#[pyproto] -impl PyObjectProtocol for PyStore { fn __str__(&self) -> String { self.inner.to_string() } @@ -436,10 +433,7 @@ impl PyObjectProtocol for PyStore { fn __bool__(&self) -> PyResult { Ok(!self.inner.is_empty()?) } -} -#[pyproto] -impl PySequenceProtocol for PyStore { fn __len__(&self) -> PyResult { Ok(self.inner.len()?) } @@ -447,13 +441,10 @@ impl PySequenceProtocol for PyStore { fn __contains__(&self, quad: PyQuad) -> PyResult { self.inner.contains(&quad).map_err(map_io_err) } -} -#[pyproto] -impl PyIterProtocol for PyStore { - fn __iter__(slf: PyRef) -> QuadIter { + fn __iter__(&self) -> QuadIter { QuadIter { - inner: slf.inner.iter(), + inner: self.inner.iter(), } } } @@ -463,14 +454,14 @@ pub struct QuadIter { inner: store::QuadIter, } -#[pyproto] -impl PyIterProtocol for QuadIter { - fn __iter__(slf: PyRefMut) -> Py { +#[pymethods] +impl QuadIter { + fn __iter__(slf: PyRef<'_, Self>) -> Py { slf.into() } - fn __next__(mut slf: PyRefMut) -> PyResult> { - slf.inner + fn __next__(&mut self) -> PyResult> { + self.inner .next() .map(|q| Ok(q.map_err(map_io_err)?.into())) .transpose() @@ -482,14 +473,14 @@ pub struct GraphNameIter { inner: store::GraphNameIter, } -#[pyproto] -impl PyIterProtocol for GraphNameIter { - fn __iter__(slf: PyRefMut) -> Py { +#[pymethods] +impl GraphNameIter { + fn __iter__(slf: PyRef<'_, Self>) -> Py { slf.into() } - fn __next__(mut slf: PyRefMut) -> PyResult> { - slf.inner + fn __next__(&mut self) -> PyResult> { + self.inner .next() .map(|q| Ok(q.map_err(map_io_err)?.into())) .transpose() @@ -511,23 +502,23 @@ pub fn extract_quads_pattern<'a>( if subject.is_none() { None } else { - Some(subject.try_into()?) + Some(TryFrom::try_from(subject)?) }, if predicate.is_none() { None } else { - Some(predicate.try_into()?) + Some(TryFrom::try_from(predicate)?) }, if object.is_none() { None } else { - Some(object.try_into()?) + Some(TryFrom::try_from(object)?) }, if let Some(graph_name) = graph_name { if graph_name.is_none() { None } else { - Some(graph_name.try_into()?) + Some(TryFrom::try_from(graph_name)?) } } else { None