Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move macros into separate feature. #897

Merged
merged 1 commit into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ 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 }
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"
Expand All @@ -36,7 +36,8 @@ trybuild = "1.0.23"
version_check = "0.9.1"

[features]
default = []
default = ["macros"]
macros = ["indoc", "inventory", "paste", "pyo3cls", "unindent"]

# this is no longer needed internally, but setuptools-rust assumes this feature
python3 = []
Expand Down
10 changes: 10 additions & 0 deletions src/class/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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;
Expand All @@ -149,6 +150,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;
Expand All @@ -161,3 +163,11 @@ pub trait PyMethodsImpl {
.collect()
}
}

#[doc(hidden)]
#[cfg(not(feature = "macros"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

pub trait PyMethodsImpl {
fn py_methods() -> Vec<&'static PyMethodDefType> {
Vec::new()
}
}
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,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)]
Expand Down Expand Up @@ -197,6 +194,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
Expand Down Expand Up @@ -278,6 +276,7 @@ macro_rules! wrap_pymodule {
/// If you need to handle failures, please use [Python::run] directly.
///
#[macro_export]
#[cfg(feature = "macros")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry for my lazy review but could you please add this definition?

#[macro_export]
#[cfg(not(feature = "macros"))]
macro_rules! py_run {
    ($py:expr, $($val:ident)+, $code:expr) => {{
        pyo3::py_run_impl!($py, $($val)+, &pyo3::unindent::unindent($code))
    }};
}

unindent is a really small dependency (see its Cargo.toml) and I think it's OK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then py_run would behave differently depending on the feature? that seems confusing...

Copy link
Contributor Author

@m-ou-se m-ou-se May 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would mean that code like

let code = "asdf";
py_run!(py, a b, code);

would compile fine with feature = "macros" disabled, but will no longer compile when it gets enabled.

Copy link
Member

@kngwyu kngwyu May 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then py_run would behave differently depending on the feature?

No, it behaves the same.
But

  1. with feature="macros" and
  2. string literal is passed like py_run!(py, a, "print(a)")

it works faster thanks to indoc.

macro_rules! py_run {
($py:expr, $($val:ident)+, $code:literal) => {{
pyo3::py_run_impl!($py, $($val)+, pyo3::indoc::indoc!($code))
Expand All @@ -289,6 +288,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;
Expand Down
4 changes: 2 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,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};