Skip to content

Commit a94c93d

Browse files
committed
Update to pyo 0.23
1 parent e994448 commit a94c93d

File tree

9 files changed

+256
-416
lines changed

9 files changed

+256
-416
lines changed

Cargo.lock

Lines changed: 139 additions & 322 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ doc = false
1818
extension-module = ["pyo3/extension-module"]
1919

2020
[dependencies]
21-
accesskit = { version = "0.17.1", features = ["pyo3"] }
22-
pyo3 = { version = "0.20", features = ["abi3-py38", "multiple-pymethods"] }
21+
accesskit = { version = "0.18.0", features = ["pyo3"] }
22+
pyo3 = { version = "0.23", features = ["abi3-py38", "multiple-pymethods"] }
2323

2424
[target.'cfg(target_os = "windows")'.dependencies]
25-
accesskit_windows = { version = "0.24.1" }
25+
accesskit_windows = { version = "0.25.0" }
2626

2727
[target.'cfg(target_os = "macos")'.dependencies]
28-
accesskit_macos = { version = "0.18.1" }
28+
accesskit_macos = { version = "0.19.0" }
2929

3030
[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))'.dependencies]
31-
accesskit_unix = { version = "0.13.1" }
31+
accesskit_unix = { version = "0.14.0" }
3232

3333
[profile.release]
3434
lto = true

examples/pygame/hello_world.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def build_initial_tree(self):
9191
button_2 = build_button(BUTTON_2_ID, "Button 2")
9292
result = accesskit.TreeUpdate(self.focus)
9393
tree = accesskit.Tree(WINDOW_ID)
94-
tree.app_name = "Hello world"
9594
result.tree = tree
9695
result.nodes.append((WINDOW_ID, root))
9796
result.nodes.append((BUTTON_1_ID, button_1))

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ classifiers = [
3434
"Programming Language :: Python :: 3.12",
3535
"Topic :: Software Development :: User Interfaces"
3636
]
37+
dynamic = ["version"]
3738

3839
[project.urls]
3940
Homepage = "https://github.com/AccessKit/accesskit-python"

src/common.rs

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
// the LICENSE-MIT file), at your option.
55

66
use crate::{Point, Rect};
7-
use pyo3::{prelude::*, types::PyList};
7+
use pyo3::{
8+
prelude::*,
9+
types::{PyList, PyTuple},
10+
IntoPyObjectExt,
11+
};
812

913
#[derive(Clone)]
1014
#[pyclass(module = "accesskit")]
@@ -151,7 +155,6 @@ impl From<accesskit::TextPosition> for TextPosition {
151155
}
152156
}
153157

