Skip to content

Commit

Permalink
Update pyo3 from v0.20 to v0.22 (#1893)
Browse files Browse the repository at this point in the history
v0.21 -> 0.22
- no implicit `None` for trailing options, we now explicitly declare it
via a macro
- explicit `eq, eq_int` annotations (question: why did we manually
implement richcmp before?)

v0.20 -> 0.21
- new `Bound` API with borrowing done in Rust instead of implicitly
- removal of `py` argument in `PyDict::from_sequence`

closes #1886
  • Loading branch information
sezna authored Aug 27, 2024
1 parent d26b3cf commit adcefe8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 134 deletions.
86 changes: 13 additions & 73 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
rand = "0.8"
serde_json = "1.0"
pyo3 = "0.20"
pyo3 = "0.22"
quantum-sparse-sim = { git = "https://github.com/qir-alliance/qir-runner", tag = "v0.7.4" }
async-trait = "0.1"
tokio = { version = "1.35", features = ["macros", "rt"] }
Expand Down
30 changes: 17 additions & 13 deletions pip/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,24 @@ impl FileSystem for Py<'_> {
}

fn read_file(py: Python, read_file: &PyObject, path: &Path) -> PyResult<(Arc<str>, Arc<str>)> {
let read_file_result = read_file.call1(py, PyTuple::new(py, &[path.to_string_lossy()]))?;
let read_file_result =
read_file.call1(py, PyTuple::new_bound(py, &[path.to_string_lossy()]))?;

let tuple = read_file_result.downcast::<PyTuple>(py)?;
let tuple = read_file_result.downcast_bound::<PyTuple>(py)?;

Ok((get_tuple_string(tuple, 0)?, get_tuple_string(tuple, 1)?))
}

fn list_directory(py: Python, list_directory: &PyObject, path: &Path) -> PyResult<Vec<Entry>> {
let list_directory_result =
list_directory.call1(py, PyTuple::new(py, &[path.to_string_lossy()]))?;
list_directory.call1(py, PyTuple::new_bound(py, &[path.to_string_lossy()]))?;

list_directory_result
.downcast::<PyList>(py)?
.downcast_bound::<PyList>(py)?
.into_iter()
.map(|e| {
let dict = e.downcast::<PyDict>()?;
let entry_type = match get_dict_string(dict, "type")? {
let entry_type = match get_dict_string(dict, "type")?.to_string().as_str() {
"file" => EntryType::File,
"folder" => EntryType::Folder,
"symlink" => EntryType::Symlink,
Expand All @@ -146,11 +147,14 @@ fn resolve_path(
) -> PyResult<PathBuf> {
let resolve_path_result = resolve_path.call1(
py,
PyTuple::new(py, &[base.to_string_lossy(), path.to_string_lossy()]),
PyTuple::new_bound(py, &[base.to_string_lossy(), path.to_string_lossy()]),
)?;

Ok(PathBuf::from(
resolve_path_result.downcast::<PyString>(py)?.to_str()?,
resolve_path_result
.downcast_bound::<PyString>(py)?
.str()?
.to_string(),
))
}

Expand All @@ -163,31 +167,31 @@ fn fetch_github(
path: &str,
) -> PyResult<Arc<str>> {
let fetch_github_result =
fetch_github.call1(py, PyTuple::new(py, [owner, repo, r#ref, path]))?;
fetch_github.call1(py, PyTuple::new_bound(py, [owner, repo, r#ref, path]))?;

Ok(fetch_github_result
.downcast::<PyString>(py)?
.downcast_bound::<PyString>(py)?
.to_string()
.into())
}

fn get_tuple_string(tuple: &PyTuple, index: usize) -> PyResult<Arc<str>> {
fn get_tuple_string(tuple: &Bound<'_, PyTuple>, index: usize) -> PyResult<Arc<str>> {
Ok(tuple
.get_item(index)?
.downcast::<PyString>()?
.to_string()
.into())
}

fn get_dict_string<'a>(dict: &'a PyDict, key: &'a str) -> PyResult<&'a str> {
fn get_dict_string<'a>(dict: &Bound<'a, PyDict>, key: &'a str) -> PyResult<Bound<'a, PyString>> {
match dict.get_item(key)? {
Some(item) => Ok(item.downcast::<PyString>()?.to_str()?),
Some(item) => Ok(item.downcast::<PyString>()?.str()?),
None => Err(PyException::new_err(format!("missing key `{key}` in dict"))),
}
}

fn diagnostic_from(py: Python<'_>, err: &PyErr) -> miette::Report {
if let Some(traceback) = err.traceback(py) {
if let Some(traceback) = err.traceback_bound(py) {
match traceback.format() {
Ok(traceback) => miette!(format!("{err}\n{traceback}",)),
Err(traceback_err) => {
Expand Down
Loading

0 comments on commit adcefe8

Please sign in to comment.