Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pyo3-gil-refs] Bound<'py, T> migration
Summary: ## Background We need to migrate `pyo3` to version [`0.22.6`](https://github.com/PyO3/pyo3/releases/tag/v0.22.6) both to stay current since we are still on `0.21.2` but also because many `third-party/pypi` packages can not be upgraded unless we are on `pyo3 = 0.22` ([post](https://fb.workplace.com/groups/1735223876972656/permalink/2115333798961660/)). Migrating to `0.22` coincides with the deprecation of the [`gil-refs`](https://pyo3.rs/v0.23.3/migration.html#deactivating-the-gil-refs-feature) feature (introduced with `0.21`) whilst upgrading to `0.23` will completely remove the feature all together. At its core, the `gil-refs` feature entails adopting the new [`Bound<'py, T>`](https://pyo3.rs/v0.23.3/types.html#boundpy-t) which has been around since `0.21`. We can thus migrate to the new API today in preparation of the `0.22` upgrade. What follows is a list of changes for supporting the `Bound<'_, T>` API, broadly following 2 migration guides from upstream: 1. [Deactivating the `gil-refs` feature](https://pyo3.rs/v0.23.3/migration.html#deactivating-the-gil-refs-feature) 2. [From `0.21` to `0.22`](https://pyo3.rs/v0.23.3/migration.html#from-021-to-022) In particular, all changes in this diff will fall in one of following 2 categories: ## [1] Changes needed for `pyo3=0.22` * Replacing function and method arg types of `&Py*` with `Bound<'_, Py*>` or `&Bound<'_, Py*>`. This applies to `#[pymodule]` decorated functions as well e.g. ```rust // BEFORE: #[pymodule] pub fn some(_py: Python, m: &PyModule) -> PyResult<()> {} // AFTER: #[pymodule] pub fn some(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {} // BEFORE: #[pyfunction] fn some(t: &PyType) -> PyResult<&PyType> {} // AFTER: #[pyfunction] fn some(t: Bound<'_, PyType>) -> PyResult<Bound<'_, PyType>> {} ``` * Replacing `.as_ref(..)` with `.bind(..)` since we are now handling on `Bound<'_, Py*>` and not `Py*` instances. * Replacing the `extract` function implementation for the `FromPyObject` trait with an `extract_bound` implementation ([reference](https://pyo3.rs/v0.23.3/migration.html#migrating-frompyobject-implementations)). * Replacing `Py*::new` with `Py*::new_bound` and `Python::import` with `Python::import_bound` ([reference](https://pyo3.rs/v0.23.3/migration.html#migrating-from-the-gil-refs-api-to-boundt)) e.g. ``` // BEFORE: PyTyple::new(..) // AFTER: PyTyple::new_bound(..) // BEFORE: py.import(..) // where py: Python<'_> // AFTER: py.import_bound(..) ``` * Replacing `T<&str>` with `T<PyBackedStr>` for method and function args as well as struct fields where `T` is a container type like `Vec` or `HashSet` ([reference](https://pyo3.rs/v0.23.3/migration.html#deactivating-the-gil-refs-feature)). For this change in particular there are many cases where one would be better off passing `Vec<String>` but I tried to preserve the downstream API as best I could so I used [`PyBackedStr`](https://docs.rs/pyo3/0.22.6/pyo3/pybacked/struct.PyBackedStr.html) wherever possible. ## [2] Other changes * Replaced `pyo3::sync::GILOnceCell::get` calls that contain an implicit set with `pyo3::sync::GILOnceCell::get_or_try_init`. * Silencing `__pyfunction_flatten_json_script::SIGNATURE` deprecation warnings for `Option<T>` args by including `#[pyo3(signature = (..)`. ## Notes * In an ideal world it would be possible to just do these changes and then remove the `gil-refs` feature before doing the `0.22` upgrade in order to let the dust settle and fix all missed migration points. That is not feasible though since by removing `gil-refs` several `third-pypi/packages` will have to be patched or upgraded since they are not compatible with the new `Bound<'_, T> `API (see errors in D68274679). Test Plan: Both on this diff & and rebasing on top of [5b0763e611](https://www.internalfb.com/intern/commit/cloud/FBS/5b0763e6117e03d2d8f0d640c915d966772424d0), spot checking via `buck2 build ...` on most affected directories and running: ``` ~/fbcode/common/rust/tools/scripts/check_all.sh ``` Reviewed By: manav-a, dtolnay Differential Revision: D68165150 fbshipit-source-id: 2932668887ba1798929c9fbb9e0a10fc01ddb40f
- Loading branch information