add PyModule::new_bound and PyModule::import_bound#3775
add PyModule::new_bound and PyModule::import_bound#3775davidhewitt merged 6 commits intoPyO3:mainfrom
PyModule::new_bound and PyModule::import_bound#3775Conversation
CodSpeed Performance ReportMerging #3775 will not alter performanceComparing Summary
|
575e0bf to
3eb45fb
Compare
PyModule::new and PyModule::import_boundPyModule::new_bound and PyModule::import_bound
3eb45fb to
2bdc80a
Compare
|
Ok, I rebased this and it should be in a suitable state now. We still need to resolve #3808, but the diff here is still pretty reviewable without that cleanup. |
Icxolu
left a comment
There was a problem hiding this comment.
Nice to see a lot of as_borrowed and as_gil_ref disappear!
wrap_pyfunction is a bit of a bound-ref-bound circle for now, but that's ok. I noticed a type error in a place or two, that the compiler/ci shout also pickup, and a few style nits.
A bit unrelated, but while reviewing I noticed that this example also needs updating. And since compile_fail examples tend to fail for the wrong reason as time passes, I might as well mention it, before I forget again 😄 .
Lines 609 to 636 in 2bdc80a
LilyFirefly
left a comment
There was a problem hiding this comment.
I think we can change some more &PyModule to &Bound<'_, PyModule>, but maybe these should all wait until we update the wrap_pyfunction! macro to support Bound.
| @@ -78,9 +78,9 @@ fn parent_module(py: Python<'_>, m: &PyModule) -> PyResult<()> { | |||
| } | |||
|
|
|||
| fn register_child_module(py: Python<'_>, parent_module: &PyModule) -> PyResult<()> { | |||
There was a problem hiding this comment.
Can we do:
| fn register_child_module(py: Python<'_>, parent_module: &PyModule) -> PyResult<()> { | |
| fn register_child_module(py: Python<'_>, parent_module: &Bound<'_, PyModule>) -> PyResult<()> { |
| parent_module.add_submodule(child_module)?; | ||
| let child_module = PyModule::new_bound(py, "child_module")?; | ||
| child_module.add_function(&wrap_pyfunction!(func, child_module.as_gil_ref())?.as_borrowed())?; | ||
| parent_module.add_submodule(child_module.as_gil_ref())?; |
There was a problem hiding this comment.
| parent_module.add_submodule(child_module.as_gil_ref())?; | |
| parent_module.add_submodule(child_module)?; |
| @@ -286,10 +336,10 @@ impl PyModule { | |||
| /// | |||
| /// #[pymodule] | |||
| /// fn my_module(py: Python<'_>, module: &PyModule) -> PyResult<()> { | |||
There was a problem hiding this comment.
| /// fn my_module(py: Python<'_>, module: &PyModule) -> PyResult<()> { | |
| /// fn my_module(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> { |
| /// submodule.add("super_useful_constant", "important")?; | ||
| /// | ||
| /// module.add_submodule(submodule)?; | ||
| /// module.add_submodule(submodule.as_gil_ref())?; |
There was a problem hiding this comment.
| /// module.add_submodule(submodule.as_gil_ref())?; | |
| /// module.add_submodule(submodule)?; |
| @@ -480,10 +530,10 @@ pub trait PyModuleMethods<'py> { | |||
| /// | |||
| /// #[pymodule] | |||
| /// fn my_module(py: Python<'_>, module: &PyModule) -> PyResult<()> { | |||
There was a problem hiding this comment.
| /// fn my_module(py: Python<'_>, module: &PyModule) -> PyResult<()> { | |
| /// fn my_module(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> { |
| /// submodule.add("super_useful_constant", "important")?; | ||
| /// | ||
| /// module.add_submodule(submodule)?; | ||
| /// module.add_submodule(submodule.as_gil_ref())?; |
There was a problem hiding this comment.
| /// module.add_submodule(submodule.as_gil_ref())?; | |
| /// module.add_submodule(submodule)?; |
| @@ -258,12 +258,12 @@ fn superfunction() -> String { | |||
| #[pymodule] | |||
| fn supermodule(py: Python<'_>, module: &PyModule) -> PyResult<()> { | |||
There was a problem hiding this comment.
| fn supermodule(py: Python<'_>, module: &PyModule) -> PyResult<()> { | |
| fn supermodule(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> { |
| module.add_submodule(module_to_add)?; | ||
| let module_to_add = PyModule::new_bound(py, "submodule")?; | ||
| submodule(&module_to_add)?; | ||
| module.add_submodule(module_to_add.as_gil_ref())?; |
There was a problem hiding this comment.
| module.add_submodule(module_to_add.as_gil_ref())?; | |
| module.add_submodule(module_to_add)?; |
| module.add_submodule(module_to_add.as_gil_ref())?; | ||
| let module_to_add = PyModule::new_bound(py, "submodule_with_init_fn")?; | ||
| submodule_with_init_fn(py, module_to_add.as_gil_ref())?; | ||
| module.add_submodule(module_to_add.as_gil_ref())?; |
There was a problem hiding this comment.
| module.add_submodule(module_to_add.as_gil_ref())?; | |
| module.add_submodule(module_to_add)?; |
|
Indeed, |
|
@LilyFoote regarding the suggestions above, the blocker is the support for |
|
Ooof can't keep up with the merge conflicts! I'll try to fixup this again this evening and hopefully see it across the line. |
Part of #3684
This is a WIP migration of
PyModule::newandPyModule::importto the new APIs.I decided to pause on this one for a bit and leave in draft, because:
Python::importtoPython::import_boundat the same time, but this got too much so I might try to do that one first.#[pymodule]taking&Bound<'_, PyModule>(probably in a similar way to the draft support&Bound<PyType>forclassmethod#3744) before continuing here, because a lot of the examples / docs get in a really bad interim state here otherwise.