Skip to content
Draft
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: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ required-features = ["macros"]
name = "test_gc"
required-features = ["macros"]

[[test]]
name = "test_pygcintegration"
required-features = ["macros"]

[[test]]
name = "test_getter_setter"
required-features = ["macros"]
Expand Down
1 change: 1 addition & 0 deletions newsfragments/6330.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `PyGcTraversable` and `#[derive(PyGcTraversable)]` for opt-in GC traversal derivation, including field-level `#[pyo3(gc = false)]` support and a `PyGcOpaque<T>` wrapper for explicit traversal opt-out in recursion breakpoints.
2 changes: 2 additions & 0 deletions pyo3-macros-backend/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub mod kw {
syn::custom_keyword!(unsendable);
syn::custom_keyword!(weakref);
syn::custom_keyword!(generic);
syn::custom_keyword!(gc);
syn::custom_keyword!(gil_used);
syn::custom_keyword!(warn);
syn::custom_keyword!(message);
Expand Down Expand Up @@ -392,6 +393,7 @@ pub type FromPyWithAttribute = KeywordAttribute<kw::from_py_with, ExprPath>;
pub type IntoPyWithAttribute = KeywordAttribute<kw::into_py_with, ExprPath>;

pub type DefaultAttribute = OptionalKeywordAttribute<Token![default], Expr>;
pub type GcAttribute = KeywordAttribute<kw::gc, LitBool>;

/// For specifying the path to the pyo3 crate.
pub type CrateAttribute = KeywordAttribute<Token![crate], LitStrValue<Path>>;
Expand Down
56 changes: 28 additions & 28 deletions pyo3-macros-backend/src/derive_attributes.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Was this file change line endings?

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::attributes::{
self, get_pyo3_options, CrateAttribute, DefaultAttribute, FromPyWithAttribute,
IntoPyWithAttribute, RenameAllAttribute,
};
use crate::attributes::{
self, get_pyo3_options, CrateAttribute, DefaultAttribute, FromPyWithAttribute,
IntoPyWithAttribute, RenameAllAttribute,
};
use proc_macro2::Span;
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
Expand Down Expand Up @@ -111,12 +111,12 @@ impl FieldGetter {
}
}

pub enum FieldAttribute {
Getter(FieldGetter),
FromPyWith(FromPyWithAttribute),
IntoPyWith(IntoPyWithAttribute),
Default(DefaultAttribute),
}
pub enum FieldAttribute {
Getter(FieldGetter),
FromPyWith(FromPyWithAttribute),
IntoPyWith(IntoPyWithAttribute),
Default(DefaultAttribute),
}

impl Parse for FieldAttribute {
fn parse(input: ParseStream<'_>) -> Result<Self> {
Expand Down Expand Up @@ -159,21 +159,21 @@ impl Parse for FieldAttribute {
input.parse().map(Self::FromPyWith)
} else if lookahead.peek(attributes::kw::into_py_with) {
input.parse().map(FieldAttribute::IntoPyWith)
} else if lookahead.peek(Token![default]) {
input.parse().map(Self::Default)
} else {
Err(lookahead.error())
}
} else if lookahead.peek(Token![default]) {
input.parse().map(Self::Default)
} else {
Err(lookahead.error())
}
}
}

#[derive(Clone, Debug, Default)]
pub struct FieldAttributes {
pub getter: Option<FieldGetter>,
pub from_py_with: Option<FromPyWithAttribute>,
pub into_py_with: Option<IntoPyWithAttribute>,
pub default: Option<DefaultAttribute>,
}
pub struct FieldAttributes {
pub getter: Option<FieldGetter>,
pub from_py_with: Option<FromPyWithAttribute>,
pub into_py_with: Option<IntoPyWithAttribute>,
pub default: Option<DefaultAttribute>,
}

impl FieldAttributes {
/// Extract the field attributes.
Expand Down Expand Up @@ -208,10 +208,10 @@ impl FieldAttributes {
FieldAttribute::Getter(getter) => {
set_option!(getter, "only one of `attribute` or `item` can be provided")
}
FieldAttribute::FromPyWith(from_py_with) => set_option!(from_py_with),
FieldAttribute::IntoPyWith(into_py_with) => set_option!(into_py_with),
FieldAttribute::Default(default) => set_option!(default),
}
Ok(())
}
}
FieldAttribute::FromPyWith(from_py_with) => set_option!(from_py_with),
FieldAttribute::IntoPyWith(into_py_with) => set_option!(into_py_with),
FieldAttribute::Default(default) => set_option!(default),
}
Ok(())
}
}
2 changes: 2 additions & 0 deletions pyo3-macros-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod params;
mod py_expr;
mod pyclass;
mod pyfunction;
mod pygcintegration;
mod pyimpl;
mod pymethod;
mod quotes;
Expand All @@ -32,5 +33,6 @@ pub use intopyobject::build_derive_into_pyobject;
pub use module::{pymodule_function_impl, pymodule_module_impl, PyModuleOptions};
pub use pyclass::{build_py_class, build_py_enum, PyClassArgs};
pub use pyfunction::{build_py_function, PyFunctionOptions};
pub use pygcintegration::build_derive_py_gc_integration;
pub use pyimpl::{build_py_methods, PyClassMethodsType};
pub use utils::get_doc;
Loading
Loading