diff --git a/native/Cargo.lock b/native/Cargo.lock index 4ca4809c..0ba138ea 100644 --- a/native/Cargo.lock +++ b/native/Cargo.lock @@ -355,9 +355,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.16.0" +version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "oorandom" @@ -443,9 +443,9 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.25.1" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8970a78afe0628a3e3430376fc5fd76b6b45c4d43360ffd6cdd40bdde72b682a" +checksum = "7ba0117f4212101ee6544044dae45abe1083d30ce7b29c4b5cbdfa2354e07383" dependencies = [ "indoc", "libc", @@ -460,19 +460,18 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.25.1" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "458eb0c55e7ece017adeba38f2248ff3ac615e53660d7c71a238d7d2a01c7598" +checksum = "4fc6ddaf24947d12a9aa31ac65431fb1b851b8f4365426e182901eabfb87df5f" dependencies = [ - "once_cell", "target-lexicon", ] [[package]] name = "pyo3-ffi" -version = "0.25.1" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7114fe5457c61b276ab77c5055f206295b812608083644a5c5b2640c3102565c" +checksum = "025474d3928738efb38ac36d4744a74a400c901c7596199e20e45d98eb194105" dependencies = [ "libc", "pyo3-build-config", @@ -480,9 +479,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.25.1" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8725c0a622b374d6cb051d11a0983786448f7785336139c3c94f5aa6bef7e50" +checksum = "2e64eb489f22fe1c95911b77c44cc41e7c19f3082fc81cce90f657cdc42ffded" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -492,9 +491,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.25.1" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4109984c22491085343c05b0dbc54ddc405c3cf7b4374fc533f5c3313a572ccc" +checksum = "100246c0ecf400b475341b8455a9213344569af29a3c841d29270e53102e0fcf" dependencies = [ "heck", "proc-macro2", diff --git a/native/libcst/Cargo.toml b/native/libcst/Cargo.toml index d65a60cc..4fb029ca 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.25.1", optional = true } +pyo3 = { version = "0.26", optional = true } thiserror = "2.0.12" peg = "0.8.5" annotate-snippets = "0.11.5" diff --git a/native/libcst/src/nodes/expression.rs b/native/libcst/src/nodes/expression.rs index 961eee9f..c72d301d 100644 --- a/native/libcst/src/nodes/expression.rs +++ b/native/libcst/src/nodes/expression.rs @@ -2670,8 +2670,8 @@ mod py { use crate::nodes::traits::py::TryIntoPy; // TODO: this could be a derive helper attribute to override the python class name - impl<'a> TryIntoPy for Element<'a> { - fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult { + impl<'a> TryIntoPy> for Element<'a> { + fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult> { match self { Self::Starred(s) => s.try_into_py(py), Self::Simple { value, comma } => { @@ -2699,8 +2699,8 @@ mod py { } // TODO: this could be a derive helper attribute to override the python class name - impl<'a> TryIntoPy for DictElement<'a> { - fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult { + impl<'a> TryIntoPy> for DictElement<'a> { + fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult> { match self { Self::Starred(s) => s.try_into_py(py), Self::Simple { diff --git a/native/libcst/src/nodes/macros.rs b/native/libcst/src/nodes/macros.rs index c9ed49c1..11028b8c 100644 --- a/native/libcst/src/nodes/macros.rs +++ b/native/libcst/src/nodes/macros.rs @@ -17,8 +17,8 @@ macro_rules! py_import { ( $module_name:expr, $member_name:expr, $getter_fn:ident ) => { paste::paste! { static [] - : pyo3::once_cell::GILOnceCell> - = pyo3::once_cell::GILOnceCell::new(); + : pyo3::once_cell::PyOnceLock>> + = pyo3::once_cell::PyOnceLock::new(); fn $getter_fn<'py>(py: pyo3::Python<'py>) -> pyo3::PyResult<&'py pyo3::PyAny> { Ok([].get_or_init(py, || { diff --git a/native/libcst/src/nodes/parser_config.rs b/native/libcst/src/nodes/parser_config.rs index c10d86d5..e274d8df 100644 --- a/native/libcst/src/nodes/parser_config.rs +++ b/native/libcst/src/nodes/parser_config.rs @@ -29,12 +29,12 @@ impl BaseWhitespaceParserConfig { } #[getter] - fn get_lines(&self, py: Python) -> PyObject { + fn get_lines(&self, py: Python) -> Py { self.lines.to_object(py) } #[getter] - fn get_default_newline(&self, py: Python) -> PyObject { + fn get_default_newline(&self, py: Python) -> Py { self.default_newline.to_object(py) } } @@ -62,23 +62,23 @@ impl BaseWhitespaceParserConfig { } } -// These fields are private and PyObject, since we don't currently care about using them from +// These fields are private and Py, since we don't currently care about using them from // within rust. #[pyclass(extends=BaseWhitespaceParserConfig, module="libcst_native.parser_config")] #[text_signature = "(*, lines, encoding, default_indent, default_newline, has_trailing_newline, version, future_imports)"] pub struct ParserConfig { // lines is inherited #[pyo3(get)] - encoding: PyObject, + encoding: Py, #[pyo3(get)] - default_indent: PyObject, + default_indent: Py, // default_newline is inherited #[pyo3(get)] - has_trailing_newline: PyObject, + has_trailing_newline: Py, #[pyo3(get)] - version: PyObject, + version: Py, #[pyo3(get)] - future_imports: PyObject, + future_imports: Py, } #[pymethods] @@ -86,12 +86,12 @@ impl ParserConfig { #[new] fn new( lines: &PySequence, - encoding: PyObject, - default_indent: PyObject, + encoding: Py, + default_indent: Py, default_newline: &PyString, - has_trailing_newline: PyObject, - version: PyObject, - future_imports: PyObject, + has_trailing_newline: Py, + version: Py, + future_imports: Py, ) -> PyResult<(Self, BaseWhitespaceParserConfig)> { Ok(( Self { diff --git a/native/libcst/src/nodes/py_cached.rs b/native/libcst/src/nodes/py_cached.rs index d2398051..307082da 100644 --- a/native/libcst/src/nodes/py_cached.rs +++ b/native/libcst/src/nodes/py_cached.rs @@ -7,11 +7,11 @@ use pyo3::prelude::*; use std::convert::AsRef; use std::ops::Deref; -/// An immutable wrapper around a rust type T and it's PyObject equivalent. Caches the conversion -/// to and from the PyObject. +/// An immutable wrapper around a rust type T and its Py equivalent. Caches the conversion +/// to and from the Py. pub struct PyCached { native: T, - py_object: PyObject, + py_object: Py, } impl PyCached @@ -31,7 +31,7 @@ where T: FromPyObject<'source>, { fn extract(ob: &'source PyAny) -> PyResult { - Python::with_gil(|py| { + Python::attach(|py| { Ok(PyCached { native: ob.extract()?, py_object: ob.to_object(py), @@ -40,14 +40,14 @@ where } } -impl IntoPy for PyCached { - fn into_py(self, _py: Python) -> PyObject { +impl IntoPy> for PyCached { + fn into_py(self, _py: Python) -> Py { self.py_object } } impl ToPyObject for PyCached { - fn to_object(&self, py: Python) -> PyObject { + fn to_object(&self, py: Python) -> Py { self.py_object.clone_ref(py) } } @@ -71,6 +71,6 @@ where T: ToPyObject, { fn from(val: T) -> Self { - Python::with_gil(|py| Self::new(py, val)) + Python::attach(|py| Self::new(py, val)) } } diff --git a/native/libcst/src/nodes/traits.rs b/native/libcst/src/nodes/traits.rs index df61538b..c15a60e1 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::PyTuple, IntoPyObjectExt, PyObject, PyResult, Python}; + use pyo3::{types::PyTuple, IntoPyObjectExt, Py, PyAny, PyResult, Python}; // TODO: replace with upstream implementation once // https://github.com/PyO3/pyo3/issues/1813 is resolved @@ -133,26 +133,26 @@ pub mod py { // } // } - impl TryIntoPy for bool { - fn try_into_py(self, py: Python) -> PyResult { + impl TryIntoPy> for bool { + fn try_into_py(self, py: Python) -> PyResult> { self.into_py_any(py) } } - impl> TryIntoPy for Box + impl>> TryIntoPy> for Box where - T: TryIntoPy, + T: TryIntoPy>, { - fn try_into_py(self, py: Python) -> PyResult { + fn try_into_py(self, py: Python) -> PyResult> { (*self).try_into_py(py) } } - impl TryIntoPy for Option + impl TryIntoPy> for Option where - T: TryIntoPy, + T: TryIntoPy>, { - fn try_into_py(self, py: Python) -> PyResult { + fn try_into_py(self, py: Python) -> PyResult> { Ok(match self { None => py.None(), Some(x) => x.try_into_py(py)?, @@ -160,11 +160,11 @@ pub mod py { } } - impl TryIntoPy for Vec + impl TryIntoPy> for Vec where - T: TryIntoPy, + T: TryIntoPy>, { - fn try_into_py(self, py: Python) -> PyResult { + fn try_into_py(self, py: Python) -> PyResult> { let converted = self .into_iter() .map(|x| x.try_into_py(py)) @@ -174,8 +174,8 @@ pub mod py { } } - impl<'a> TryIntoPy for &'a str { - fn try_into_py(self, py: Python) -> PyResult { + impl<'a> TryIntoPy> for &'a str { + fn try_into_py(self, py: Python) -> PyResult> { self.into_py_any(py) } } diff --git a/native/libcst/src/parser/errors.rs b/native/libcst/src/parser/errors.rs index 8237cd0b..7fb3b740 100644 --- a/native/libcst/src/parser/errors.rs +++ b/native/libcst/src/parser/errors.rs @@ -35,7 +35,7 @@ mod py_error { impl<'a> From> for PyErr { fn from(e: ParserError) -> Self { - Python::with_gil(|py| { + Python::attach(|py| { let lines = match &e { ParserError::TokenizerError(_, text) | ParserError::ParserError(_, text) => { text.lines().collect::>() diff --git a/native/libcst/src/py.rs b/native/libcst/src/py.rs index 57da11e7..68c03744 100644 --- a/native/libcst/src/py.rs +++ b/native/libcst/src/py.rs @@ -11,21 +11,21 @@ use pyo3::prelude::*; pub fn libcst_native(_py: Python, m: &Bound) -> PyResult<()> { #[pyfn(m)] #[pyo3(signature = (source, encoding=None))] - fn parse_module(source: String, encoding: Option<&str>) -> PyResult { + fn parse_module(source: String, encoding: Option<&str>) -> PyResult> { let m = crate::parse_module(source.as_str(), encoding)?; - Python::with_gil(|py| m.try_into_py(py)) + Python::attach(|py| m.try_into_py(py)) } #[pyfn(m)] - fn parse_expression(source: String) -> PyResult { + fn parse_expression(source: String) -> PyResult> { let expr = crate::parse_expression(source.as_str())?; - Python::with_gil(|py| expr.try_into_py(py)) + Python::attach(|py| expr.try_into_py(py)) } #[pyfn(m)] - fn parse_statement(source: String) -> PyResult { + fn parse_statement(source: String) -> PyResult> { let stm = crate::parse_statement(source.as_str())?; - Python::with_gil(|py| stm.try_into_py(py)) + Python::attach(|py| stm.try_into_py(py)) } Ok(())