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

Improve native object initialization #4798

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mbway
Copy link

@mbway mbway commented Dec 14, 2024

Before this change, classes inheriting the base object were constructed by using TP_ALLOC directly, obscuring the intention of PyNativeTypeInitializer, and inheriting from other base classes requires use of the unlimited API.

Previously this was not very limiting, but with #4678 it will be possible to inherit from native base classes with the limited API and possibly from dynamically imported native base classes which may require __new__ arguments to reach them.

@mbway mbway force-pushed the improve_native_new branch 3 times, most recently from 43ba51b to 377feb8 Compare December 14, 2024 16:56
@@ -830,12 +830,14 @@ impl<'a> FnSpec<'a> {
_kwargs: *mut #pyo3_path::ffi::PyObject
Copy link
Author

Choose a reason for hiding this comment

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

I assume these are named with underscores because sometimes the generated code uses them and sometimes it doesn't?

src/impl_/pyclass.rs Outdated Show resolved Hide resolved
) -> PyResult<*mut ffi::PyObject> {
// HACK (due to FIXME below): PyBaseObject_Type's tp_new isn't happy with NULL arguments
Copy link
Author

@mbway mbway Dec 14, 2024

Choose a reason for hiding this comment

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

I think there was a misunderstanding here, calling PyBaseObject_Type::tp_new with an empty tuple and NULL kwargs is OK, but the implementation here was passing NULL for both, which is invalid.

src/impl_/pyclass_init.rs Outdated Show resolved Hide resolved
@@ -1,9 +1,60 @@
//! Contains initialization utilities for `#[pyclass]`.
//!
//! # Background
Copy link
Author

Choose a reason for hiding this comment

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

although it seems fairly straightforward once compiled like this, it took quite a bit of experimentation to make sure I was getting things correct so I wanted to document my findings somewhere

@mbway mbway force-pushed the improve_native_new branch from 377feb8 to 79ba367 Compare December 14, 2024 17:39
@mbway mbway marked this pull request as draft December 14, 2024 17:48
Comment on lines +65 to +71
let obj = tp_new(
subtype,
args.as_ptr(),
kwargs
.map(|obj| obj.as_ptr())
.unwrap_or(std::ptr::null_mut()),
);
Copy link
Author

Choose a reason for hiding this comment

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

there is a limitation here, where in python a class has to call super(MyClass, cls).__new__(cls, *args, **kwargs) manually so it can choose which options to propagate to the super class. With the current approach the arguments propagate as-is

@mbway mbway force-pushed the improve_native_new branch from c5e2f75 to 9b946aa Compare December 14, 2024 19:49
@mbway mbway marked this pull request as ready for review December 14, 2024 20:19
Before this change, classes inheriting the base object was a special
case where `object.__new__` is not called, and inheriting from other
base classes requires use of the unlimited API.

Previously this was not very limiting, but with <PyO3#4678>
it will be possible to inherit from native base classes with the
limited API and possibly from dynamically imported native base classes
which may require `__new__` arguments to reach them.
@mbway mbway force-pushed the improve_native_new branch from 9b946aa to 57128c0 Compare December 16, 2024 23:31
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.

1 participant