From 467cd124faedb82855dfcc5bf97580c141afc0ab Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 28 Jan 2025 15:28:58 -0700 Subject: [PATCH 1/5] Update Cargo.lock and Cargo.toml for PyO3 0.23 support --- native/Cargo.lock | 20 ++++++++++---------- native/libcst/Cargo.toml | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/native/Cargo.lock b/native/Cargo.lock index 2d37e231f..168647451 100644 --- a/native/Cargo.lock +++ b/native/Cargo.lock @@ -506,9 +506,9 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.22.2" +version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "831e8e819a138c36e212f3af3fd9eeffed6bf1510a805af35b0edee5ffa59433" +checksum = "57fe09249128b3173d092de9523eaa75136bf7ba85e0d69eca241c7939c933cc" dependencies = [ "cfg-if", "indoc", @@ -524,9 +524,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.22.2" +version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e8730e591b14492a8945cdff32f089250b05f5accecf74aeddf9e8272ce1fa8" +checksum = "1cd3927b5a78757a0d71aa9dff669f903b1eb64b54142a9bd9f757f8fde65fd7" dependencies = [ "once_cell", "target-lexicon", @@ -534,9 +534,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.22.2" +version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e97e919d2df92eb88ca80a037969f44e5e70356559654962cbb3316d00300c6" +checksum = "dab6bb2102bd8f991e7749f130a70d05dd557613e39ed2deeee8e9ca0c4d548d" dependencies = [ "libc", "pyo3-build-config", @@ -544,9 +544,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.22.2" +version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb57983022ad41f9e683a599f2fd13c3664d7063a3ac5714cae4b7bee7d3f206" +checksum = "91871864b353fd5ffcb3f91f2f703a22a9797c91b9ab497b1acac7b07ae509c7" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -556,9 +556,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.22.2" +version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec480c0c51ddec81019531705acac51bcdbeae563557c982aa8263bb96880372" +checksum = "43abc3b80bc20f3facd86cd3c60beed58c3e2aa26213f3cda368de39c60a27e4" dependencies = [ "heck", "proc-macro2", diff --git a/native/libcst/Cargo.toml b/native/libcst/Cargo.toml index b8abcd1ff..3bf53f950 100644 --- a/native/libcst/Cargo.toml +++ b/native/libcst/Cargo.toml @@ -36,7 +36,7 @@ trace = ["peg/trace"] [dependencies] paste = "1.0.15" -pyo3 = { version = "0.22", optional = true } +pyo3 = { version = "0.23", optional = true } thiserror = "1.0.63" peg = "0.8.4" chic = "1.2.2" From 0ddd59a5457a3975b056806a03e045e1bfaf3837 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 28 Jan 2025 15:29:55 -0700 Subject: [PATCH 2/5] Replace deprecated _bound methods with their new undeprecated names --- native/libcst/src/nodes/expression.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/native/libcst/src/nodes/expression.rs b/native/libcst/src/nodes/expression.rs index bc3699bf7..cbd1e3611 100644 --- a/native/libcst/src/nodes/expression.rs +++ b/native/libcst/src/nodes/expression.rs @@ -2536,7 +2536,7 @@ mod py { match self { Self::Starred(s) => s.try_into_py(py), Self::Simple { value, comma } => { - let libcst = PyModule::import_bound(py, "libcst")?; + let libcst = PyModule::import(py, "libcst")?; let kwargs = [ Some(("value", value.try_into_py(py)?)), comma @@ -2548,7 +2548,7 @@ mod py { .filter(|x| x.is_some()) .map(|x| x.as_ref().unwrap()) .collect::>() - .into_py_dict_bound(py); + .into_py_dict(py)?; Ok(libcst .getattr("Element") .expect("no Element found in libcst") @@ -2572,7 +2572,7 @@ mod py { whitespace_before_colon, .. } => { - let libcst = PyModule::import_bound(py, "libcst")?; + let libcst = PyModule::import(py, "libcst")?; let kwargs = [ Some(("key", key.try_into_py(py)?)), Some(("value", value.try_into_py(py)?)), @@ -2593,7 +2593,7 @@ mod py { .filter(|x| x.is_some()) .map(|x| x.as_ref().unwrap()) .collect::>() - .into_py_dict_bound(py); + .into_py_dict(py)?; Ok(libcst .getattr("DictElement") .expect("no Element found in libcst") From efe3fbdc6f4aa5e290b68b560c4bb8c93e231876 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 28 Jan 2025 15:30:37 -0700 Subject: [PATCH 3/5] Update TryIntoPy trait to use IntoPyObject --- native/libcst/src/nodes/traits.rs | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/native/libcst/src/nodes/traits.rs b/native/libcst/src/nodes/traits.rs index 397f64042..0d6422529 100644 --- a/native/libcst/src/nodes/traits.rs +++ b/native/libcst/src/nodes/traits.rs @@ -118,7 +118,7 @@ impl<'a, T: Inflate<'a>> Inflate<'a> for Vec { } #[cfg(feature = "py")] pub mod py { - use pyo3::{types::PyAny, types::PyTuple, IntoPy, PyObject, PyResult, Python}; + use pyo3::{types::PyTuple, IntoPyObjectExt, PyObject, PyResult, Python}; // TODO: replace with upstream implementation once // https://github.com/PyO3/pyo3/issues/1813 is resolved @@ -135,7 +135,7 @@ pub mod py { impl TryIntoPy for bool { fn try_into_py(self, py: Python) -> PyResult { - Ok(self.into_py(py)) + self.into_py_any(py) } } @@ -170,28 +170,13 @@ pub mod py { .map(|x| x.try_into_py(py)) .collect::>>()? .into_iter(); - Ok(PyTuple::new_bound(py, converted).into()) - } - } - - impl TryIntoPy for PyTuple { - fn try_into_py(self, py: Python) -> PyResult { - Ok(self.into_py(py)) + PyTuple::new(py, converted).unwrap().into_py_any(py) } } impl<'a> TryIntoPy for &'a str { fn try_into_py(self, py: Python) -> PyResult { - Ok(self.into_py(py)) - } - } - - impl TryIntoPy for &'_ T - where - T: AsRef, - { - fn try_into_py(self, py: Python) -> PyResult { - Ok(self.into_py(py)) + self.into_py_any(py) } } } From c2e86738feaf2e6a9b7681da6bdfd76158269f08 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 28 Jan 2025 15:31:07 -0700 Subject: [PATCH 4/5] Update ParserError wrapper to use IntoPyObject --- native/libcst/src/parser/errors.rs | 54 +++++++++--------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/native/libcst/src/parser/errors.rs b/native/libcst/src/parser/errors.rs index 85690c978..8237cd0b8 100644 --- a/native/libcst/src/parser/errors.rs +++ b/native/libcst/src/parser/errors.rs @@ -28,18 +28,11 @@ pub enum ParserError<'a> { #[cfg(feature = "py")] mod py_error { - use pyo3::types::{IntoPyDict, PyAnyMethods, PyModule}; - use pyo3::{IntoPy, PyErr, PyErrArguments, Python}; + use pyo3::types::{IntoPyDict, PyAny, PyAnyMethods, PyModule}; + use pyo3::{Bound, IntoPyObject, PyErr, PyResult, Python}; use super::ParserError; - struct Details { - message: String, - lines: Vec, - raw_line: u32, - raw_column: u32, - } - impl<'a> From> for PyErr { fn from(e: ParserError) -> Self { Python::with_gil(|py| { @@ -59,36 +52,21 @@ mod py_error { line = lines.len() - 1; col = 0; } - let kwargs = [ - ("message", e.to_string().into_py(py)), - ("lines", lines.into_py(py)), - ("raw_line", (line + 1).into_py(py)), - ("raw_column", col.into_py(py)), - ] - .into_py_dict_bound(py); - let libcst = - PyModule::import_bound(py, "libcst").expect("libcst cannot be imported"); - PyErr::from_value_bound( - libcst - .getattr("ParserSyntaxError") - .expect("ParserSyntaxError not found") - .call((), Some(&kwargs)) - .expect("failed to instantiate"), - ) + match || -> PyResult> { + let kwargs = [ + ("message", e.to_string().into_pyobject(py)?.into_any()), + ("lines", lines.into_pyobject(py)?.into_any()), + ("raw_line", (line + 1).into_pyobject(py)?.into_any()), + ("raw_column", col.into_pyobject(py)?.into_any()), + ] + .into_py_dict(py)?; + let libcst = PyModule::import(py, "libcst")?; + libcst.getattr("ParserSyntaxError")?.call((), Some(&kwargs)) + }() { + Ok(py_err_value) => PyErr::from_value(py_err_value), + Err(e) => e, + } }) } } - - impl<'a> PyErrArguments for Details { - fn arguments(self, py: pyo3::Python) -> pyo3::PyObject { - [ - ("message", self.message.into_py(py)), - ("lines", self.lines.into_py(py)), - ("raw_line", self.raw_line.into_py(py)), - ("raw_column", self.raw_column.into_py(py)), - ] - .into_py_dict_bound(py) - .into_py(py) - } - } } From b038df7b0dcfae149beb0b6a4683bd46b836a52e Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Mon, 3 Mar 2025 09:27:52 -0700 Subject: [PATCH 5/5] replace unwrap with early return --- native/libcst/src/nodes/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/libcst/src/nodes/traits.rs b/native/libcst/src/nodes/traits.rs index 0d6422529..df61538b6 100644 --- a/native/libcst/src/nodes/traits.rs +++ b/native/libcst/src/nodes/traits.rs @@ -170,7 +170,7 @@ pub mod py { .map(|x| x.try_into_py(py)) .collect::>>()? .into_iter(); - PyTuple::new(py, converted).unwrap().into_py_any(py) + PyTuple::new(py, converted)?.into_py_any(py) } }