Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-Authored-By: Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com>
Co-Authored-By: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 13, 2020
1 parent 9edf566 commit 56edf21
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion guide/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The easiest way to convert a Python object to a Rust value is using
`.extract()`. It returns a `PyResult` with a type error if the conversion
fails, so usually you will use something like

```
```ignore
let v: Vec<i32> = obj.extract()?;
```

Expand Down
2 changes: 1 addition & 1 deletion guide/src/exception.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ err.is_instance::<exceptions::TypeError>(py);
# }
```

## Handling Rust Errors
## Handling Rust errors

The vast majority of operations in this library will return [`PyResult<T>`](https://docs.rs/pyo3/latest/pyo3/prelude/type.PyResult.html).
This is an alias for the type `Result<T, PyErr>`.
Expand Down
8 changes: 4 additions & 4 deletions guide/src/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ Type: builtin_function_or_method

Currently, there are no conversions between `Fn`s in Rust and callables in Python. This would definitely be possible and very useful, so contributions are welcome. In the meantime, you can do the following:

### Calling a Python function in Rust
### Calling Python functions in Rust

You can use `ObjectProtocol::is_callable` to check if you got a callable, which is true for functions (including lambdas), methods and objects with a `__call__` method. You can call the object with `ObjectProtocol::call` with the args as first parameter and the kwargs (or `None`) as second parameter. There are also `ObjectProtocol::call0` with no args and `ObjectProtocol::call1` with only positional args.
You can use `ObjectProtocol::is_callable` to check if you have a callable object. `is_callable` will return `true` for functions (including lambdas), methods and objects with a `__call__` method. You can call the object with `ObjectProtocol::call` with the args as first parameter and the kwargs (or `None`) as second parameter. There are also `ObjectProtocol::call0` with no args and `ObjectProtocol::call1` with only positional args.

### Calling Rust `Fn`s in Python
### Calling Rust functions in Python

If you have a static function, you can expose it with `#[pyfunction]` and use `wrap_pyfunction!` to get the corresponding `PyObject`. For dynamic functions, e.g. lambdas and functions that were passed as arguments, you must put them in some kind of owned container, e.g. a box. (A long-term solution will be a special container similar to wasm-bindgen's `Closure`). You can then use a `#[pyclass]` struct with that container as a field as a way to pass the function over the FFI barrier. You can even make that class callable with `__call__` so it looks like a function in Python code.
If you have a static function, you can expose it with `#[pyfunction]` and use `wrap_pyfunction!` to get the corresponding `PyObject`. For dynamic functions, e.g. lambdas and functions that were passed as arguments, you must put them in some kind of owned container, e.g. a `Box`. (A long-term solution will be a special container similar to wasm-bindgen's `Closure`). You can then use a `#[pyclass]` struct with that container as a field as a way to pass the function over the FFI barrier. You can even make that class callable with `__call__` so it looks like a function in Python code.
2 changes: 1 addition & 1 deletion guide/src/pypy.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Compilation against PyPy is done by exporting the `PYTHON_SYS_EXECUTABLE` to poi
For example, `PYTHON_SYS_EXECUTABLE="/path/to/pypy3" /path/to/pypy3 setup.py install`


## Unsupported Features
## Unsupported features

These are features currently supported by PyO3, but not yet implemented in cpyext.

Expand Down

0 comments on commit 56edf21

Please sign in to comment.