Skip to content

Commit dab8c2e

Browse files
committed
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.
1 parent 818ebf7 commit dab8c2e

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ jobs:
2828
toolchain: nightly
2929
default: true
3030
- run: rustup set default-host ${{ matrix.platform.rust-target }}
31-
- name: Build
31+
- name: Build without default features
32+
run: cargo build --no-default-features --verbose
33+
- name: Build with default features
3234
run: cargo build --verbose
3335
- name: Install test dependencies
3436
run: |

Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ travis-ci = { repository = "PyO3/pyo3", branch = "master" }
1919
appveyor = { repository = "fafhrd91/pyo3" }
2020

2121
[dependencies]
22-
indoc = "0.3.4"
23-
inventory = "0.1.4"
22+
indoc = { version = "0.3.4", optional = true }
23+
inventory = { version = "0.1.4", optional = true }
2424
libc = "0.2.62"
2525
num-bigint = { version = "0.2", optional = true }
2626
num-complex = { version = "0.2", optional = true }
27-
paste = "0.1.6"
28-
pyo3cls = { path = "pyo3cls", version = "=0.9.2" }
29-
unindent = "0.1.4"
27+
paste = { version = "0.1.6", optional = true }
28+
pyo3cls = { path = "pyo3cls", version = "=0.9.2", optional = true }
29+
unindent = { version = "0.1.4", optional = true }
3030

3131
[dev-dependencies]
3232
assert_approx_eq = "1.1.0"
@@ -36,7 +36,8 @@ trybuild = "1.0.23"
3636
version_check = "0.9.1"
3737

3838
[features]
39-
default = []
39+
default = ["macros"]
40+
macros = ["indoc", "inventory", "paste", "pyo3cls", "unindent"]
4041

4142
# this is no longer needed internally, but setuptools-rust assumes this feature
4243
python3 = []

src/class/methods.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ impl PySetterDef {
138138
/// Allows arbitrary pymethod blocks to submit their methods, which are eventually
139139
/// collected by pyclass.
140140
#[doc(hidden)]
141+
#[cfg(feature = "macros")]
141142
pub trait PyMethodsInventory: inventory::Collect {
142143
/// Create a new instance
143144
fn new(methods: &'static [PyMethodDefType]) -> Self;
@@ -149,6 +150,7 @@ pub trait PyMethodsInventory: inventory::Collect {
149150
/// Implementation detail. Only to be used through the proc macros.
150151
/// For pyclass derived structs, this trait collects method from all impl blocks using inventory.
151152
#[doc(hidden)]
153+
#[cfg(feature = "macros")]
152154
pub trait PyMethodsImpl {
153155
/// Normal methods. Mainly defined by `#[pymethod]`.
154156
type Methods: PyMethodsInventory;
@@ -161,3 +163,11 @@ pub trait PyMethodsImpl {
161163
.collect()
162164
}
163165
}
166+
167+
#[doc(hidden)]
168+
#[cfg(not(feature = "macros"))]
169+
pub trait PyMethodsImpl {
170+
fn py_methods() -> Vec<&'static PyMethodDefType> {
171+
Vec::new()
172+
}
173+
}

src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,18 @@ pub use crate::type_object::{type_flags, PyTypeInfo};
150150
// Since PyAny is as important as PyObject, we expose it to the top level.
151151
pub use crate::types::PyAny;
152152

153-
// Re-exported for wrap_function
153+
#[cfg(feature = "macros")]
154154
#[doc(hidden)]
155-
pub use paste;
156-
// Re-exported for py_run
157-
#[doc(hidden)]
158-
pub use indoc;
159-
// Re-exported for pymethods
160-
#[doc(hidden)]
161-
pub use inventory;
155+
pub use {
156+
indoc, // Re-exported for py_run
157+
inventory, // Re-exported for pymethods
158+
paste, // Re-exported for wrap_function
159+
unindent, // Re-exported for py_run
160+
};
161+
162162
// Re-exported for the `__wrap` functions
163163
#[doc(hidden)]
164164
pub use libc;
165-
// Re-exported for py_run
166-
#[doc(hidden)]
167-
pub use unindent;
168165

169166
pub mod buffer;
170167
#[doc(hidden)]
@@ -197,6 +194,7 @@ pub mod type_object;
197194
pub mod types;
198195

199196
/// The proc macros, which are also part of the prelude.
197+
#[cfg(feature = "macros")]
200198
pub mod proc_macro {
201199
pub use pyo3cls::pymodule;
202200
/// The proc macro attributes
@@ -278,6 +276,7 @@ macro_rules! wrap_pymodule {
278276
/// If you need to handle failures, please use [Python::run] directly.
279277
///
280278
#[macro_export]
279+
#[cfg(feature = "macros")]
281280
macro_rules! py_run {
282281
($py:expr, $($val:ident)+, $code:literal) => {{
283282
pyo3::py_run_impl!($py, $($val)+, pyo3::indoc::indoc!($code))
@@ -289,6 +288,7 @@ macro_rules! py_run {
289288

290289
#[macro_export]
291290
#[doc(hidden)]
291+
#[cfg(feature = "macros")]
292292
macro_rules! py_run_impl {
293293
($py:expr, $($val:ident)+, $code:expr) => {{
294294
use pyo3::types::IntoPyDict;

src/prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ pub use crate::python::Python;
2020
pub use crate::{FromPy, FromPyObject, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto, ToPyObject};
2121
// PyModule is only part of the prelude because we need it for the pymodule function
2222
pub use crate::types::{PyAny, PyModule};
23-
pub use pyo3cls::pymodule;
24-
pub use pyo3cls::{pyclass, pyfunction, pymethods, pyproto};
23+
#[cfg(feature = "macros")]
24+
pub use pyo3cls::{pyclass, pyfunction, pymethods, pymodule, pyproto};

0 commit comments

Comments
 (0)