-
Notifications
You must be signed in to change notification settings - Fork 220
Update for Pyo3 0.23 #1289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update for Pyo3 0.23 #1289
Changes from 4 commits
467cd12
0ddd59a
efe3fbd
c2e8673
b038df7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,7 +118,7 @@ impl<'a, T: Inflate<'a>> Inflate<'a> for Vec<T> { | |
} | ||
#[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<PyObject> for bool { | ||
fn try_into_py(self, py: Python) -> PyResult<PyObject> { | ||
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::<PyResult<Vec<_>>>()? | ||
.into_iter(); | ||
Ok(PyTuple::new_bound(py, converted).into()) | ||
} | ||
} | ||
|
||
impl TryIntoPy<PyObject> for PyTuple { | ||
fn try_into_py(self, py: Python) -> PyResult<PyObject> { | ||
Ok(self.into_py(py)) | ||
PyTuple::new(py, converted).unwrap().into_py_any(py) | ||
|
||
} | ||
} | ||
|
||
impl<'a> TryIntoPy<PyObject> for &'a str { | ||
fn try_into_py(self, py: Python) -> PyResult<PyObject> { | ||
Ok(self.into_py(py)) | ||
} | ||
} | ||
|
||
impl<T> TryIntoPy<PyObject> for &'_ T | ||
where | ||
T: AsRef<PyAny>, | ||
{ | ||
fn try_into_py(self, py: Python) -> PyResult<PyObject> { | ||
Ok(self.into_py(py)) | ||
self.into_py_any(py) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<String>, | ||
raw_line: u32, | ||
raw_column: u32, | ||
} | ||
|
||
impl<'a> From<ParserError<'a>> 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<Bound<'_, PyAny>> { | ||
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, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since ultimately we want a PyErr in both the success and error cases, we can make use of a closure that returns a result and |
||
}) | ||
} | ||
} | ||
|
||
impl<'a> PyErrArguments for Details { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both this and |
||
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) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that with IntoPyObject that some of these impls aren't needed at all. I didn't look too closely to try to understand why though so there could be something subtle happening.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, I was hoping to get rid of these.