Skip to content

Commit 1ea3463

Browse files
authored
Merge pull request #2096 from davidhewitt/pyproto-feature
Add a `pyproto` feature
2 parents 6db7dd7 + ecfd2c2 commit 1ea3463

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2147
-1516
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ serde_json = "1.0.61"
5252
pyo3-build-config = { path = "pyo3-build-config", version = "0.15.1", features = ["resolve-config"] }
5353

5454
[features]
55-
default = ["macros"]
55+
default = ["macros", "pyproto"]
5656

5757
# Enables macros: #[pyclass], #[pymodule], #[pyfunction] etc.
5858
macros = ["pyo3-macros", "indoc", "unindent"]
5959

6060
# Enables multiple #[pymethods] per #[pyclass]
6161
multiple-pymethods = ["inventory", "pyo3-macros/multiple-pymethods"]
6262

63+
# Enables deprecated #[pyproto] macro
64+
pyproto = ["pyo3-macros/pyproto"]
65+
6366
# Use this feature when building an extension module.
6467
# It tells the linker to keep the python symbols unresolved,
6568
# so that the module can also be used with statically linked python interpreters.
@@ -83,7 +86,7 @@ nightly = []
8386

8487
# Activates all additional features
8588
# This is mostly intended for testing purposes - activating *all* of these isn't particularly useful.
86-
full = ["macros", "multiple-pymethods", "num-bigint", "num-complex", "hashbrown", "serde", "indexmap", "eyre", "anyhow"]
89+
full = ["macros", "pyproto", "multiple-pymethods", "num-bigint", "num-complex", "hashbrown", "serde", "indexmap", "eyre", "anyhow"]
8790

8891
[[bench]]
8992
name = "bench_call"

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
ALL_ADDITIVE_FEATURES = macros multiple-pymethods num-bigint num-complex hashbrown serde indexmap eyre anyhow
44
COVERAGE_PACKAGES = --package pyo3 --package pyo3-build-config --package pyo3-macros-backend --package pyo3-macros
55

6-
list_all_additive_features:
7-
@echo $(ALL_ADDITIVE_FEATURES)
8-
96
test: lint test_py
107
cargo test
118
cargo test --features="abi3"

guide/src/class.md

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -849,56 +849,36 @@ impl pyo3::IntoPy<PyObject> for MyClass {
849849
}
850850
}
851851

