Skip to content

Commit

Permalink
Doc fixes, changelog and rename.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebpuetz committed Sep 5, 2020
1 parent 9137855 commit 410dfc0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add optional implementations of `ToPyObject`, `IntoPy`, and `FromPyObject` for [hashbrown](https://crates.io/crates/hashbrown)'s `HashMap` and `HashSet` types. The `hashbrown` feature must be enabled for these implementations to be built. [#1114](https://github.com/PyO3/pyo3/pull/1114/)
- Allow other `Result` types when using `#[pyfunction]`. [#1106](https://github.com/PyO3/pyo3/issues/1106).
- Add `#[derive(FromPyObject)]` macro for enums and structs. [#1065](https://github.com/PyO3/pyo3/pull/1065)
- Add macro attribute to `#[pyfn]` and `#[pyfunction]` to pass the module of a Python function to the function
body. [#1143](https://github.com/PyO3/pyo3/pull/1143)
- Add `add_function()` and `add_submodule()` functions to `PyModule` [#1143](https://github.com/PyO3/pyo3/pull/1143)

### Changed
- Exception types have been renamed from e.g. `RuntimeError` to `PyRuntimeError`, and are now only accessible by `&T` or `Py<T>` similar to other Python-native types. The old names continue to exist but are deprecated. [#1024](https://github.com/PyO3/pyo3/pull/1024)
Expand Down Expand Up @@ -50,6 +53,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Link against libpython on android with `extension-module` set. [#1095](https://github.com/PyO3/pyo3/pull/1095)
- Fix support for both `__add__` and `__radd__` in the `+` operator when both are defined in `PyNumberProtocol`
(and similar for all other reversible operators). [#1107](https://github.com/PyO3/pyo3/pull/1107)
- Associate Python functions with their module by passing the Module and Module name [#1143](https://github.com/PyO3/pyo3/pull/1143)

## [0.11.1] - 2020-06-30
### Added
Expand Down
16 changes: 6 additions & 10 deletions guide/src/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,14 @@ If you have a static function, you can expose it with `#[pyfunction]` and use [`

### 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:
It is possible to access the module of a `#[pyfunction]` and `#[pyfn]` in the
function body by passing the `pass_module` argument to the attribute:

```rust
use pyo3::wrap_pyfunction;
use pyo3::prelude::*;

#[pyfunction(need_module)]
#[pyfunction(pass_module)]
fn pyfunction_with_module(
module: &PyModule
) -> PyResult<&str> {
Expand All @@ -215,8 +214,8 @@ fn module_with_fn(py: Python, m: &PyModule) -> PyResult<()> {
# fn main() {}
```

If `need_module` is set, the first argument **must** be the `&PyModule`. It is then possible to interact with
the module.
If `pass_module` is set, the first argument **must** be the `&PyModule`. It is then possible to use the module
in the function body.

The same works for `#[pyfn]`:

Expand All @@ -227,7 +226,7 @@ use pyo3::prelude::*;
#[pymodule]
fn module_with_fn(py: Python, m: &PyModule) -> PyResult<()> {

#[pyfn(m, "module_name", need_module)]
#[pyfn(m, "module_name", pass_module)]
fn module_name(module: &PyModule) -> PyResult<&str> {
module.name()
}
Expand All @@ -236,6 +235,3 @@ fn module_with_fn(py: Python, m: &PyModule) -> PyResult<()> {

# fn main() {}
```

Within Python, the name of the module that a function belongs to can be accessed through the `__module__`
attribute.
10 changes: 5 additions & 5 deletions pyo3-derive-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub fn add_fn_to_module(
))
}
syn::FnArg::Typed(ref cap) => {
if pyfn_attrs.need_module && i == 0 {
if pyfn_attrs.pass_module && i == 0 {
if let syn::Type::Reference(tyref) = cap.ty.as_ref() {
if let syn::Type::Path(typath) = tyref.elem.as_ref() {
if typath
Expand All @@ -174,7 +174,7 @@ pub fn add_fn_to_module(
}
return Err(syn::Error::new_spanned(
cap,
"Expected &PyModule as first argument with `need_module`.",
"Expected &PyModule as first argument with `pass_module`.",
));
} else {
arguments.push(wrap_fn_argument(cap)?);
Expand Down Expand Up @@ -204,7 +204,7 @@ pub fn add_fn_to_module(

let python_name = &spec.python_name;

let wrapper = function_c_wrapper(&func.sig.ident, &spec, pyfn_attrs.need_module);
let wrapper = function_c_wrapper(&func.sig.ident, &spec, pyfn_attrs.pass_module);

Ok(quote! {
fn #function_wrapper_ident<'a>(
Expand Down Expand Up @@ -247,11 +247,11 @@ pub fn add_fn_to_module(
}

/// Generate static function wrapper (PyCFunction, PyCFunctionWithKeywords)
fn function_c_wrapper(name: &Ident, spec: &method::FnSpec<'_>, need_module: bool) -> TokenStream {
fn function_c_wrapper(name: &Ident, spec: &method::FnSpec<'_>, pass_module: bool) -> TokenStream {
let names: Vec<Ident> = get_arg_names(&spec);
let cb;
let slf_module;
if need_module {
if pass_module {
cb = quote! {
#name(_slf, #(#names),*)
};
Expand Down
6 changes: 3 additions & 3 deletions pyo3-derive-backend/src/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct PyFunctionAttr {
has_kw: bool,
has_varargs: bool,
has_kwargs: bool,
pub need_module: bool,
pub pass_module: bool,
}

impl syn::parse::Parse for PyFunctionAttr {
Expand All @@ -46,8 +46,8 @@ impl PyFunctionAttr {

pub fn add_item(&mut self, item: &NestedMeta) -> syn::Result<()> {
match item {
NestedMeta::Meta(syn::Meta::Path(ref ident)) if ident.is_ident("need_module") => {
self.need_module = true;
NestedMeta::Meta(syn::Meta::Path(ref ident)) if ident.is_ident("pass_module") => {
self.pass_module = true;
}
NestedMeta::Meta(syn::Meta::Path(ref ident)) => self.add_work(item, ident)?,
NestedMeta::Meta(syn::Meta::NameValue(ref nv)) => {
Expand Down
10 changes: 5 additions & 5 deletions tests/test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,33 +312,33 @@ fn test_module_with_constant() {
});
}

#[pyfunction(need_module)]
#[pyfunction(pass_module)]
fn pyfunction_with_module(module: &PyModule) -> PyResult<&str> {
module.name()
}

#[pyfunction(need_module)]
#[pyfunction(pass_module)]
fn pyfunction_with_module_and_py<'a>(
module: &'a PyModule,
_python: Python<'a>,
) -> PyResult<&'a str> {
module.name()
}

#[pyfunction(need_module)]
#[pyfunction(pass_module)]
fn pyfunction_with_module_and_arg(module: &PyModule, string: String) -> PyResult<(&str, String)> {
module.name().map(|s| (s, string))
}

#[pyfunction(need_module, string = "\"foo\"")]
#[pyfunction(pass_module, string = "\"foo\"")]
fn pyfunction_with_module_and_default_arg<'a>(
module: &'a PyModule,
string: &str,
) -> PyResult<(&'a str, String)> {
module.name().map(|s| (s, string.into()))
}

#[pyfunction(need_module, args = "*", kwargs = "**")]
#[pyfunction(pass_module, args = "*", kwargs = "**")]
fn pyfunction_with_module_and_args_kwargs<'a>(
module: &'a PyModule,
args: &PyTuple,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_need_module_arg_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pyo3::prelude::*;

#[pymodule]
fn module(_py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "with_module", need_module)]
#[pyfn(m, "with_module", pass_module)]
fn fail(string: &str, module: &PyModule) -> PyResult<&str> {
module.name()
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_need_module_arg_position.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: Expected &PyModule as first argument with `need_module`.
error: Expected &PyModule as first argument with `pass_module`.
--> $DIR/invalid_need_module_arg_position.rs:6:13
|
6 | fn fail(string: &str, module: &PyModule) -> PyResult<&str> {
Expand Down

0 comments on commit 410dfc0

Please sign in to comment.