Skip to content

Commit 727e433

Browse files
authored
Update for Pyo3 0.23 (#1289)
* Update Cargo.lock and Cargo.toml for PyO3 0.23 support * Replace deprecated _bound methods with their new undeprecated names * Update TryIntoPy trait to use IntoPyObject * Update ParserError wrapper to use IntoPyObject * replace unwrap with early return
1 parent 5eccb5f commit 727e433

File tree

5 files changed

+35
-72
lines changed

5 files changed

+35
-72
lines changed

native/Cargo.lock

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

native/libcst/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ trace = ["peg/trace"]
3636

3737
[dependencies]
3838
paste = "1.0.15"
39-
pyo3 = { version = "0.22", optional = true }
39+
pyo3 = { version = "0.23", optional = true }
4040
thiserror = "1.0.63"
4141
peg = "0.8.4"
4242
annotate-snippets = "0.11.5"

native/libcst/src/nodes/expression.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,7 +2536,7 @@ mod py {
25362536
match self {
25372537
Self::Starred(s) => s.try_into_py(py),
25382538
Self::Simple { value, comma } => {
2539-
let libcst = PyModule::import_bound(py, "libcst")?;
2539+
let libcst = PyModule::import(py, "libcst")?;
25402540
let kwargs = [
25412541
Some(("value", value.try_into_py(py)?)),
25422542
comma
@@ -2548,7 +2548,7 @@ mod py {
25482548
.filter(|x| x.is_some())
25492549
.map(|x| x.as_ref().unwrap())
25502550
.collect::<Vec<_>>()
2551-
.into_py_dict_bound(py);
2551+
.into_py_dict(py)?;
25522552
Ok(libcst
25532553
.getattr("Element")
25542554
.expect("no Element found in libcst")
@@ -2572,7 +2572,7 @@ mod py {
25722572
whitespace_before_colon,
25732573
..
25742574
} => {
2575-
let libcst = PyModule::import_bound(py, "libcst")?;
2575+
let libcst = PyModule::import(py, "libcst")?;
25762576
let kwargs = [
25772577
Some(("key", key.try_into_py(py)?)),
25782578
Some(("value", value.try_into_py(py)?)),
@@ -2593,7 +2593,7 @@ mod py {
25932593
.filter(|x| x.is_some())
25942594
.map(|x| x.as_ref().unwrap())
25952595
.collect::<Vec<_>>()
2596-
.into_py_dict_bound(py);
2596+
.into_py_dict(py)?;
25972597
Ok(libcst
25982598
.getattr("DictElement")
25992599
.expect("no Element found in libcst")

native/libcst/src/nodes/traits.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a, T: Inflate<'a>> Inflate<'a> for Vec<T> {
118118
}
119119
#[cfg(feature = "py")]
120120
pub mod py {
121-
use pyo3::{types::PyAny, types::PyTuple, IntoPy, PyObject, PyResult, Python};
121+
use pyo3::{types::PyTuple, IntoPyObjectExt, PyObject, PyResult, Python};
122122

123123
// TODO: replace with upstream implementation once
124124
// https://github.com/PyO3/pyo3/issues/1813 is resolved
@@ -135,7 +135,7 @@ pub mod py {
135135

136136
impl TryIntoPy<PyObject> for bool {
137137
fn try_into_py(self, py: Python) -> PyResult<PyObject> {
138-
Ok(self.into_py(py))
138+
self.into_py_any(py)
139139
}
140140
}
141141

@@ -170,28 +170,13 @@ pub mod py {
170170
.map(|x| x.try_into_py(py))
171171
.collect::<PyResult<Vec<_>>>()?
172172
.into_iter();
173-
Ok(PyTuple::new_bound(py, converted).into())
174-
}
175-
}
176-
177-
impl TryIntoPy<PyObject> for PyTuple {
178-
fn try_into_py(self, py: Python) -> PyResult<PyObject> {
179-
Ok(self.into_py(py))
173+
PyTuple::new(py, converted)?.into_py_any(py)
180174
}
181175
}
182176

183177
impl<'a> TryIntoPy<PyObject> for &'a str {
184178
fn try_into_py(self, py: Python) -> PyResult<PyObject> {
185-
Ok(self.into_py(py))
186-
}
187-
}
188-
189-
impl<T> TryIntoPy<PyObject> for &'_ T
190-
where
191-
T: AsRef<PyAny>,
192-
{
193-
fn try_into_py(self, py: Python) -> PyResult<PyObject> {
194-
Ok(self.into_py(py))
179+
self.into_py_any(py)
195180
}
196181
}
197182
}

native/libcst/src/parser/errors.rs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,11 @@ pub enum ParserError<'a> {
2828
#[cfg(feature = "py")]
2929
mod py_error {
3030

31-
use pyo3::types::{IntoPyDict, PyAnyMethods, PyModule};
32-
use pyo3::{IntoPy, PyErr, PyErrArguments, Python};
31+
use pyo3::types::{IntoPyDict, PyAny, PyAnyMethods, PyModule};
32+
use pyo3::{Bound, IntoPyObject, PyErr, PyResult, Python};
3333

3434
use super::ParserError;
3535

36-
struct Details {
37-
message: String,
38-
lines: Vec<String>,
39-
raw_line: u32,
40-
raw_column: u32,
41-
}
42-
4336
impl<'a> From<ParserError<'a>> for PyErr {
4437
fn from(e: ParserError) -> Self {
4538
Python::with_gil(|py| {
@@ -59,36 +52,21 @@ mod py_error {
5952
line = lines.len() - 1;
6053
col = 0;
6154
}
62-
let kwargs = [
63-
("message", e.to_string().into_py(py)),
64-
("lines", lines.into_py(py)),
65-
("raw_line", (line + 1).into_py(py)),
66-
("raw_column", col.into_py(py)),
67-
]
68-
.into_py_dict_bound(py);
69-
let libcst =
70-
PyModule::import_bound(py, "libcst").expect("libcst cannot be imported");
71-
PyErr::from_value_bound(
72-
libcst
73-
.getattr("ParserSyntaxError")
74-
.expect("ParserSyntaxError not found")
75-
.call((), Some(&kwargs))
76-
.expect("failed to instantiate"),
77-
)
55+
match || -> PyResult<Bound<'_, PyAny>> {
56+
let kwargs = [
57+
("message", e.to_string().into_pyobject(py)?.into_any()),
58+
("lines", lines.into_pyobject(py)?.into_any()),
59+
("raw_line", (line + 1).into_pyobject(py)?.into_any()),
60+
("raw_column", col.into_pyobject(py)?.into_any()),
61+
]
62+
.into_py_dict(py)?;
63+
let libcst = PyModule::import(py, "libcst")?;
64+
libcst.getattr("ParserSyntaxError")?.call((), Some(&kwargs))
65+
}() {
66+
Ok(py_err_value) => PyErr::from_value(py_err_value),
67+
Err(e) => e,
68+
}
7869
})
7970
}
8071
}
81-
82-
impl<'a> PyErrArguments for Details {
83-
fn arguments(self, py: pyo3::Python) -> pyo3::PyObject {
84-
[
85-
("message", self.message.into_py(py)),
86-
("lines", self.lines.into_py(py)),
87-
("raw_line", self.raw_line.into_py(py)),
88-
("raw_column", self.raw_column.into_py(py)),
89-
]
90-
.into_py_dict_bound(py)
91-
.into_py(py)
92-
}
93-
}
9472
}

0 commit comments

Comments
 (0)