Upgrades to latest pyo3 version

pull/173/head
Tpt 3 years ago
parent 7cdabe9417
commit ad4dd2832e
  1. 2
      lib/Cargo.toml
  2. 1
      lib/benches/store.rs
  3. 2
      python/Cargo.toml
  4. 26
      python/src/io.rs
  5. 95
      python/src/model.rs
  6. 50
      python/src/sparql.rs
  7. 49
      python/src/store.rs

@ -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"

@ -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),

@ -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]

@ -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<BufReader<PyFileLike>>,
}
#[pyproto]
impl PyIterProtocol for PyTripleReader {
fn __iter__(slf: PyRefMut<Self>) -> Py<Self> {
#[pymethods]
impl PyTripleReader {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyTriple>> {
slf.inner
fn __next__(&mut self) -> PyResult<Option<PyTriple>> {
self.inner
.next()
.map(|q| Ok(q.map_err(map_io_err)?.into()))
.transpose()
@ -168,14 +168,14 @@ pub struct PyQuadReader {
inner: QuadReader<BufReader<PyFileLike>>,
}
#[pyproto]
impl PyIterProtocol for PyQuadReader {
fn __iter__(slf: PyRefMut<Self>) -> Py<Self> {
#[pymethods]
impl PyQuadReader {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyQuad>> {
slf.inner
fn __next__(&mut self) -> PyResult<Option<PyQuad>> {
self.inner
.next()
.map(|q| Ok(q.map_err(map_io_err)?.into()))
.transpose()

@ -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<bool> {
if let Ok(other) = other.downcast::<PyCell<PyNamedNode>>() {
if let Ok(other) = other.downcast::<PyCell<Self>>() {
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<bool> {
if let Ok(other) = other.downcast::<PyCell<PyBlankNode>>() {
if let Ok(other) = other.downcast::<PyCell<Self>>() {
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<bool> {
if let Ok(other) = other.downcast::<PyCell<PyLiteral>>() {
if let Ok(other) = other.downcast::<PyCell<Self>>() {
eq_compare(self, &other.borrow(), op)
} else if PyNamedNode::is_type_of(other)
|| PyBlankNode::is_type_of(other)
@ -376,22 +367,19 @@ impl From<PyDefaultGraph> 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 {
"<DefaultGraph>"
}
@ -400,7 +388,7 @@ impl PyObjectProtocol for PyDefaultGraph {
}
fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<bool> {
if let Ok(other) = other.downcast::<PyCell<PyDefaultGraph>>() {
if let Ok(other) = other.downcast::<PyCell<Self>>() {
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<Self>, op: CompareOp) -> PyResult<bool> {
eq_compare(self, &other.borrow(), op)
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
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<Self>) -> 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<Self>, op: CompareOp) -> PyResult<bool> {
eq_compare(self, &other.borrow(), op)
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
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<Self>) -> 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<Self>, op: CompareOp) -> PyResult<bool> {
eq_compare(self, &other.borrow(), op)
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
eq_compare(self, other, op)
}
}
@ -1233,14 +1200,14 @@ pub struct TripleComponentsIter {
inner: IntoIter<Term>,
}
#[pyproto]
impl PyIterProtocol for TripleComponentsIter {
fn __iter__(slf: PyRefMut<Self>) -> Py<Self> {
#[pymethods]
impl TripleComponentsIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> Option<PyTerm> {
slf.inner.next().map(PyTerm::from)
fn __next__(&mut self) -> Option<PyTerm> {
self.inner.next().map(PyTerm::from)
}
}
@ -1249,18 +1216,18 @@ pub struct QuadComponentsIter {
inner: IntoIter<Option<Term>>,
}
#[pyproto]
impl PyIterProtocol for QuadComponentsIter {
fn __iter__(slf: PyRefMut<Self>) -> Py<Self> {
#[pymethods]
impl QuadComponentsIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> Option<PyObject> {
slf.inner.next().map(move |t| {
fn __next__(&mut self, py: Python<'_>) -> Option<PyObject> {
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)
}
})
}

@ -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("<QuerySolution");
@ -104,10 +101,7 @@ impl PyObjectProtocol for PyQuerySolution {
buffer.push('>');
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<Self>) -> 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<Option<Term>>,
}
#[pyproto]
impl PyIterProtocol for SolutionValueIter {
fn __iter__(slf: PyRefMut<Self>) -> Py<Self> {
#[pymethods]
impl SolutionValueIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> Option<Option<PyTerm>> {
slf.inner.next().map(|v| v.map(PyTerm::from))
fn __next__(&mut self) -> Option<Option<PyTerm>> {
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<Self>) -> Py<Self> {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyQuerySolution>> {
Ok(slf
fn __next__(&mut self) -> PyResult<Option<PyQuerySolution>> {
Ok(self
.inner
.next()
.transpose()
@ -218,14 +206,14 @@ pub struct PyQueryTriples {
inner: QueryTripleIter,
}
#[pyproto]
impl PyIterProtocol for PyQueryTriples {
fn __iter__(slf: PyRefMut<Self>) -> Py<Self> {
#[pymethods]
impl PyQueryTriples {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyTriple>> {
Ok(slf
fn __next__(&mut self) -> PyResult<Option<PyTriple>> {
Ok(self
.inner
.next()
.transpose()

@ -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<bool> {
Ok(!self.inner.is_empty()?)
}
}
#[pyproto]
impl PySequenceProtocol for PyStore {
fn __len__(&self) -> PyResult<usize> {
Ok(self.inner.len()?)
}
@ -447,13 +441,10 @@ impl PySequenceProtocol for PyStore {
fn __contains__(&self, quad: PyQuad) -> PyResult<bool> {
self.inner.contains(&quad).map_err(map_io_err)
}
}
#[pyproto]
impl PyIterProtocol for PyStore {
fn __iter__(slf: PyRef<Self>) -> 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<Self>) -> Py<Self> {
#[pymethods]
impl QuadIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyQuad>> {
slf.inner
fn __next__(&mut self) -> PyResult<Option<PyQuad>> {
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<Self>) -> Py<Self> {
#[pymethods]
impl GraphNameIter {
fn __iter__(slf: PyRef<'_, Self>) -> Py<Self> {
slf.into()
}
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyNamedOrBlankNode>> {
slf.inner
fn __next__(&mut self) -> PyResult<Option<PyNamedOrBlankNode>> {
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

Loading…
Cancel
Save