Skip to content

Commit 66376be

Browse files
committed
Python: Upgrade to Pyo3 0.26
1 parent 13ce62d commit 66376be

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

api/python/slint/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ i-slint-backend-selector = { workspace = true }
4646
i-slint-core = { workspace = true }
4747
slint-interpreter = { workspace = true, features = ["default", "display-diagnostics", "internal"] }
4848
i-slint-compiler = { workspace = true }
49-
pyo3 = { version = "0.25", features = ["extension-module", "indexmap", "chrono", "abi3-py311"] }
49+
pyo3 = { version = "0.26", features = ["extension-module", "indexmap", "chrono", "abi3-py311"] }
5050
indexmap = { version = "2.1.0" }
5151
chrono = "0.4"
5252
spin_on = { workspace = true }

api/python/slint/interpreter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl ComponentInstance {
409409
))
410410
}
411411

412-
fn set_callback(&self, name: &str, callable: PyObject) -> Result<(), PySetCallbackError> {
412+
fn set_callback(&self, name: &str, callable: Py<PyAny>) -> Result<(), PySetCallbackError> {
413413
let rust_cb = self.callbacks.register(name.to_string(), callable);
414414
Ok(self.instance.set_callback(name, rust_cb)?.into())
415415
}
@@ -418,7 +418,7 @@ impl ComponentInstance {
418418
&mut self,
419419
global_name: &str,
420420
callback_name: &str,
421-
callable: PyObject,
421+
callable: Py<PyAny>,
422422
) -> Result<(), PySetCallbackError> {
423423
let rust_cb = self
424424
.global_callbacks
@@ -458,12 +458,12 @@ impl ComponentInstance {
458458
}
459459

460460
struct GcVisibleCallbacks {
461-
callables: Rc<RefCell<HashMap<String, PyObject>>>,
461+
callables: Rc<RefCell<HashMap<String, Py<PyAny>>>>,
462462
type_collection: TypeCollection,
463463
}
464464

465465
impl GcVisibleCallbacks {
466-
fn register(&self, name: String, callable: PyObject) -> impl Fn(&[Value]) -> Value + 'static {
466+
fn register(&self, name: String, callable: Py<PyAny>) -> impl Fn(&[Value]) -> Value + 'static {
467467
self.callables.borrow_mut().insert(name.clone(), callable);
468468

469469
let callables = self.callables.clone();
@@ -472,7 +472,7 @@ impl GcVisibleCallbacks {
472472
move |args| {
473473
let callables = callables.borrow();
474474
let callable = callables.get(&name).unwrap();
475-
Python::with_gil(|py| {
475+
Python::attach(|py| {
476476
let py_args =
477477
PyTuple::new(py, args.iter().map(|v| type_collection.to_py_value(v.clone())))
478478
.unwrap();

api/python/slint/models.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::value::{SlintToPyValue, TypeCollection};
1515

1616
pub struct PyModelShared {
1717
notify: ModelNotify,
18-
self_ref: RefCell<Option<PyObject>>,
18+
self_ref: RefCell<Option<Py<PyAny>>>,
1919
/// The type collection is needed when calling a Python implementation of set_row_data and
2020
/// the model data provided (for example from within a .slint file) contains an enum. Then
2121
/// we need to know how to map it to the correct Python enum. This field is lazily set, whenever
@@ -56,7 +56,7 @@ impl PyModelBase {
5656
}
5757
}
5858

59-
fn init_self(&self, self_ref: PyObject) {
59+
fn init_self(&self, self_ref: Py<PyAny>) {
6060
*self.inner.self_ref.borrow_mut() = Some(self_ref);
6161
}
6262

@@ -88,7 +88,7 @@ impl i_slint_core::model::Model for PyModelShared {
8888
type Data = slint_interpreter::Value;
8989

9090
fn row_count(&self) -> usize {
91-
Python::with_gil(|py| {
91+
Python::attach(|py| {
9292
let obj = self.self_ref.borrow();
9393
let Some(obj) = obj.as_ref() else {
9494
eprintln!("Python: Model implementation is lacking self object (in row_count)");
@@ -115,7 +115,7 @@ impl i_slint_core::model::Model for PyModelShared {
115115
}
116116

117117
fn row_data(&self, row: usize) -> Option<Self::Data> {
118-
Python::with_gil(|py| {
118+
Python::attach(|py| {
119119
let obj = self.self_ref.borrow();
120120
let Some(obj) = obj.as_ref() else {
121121
eprintln!("Python: Model implementation is lacking self object (in row_data)");
@@ -148,7 +148,7 @@ impl i_slint_core::model::Model for PyModelShared {
148148
}
149149

150150
fn set_row_data(&self, row: usize, data: Self::Data) {
151-
Python::with_gil(|py| {
151+
Python::attach(|py| {
152152
let obj = self.self_ref.borrow();
153153
let Some(obj) = obj.as_ref() else {
154154
eprintln!("Python: Model implementation is lacking self object (in set_row_data)");

api/python/slint/timer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ impl PyTimer {
6565
&self,
6666
mode: PyTimerMode,
6767
interval: chrono::Duration,
68-
callback: PyObject,
68+
callback: Py<PyAny>,
6969
) -> PyResult<()> {
7070
let interval = interval
7171
.to_std()
7272
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
7373
self.timer.start(mode.into(), interval, move || {
74-
Python::with_gil(|py| {
74+
Python::attach(|py| {
7575
callback.call0(py).expect("unexpected failure running python timer callback");
7676
});
7777
});
@@ -85,12 +85,12 @@ impl PyTimer {
8585
/// * `duration`: The duration from now until when the timer should fire.
8686
/// * `callback`: The function to call when the time has been reached or exceeded.
8787
#[staticmethod]
88-
fn single_shot(duration: chrono::Duration, callback: PyObject) -> PyResult<()> {
88+
fn single_shot(duration: chrono::Duration, callback: Py<PyAny>) -> PyResult<()> {
8989
let duration = duration
9090
.to_std()
9191
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
9292
i_slint_core::timers::Timer::single_shot(duration, move || {
93-
Python::with_gil(|py| {
93+
Python::attach(|py| {
9494
callback.call0(py).expect("unexpected failure running python timer callback");
9595
});
9696
});

api/python/slint/value.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl PyStruct {
7979
|value| Ok(self.type_collection.to_py_value(value.clone())),
8080
)
8181
}
82-
fn __setattr__(&mut self, py: Python<'_>, key: String, value: PyObject) -> PyResult<()> {
82+
fn __setattr__(&mut self, py: Python<'_>, key: String, value: Py<PyAny>) -> PyResult<()> {
8383
let pv =
8484
TypeCollection::slint_value_from_py_value(py, &value, Some(&self.type_collection))?;
8585
self.data.set_field(key, pv);
@@ -123,12 +123,12 @@ impl PyStructFieldIterator {
123123
}
124124

125125
thread_local! {
126-
static ENUM_CLASS: OnceCell<PyObject> = OnceCell::new();
126+
static ENUM_CLASS: OnceCell<Py<PyAny>> = OnceCell::new();
127127
}
128128

129-
pub fn enum_class(py: Python) -> PyObject {
129+
pub fn enum_class(py: Python) -> Py<PyAny> {
130130
ENUM_CLASS.with(|cls| {
131-
cls.get_or_init(|| -> PyObject {
131+
cls.get_or_init(|| -> Py<PyAny> {
132132
let enum_module = py.import("enum").unwrap();
133133
enum_module.getattr("Enum").unwrap().into()
134134
})
@@ -141,7 +141,7 @@ pub fn enum_class(py: Python) -> PyObject {
141141
/// a `.slint` file loaded with load_file. This is used to map enums
142142
/// provided by Slint to the correct python enum classes.
143143
pub struct TypeCollection {
144-
enum_classes: Rc<HashMap<String, PyObject>>,
144+
enum_classes: Rc<HashMap<String, Py<PyAny>>>,
145145
}
146146

147147
impl TypeCollection {
@@ -193,7 +193,7 @@ impl TypeCollection {
193193
enum_name: &str,
194194
enum_value: &str,
195195
py: Python<'_>,
196-
) -> Result<PyObject, PyErr> {
196+
) -> Result<Py<PyAny>, PyErr> {
197197
let enum_cls = self.enum_classes.get(enum_name).ok_or_else(|| {
198198
PyErr::new::<pyo3::exceptions::PyTypeError, _>(format!(
199199
"Slint provided enum {enum_name} is unknown"
@@ -209,13 +209,13 @@ impl TypeCollection {
209209
crate::models::ReadOnlyRustModel { model: model.clone(), type_collection: self.clone() }
210210
}
211211

212-
pub fn enums(&self) -> impl Iterator<Item = (&String, &PyObject)> {
212+
pub fn enums(&self) -> impl Iterator<Item = (&String, &Py<PyAny>)> {
213213
self.enum_classes.iter()
214214
}
215215

216216
pub fn slint_value_from_py_value(
217217
py: Python<'_>,
218-
ob: &PyObject,
218+
ob: &Py<PyAny>,
219219
type_collection: Option<&Self>,
220220
) -> PyResult<slint_interpreter::Value> {
221221
Self::slint_value_from_py_value_bound(&ob.bind(py), type_collection)

0 commit comments

Comments
 (0)