Skip to content

Commit ada41c1

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 0f07cf8 commit ada41c1

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

.github/workflows/test.yml

+3-1
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

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ 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 }
2727
num-traits = "0.2.8"
2828
parking_lot = { version = "0.10.2" }
29-
paste = "0.1.6"
30-
pyo3cls = { path = "pyo3cls", version = "=0.9.2" }
31-
unindent = "0.1.4"
29+
paste = { version = "0.1.6", optional = true }
30+
pyo3cls = { path = "pyo3cls", version = "=0.9.2", optional = true }
31+
unindent = { version = "0.1.4", optional = true }
3232

3333
[dev-dependencies]
3434
assert_approx_eq = "1.1.0"
@@ -41,7 +41,8 @@ serde = { version = "1.0", features = ["derive"] }
4141
serde_json = "1.0"
4242

4343
[features]
44-
default = []
44+
default = ["macros"]
45+
macros = ["indoc", "inventory", "paste", "pyo3cls", "unindent"]
4546

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

src/class/methods.rs

+10
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl PySetterDef {
119119
/// Allows arbitrary pymethod blocks to submit their methods, which are eventually
120120
/// collected by pyclass.
121121
#[doc(hidden)]
122+
#[cfg(feature = "macros")]
122123
pub trait PyMethodsInventory: inventory::Collect {
123124
/// Create a new instance
124125
fn new(methods: &'static [PyMethodDefType]) -> Self;
@@ -130,6 +131,7 @@ pub trait PyMethodsInventory: inventory::Collect {
130131
/// Implementation detail. Only to be used through the proc macros.
131132
/// For pyclass derived structs, this trait collects method from all impl blocks using inventory.
132133
#[doc(hidden)]
134+
#[cfg(feature = "macros")]
133135
pub trait PyMethodsImpl {
134136
/// Normal methods. Mainly defined by `#[pymethod]`.
135137
type Methods: PyMethodsInventory;
@@ -142,3 +144,11 @@ pub trait PyMethodsImpl {
142144
.collect()
143145
}
144146
}
147+
148+
#[doc(hidden)]
149+
#[cfg(not(feature = "macros"))]
150+
pub trait PyMethodsImpl {
151+
fn py_methods() -> Vec<&'static PyMethodDefType> {
152+
Vec::new()
153+
}
154+
}

src/lib.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,18 @@ pub use crate::type_object::{type_flags, PyTypeInfo};
152152
// Since PyAny is as important as PyObject, we expose it to the top level.
153153
pub use crate::types::PyAny;
154154

155-
// Re-exported for wrap_function
155+
#[cfg(feature = "macros")]
156156
#[doc(hidden)]
157-
pub use paste;
158-
// Re-exported for py_run
159-
#[doc(hidden)]
160-
pub use indoc;
161-
// Re-exported for pymethods
162-
#[doc(hidden)]
163-
pub use inventory;
157+
pub use {
158+
paste, // Re-exported for wrap_function
159+
indoc, // Re-exported for py_run
160+
inventory, // Re-exported for pymethods
161+
unindent, // Re-exported for py_run
162+
};
163+
164164
// Re-exported for the `__wrap` functions
165165
#[doc(hidden)]
166166
pub use libc;
167-
// Re-exported for py_run
168-
#[doc(hidden)]
169-
pub use unindent;
170167

171168
pub mod buffer;
172169
#[doc(hidden)]
@@ -199,6 +196,7 @@ pub mod type_object;
199196
pub mod types;
200197

201198
/// The proc macros, which are also part of the prelude.
199+
#[cfg(feature = "macros")]
202200
pub mod proc_macro {
203201
pub use pyo3cls::pymodule;
204202
/// The proc macro attributes
@@ -280,6 +278,7 @@ macro_rules! wrap_pymodule {
280278
/// If you need to handle failures, please use [Python::run] directly.
281279
///
282280
#[macro_export]
281+
#[cfg(feature = "macros")]
283282
macro_rules! py_run {
284283
($py:expr, $($val:ident)+, $code:literal) => {{
285284
pyo3::py_run_impl!($py, $($val)+, pyo3::indoc::indoc!($code))
@@ -291,6 +290,7 @@ macro_rules! py_run {
291290

292291
#[macro_export]
293292
#[doc(hidden)]
293+
#[cfg(feature = "macros")]
294294
macro_rules! py_run_impl {
295295
($py:expr, $($val:ident)+, $code:expr) => {{
296296
use pyo3::types::IntoPyDict;

src/prelude.rs

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

0 commit comments

Comments
 (0)