852-
impl pyo3::class::impl_::PyClassImpl for MyClass {
852+
impl pyo3::impl_::pyclass::PyClassImpl for MyClass {
853853
const DOC: &'static str = "Class for demonstration\u{0}";
854854
const IS_GC: bool = false;
855855
const IS_BASETYPE: bool = false;
856856
const IS_SUBCLASS: bool = false;
857857
type Layout = PyCell<MyClass>;
858858
type BaseType = PyAny;
859-
type ThreadChecker = pyo3::class::impl_::ThreadCheckerStub<MyClass>;
859+
type ThreadChecker = pyo3::impl_::pyclass::ThreadCheckerStub<MyClass>;
860860

861-
fn for_each_method_def(visitor: &mut dyn FnMut(&[pyo3::class::PyMethodDefType])) {
862-
use pyo3::class::impl_::*;
861+
fn for_all_items(visitor: &mut dyn FnMut(&pyo3::impl_::pyclass::PyClassItems)) {
862+
use pyo3::impl_::pyclass::*;
863863
let collector = PyClassImplCollector::<MyClass>::new();
864864
visitor(collector.py_methods());
865-
visitor(collector.py_class_descriptors());
866-
visitor(collector.object_protocol_methods());
867-
visitor(collector.async_protocol_methods());
868-
visitor(collector.descr_protocol_methods());
869-
visitor(collector.mapping_protocol_methods());
870-
visitor(collector.number_protocol_methods());
865+
visitor(collector.pyclass_intrinsic_items());
871866
}
872867
fn get_new() -> Option<pyo3::ffi::newfunc> {
873-
use pyo3::class::impl_::*;
868+
use pyo3::impl_::pyclass::*;
874869
let collector = PyClassImplCollector::<Self>::new();
875870
collector.new_impl()
876871
}
877872
fn get_alloc() -> Option<pyo3::ffi::allocfunc> {
878-
use pyo3::class::impl_::*;
873+
use pyo3::impl_::pyclass::*;
879874
let collector = PyClassImplCollector::<Self>::new();
880875
collector.alloc_impl()
881876
}
882877
fn get_free() -> Option<pyo3::ffi::freefunc> {
883-
use pyo3::class::impl_::*;
878+
use pyo3::impl_::pyclass::*;
884879
let collector = PyClassImplCollector::<Self>::new();
885880
collector.free_impl()
886881
}
887-
fn for_each_proto_slot(visitor: &mut dyn FnMut(&[pyo3::ffi::PyType_Slot])) {
888-
// Implementation which uses dtolnay specialization to load all slots.
889-
use pyo3::class::impl_::*;
890-
let collector = PyClassImplCollector::<Self>::new();
891-
visitor(collector.object_protocol_slots());
892-
visitor(collector.number_protocol_slots());
893-
visitor(collector.iter_protocol_slots());
894-
visitor(collector.gc_protocol_slots());
895-
visitor(collector.descr_protocol_slots());
896-
visitor(collector.mapping_protocol_slots());
897-
visitor(collector.sequence_protocol_slots());
898-
visitor(collector.async_protocol_slots());
899-
visitor(collector.buffer_protocol_slots());
900-
visitor(collector.methods_protocol_slots());
901-
}
902882
}
903883
# Python::with_gil(|py| {
904884
# let cls = py.get_type::<MyClass>();

guide/src/features.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ Most users should only need a single `#[pymethods]` per `#[pyclass]`. In additio
6565

6666
See [the `#[pyclass]` implementation details](class.md#implementation-details) for more information.
6767

68+
### `pyproto`
69+
70+
This feature enables the `#[pyproto]` macro, which is an alternative (older, soon-to-be-deprecated) to `#[pymethods]` for defining magic methods such as `__eq__`.
71+
72+
> This feature is enabled by default. To disable it, set `default-features = false` for the `pyo3` entry in your Cargo.toml.
73+
6874
### `nightly`
6975

7076
The `nightly` feature needs the nightly Rust compiler. This allows PyO3 to use Rust's unstable specialization feature to apply the following optimizations:

pyo3-macros-backend/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ pyo3-build-config = { path = "../pyo3-build-config", version = "0.15.1", feature
2222
version = "1"
2323
default-features = false
2424
features = ["derive", "parsing", "printing", "clone-impls", "full", "extra-traits"]
25+
26+
[features]
27+
pyproto = []

pyo3-macros-backend/src/defs.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,13 @@ impl Proto {
6464
})
6565
}
6666

67-
pub(crate) fn slots_trait(&self) -> syn::Ident {
68-
syn::Ident::new(&format!("Py{}ProtocolSlots", self.name), Span::call_site())
67+
pub(crate) fn items_trait(&self) -> syn::Ident {
68+
syn::Ident::new(&format!("Py{}ProtocolItems", self.name), Span::call_site())
6969
}
7070

71-
pub(crate) fn slots_trait_slots(&self) -> syn::Ident {
71+
pub(crate) fn items_trait_items(&self) -> syn::Ident {
7272
syn::Ident::new(
73-
&format!("{}_protocol_slots", self.name.to_ascii_lowercase()),
74-
Span::call_site(),
75-
)
76-
}
77-
78-
pub(crate) fn methods_trait(&self) -> syn::Ident {
79-
syn::Ident::new(
80-
&format!("Py{}ProtocolMethods", self.name),
81-
Span::call_site(),
82-
)
83-
}
84-
85-
pub(crate) fn methods_trait_methods(&self) -> syn::Ident {
86-
syn::Ident::new(
87-
&format!("{}_protocol_methods", self.name.to_ascii_lowercase()),
73+
&format!("{}_protocol_items", self.name.to_ascii_lowercase()),
8874
Span::call_site(),
8975
)
9076
}

pyo3-macros-backend/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
99
mod utils;
1010

1111
mod attributes;
12+
#[cfg(feature = "pyproto")]
1213
mod defs;
1314
mod deprecations;
1415
mod frompyobject;
1516
mod konst;
1617
mod method;
1718
mod module;
1819
mod params;
20+
#[cfg(feature = "pyproto")]
1921
mod proto_method;
2022
mod pyclass;
2123
mod pyfunction;
2224
mod pyimpl;
2325
mod pymethod;
26+
#[cfg(feature = "pyproto")]
2427
mod pyproto;
2528
mod wrap;
2629

@@ -29,6 +32,7 @@ pub use module::{process_functions_in_module, pymodule_impl, PyModuleOptions};
2932
pub use pyclass::{build_py_class, build_py_enum, PyClassArgs};
3033
pub use pyfunction::{build_py_function, PyFunctionOptions};
3134
pub use pyimpl::{build_py_methods, PyClassMethodsType};
35+
#[cfg(feature = "pyproto")]
3236
pub use pyproto::build_py_proto;
3337
pub use utils::get_doc;
3438
pub use wrap::{wrap_pyfunction_impl, wrap_pymodule_impl, WrapPyFunctionArgs};

pyo3-macros-backend/src/method.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -566,23 +566,23 @@ impl<'a> FnSpec<'a> {
566566
let doc = &self.doc;
567567
match self.convention {
568568
CallingConvention::Noargs => quote! {
569-
_pyo3::class::methods::PyMethodDef::noargs(
569+
_pyo3::impl_::pymethods::PyMethodDef::noargs(
570570
#python_name,
571-
_pyo3::class::methods::PyCFunction(#wrapper),
571+
_pyo3::impl_::pymethods::PyCFunction(#wrapper),
572572
#doc,
573573
)
574574
},
575575
CallingConvention::Fastcall => quote! {
576-
_pyo3::class::methods::PyMethodDef::fastcall_cfunction_with_keywords(
576+
_pyo3::impl_::pymethods::PyMethodDef::fastcall_cfunction_with_keywords(
577577
#python_name,
578-
_pyo3::class::methods::PyCFunctionFastWithKeywords(#wrapper),
578+
_pyo3::impl_::pymethods::PyCFunctionFastWithKeywords(#wrapper),
579579
#doc,
580580
)
581581
},
582582
CallingConvention::Varargs => quote! {
583-
_pyo3::class::methods::PyMethodDef::cfunction_with_keywords(
583+
_pyo3::impl_::pymethods::PyMethodDef::cfunction_with_keywords(
584584
#python_name,
585-
_pyo3::class::methods::PyCFunctionWithKeywords(#wrapper),
585+
_pyo3::impl_::pymethods::PyCFunctionWithKeywords(#wrapper),
586586
#doc,
587587
)
588588
},

0 commit comments

Comments
 (0)