Skip to content

Commit

Permalink
Upgrade pyo3 to 0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Sep 9, 2024
1 parent 9834694 commit 5618e82
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 97 deletions.
93 changes: 18 additions & 75 deletions native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/libcst/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trace = ["peg/trace"]

[dependencies]
paste = "1.0.15"
pyo3 = { version = "0.20", optional = true }
pyo3 = { version = "0.22", optional = true }
thiserror = "1.0.63"
peg = "0.8.4"
chic = "1.2.2"
Expand Down
13 changes: 7 additions & 6 deletions native/libcst/src/nodes/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2524,6 +2524,7 @@ impl<'r, 'a> Inflate<'a> for DeflatedNamedExpr<'r, 'a> {
#[cfg(feature = "py")]
mod py {

use pyo3::types::PyAnyMethods;
use pyo3::types::PyModule;

use super::*;
Expand All @@ -2535,7 +2536,7 @@ mod py {
match self {
Self::Starred(s) => s.try_into_py(py),
Self::Simple { value, comma } => {
let libcst = PyModule::import(py, "libcst")?;
let libcst = PyModule::import_bound(py, "libcst")?;
let kwargs = [
Some(("value", value.try_into_py(py)?)),
comma
Expand All @@ -2547,11 +2548,11 @@ mod py {
.filter(|x| x.is_some())
.map(|x| x.as_ref().unwrap())
.collect::<Vec<_>>()
.into_py_dict(py);
.into_py_dict_bound(py);
Ok(libcst
.getattr("Element")
.expect("no Element found in libcst")
.call((), Some(kwargs))?
.call((), Some(&kwargs))?
.into())
}
}
Expand All @@ -2571,7 +2572,7 @@ mod py {
whitespace_before_colon,
..
} => {
let libcst = PyModule::import(py, "libcst")?;
let libcst = PyModule::import_bound(py, "libcst")?;
let kwargs = [
Some(("key", key.try_into_py(py)?)),
Some(("value", value.try_into_py(py)?)),
Expand All @@ -2592,11 +2593,11 @@ mod py {
.filter(|x| x.is_some())
.map(|x| x.as_ref().unwrap())
.collect::<Vec<_>>()
.into_py_dict(py);
.into_py_dict_bound(py);
Ok(libcst
.getattr("DictElement")
.expect("no Element found in libcst")
.call((), Some(kwargs))?
.call((), Some(&kwargs))?
.into())
}
}
Expand Down
2 changes: 1 addition & 1 deletion native/libcst/src/nodes/parser_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn parser_config_asdict<'py>(py: Python<'py>, config: PyRef<'py, ParserConfig>)
("version", config.version.clone_ref(py)),
("future_imports", config.future_imports.clone_ref(py)),
]
.into_py_dict(py)
.into_py_dict_bound(py)
}

pub fn init_module(_py: Python, m: &PyModule) -> PyResult<()> {
Expand Down
2 changes: 1 addition & 1 deletion native/libcst/src/nodes/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub mod py {
.map(|x| x.try_into_py(py))
.collect::<PyResult<Vec<_>>>()?
.into_iter();
Ok(PyTuple::new(py, converted).into())
Ok(PyTuple::new_bound(py, converted).into())
}
}

Expand Down
13 changes: 7 additions & 6 deletions native/libcst/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum ParserError<'a> {
#[cfg(feature = "py")]
mod py_error {

use pyo3::types::{IntoPyDict, PyModule};
use pyo3::types::{IntoPyDict, PyAnyMethods, PyModule};
use pyo3::{IntoPy, PyErr, PyErrArguments, Python};

use super::ParserError;
Expand Down Expand Up @@ -65,13 +65,14 @@ mod py_error {
("raw_line", (line + 1).into_py(py)),
("raw_column", col.into_py(py)),
]
.into_py_dict(py);
let libcst = PyModule::import(py, "libcst").expect("libcst cannot be imported");
PyErr::from_value(
.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))
.call((), Some(&kwargs))
.expect("failed to instantiate"),
)
})
Expand All @@ -86,7 +87,7 @@ mod py_error {
("raw_line", self.raw_line.into_py(py)),
("raw_column", self.raw_column.into_py(py)),
]
.into_py_dict(py)
.into_py_dict_bound(py)
.into_py(py)
}
}
Expand Down
2 changes: 1 addition & 1 deletion native/libcst/src/py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use pyo3::prelude::*;

#[pymodule]
#[pyo3(name = "native")]
pub fn libcst_native(_py: Python, m: &PyModule) -> PyResult<()> {
pub fn libcst_native(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
#[pyfn(m)]
fn parse_module(source: String, encoding: Option<&str>) -> PyResult<PyObject> {
let m = crate::parse_module(source.as_str(), encoding)?;
Expand Down
15 changes: 9 additions & 6 deletions native/libcst_derive/src/into_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ fn impl_into_py_enum(ast: &DeriveInput, e: &DataEnum) -> TokenStream {
let kwargs_toks = fields_to_kwargs(&var.fields, true);
toks.push(quote! {
Self::#varname { #(#fieldnames,)* .. } => {
let libcst = pyo3::types::PyModule::import(py, "libcst")?;
use pyo3::types::PyAnyMethods;

let libcst = pyo3::types::PyModule::import_bound(py, "libcst")?;
let kwargs = #kwargs_toks ;
Ok(libcst
.getattr(stringify!(#varname))
.expect(stringify!(no #varname found in libcst))
.call((), Some(kwargs))?
.call((), Some(&kwargs))?
.into())
}
})
Expand Down Expand Up @@ -87,12 +89,13 @@ fn impl_into_py_struct(ast: &DeriveInput, e: &DataStruct) -> TokenStream {
#[automatically_derived]
impl#generics crate::nodes::traits::py::TryIntoPy<pyo3::PyObject> for #ident #generics {
fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult<pyo3::PyObject> {
let libcst = pyo3::types::PyModule::import(py, "libcst")?;
use pyo3::types::PyAnyMethods;
let libcst = pyo3::types::PyModule::import_bound(py, "libcst")?;
let kwargs = #kwargs_toks ;
Ok(libcst
.getattr(stringify!(#ident))
.expect(stringify!(no #ident found in libcst))
.call((), Some(kwargs))?
.call((), Some(&kwargs))?
.into())
}
}
Expand Down Expand Up @@ -162,15 +165,15 @@ fn fields_to_kwargs(fields: &Fields, is_enum: bool) -> quote::__private::TokenSt
#(#optional_rust_varnames.map(|x| x.try_into_py(py)).transpose()?.map(|x| (stringify!(#optional_py_varnames), x)),)*
};
if empty_kwargs {
quote! { pyo3::types::PyDict::new(py) }
quote! { pyo3::types::PyDict::new_bound(py) }
} else {
quote! {
[ #kwargs_pairs #optional_pairs ]
.iter()
.filter(|x| x.is_some())
.map(|x| x.as_ref().unwrap())
.collect::<Vec<_>>()
.into_py_dict(py)
.into_py_dict_bound(py)
}
}
}
Expand Down

0 comments on commit 5618e82

Please sign in to comment.