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

Use subclass correctly in tp_new #990

Merged
merged 2 commits into from Jun 22, 2020
Merged

Use subclass correctly in tp_new #990

merged 2 commits into from Jun 22, 2020

Conversation

kngwyu
Copy link
Member

@kngwyu kngwyu commented Jun 21, 2020

Fixes #947.
As documented, we have to call subtype->tp_alloc(subtype, nitems) in tp_new.
In this PR, I also renamed PyClassAlloc::alloc to new, since it does not actually override tp_alloc.

@kngwyu kngwyu force-pushed the tpnew-fix branch 2 times, most recently from 3eade55 to b27f713 Compare June 21, 2020 15:06
@kngwyu
Copy link
Member Author

kngwyu commented Jun 21, 2020

image

This segfault means that our tp_dealloc does something wrong when it is called for subtype 🤔

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

👍 Nicely fixed!

CHANGELOG.md Outdated Show resolved Hide resolved
examples/rustapi_module/tests/test_subclassing.py Outdated Show resolved Hide resolved
src/pyclass_init.rs Outdated Show resolved Hide resolved
src/pyclass_init.rs Outdated Show resolved Hide resolved
@@ -193,7 +193,7 @@ pub fn impl_wrap_new(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
quote! {
#[allow(unused_mut)]
unsafe extern "C" fn __wrap(
_cls: *mut pyo3::ffi::PyTypeObject,
subcls: *mut pyo3::ffi::PyTypeObject,
Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to keep the cls name convention maybe.

Suggested change
subcls: *mut pyo3::ffi::PyTypeObject,
cls: *mut pyo3::ffi::PyTypeObject,

Copy link
Member Author

Choose a reason for hiding this comment

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

image
Maybe subtype is better?

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes good find 👍 I think subtype is probably better than cls here and also for create_cell_with_type in that case!

@davidhewitt davidhewitt mentioned this pull request Jun 21, 2020
1 task
@kngwyu
Copy link
Member Author

kngwyu commented Jun 22, 2020

Thank you for the reviews 👍

@kngwyu kngwyu merged commit 4c04268 into PyO3:master Jun 22, 2020
@kngwyu kngwyu deleted the tpnew-fix branch June 22, 2020 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Subclassing pyclass from python - new returns wrong type
2 participants