Skip to content

Commit

Permalink
Bump pyo3 v0.23.3 (#202)
Browse files Browse the repository at this point in the history
* Bump pyo3 v0.23.3

* Drop PyPy 3.8
  • Loading branch information
davidbrochart authored Dec 13, 2024
1 parent a15dc9b commit fb7946a
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 328 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
- '3.11'
- '3.12'
- '3.13'
- 'pypy3.8'
- 'pypy3.9'
- 'pypy3.10'

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ crate-type = ["cdylib"]
yrs = "0.21.3"

[dependencies.pyo3]
version = "0.22.5"
version = "0.23.3"
89 changes: 42 additions & 47 deletions src/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use pyo3::prelude::*;
use pyo3::IntoPyObjectExt;
use pyo3::exceptions::{PyValueError, PyTypeError};
use pyo3::types::{PyList, PyString};
use yrs::{
Expand Down Expand Up @@ -50,36 +51,36 @@ impl Array {
}
}

fn insert_text_prelim(&self, txn: &mut Transaction, index: u32) -> PyResult<PyObject> {
fn insert_text_prelim(&self, txn: &mut Transaction, index: u32) -> PyResult<Text> {
let mut _t = txn.transaction();
let mut t = _t.as_mut().unwrap().as_mut();
let integrated = self.array.insert(&mut t, index, TextPrelim::new(""));
let shared = Text::from(integrated);
Python::with_gil(|py| { Ok(shared.into_py(py)) })
Ok(shared)
}

fn insert_array_prelim(&self, txn: &mut Transaction, index: u32) -> PyResult<PyObject> {
fn insert_array_prelim(&self, txn: &mut Transaction, index: u32) -> PyResult<Array> {
let mut _t = txn.transaction();
let mut t = _t.as_mut().unwrap().as_mut();
let integrated = self.array.insert(&mut t, index, ArrayPrelim::default());
let shared = Array::from(integrated);
Python::with_gil(|py| { Ok(shared.into_py(py)) })
Ok(shared)
}

fn insert_map_prelim(&self, txn: &mut Transaction, index: u32) -> PyResult<PyObject> {
fn insert_map_prelim(&self, txn: &mut Transaction, index: u32) -> PyResult<Map> {
let mut _t = txn.transaction();
let mut t = _t.as_mut().unwrap().as_mut();
let integrated = self.array.insert(&mut t, index, MapPrelim::default());
let shared = Map::from(integrated);
Python::with_gil(|py| { Ok(shared.into_py(py)) })
Ok(shared)
}

fn insert_xmlfragment_prelim(&self, txn: &mut Transaction, index: u32) -> PyResult<PyObject> {
fn insert_xmlfragment_prelim(&self, txn: &mut Transaction, index: u32) -> PyResult<XmlFragment> {
let mut _t = txn.transaction();
let mut t = _t.as_mut().unwrap().as_mut();
let integrated = self.array.insert(&mut t, index, XmlFragmentPrelim::default());
let shared = XmlFragment::from(integrated);
Python::with_gil(|py| { Ok(shared.into_py(py)) })
Ok(shared)
}

fn insert_xmlelement_prelim(&self, _txn: &mut Transaction, _index: u32) -> PyResult<PyObject> {
Expand Down Expand Up @@ -114,25 +115,25 @@ impl Array {
Ok(())
}

fn get(&self, txn: &mut Transaction, index: u32) -> PyResult<PyObject> {
fn get<'py>(&self, py: Python<'py>, txn: &mut Transaction, index: u32) -> PyResult<Bound<'py, PyAny>> {
let mut t0 = txn.transaction();
let t1 = t0.as_mut().unwrap();
let t = t1.as_ref();
let v = self.array.get(t, index);
if v == None {
Err(PyValueError::new_err("Index error"))
} else {
Python::with_gil(|py| { Ok(v.unwrap().into_py(py)) })
Ok(v.unwrap().into_py(py))
}
}

fn to_json(&mut self, txn: &mut Transaction) -> PyObject {
fn to_json<'py>(&mut self, py: Python<'py>, txn: &mut Transaction) -> Bound<'py, PyString> {
let mut t0 = txn.transaction();
let t1 = t0.as_mut().unwrap();
let t = t1.as_ref();
let mut s = String::new();
self.array.to_json(t).to_json(&mut s);
Python::with_gil(|py| PyString::new_bound(py, s.as_str()).into())
PyString::new(py, s.as_str())
}

pub fn observe(&mut self, py: Python<'_>, f: PyObject) -> PyResult<Py<Subscription>> {
Expand All @@ -153,7 +154,7 @@ impl Array {
let sub = self.array
.observe_deep(move |txn, events| {
Python::with_gil(|py| {
let events = events_into_py(txn, events);
let events = events_into_py(py, txn, events);
if let Err(err) = f.call1(py, (events,)) {
err.restore(py)
}
Expand All @@ -178,19 +179,14 @@ impl ArrayEvent {
pub fn new(event: &_ArrayEvent, txn: &TransactionMut) -> Self {
let event = event as *const _ArrayEvent;
let txn = unsafe { std::mem::transmute::<&TransactionMut, &TransactionMut<'static>>(txn) };
let mut array_event = ArrayEvent {
let array_event = ArrayEvent {
event,
txn,
target: None,
delta: None,
path: None,
transaction: None,
};
Python::with_gil(|py| {
array_event.target(py);
array_event.path(py);
array_event.delta(py);
});
array_event
}

Expand All @@ -206,55 +202,54 @@ impl ArrayEvent {
#[pymethods]
impl ArrayEvent {
#[getter]
pub fn transaction(&mut self, py: Python<'_>) -> PyObject {
pub fn transaction<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyAny> {
if let Some(transaction) = &self.transaction {
transaction.clone_ref(py)
transaction.clone_ref(py).into_bound(py)
} else {
let transaction: PyObject = Transaction::from(self.txn()).into_py(py);
let res = transaction.clone_ref(py);
self.transaction = Some(transaction);
res
let transaction = Transaction::from(self.txn()).into_bound_py_any(py).unwrap();
self.transaction = Some(transaction.clone().unbind());
transaction
}
}

#[getter]
pub fn target(&mut self, py: Python<'_>) -> PyObject {
pub fn target<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyAny> {
if let Some(target) = &self.target {
target.clone_ref(py)
target.clone_ref(py).into_bound(py)
} else {
let target: PyObject = Array::from(self.event().target().clone()).into_py(py);
let res = target.clone_ref(py);
self.target = Some(target);
res
let target = Array::from(self.event().target().clone()).into_bound_py_any(py).unwrap();
self.target = Some(target.clone().unbind());
target
}
}

#[getter]
pub fn path(&mut self, py: Python<'_>) -> PyObject {
pub fn path<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyAny> {
if let Some(path) = &self.path {
path.clone_ref(py)
path.clone_ref(py).into_bound(py)
} else {
let path: PyObject = self.event().path().into_py(py);
let res = path.clone_ref(py);
self.path = Some(path);
res
let path = self.event().path().into_py(py);
self.path = Some(path.clone().unbind());
path
}
}

#[getter]
pub fn delta(&mut self, py: Python<'_>) -> PyObject {
pub fn delta<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyAny> {
if let Some(delta) = &self.delta {
delta.clone_ref(py)
delta.clone_ref(py).into_bound(py)
} else {
let delta: PyObject = {
let delta = self.event().delta(self.txn()).iter().map(|change| {
change.clone().into_py(py)
});
PyList::new_bound(py, delta).into()
let delta = {
let delta =
self.event()
.delta(self.txn())
.into_iter()
.map(|d| d.clone().into_py(py));
delta
};
let res = delta.clone_ref(py);
self.delta = Some(delta);
res
let delta = PyList::new(py, delta).unwrap().into_bound_py_any(py).unwrap();
self.delta = Some(delta.clone().unbind());
delta
}
}

Expand Down
84 changes: 40 additions & 44 deletions src/doc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use pyo3::prelude::*;
use pyo3::IntoPyObjectExt;
use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::types::{PyBytes, PyDict, PyLong, PyList};
use pyo3::types::{PyBytes, PyDict, PyInt, PyList};
use yrs::{
Doc as _Doc, ReadTxn, StateVector, SubdocsEvent as _SubdocsEvent, Transact, TransactionCleanupEvent, TransactionMut, Update
};
Expand Down Expand Up @@ -35,7 +36,7 @@ impl Doc {
let doc = _Doc::new();
return Doc { doc };
}
let id: u64 = client_id.downcast::<PyLong>().unwrap().extract().unwrap();
let id: u64 = client_id.downcast::<PyInt>().unwrap().extract().unwrap();
let doc = _Doc::with_client_id(id);
Doc { doc }
}
Expand Down Expand Up @@ -90,7 +91,7 @@ impl Doc {
let txn = self.doc.transact_mut();
let state = txn.state_vector().encode_v1();
drop(txn);
Python::with_gil(|py| PyBytes::new_bound(py, &state).into())
Python::with_gil(|py| PyBytes::new(py, &state).into())
}

fn get_update(&mut self, state: &Bound<'_, PyBytes>) -> PyResult<PyObject> {
Expand All @@ -99,7 +100,7 @@ impl Doc {
let Ok(state_vector) = StateVector::decode_v1(&state) else { return Err(PyValueError::new_err("Cannot decode state")) };
let update = txn.encode_diff_v1(&state_vector);
drop(txn);
let bytes: PyObject = Python::with_gil(|py| PyBytes::new_bound(py, &update).into());
let bytes: PyObject = Python::with_gil(|py| PyBytes::new(py, &update).into());
Ok(bytes)
}

Expand All @@ -115,7 +116,7 @@ impl Doc {
let mut t0 = txn.transaction();
let t1 = t0.as_mut().unwrap();
let t = t1.as_ref();
let result = PyDict::new_bound(py);
let result = PyDict::new(py);
for (k, v) in t.root_refs() {
result.set_item(k, v.into_py(py)).unwrap();
}
Expand Down Expand Up @@ -143,7 +144,7 @@ impl Doc {
let sub = self.doc
.observe_subdocs(move |_, event| {
Python::with_gil(|py| {
let event = SubdocsEvent::new(event);
let event = SubdocsEvent::new(py, event);
if let Err(err) = f.call1(py, (event,)) {
err.restore(py)
}
Expand All @@ -159,10 +160,10 @@ impl Doc {
pub struct TransactionEvent {
event: *const TransactionCleanupEvent,
txn: *const TransactionMut<'static>,
before_state: Option<PyObject>,
after_state: Option<PyObject>,
delete_set: Option<PyObject>,
update: Option<PyObject>,
before_state: Option<Py<PyBytes>>,
after_state: Option<Py<PyBytes>>,
delete_set: Option<Py<PyBytes>>,
update: Option<Py<PyBytes>>,
transaction: Option<PyObject>,
}

Expand Down Expand Up @@ -194,66 +195,61 @@ impl TransactionEvent {
#[pymethods]
impl TransactionEvent {
#[getter]
pub fn transaction(&mut self, py: Python<'_>) -> PyObject {
pub fn transaction<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyAny> {
if let Some(transaction) = &self.transaction {
transaction.clone_ref(py)
transaction.clone_ref(py).into_bound(py)
} else {
let transaction: PyObject = Transaction::from(self.txn()).into_py(py);
let res = transaction.clone_ref(py);
self.transaction = Some(transaction);
res
let transaction = Transaction::from(self.txn()).into_bound_py_any(py).unwrap();
self.transaction = Some(transaction.clone().unbind());
transaction
}
}

#[getter]
pub fn before_state(&mut self, py: Python<'_>) -> PyObject {
pub fn before_state<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyBytes> {
if let Some(before_state) = &self.before_state {
before_state.clone_ref(py)
before_state.clone_ref(py).into_bound(py)
} else {
let before_state = self.event().before_state.encode_v1();
let before_state: PyObject = PyBytes::new_bound(py, &before_state).into();
let res = before_state.clone_ref(py);
self.before_state = Some(before_state);
res
let before_state = PyBytes::new(py, &before_state);
self.before_state = Some(before_state.clone().unbind());
before_state
}
}

#[getter]
pub fn after_state(&mut self, py: Python<'_>) -> PyObject {
pub fn after_state<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyBytes> {
if let Some(after_state) = &self.after_state {
after_state.clone_ref(py)
after_state.clone_ref(py).into_bound(py)
} else {
let after_state = self.event().after_state.encode_v1();
let after_state: PyObject = PyBytes::new_bound(py, &after_state).into();
let res = after_state.clone_ref(py);
self.after_state = Some(after_state);
res
let after_state = PyBytes::new(py, &after_state);
self.after_state = Some(after_state.clone().unbind());
after_state
}
}

#[getter]
pub fn delete_set(&mut self, py: Python<'_>) -> PyObject {
pub fn delete_set<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyBytes> {
if let Some(delete_set) = &self.delete_set {
delete_set.clone_ref(py)
delete_set.clone_ref(py).into_bound(py)
} else {
let delete_set = self.event().delete_set.encode_v1();
let delete_set: PyObject = PyBytes::new_bound(py, &delete_set).into();
let res = delete_set.clone_ref(py);
self.delete_set = Some(delete_set);
res
let delete_set = PyBytes::new(py, &delete_set);
self.delete_set = Some(delete_set.clone().unbind());
delete_set
}
}

#[getter]
pub fn update(&mut self, py: Python<'_>) -> PyObject {
pub fn update<'py>(&mut self, py: Python<'py>) -> Bound<'py, PyBytes> {
if let Some(update) = &self.update {
update.clone_ref(py)
update.clone_ref(py).into_bound(py)
} else {
let update = self.txn().encode_update_v1();
let update: PyObject = PyBytes::new_bound(py, &update).into();
let res = update.clone_ref(py);
self.update = Some(update);
res
let update = PyBytes::new(py, &update);
self.update = Some(update.clone().unbind());
update
}
}
}
Expand All @@ -266,13 +262,13 @@ pub struct SubdocsEvent {
}

impl SubdocsEvent {
fn new(event: &_SubdocsEvent) -> Self {
fn new<'py>(py: Python<'py>, event: &_SubdocsEvent) -> Self {
let added: Vec<String> = event.added().map(|d| d.guid().clone().to_string()).collect();
let added: PyObject = Python::with_gil(|py| PyList::new_bound(py, &added).into());
let added = PyList::new(py, added).unwrap().into_py_any(py).unwrap();
let removed: Vec<String> = event.removed().map(|d| d.guid().clone().to_string()).collect();
let removed: PyObject = Python::with_gil(|py| PyList::new_bound(py, &removed).into());
let removed = PyList::new(py, removed).unwrap().into_py_any(py).unwrap();
let loaded: Vec<String> = event.loaded().map(|d| d.guid().clone().to_string()).collect();
let loaded: PyObject = Python::with_gil(|py| PyList::new_bound(py, &loaded).into());
let loaded = PyList::new(py, loaded).unwrap().into_py_any(py).unwrap();
SubdocsEvent {
added,
removed,
Expand Down
Loading

0 comments on commit fb7946a

Please sign in to comment.