Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyfunction: allow wrap_pyfunction to work on imports (even cross-crate) #2091

Merged
merged 1 commit into from Jan 9, 2022

Conversation

davidhewitt
Copy link
Member

@davidhewitt davidhewitt commented Jan 6, 2022

I'm really pumped about this! I had an idea earlier today how to make wrap_pyfunction! work on a #[pyfunction] imported from another module.

How it works: #[pyfunction] fn foo now generates a hidden mod foo alongside the function. They're allowed to coexist, and use foo will bring both into the new scope. The hidden module contains just enough exports for wrap_pyfunction to be able to get what it needs. See pyo3-macros-backend/src/pyfunction.rs for the details.

This works really nicely - it even works across different crates! 馃殌

(This trick could potentially also be used for #[pymodule] / wrap_pymodule!, though I'll save that for another day, because things may also change when we implement #[pymodule] on mod as proposed in #694 (comment).)

@adamreichold
Copy link
Member

This will really remove a lot of boiler plate init functions from some code bases I work with and remove a cost factor from having more fine-grained Rust-side modules. Thanks!

@davidhewitt davidhewitt merged commit 43077da into PyO3:main Jan 9, 2022
@davidhewitt davidhewitt deleted the use-pyfunction branch January 9, 2022 19:53
@frameadvisors
Copy link

I am still getting this error. Is there a example/template that someone could share?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants