From b5b9a480cd6d18e8d451ffe297c5e6ef9878a4a5 Mon Sep 17 00:00:00 2001 From: messense Date: Thu, 24 Jun 2021 22:18:48 +0800 Subject: [PATCH] Add `wrap_pyfunction` macro to prelude --- CHANGELOG.md | 1 + README.md | 1 - examples/pyo3-benchmarks/src/lib.rs | 1 - examples/pyo3-pytests/src/datetime.rs | 1 - examples/pyo3-pytests/src/misc.rs | 1 - examples/pyo3-pytests/src/othermod.rs | 1 - examples/pyo3-pytests/src/path.rs | 1 - examples/word-count/src/lib.rs | 1 - guide/src/ecosystem/logging.md | 1 - guide/src/function.md | 6 +----- guide/src/module.md | 2 +- guide/src/trait_bounds.md | 1 - src/lib.rs | 1 - src/num_bigint.rs | 1 - src/num_complex.rs | 1 - src/prelude.rs | 1 + src/python.rs | 2 +- src/types/module.rs | 1 - tests/test_bytes.rs | 1 - tests/test_exceptions.rs | 2 +- tests/test_macros.rs | 1 - tests/test_module.rs | 2 +- tests/test_pyfunction.rs | 1 - tests/test_string.rs | 1 - tests/test_text_signature.rs | 2 +- tests/test_various.rs | 2 +- tests/test_wrap_pyfunction_deduction.rs | 2 +- tests/ui/invalid_result_conversion.rs | 1 - tests/ui/invalid_result_conversion.stderr | 4 ++-- 29 files changed, 12 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 465451d8b30..56da0781c15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Add `#[pyo3(text_signature = "...")]` syntax for setting text signature. [#1658](https://github.com/PyO3/pyo3/pull/1658) - Add support for setting and retrieving exception cause. [#1679](https://github.com/PyO3/pyo3/pull/1679) - Add FFI definitions from `cpython/pystate.h`.[#1687](https://github.com/PyO3/pyo3/pull/1687/) +- Add `wrap_pyfunction` macro to prelude. [#1695](https://github.com/PyO3/pyo3/pull/1695) ### Changed diff --git a/README.md b/README.md index 310201ba4b3..232e4d2d70b 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,6 @@ features = ["extension-module"] ```rust use pyo3::prelude::*; -use pyo3::wrap_pyfunction; /// Formats the sum of two numbers as string. #[pyfunction] diff --git a/examples/pyo3-benchmarks/src/lib.rs b/examples/pyo3-benchmarks/src/lib.rs index 475436909c7..e649fd1c65d 100644 --- a/examples/pyo3-benchmarks/src/lib.rs +++ b/examples/pyo3-benchmarks/src/lib.rs @@ -1,6 +1,5 @@ use pyo3::prelude::*; use pyo3::types::{PyDict, PyTuple}; -use pyo3::wrap_pyfunction; #[pyfunction] fn none() {} diff --git a/examples/pyo3-pytests/src/datetime.rs b/examples/pyo3-pytests/src/datetime.rs index b7231841c2f..a3522b4845e 100644 --- a/examples/pyo3-pytests/src/datetime.rs +++ b/examples/pyo3-pytests/src/datetime.rs @@ -3,7 +3,6 @@ use pyo3::types::{ PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess, PyTuple, PyTzInfo, }; -use pyo3::wrap_pyfunction; #[pyfunction] fn make_date(py: Python, year: i32, month: u8, day: u8) -> PyResult<&PyDate> { diff --git a/examples/pyo3-pytests/src/misc.rs b/examples/pyo3-pytests/src/misc.rs index 33ad86d0250..59dc63b669e 100644 --- a/examples/pyo3-pytests/src/misc.rs +++ b/examples/pyo3-pytests/src/misc.rs @@ -1,5 +1,4 @@ use pyo3::prelude::*; -use pyo3::wrap_pyfunction; #[pyfunction] fn issue_219() { diff --git a/examples/pyo3-pytests/src/othermod.rs b/examples/pyo3-pytests/src/othermod.rs index 5c149d29ed8..ba6319fe6d9 100644 --- a/examples/pyo3-pytests/src/othermod.rs +++ b/examples/pyo3-pytests/src/othermod.rs @@ -3,7 +3,6 @@ //! The code below just tries to use the most important code generation paths use pyo3::prelude::*; -use pyo3::wrap_pyfunction; #[pyclass] pub struct ModClass { diff --git a/examples/pyo3-pytests/src/path.rs b/examples/pyo3-pytests/src/path.rs index d3214aa0c91..3f2a22fc8b7 100644 --- a/examples/pyo3-pytests/src/path.rs +++ b/examples/pyo3-pytests/src/path.rs @@ -1,5 +1,4 @@ use pyo3::prelude::*; -use pyo3::wrap_pyfunction; use std::path::{Path, PathBuf}; #[pyfunction] diff --git a/examples/word-count/src/lib.rs b/examples/word-count/src/lib.rs index 36ae89ef7c0..1a078c6bdae 100644 --- a/examples/word-count/src/lib.rs +++ b/examples/word-count/src/lib.rs @@ -2,7 +2,6 @@ // https://github.com/tildeio/helix-website/blob/master/crates/word_count/src/lib.rs use pyo3::prelude::*; -use pyo3::wrap_pyfunction; use rayon::prelude::*; /// Searches for the word, parallelized by rayon diff --git a/guide/src/ecosystem/logging.md b/guide/src/ecosystem/logging.md index 500a8804709..d0ecfacec0e 100644 --- a/guide/src/ecosystem/logging.md +++ b/guide/src/ecosystem/logging.md @@ -21,7 +21,6 @@ It's also possible to tweak its configuration (mostly to tune its performance). ```rust use log::info; use pyo3::prelude::*; -use pyo3::wrap_pyfunction; #[pyfunction] fn log_something() { diff --git a/guide/src/function.md b/guide/src/function.md index d3355a54fa6..2393b946514 100644 --- a/guide/src/function.md +++ b/guide/src/function.md @@ -6,7 +6,6 @@ One way is annotating a function with `#[pyfunction]` and then adding it to the ```rust use pyo3::prelude::*; -use pyo3::wrap_pyfunction; #[pyfunction] fn double(x: usize) -> usize { @@ -54,7 +53,7 @@ fn rust2py(py: Python, m: &PyModule) -> PyResult<()> { Ok(format!("{}", a + b)) } - m.add_function(pyo3::wrap_pyfunction!(sum_as_string, m)?)?; + m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; Ok(()) } @@ -72,7 +71,6 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python ```rust use pyo3::prelude::*; - use pyo3::wrap_pyfunction; #[pyfunction] #[pyo3(name = "no_args")] @@ -97,7 +95,6 @@ The `#[pyfunction]` attribute supports specifying details of argument parsing. T ```rust use pyo3::prelude::*; -use pyo3::wrap_pyfunction; use pyo3::types::PyDict; #[pyfunction(kwds="**")] @@ -258,7 +255,6 @@ in Python code. It is possible to access the module of a `#[pyfunction]` in the function body by using `#[pyo3(pass_module)]` option: ```rust -use pyo3::wrap_pyfunction; use pyo3::prelude::*; #[pyfunction] diff --git a/guide/src/module.md b/guide/src/module.md index 4a8d9c1e2af..330e1bb52dc 100644 --- a/guide/src/module.md +++ b/guide/src/module.md @@ -66,7 +66,7 @@ dicts or other modules: ```rust use pyo3::prelude::*; -use pyo3::{wrap_pyfunction, wrap_pymodule}; +use pyo3::wrap_pymodule; use pyo3::types::IntoPyDict; #[pyfunction] diff --git a/guide/src/trait_bounds.md b/guide/src/trait_bounds.md index 10e9ab72c4c..c9c47d39ea4 100644 --- a/guide/src/trait_bounds.md +++ b/guide/src/trait_bounds.md @@ -461,7 +461,6 @@ It is also required to make the struct public. ```rust use pyo3::prelude::*; -use pyo3::wrap_pyfunction; use pyo3::types::PyAny; pub trait Model { diff --git a/src/lib.rs b/src/lib.rs index 3dc0fb2f9d8..83b48cb98fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,7 +157,6 @@ //! //! ```rust //! use pyo3::prelude::*; -//! use pyo3::wrap_pyfunction; //! //! /// Formats the sum of two numbers as string. //! #[pyfunction] diff --git a/src/num_bigint.rs b/src/num_bigint.rs index aa2050257f4..2252338baa5 100644 --- a/src/num_bigint.rs +++ b/src/num_bigint.rs @@ -34,7 +34,6 @@ //! ```rust //! use num_bigint::BigInt; //! use pyo3::prelude::*; -//! use pyo3::wrap_pyfunction; //! //! #[pyfunction] //! fn add_one(n: BigInt) -> BigInt { diff --git a/src/num_complex.rs b/src/num_complex.rs index 4e4776a0df8..5cb45decef3 100644 --- a/src/num_complex.rs +++ b/src/num_complex.rs @@ -31,7 +31,6 @@ //! use nalgebra::base::{dimension::Const, storage::Storage, Matrix}; //! use num_complex::Complex; //! use pyo3::prelude::*; -//! use pyo3::wrap_pyfunction; //! //! type T = Complex; //! diff --git a/src/prelude.rs b/src/prelude.rs index 15dbcc7c072..04eae682e93 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -19,5 +19,6 @@ pub use crate::python::Python; pub use crate::{FromPyObject, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto, ToPyObject}; // PyModule is only part of the prelude because we need it for the pymodule function pub use crate::types::{PyAny, PyModule}; +pub use crate::wrap_pyfunction; #[cfg(feature = "macros")] pub use {crate::proc_macro::*, pyo3_macros::FromPyObject}; diff --git a/src/python.rs b/src/python.rs index 8b1cff1819d..62d247744dc 100644 --- a/src/python.rs +++ b/src/python.rs @@ -189,7 +189,7 @@ impl<'p> Python<'p> { /// /// # Examples /// ``` - /// # use pyo3::prelude::*; use pyo3::types::IntoPyDict; use pyo3::wrap_pyfunction; + /// # use pyo3::prelude::*; use pyo3::types::IntoPyDict; /// use pyo3::exceptions::PyRuntimeError; /// use std::sync::Arc; /// use std::thread; diff --git a/src/types/module.rs b/src/types/module.rs index 23b100286d7..44a0199820d 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -346,7 +346,6 @@ impl PyModule { /// /// ```rust /// use pyo3::prelude::*; - /// use pyo3::wrap_pyfunction; /// /// #[pyfunction] /// fn say_hello() { diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index e458e35b553..a9300cec1f5 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -1,6 +1,5 @@ use pyo3::prelude::*; use pyo3::types::PyBytes; -use pyo3::wrap_pyfunction; mod common; diff --git a/tests/test_exceptions.rs b/tests/test_exceptions.rs index 710b170584b..ad7885c228c 100644 --- a/tests/test_exceptions.rs +++ b/tests/test_exceptions.rs @@ -1,5 +1,5 @@ use pyo3::prelude::*; -use pyo3::{exceptions, py_run, wrap_pyfunction, PyErr, PyResult}; +use pyo3::{exceptions, py_run, PyErr, PyResult}; use std::error::Error; use std::fmt; #[cfg(not(target_os = "windows"))] diff --git a/tests/test_macros.rs b/tests/test_macros.rs index 46c31b4c980..78d68ab586e 100644 --- a/tests/test_macros.rs +++ b/tests/test_macros.rs @@ -1,7 +1,6 @@ //! Ensure that pyo3 macros can be used inside macro_rules! use pyo3::prelude::*; -use pyo3::wrap_pyfunction; #[macro_use] mod common; diff --git a/tests/test_module.rs b/tests/test_module.rs index ffb065b2626..bd6a5528aea 100644 --- a/tests/test_module.rs +++ b/tests/test_module.rs @@ -1,7 +1,7 @@ use pyo3::prelude::*; +use pyo3::py_run; use pyo3::types::{IntoPyDict, PyDict, PyTuple}; -use pyo3::{py_run, wrap_pyfunction}; mod common; #[pyclass] diff --git a/tests/test_pyfunction.rs b/tests/test_pyfunction.rs index 7f9dadd2caa..d3cecdb4820 100644 --- a/tests/test_pyfunction.rs +++ b/tests/test_pyfunction.rs @@ -4,7 +4,6 @@ use pyo3::prelude::*; use pyo3::types::PyCFunction; #[cfg(not(Py_LIMITED_API))] use pyo3::types::{PyDateTime, PyFunction}; -use pyo3::wrap_pyfunction; mod common; diff --git a/tests/test_string.rs b/tests/test_string.rs index 63f786618bf..f1ee07e95a7 100644 --- a/tests/test_string.rs +++ b/tests/test_string.rs @@ -1,5 +1,4 @@ use pyo3::prelude::*; -use pyo3::wrap_pyfunction; mod common; diff --git a/tests/test_text_signature.rs b/tests/test_text_signature.rs index 1c55216fcc8..5d537eed4ab 100644 --- a/tests/test_text_signature.rs +++ b/tests/test_text_signature.rs @@ -1,5 +1,5 @@ use pyo3::prelude::*; -use pyo3::{types::PyType, wrap_pyfunction, wrap_pymodule, PyCell}; +use pyo3::{types::PyType, wrap_pymodule, PyCell}; mod common; diff --git a/tests/test_various.rs b/tests/test_various.rs index 04c8dc82100..6e570fa21a1 100644 --- a/tests/test_various.rs +++ b/tests/test_various.rs @@ -1,6 +1,6 @@ use pyo3::prelude::*; use pyo3::types::{PyDict, PyTuple}; -use pyo3::{py_run, wrap_pyfunction, PyCell}; +use pyo3::{py_run, PyCell}; use std::fmt; diff --git a/tests/test_wrap_pyfunction_deduction.rs b/tests/test_wrap_pyfunction_deduction.rs index c561a419fbf..010a76e1c22 100644 --- a/tests/test_wrap_pyfunction_deduction.rs +++ b/tests/test_wrap_pyfunction_deduction.rs @@ -1,4 +1,4 @@ -use pyo3::{prelude::*, types::PyCFunction, wrap_pyfunction}; +use pyo3::{prelude::*, types::PyCFunction}; #[pyfunction] fn f() {} diff --git a/tests/ui/invalid_result_conversion.rs b/tests/ui/invalid_result_conversion.rs index 3da4d217182..c053edea943 100644 --- a/tests/ui/invalid_result_conversion.rs +++ b/tests/ui/invalid_result_conversion.rs @@ -2,7 +2,6 @@ //! *doesn't* implement `From for PyErr` won't be automatically //! converted when using `#[pyfunction]`. use pyo3::prelude::*; -use pyo3::wrap_pyfunction; use std::fmt; diff --git a/tests/ui/invalid_result_conversion.stderr b/tests/ui/invalid_result_conversion.stderr index 227aa02c012..c422e27d9cd 100644 --- a/tests/ui/invalid_result_conversion.stderr +++ b/tests/ui/invalid_result_conversion.stderr @@ -1,7 +1,7 @@ error[E0277]: the trait bound `Result<(), MyError>: IntoPyCallbackOutput<_>` is not satisfied - --> $DIR/invalid_result_conversion.rs:22:1 + --> $DIR/invalid_result_conversion.rs:21:1 | -22 | #[pyfunction] +21 | #[pyfunction] | ^^^^^^^^^^^^^ the trait `IntoPyCallbackOutput<_>` is not implemented for `Result<(), MyError>` | ::: $WORKSPACE/src/callback.rs