-
Notifications
You must be signed in to change notification settings - Fork 784
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
Complete abi3 support #1152
Complete abi3 support #1152
Changes from all commits
4004620
62a175e
d2a10b6
c2f10e2
1941f4d
e0f75f8
4cd6d4c
3b61df2
80e2497
71a7b1a
4325a59
e8936be
0709a02
679326e
117f60b
a009c23
d6c9435
0bc2393
7a4c5e2
5bfb467
4d5c208
3cb0b11
afc2d10
d0c2ebf
1b2d267
517af8c
2ec1c3b
870914d
a2dc4c1
ba10560
c87a59c
c07e1aa
2a85c17
7644d67
9d85591
4862f56
869a5e2
1985578
e33e58f
d8c8c17
e615ce8
c22dd6c
0fde737
20a93ed
140790b
398369f
d42dbda
877667a
137196d
aabad7c
0665c02
9e34835
5060379
2923b4d
4298435
ba6f0ec
265db33
781bb9f
90a825d
f74b649
eb8ff15
6627658
16ad3bf
95bec25
eb0e6f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,7 +205,7 @@ or by `self_.into_super()` as `PyRef<Self::BaseClass>`. | |
```rust | ||
# use pyo3::prelude::*; | ||
|
||
#[pyclass] | ||
#[pyclass(subclass)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like a breaking change that needs to go in the CHANGELOG, and possibly other guide documentation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I agree. |
||
struct BaseClass { | ||
val1: usize, | ||
} | ||
|
@@ -222,7 +222,7 @@ impl BaseClass { | |
} | ||
} | ||
|
||
#[pyclass(extends=BaseClass)] | ||
#[pyclass(extends=BaseClass, subclass)] | ||
struct SubClass { | ||
val2: usize, | ||
} | ||
|
@@ -266,12 +266,14 @@ impl SubSubClass { | |
``` | ||
|
||
You can also inherit native types such as `PyDict`, if they implement | ||
[`PySizedLayout`](https://docs.rs/pyo3/latest/pyo3/type_object/trait.PySizedLayout.html). | ||
[`PySizedLayout`](https://docs.rs/pyo3/latest/pyo3/type_object/trait.PySizedLayout.html). However, this is not supported when building for the Python limited API (aka the `abi3` feature of PyO3). | ||
|
||
However, because of some technical problems, we don't currently provide safe upcasting methods for types | ||
that inherit native types. Even in such cases, you can unsafely get a base class by raw pointer conversion. | ||
|
||
```rust | ||
# #[cfg(Py_LIMITED_API)] fn main() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What part of this example is not supported by the limited api? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Subclassing a builtin type ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, of course. It could be worth adding a comment in this example which says this only works with the unlimited API. (Also the paragraph above starting on line 268 could also benefit from a similar statement saying inheriting native types is not possible with the limited api.) |
||
# #[cfg(not(Py_LIMITED_API))] fn main() { | ||
# use pyo3::prelude::*; | ||
use pyo3::types::PyDict; | ||
use pyo3::{AsPyPointer, PyNativeType}; | ||
|
@@ -300,6 +302,7 @@ impl DictWithCounter { | |
# let py = gil.python(); | ||
# let cnt = pyo3::PyCell::new(py, DictWithCounter::new()).unwrap(); | ||
# pyo3::py_run!(py, cnt, "cnt.set('abc', 10); assert cnt['abc'] == 10") | ||
# } | ||
``` | ||
|
||
If `SubClass` does not provide a baseclass initialization, the compilation fails. | ||
|
@@ -769,13 +772,23 @@ impl pyo3::class::methods::HasMethodsInventory for MyClass { | |
} | ||
pyo3::inventory::collect!(Pyo3MethodsInventoryForMyClass); | ||
|
||
impl pyo3::class::proto_methods::HasProtoRegistry for MyClass { | ||
fn registry() -> &'static pyo3::class::proto_methods::PyProtoRegistry { | ||
static REGISTRY: pyo3::class::proto_methods::PyProtoRegistry | ||
= pyo3::class::proto_methods::PyProtoRegistry::new(); | ||
®ISTRY | ||
|
||
pub struct Pyo3ProtoInventoryForMyClass { | ||
def: pyo3::class::proto_methods::PyProtoMethodDef, | ||
} | ||
impl pyo3::class::proto_methods::PyProtoInventory for Pyo3ProtoInventoryForMyClass { | ||
fn new(def: pyo3::class::proto_methods::PyProtoMethodDef) -> Self { | ||
Self { def } | ||
} | ||
fn get(&'static self) -> &'static pyo3::class::proto_methods::PyProtoMethodDef { | ||
&self.def | ||
} | ||
} | ||
impl pyo3::class::proto_methods::HasProtoInventory for MyClass { | ||
type ProtoMethods = Pyo3ProtoInventoryForMyClass; | ||
} | ||
pyo3::inventory::collect!(Pyo3ProtoInventoryForMyClass); | ||
|
||
|
||
impl pyo3::pyclass::PyClassSend for MyClass { | ||
type ThreadChecker = pyo3::pyclass::ThreadCheckerStub<MyClass>; | ||
|
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.
@konstin
Is it OK to say this?
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.
👍
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.
Thanks!