From 780f5fde935fff31caab582ca0ebd7a311558223 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 4 May 2020 17:02:10 +0200 Subject: [PATCH] Move macros into separate feature. It's enabled by default to avoid breakage, but this allows compiling pyo3 with a lot less dependencies in case the macros are not needed. --- .github/workflows/test.yml | 4 +++- Cargo.toml | 13 +++++++------ src/class/methods.rs | 10 ++++++++++ src/lib.rs | 22 +++++++++++----------- src/prelude.rs | 4 ++-- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 941f4e3fb3b..c0ccb60001a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,7 +28,9 @@ jobs: toolchain: nightly default: true - run: rustup set default-host ${{ matrix.platform.rust-target }} - - name: Build + - name: Build without default features + run: cargo build --no-default-features --verbose + - name: Build with default features run: cargo build --verbose - name: Install test dependencies run: | diff --git a/Cargo.toml b/Cargo.toml index 02839551f5a..f256110633c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,16 +19,16 @@ travis-ci = { repository = "PyO3/pyo3", branch = "master" } appveyor = { repository = "fafhrd91/pyo3" } [dependencies] -indoc = "0.3.4" -inventory = "0.1.4" +indoc = { version = "0.3.4", optional = true } +inventory = { version = "0.1.4", optional = true } libc = "0.2.62" num-bigint = { version = "0.2", optional = true } num-complex = { version = "0.2", optional = true } num-traits = "0.2.8" parking_lot = { version = "0.10.2" } -paste = "0.1.6" -pyo3cls = { path = "pyo3cls", version = "=0.9.2" } -unindent = "0.1.4" +paste = { version = "0.1.6", optional = true } +pyo3cls = { path = "pyo3cls", version = "=0.9.2", optional = true } +unindent = { version = "0.1.4", optional = true } [dev-dependencies] assert_approx_eq = "1.1.0" @@ -41,7 +41,8 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" [features] -default = [] +default = ["macros"] +macros = ["indoc", "inventory", "paste", "pyo3cls", "unindent"] # this is no longer needed internally, but setuptools-rust assumes this feature python3 = [] diff --git a/src/class/methods.rs b/src/class/methods.rs index 1240c97296c..de5f204ae98 100644 --- a/src/class/methods.rs +++ b/src/class/methods.rs @@ -119,6 +119,7 @@ impl PySetterDef { /// Allows arbitrary pymethod blocks to submit their methods, which are eventually /// collected by pyclass. #[doc(hidden)] +#[cfg(feature = "macros")] pub trait PyMethodsInventory: inventory::Collect { /// Create a new instance fn new(methods: &'static [PyMethodDefType]) -> Self; @@ -130,6 +131,7 @@ pub trait PyMethodsInventory: inventory::Collect { /// Implementation detail. Only to be used through the proc macros. /// For pyclass derived structs, this trait collects method from all impl blocks using inventory. #[doc(hidden)] +#[cfg(feature = "macros")] pub trait PyMethodsImpl { /// Normal methods. Mainly defined by `#[pymethod]`. type Methods: PyMethodsInventory; @@ -142,3 +144,11 @@ pub trait PyMethodsImpl { .collect() } } + +#[doc(hidden)] +#[cfg(not(feature = "macros"))] +pub trait PyMethodsImpl { + fn py_methods() -> Vec<&'static PyMethodDefType> { + Vec::new() + } +} diff --git a/src/lib.rs b/src/lib.rs index 883b43622dc..cdbd9539e97 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -152,21 +152,18 @@ pub use crate::type_object::{type_flags, PyTypeInfo}; // Since PyAny is as important as PyObject, we expose it to the top level. pub use crate::types::PyAny; -// Re-exported for wrap_function +#[cfg(feature = "macros")] #[doc(hidden)] -pub use paste; -// Re-exported for py_run -#[doc(hidden)] -pub use indoc; -// Re-exported for pymethods -#[doc(hidden)] -pub use inventory; +pub use { + indoc, // Re-exported for py_run + inventory, // Re-exported for pymethods + paste, // Re-exported for wrap_function + unindent, // Re-exported for py_run +}; + // Re-exported for the `__wrap` functions #[doc(hidden)] pub use libc; -// Re-exported for py_run -#[doc(hidden)] -pub use unindent; pub mod buffer; #[doc(hidden)] @@ -199,6 +196,7 @@ pub mod type_object; pub mod types; /// The proc macros, which are also part of the prelude. +#[cfg(feature = "macros")] pub mod proc_macro { pub use pyo3cls::pymodule; /// The proc macro attributes @@ -280,6 +278,7 @@ macro_rules! wrap_pymodule { /// If you need to handle failures, please use [Python::run] directly. /// #[macro_export] +#[cfg(feature = "macros")] macro_rules! py_run { ($py:expr, $($val:ident)+, $code:literal) => {{ pyo3::py_run_impl!($py, $($val)+, pyo3::indoc::indoc!($code)) @@ -291,6 +290,7 @@ macro_rules! py_run { #[macro_export] #[doc(hidden)] +#[cfg(feature = "macros")] macro_rules! py_run_impl { ($py:expr, $($val:ident)+, $code:expr) => {{ use pyo3::types::IntoPyDict; diff --git a/src/prelude.rs b/src/prelude.rs index 09ce54773a0..e910a02d53c 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -21,5 +21,5 @@ pub use crate::python::Python; pub use crate::{FromPy, 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 pyo3cls::pymodule; -pub use pyo3cls::{pyclass, pyfunction, pymethods, pyproto}; +#[cfg(feature = "macros")] +pub use pyo3cls::{pyclass, pyfunction, pymethods, pymodule, pyproto};