154-
#[derive(Clone)]
155158
#[pyclass(get_all, set_all, module = "accesskit")]
156159
pub struct TextSelection {
157160
pub anchor: Py<TextPosition>,
@@ -178,15 +181,20 @@ impl From<&accesskit::TextSelection> for TextSelection {
178181
impl From<TextSelection> for accesskit::TextSelection {
179182
fn from(selection: TextSelection) -> Self {
180183
Python::with_gil(|py| accesskit::TextSelection {
181-
anchor: selection.anchor.as_ref(py).borrow().0,
182-
focus: selection.focus.as_ref(py).borrow().0,
184+
anchor: selection.anchor.bind(py).borrow().0,
185+
focus: selection.focus.bind(py).borrow().0,
183186
})
184187
}
185188
}
186189

187-
impl From<TextSelection> for Box<accesskit::TextSelection> {
188-
fn from(selection: TextSelection) -> Self {
189-
Box::new(selection.into())
190+
impl From<&TextSelection> for Box<accesskit::TextSelection> {
191+
fn from(selection: &TextSelection) -> Self {
192+
Python::with_gil(|py| {
193+
Box::new(accesskit::TextSelection {
194+
anchor: selection.anchor.borrow(py).0,
195+
focus: selection.focus.borrow(py).0,
196+
})
197+
})
190198
}
191199
}
192200

@@ -293,14 +301,14 @@ macro_rules! vec_property_methods {
293301
$(#[pymethods]
294302
impl Node {
295303
#[getter]
296-
pub fn $getter(&self, py: Python) -> Py<PyList> {
297-
let values = self.inner().$getter().iter().cloned().map(<$py_item_type>::from).map(|i| i.into_py(py));
298-
PyList::new(py, values).into()
304+
pub fn $getter(&self, py: Python) -> PyResult<Py<PyList>> {
305+
let values = self.inner().$getter().iter().cloned().map(<$py_item_type>::from);
306+
Ok(PyList::new(py, values)?.unbind())
299307
}
300-
pub fn $setter(&mut self, values: &PyList) {
308+
pub fn $setter(&mut self, values: &Bound<'_, PyList>) {
301309
let values = values
302310
.iter()
303-
.map(PyAny::extract::<$py_item_type>)
311+
.map(|item| item.extract::<$py_item_type>())
304312
.filter_map(PyResult::ok)
305313
.map(<$accesskit_item_type>::from)
306314
.collect::<Vec<$accesskit_item_type>>();
@@ -540,7 +548,7 @@ unique_enum_property_methods! {
540548
property_methods! {
541549
(transform, option_getter, Option<crate::Affine>, set_transform, simple_setter, crate::Affine, clear_transform),
542550
(bounds, option_getter, Option<crate::Rect>, set_bounds, converting_setter, crate::Rect, clear_bounds),
543-
(text_selection, option_getter, Option<TextSelection>, set_text_selection, simple_setter, TextSelection, clear_text_selection)
551+
(text_selection, option_getter, Option<TextSelection>, set_text_selection, simple_setter, &TextSelection, clear_text_selection)
544552
}
545553

546554
vec_property_methods! {
@@ -551,7 +559,6 @@ vec_property_methods! {
551559
#[pyclass(module = "accesskit", get_all, set_all)]
552560
pub struct Tree {
553561
pub root: NodeId,
554-
pub app_name: Option<String>,
555562
pub toolkit_name: Option<String>,
556563
pub toolkit_version: Option<String>,
557564
}
@@ -562,7 +569,6 @@ impl Tree {
562569
pub fn new(root: NodeId) -> Self {
563570
Self {
564571
root,
565-
app_name: None,
566572
toolkit_name: None,
567573
toolkit_version: None,
568574
}
@@ -573,14 +579,12 @@ impl From<Tree> for accesskit::Tree {
573579
fn from(tree: Tree) -> Self {
574580
Self {
575581
root: tree.root.into(),
576-
app_name: tree.app_name,
577582
toolkit_name: tree.toolkit_name,
578583
toolkit_version: tree.toolkit_version,
579584
}
580585
}
581586
}
582587

583-
#[derive(Clone)]
584588
#[pyclass(module = "accesskit", get_all, set_all)]
585589
pub struct TreeUpdate {
586590
pub nodes: Py<PyList>,
@@ -600,22 +604,21 @@ impl TreeUpdate {
600604
}
601605
}
602606

603-
impl From<TreeUpdate> for accesskit::TreeUpdate {
604-
fn from(update: TreeUpdate) -> Self {
607+
impl From<&TreeUpdate> for accesskit::TreeUpdate {
608+
fn from(update: &TreeUpdate) -> Self {
605609
Python::with_gil(|py| Self {
606610
nodes: update
607611
.nodes
608-
.as_ref(py)
612+
.bind(py)
609613
.iter()
610-
.map(PyAny::extract::<(NodeId, Node)>)
614+
.map(|n| n.extract::<(NodeId, Node)>())
611615
.filter_map(Result::ok)
612616
.map(|(id, node)| (id.into(), node.into()))
613617
.collect(),
614-
tree: update.tree.map(|tree| {
615-
let tree = tree.as_ref(py).borrow();
618+
tree: update.tree.as_ref().map(|tree| {
619+
let tree = tree.bind(py).borrow();
616620
accesskit::Tree {
617621
root: tree.root.into(),
618-
app_name: tree.app_name.clone(),
619622
toolkit_name: tree.toolkit_name.clone(),
620623
toolkit_version: tree.toolkit_version.clone(),
621624
}
@@ -625,8 +628,8 @@ impl From<TreeUpdate> for accesskit::TreeUpdate {
625628
}
626629
}
627630

628-
#[derive(Clone)]
629-
#[pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")]
631+
#[derive(PartialEq)]
632+
#[pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE", eq, eq_int)]
630633
pub enum ActionDataKind {
631634
CustomAction,
632635
Value,
@@ -641,38 +644,46 @@ pub enum ActionDataKind {
641644
pub struct ActionRequest {
642645
pub action: accesskit::Action,
643646
pub target: NodeId,
644-
pub data: Option<(ActionDataKind, Py<PyAny>)>,
647+
pub data: Option<Py<PyTuple>>,
645648
}
646649

647650
impl From<accesskit::ActionRequest> for ActionRequest {
648651
fn from(request: accesskit::ActionRequest) -> Self {
649652
Python::with_gil(|py| Self {
650653
action: request.action,
651654
target: request.target.into(),
652-
data: request.data.map(|data| match data {
653-
accesskit::ActionData::CustomAction(action) => {
654-
(ActionDataKind::CustomAction, action.into_py(py))
655+
data: request.data.map(|data| {
656+
match data {
657+
accesskit::ActionData::CustomAction(action) => (
658+
ActionDataKind::CustomAction,
659+
action.into_py_any(py).unwrap(),
660+
),
661+
accesskit::ActionData::Value(value) => {
662+
(ActionDataKind::Value, value.into_py_any(py).unwrap())
663+
}
664+
accesskit::ActionData::NumericValue(value) => {
665+
(ActionDataKind::NumericValue, value.into_py_any(py).unwrap())
666+
}
667+
accesskit::ActionData::ScrollTargetRect(rect) => (
668+
ActionDataKind::ScrollTargetRect,
669+
Rect::from(rect).into_py_any(py).unwrap(),
670+
),
671+
accesskit::ActionData::ScrollToPoint(point) => (
672+
ActionDataKind::ScrollToPoint,
673+
Point::from(point).into_py_any(py).unwrap(),
674+
),
675+
accesskit::ActionData::SetScrollOffset(point) => (
676+
ActionDataKind::SetScrollOffset,
677+
Point::from(point).into_py_any(py).unwrap(),
678+
),
679+
accesskit::ActionData::SetTextSelection(selection) => (
680+
ActionDataKind::SetTextSelection,
681+
TextSelection::from(&selection).into_py_any(py).unwrap(),
682+
),
655683
}
656-
accesskit::ActionData::Value(value) => (ActionDataKind::Value, value.into_py(py)),
657-
accesskit::ActionData::NumericValue(value) => {
658-
(ActionDataKind::NumericValue, value.into_py(py))
659-
}
660-
accesskit::ActionData::ScrollTargetRect(rect) => (
661-
ActionDataKind::ScrollTargetRect,
662-
Rect::from(rect).into_py(py),
663-
),
664-
accesskit::ActionData::ScrollToPoint(point) => (
665-
ActionDataKind::ScrollToPoint,
666-
Point::from(point).into_py(py),
667-
),
668-
accesskit::ActionData::SetScrollOffset(point) => (
669-
ActionDataKind::SetScrollOffset,
670-
Point::from(point).into_py(py),
671-
),
672-
accesskit::ActionData::SetTextSelection(selection) => (
673-
ActionDataKind::SetTextSelection,
674-
TextSelection::from(&selection).into_py(py),
675-
),
684+
.into_pyobject(py)
685+
.unwrap()
686+
.unbind()
676687
}),
677688
})
678689
}
@@ -690,9 +701,9 @@ impl accesskit::ActivationHandler for LocalPythonActivationHandler<'_> {
690701
fn request_initial_tree(&mut self) -> Option<accesskit::TreeUpdate> {
691702
let result = self.handler.call0(self.py).unwrap();
692703
result
693-
.extract::<Option<TreeUpdate>>(self.py)
704+
.extract::<Option<PyRef<TreeUpdate>>>(self.py)
694705
.unwrap()
695-
.map(Into::into)
706+
.map(|tree| (&*tree).into())
696707
}
697708
}
698709

@@ -703,9 +714,9 @@ impl accesskit::ActivationHandler for PythonActivationHandler {
703714
Python::with_gil(|py| {
704715
let result = self.0.call0(py).unwrap();
705716
result
706-
.extract::<Option<TreeUpdate>>(py)
717+
.extract::<Option<PyRef<TreeUpdate>>>(py)
707718
.unwrap()
708-
.map(Into::into)
719+
.map(|tree| (&*tree).into())
709720
})
710721
}
711722
}

src/lib.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
// the LICENSE-APACHE file) or the MIT license (found in
44
// the LICENSE-MIT file), at your option.
55

6-
// TODO: Remove this exception once we update pyo3.
7-
#![allow(non_local_definitions)]
8-
96
mod common;
107
mod geometry;
118

@@ -29,7 +26,7 @@ pub use common::*;
2926
pub use geometry::*;
3027

3128
#[pymodule]
32-
fn accesskit(py: Python<'_>, m: &PyModule) -> PyResult<()> {
29+
fn accesskit(m: &Bound<'_, PyModule>) -> PyResult<()> {
3330
m.add_class::<::accesskit::Role>()?;
3431
m.add_class::<::accesskit::Action>()?;
3532
m.add_class::<::accesskit::Orientation>()?;
@@ -57,15 +54,15 @@ fn accesskit(py: Python<'_>, m: &PyModule) -> PyResult<()> {
5754

5855
#[cfg(target_os = "macos")]
5956
{
60-
let macos_module = PyModule::new(py, "macos")?;
57+
let macos_module = PyModule::new(m.py(), "macos")?;
6158
macos_module.add_class::<macos::QueuedEvents>()?;
6259
macos_module.add_class::<macos::Adapter>()?;
6360
macos_module.add_class::<macos::SubclassingAdapter>()?;
6461
macos_module.add_function(wrap_pyfunction!(
6562
macos::add_focus_forwarder_to_window_class,
66-
macos_module
63+
&macos_module
6764
)?)?;
68-
m.add_submodule(macos_module)?;
65+
m.add_submodule(&macos_module)?;
6966
}
7067
#[cfg(any(
7168
target_os = "linux",
@@ -75,17 +72,17 @@ fn accesskit(py: Python<'_>, m: &PyModule) -> PyResult<()> {
7572
target_os = "openbsd",
7673
))]
7774
{
78-
let unix_module = PyModule::new(py, "unix")?;
75+
let unix_module = PyModule::new(m.py(), "unix")?;
7976
unix_module.add_class::<unix::Adapter>()?;
80-
m.add_submodule(unix_module)?;
77+
m.add_submodule(&unix_module)?;
8178
}
8279
#[cfg(target_os = "windows")]
8380
{
84-
let windows_module = PyModule::new(py, "windows")?;
81+
let windows_module = PyModule::new(m.py(), "windows")?;
8582
windows_module.add_class::<windows::QueuedEvents>()?;
8683
windows_module.add_class::<windows::Adapter>()?;
8784
windows_module.add_class::<windows::SubclassingAdapter>()?;
88-
m.add_submodule(windows_module)?;
85+
m.add_submodule(&windows_module)?;
8986
}
9087

9188
Ok(())
@@ -94,10 +91,10 @@ fn accesskit(py: Python<'_>, m: &PyModule) -> PyResult<()> {
9491
// The following exception is needed because this function is only used
9592
// in the bindings for some platform adapters.
9693
#[allow(dead_code)]
97-
fn to_void_ptr(value: &PyAny) -> *mut c_void {
98-
if let Ok(value) = value.extract::<&PyCapsule>() {
99-
return value.pointer();
94+
fn to_void_ptr(value: &Bound<'_, PyAny>) -> *mut c_void {
95+
if let Ok(capsule) = value.downcast::<PyCapsule>() {
96+
return capsule.pointer();
10097
}
101-
let value = value.getattr("value").unwrap_or(value);
98+
let value = value.getattr("value").unwrap_or(value.clone());
10299
value.extract::<isize>().unwrap() as *mut _
103100
}

0 commit comments

Comments
 (0)