Skip to content

Add documentation for raw_pycfuntion#1174

Merged
davidhewitt merged 1 commit intoPyO3:masterfrom
sebpuetz:raw-pycfun-docs
Sep 19, 2020
Merged

Add documentation for raw_pycfuntion#1174
davidhewitt merged 1 commit intoPyO3:masterfrom
sebpuetz:raw-pycfun-docs

Conversation

@sebpuetz
Copy link
Contributor

This adds instructions on how to use the raw_pycfunction!() macro to the guide. I forgot to include this in #1163.

It doesn't touch any .rs file, so this could probably be merged post release, too.

@davidhewitt
Copy link
Member

(Just a note to say thanks very much for writing these docs; I've been very busy lately, hoping to find time to read through these and review hopefully Monday.)

@kngwyu
Copy link
Member

kngwyu commented Sep 13, 2020

I think the doc comment of raw_pycfunction could be a better home for this doc, and then we can link it from the guide.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, just got round to reading it.

Thanks for taking the time to write this. It's some great documentation. I agree with @kngwyu that the example on the bottom is probably better to go as API documentation rather than letting the guide get too long.

Comment on lines +286 to +289
The `wrap_pyfunction` macro can be used to directly get a `PyCFunction` given a
`#[pyfunction]` and a `PyModule`: `wrap_pyfunction!(rust_fun, module)`. In the single-argument
variant, the macro only takes the identifier of the Rust function and returns a function that
can be called with either `Python` or `PyModule` and returns a `PyCFunction`. No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this paragraph may go into more detail than needed. (I'd rather not talk about the single-argument variant of wrap_pyfunction any more so that the ecosystem naturally migrates to the two-argument form.)

Comment on lines +262 to +284
E.g.:

```rust
use pyo3::prelude::*;
use pyo3::types::PyCFunction;
use pyo3::raw_pycfunction;

#[pyfunction]
fn some_fun() -> PyResult<()> {
Ok(())
}

#[pymodule]
fn module(_py: Python, module: &PyModule) -> PyResult<()> {
let ffi_wrapper_fun = raw_pycfunction!(some_fun);
let docs = "Some documentation string with null-termination\0";
let py_cfunction =
PyCFunction::new_with_keywords(ffi_wrapper_fun, "function_name", docs, module.into())?;
module.add_function(py_cfunction)
}

# fn main() {}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @kngwyu is correct; this document is great to go as a docstring on raw_pycfunction or maybe even `PyCFunction::new_with_keywords.

And then in the paragraph above can just have a link e.g. `(See the docs for raw_pycfunction).

I can't decide whether it's better for raw_pycfunction or PyCFunction::new_with_keywords though - happy for you to decide!

@davidhewitt
Copy link
Member

Thanks!

@davidhewitt davidhewitt merged commit 0ec10a2 into PyO3:master Sep 19, 2020
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.

3 participants