-
Notifications
You must be signed in to change notification settings - Fork 763
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
pymethods: support gc protocol #2159
Conversation
|
||
/// Enforce at compile time that PyGCProtocol is implemented | ||
fn impl_gc(&self) -> TokenStream { | ||
let cls = self.cls; | ||
let attr = self.attr; | ||
if attr.is_gc { | ||
let closure_name = format!("__assertion_closure_{}", cls); | ||
let closure_token = syn::Ident::new(&closure_name, Span::call_site()); | ||
quote! { | ||
fn #closure_token() { | ||
use _pyo3::class; | ||
|
||
fn _assert_implements_protocol<'p, T: _pyo3::class::PyGCProtocol<'p>>() {} | ||
_assert_implements_protocol::<#cls>(); | ||
} | ||
} | ||
} else { | ||
quote! {} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check isn't needed on Python 3.11, however it is needed on older Python versions else #[pyclass(gc)]
without __traverse__
will cause segfaults.
I think a better solution might be to deprecate #[pyclass(gc)]
and then in src/pyclass.rs
we can check for the presence of __traverse__
to decide whether to set the Py_TPFLAGS_HAVE_GC
flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed a commit which deprecates #[pyclass(gc)]
as suggested.
I'll leave this one up over the weekend in case anyone wants to review! Then some time next week I'll merge, deprecate |
It seems like you're generating a normal slot for |
Good question. I'm not 100% on this; but what I do see is in the usage of Whereas uses of |
This piece of documentation still mentions pyclass(gc) and needs deleting: https://github.com/PyO3/pyo3/blob/main/pyo3-macros/src/lib.rs#L90 |
Regarding error messages, it does indeed look like errors in
I get the following output when the GC runs:
|
Thanks, pushed! |
Ok, nice. The other question is then if |
I would argue even if it can't execute Python code, it doesn't need to be We should definitely document that a correct implementation of |
f1ab735
to
79123b3
Compare
I'm going to merge this shortly and proceed forwards with a final PR to deprecate |
All the docs still need to be moved from proto-centric to method-centric, with (maybe?) mentioning the old proto stuff. |
Agreed, I'll do that in the next PR 👍 |
Are you already working on the docs PR? If not I'd make a start today. |
See #2173 (comment) - please feel free to take over that branch! |
This is the last step for #1884. Copies the same design for
__traverse__
and__clear__
fromPyGCProtocol
onto#[pymethods]
.It's not the cleanest implementation, but it works fine. Once merged, we can mark
#[pyproto]
as deprecated and get on with releasing 0.16 😄