-
Notifications
You must be signed in to change notification settings - Fork 801
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
PyModule in #[pyfunction] #1143
Changes from 7 commits
acbb3ee
5bbca1a
1f017b6
3214249
795c054
4aae523
9137855
e65b849
06cd7c7
64b06ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ fn double(x: usize) -> usize { | |
|
||
#[pymodule] | ||
fn module_with_functions(py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_wrapped(wrap_pyfunction!(double)).unwrap(); | ||
m.add_function(wrap_pyfunction!(double)).unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
@@ -65,7 +65,7 @@ fn num_kwds(kwds: Option<&PyDict>) -> usize { | |
|
||
#[pymodule] | ||
fn module_with_functions(py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_wrapped(wrap_pyfunction!(num_kwds)).unwrap(); | ||
m.add_function(wrap_pyfunction!(num_kwds)).unwrap(); | ||
Ok(()) | ||
} | ||
|
||
|
@@ -189,3 +189,53 @@ If you have a static function, you can expose it with `#[pyfunction]` and use [` | |
[`PyAny::call1`]: https://docs.rs/pyo3/latest/pyo3/struct.PyAny.html#tymethod.call1 | ||
[`PyObject`]: https://docs.rs/pyo3/latest/pyo3/type.PyObject.html | ||
[`wrap_pyfunction!`]: https://docs.rs/pyo3/latest/pyo3/macro.wrap_pyfunction.html | ||
|
||
### Accessing the module of a function | ||
|
||
Functions are usually associated with modules, in the C-API, the self parameter in a function call corresponds | ||
to the module of the function. It is possible to access the module of a `#[pyfunction]` and `#[pyfn]` in the | ||
function body by passing the `need_module` argument to the attribute: | ||
|
||
```rust | ||
use pyo3::wrap_pyfunction; | ||
use pyo3::prelude::*; | ||
|
||
#[pyfunction(need_module)] | ||
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. Bikeshedding: I'd like to propose to call the attribute Motivation is that this was already the verb used in #828 Also I've seen this phrasing before e.g. in Python's |
||
fn pyfunction_with_module( | ||
sebpuetz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
module: &PyModule | ||
) -> PyResult<&str> { | ||
module.name() | ||
} | ||
|
||
#[pymodule] | ||
fn module_with_fn(py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(pyfunction_with_module)) | ||
} | ||
|
||
# fn main() {} | ||
``` | ||
|
||
If `need_module` is set, the first argument **must** be the `&PyModule`. It is then possible to interact with | ||
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. Maybe "it is then possible to use the module in the function body"? |
||
the module. | ||
|
||
The same works for `#[pyfn]`: | ||
|
||
```rust | ||
use pyo3::wrap_pyfunction; | ||
use pyo3::prelude::*; | ||
|
||
#[pymodule] | ||
fn module_with_fn(py: Python, m: &PyModule) -> PyResult<()> { | ||
|
||
#[pyfn(m, "module_name", need_module)] | ||
fn module_name(module: &PyModule) -> PyResult<&str> { | ||
module.name() | ||
} | ||
Ok(()) | ||
} | ||
|
||
# fn main() {} | ||
``` | ||
|
||
Within Python, the name of the module that a function belongs to can be accessed through the `__module__` | ||
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. I think this is probably true of all Python functions (although you've just fixed this for pyO3) so not sure this sentence needs to be in this section? |
||
attribute. |
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 think this first sentence is probably an implementation detail that the user doesn't need to know? Might want to take it out for simplicity.