Skip to content

Commit de0d708

Browse files
committed
Doc fixes, changelog and rename.
1 parent 9137855 commit de0d708

File tree

7 files changed

+25
-25
lines changed

7 files changed

+25
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515
- 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/)
1616
- Allow other `Result` types when using `#[pyfunction]`. [#1106](https://github.com/PyO3/pyo3/issues/1106).
1717
- Add `#[derive(FromPyObject)]` macro for enums and structs. [#1065](https://github.com/PyO3/pyo3/pull/1065)
18+
- Add macro attribute to `#[pyfn]` and `#[pyfunction]` to pass the module of a Python function to the function
19+
body. [#1143](https://github.com/PyO3/pyo3/pull/1143)
20+
- Add `add_function()` and `add_module()` functions to `PyModule` [#1143](https://github.com/PyO3/pyo3/pull/1143)
1821

1922
### Changed
2023
- 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)
@@ -50,6 +53,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
5053
- Link against libpython on android with `extension-module` set. [#1095](https://github.com/PyO3/pyo3/pull/1095)
5154
- Fix support for both `__add__` and `__radd__` in the `+` operator when both are defined in `PyNumberProtocol`
5255
(and similar for all other reversible operators). [#1107](https://github.com/PyO3/pyo3/pull/1107)
56+
- Associate Python functions with their module by passing the Module and Module name [#1143](https://github.com/PyO3/pyo3/pull/1143)
5357

5458
## [0.11.1] - 2020-06-30
5559
### Added

guide/src/function.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,14 @@ If you have a static function, you can expose it with `#[pyfunction]` and use [`
192192

193193
### Accessing the module of a function
194194

195-
Functions are usually associated with modules, in the C-API, the self parameter in a function call corresponds
196-
to the module of the function. It is possible to access the module of a `#[pyfunction]` and `#[pyfn]` in the
197-
function body by passing the `need_module` argument to the attribute:
195+
It is possible to access the module of a `#[pyfunction]` and `#[pyfn]` in the
196+
function body by passing the `pass_module` argument to the attribute:
198197

199198
```rust
200199
use pyo3::wrap_pyfunction;
201200
use pyo3::prelude::*;
202201

203-
#[pyfunction(need_module)]
202+
#[pyfunction(pass_module)]
204203
fn pyfunction_with_module(
205204
module: &PyModule
206205
) -> PyResult<&str> {
@@ -215,8 +214,8 @@ fn module_with_fn(py: Python, m: &PyModule) -> PyResult<()> {
215214
# fn main() {}
216215
```
217216

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

221220
The same works for `#[pyfn]`:
222221

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

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

237236
# fn main() {}
238237
```
239-
240-
Within Python, the name of the module that a function belongs to can be accessed through the `__module__`
241-
attribute.

pyo3-derive-backend/src/module.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub fn add_fn_to_module(
158158
))
159159
}
160160
syn::FnArg::Typed(ref cap) => {
161-
if pyfn_attrs.need_module && i == 0 {
161+
if pyfn_attrs.pass_module && i == 0 {
162162
if let syn::Type::Reference(tyref) = cap.ty.as_ref() {
163163
if let syn::Type::Path(typath) = tyref.elem.as_ref() {
164164
if typath
@@ -174,7 +174,7 @@ pub fn add_fn_to_module(
174174
}
175175
return Err(syn::Error::new_spanned(
176176
cap,
177-
"Expected &PyModule as first argument with `need_module`.",
177+
"Expected &PyModule as first argument with `pass_module`.",
178178
));
179179
} else {
180180
arguments.push(wrap_fn_argument(cap)?);
@@ -204,7 +204,7 @@ pub fn add_fn_to_module(
204204

205205
let python_name = &spec.python_name;
206206

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

209209
Ok(quote! {
210210
fn #function_wrapper_ident<'a>(
@@ -247,11 +247,11 @@ pub fn add_fn_to_module(
247247
}
248248

249249
/// Generate static function wrapper (PyCFunction, PyCFunctionWithKeywords)
250-
fn function_c_wrapper(name: &Ident, spec: &method::FnSpec<'_>, need_module: bool) -> TokenStream {
250+
fn function_c_wrapper(name: &Ident, spec: &method::FnSpec<'_>, pass_module: bool) -> TokenStream {
251251
let names: Vec<Ident> = get_arg_names(&spec);
252252
let cb;
253253
let slf_module;
254-
if need_module {
254+
if pass_module {
255255
cb = quote! {
256256
#name(_slf, #(#names),*)
257257
};

pyo3-derive-backend/src/pyfunction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct PyFunctionAttr {
2424
has_kw: bool,
2525
has_varargs: bool,
2626
has_kwargs: bool,
27-
pub need_module: bool,
27+
pub pass_module: bool,
2828
}
2929

3030
impl syn::parse::Parse for PyFunctionAttr {
@@ -46,8 +46,8 @@ impl PyFunctionAttr {
4646

4747
pub fn add_item(&mut self, item: &NestedMeta) -> syn::Result<()> {
4848
match item {
49-
NestedMeta::Meta(syn::Meta::Path(ref ident)) if ident.is_ident("need_module") => {
50-
self.need_module = true;
49+
NestedMeta::Meta(syn::Meta::Path(ref ident)) if ident.is_ident("pass_module") => {
50+
self.pass_module = true;
5151
}
5252
NestedMeta::Meta(syn::Meta::Path(ref ident)) => self.add_work(item, ident)?,
5353
NestedMeta::Meta(syn::Meta::NameValue(ref nv)) => {

tests/test_module.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,33 +312,33 @@ fn test_module_with_constant() {
312312
});
313313
}
314314

315-
#[pyfunction(need_module)]
315+
#[pyfunction(pass_module)]
316316
fn pyfunction_with_module(module: &PyModule) -> PyResult<&str> {
317317
module.name()
318318
}
319319

320-
#[pyfunction(need_module)]
320+
#[pyfunction(pass_module)]
321321
fn pyfunction_with_module_and_py<'a>(
322322
module: &'a PyModule,
323323
_python: Python<'a>,
324324
) -> PyResult<&'a str> {
325325
module.name()
326326
}
327327

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

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

341-
#[pyfunction(need_module, args = "*", kwargs = "**")]
341+
#[pyfunction(pass_module, args = "*", kwargs = "**")]
342342
fn pyfunction_with_module_and_args_kwargs<'a>(
343343
module: &'a PyModule,
344344
args: &PyTuple,

tests/ui/invalid_need_module_arg_position.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use pyo3::prelude::*;
22

33
#[pymodule]
44
fn module(_py: Python, m: &PyModule) -> PyResult<()> {
5-
#[pyfn(m, "with_module", need_module)]
5+
#[pyfn(m, "with_module", pass_module)]
66
fn fail(string: &str, module: &PyModule) -> PyResult<&str> {
77
module.name()
88
}

tests/ui/invalid_need_module_arg_position.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Expected &PyModule as first argument with `need_module`.
1+
error: Expected &PyModule as first argument with `pass_module`.
22
--> $DIR/invalid_need_module_arg_position.rs:6:13
33
|
44
6 | fn fail(string: &str, module: &PyModule) -> PyResult<&str> {

0 commit comments

Comments
 (0)