Skip to content

First steps towards native subclassing #5679

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions Lib/test/test_raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,6 @@ def test_attrs(self):
tb.tb_next = new_tb
self.assertIs(tb.tb_next, new_tb)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_constructor(self):
other_tb = get_tb()
frame = sys._getframe()
Expand Down
92 changes: 53 additions & 39 deletions derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,15 @@ pub(crate) fn impl_pyclass_impl(attr: PunctuatedNestedMeta, item: Item) -> Resul
Ok(tokens)
}

#[allow(clippy::too_many_arguments)]
fn generate_class_def(
ident: &Ident,
name: &str,
module_name: Option<&str>,
base: Option<String>,
base: Option<&syn::ExprPath>,
metaclass: Option<String>,
unhashable: bool,
is_pystruct: bool,
attrs: &[Attribute],
) -> Result<TokenStream> {
let doc = attrs.doc().or_else(|| {
Expand Down Expand Up @@ -340,26 +342,13 @@ fn generate_class_def(
quote!(false)
};
let basicsize = quote!(std::mem::size_of::<#ident>());
let is_pystruct = attrs.iter().any(|attr| {
attr.path().is_ident("derive")
&& if let Ok(Meta::List(l)) = attr.parse_meta() {
l.nested
.into_iter()
.any(|n| n.get_ident().is_some_and(|p| p == "PyStructSequence"))
} else {
false
}
});
if base.is_some() && is_pystruct {
bail_span!(ident, "PyStructSequence cannot have `base` class attr",);
}
let base_class = if is_pystruct {
Some(quote! { rustpython_vm::builtins::PyTuple })
} else {
base.as_ref().map(|typ| {
let typ = Ident::new(typ, ident.span());
quote_spanned! { ident.span() => #typ }
})
base.map(|typ| quote!(#typ))
}
.map(|typ| {
quote! {
Expand All @@ -381,12 +370,21 @@ fn generate_class_def(
});

let base_or_object = if let Some(base) = base {
let base = Ident::new(&base, ident.span());
quote! { #base }
} else {
quote! { ::rustpython_vm::builtins::PyBaseObject }
};

let type_id = if is_pystruct {
quote!(::std::option::Option::None)
} else {
quote! {
fn _check<__T: ::rustpython_vm::PyPayload>() {}
_check::<Self>();
::std::option::Option::Some(::std::any::TypeId::of::<Self>())
}
};

let tokens = quote! {
impl ::rustpython_vm::class::PyClassDef for #ident {
const NAME: &'static str = #name;
Expand All @@ -399,14 +397,18 @@ fn generate_class_def(
type Base = #base_or_object;
}

impl ::rustpython_vm::class::StaticType for #ident {
unsafe impl ::rustpython_vm::class::StaticType for #ident {
fn static_cell() -> &'static ::rustpython_vm::common::static_cell::StaticCell<::rustpython_vm::builtins::PyTypeRef> {
::rustpython_vm::common::static_cell! {
static CELL: ::rustpython_vm::builtins::PyTypeRef;
}
&CELL
}

fn type_id() -> ::std::option::Option<::std::any::TypeId> {
#type_id
}

#meta_class

#base_class
Expand All @@ -427,6 +429,18 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result<Tok
let base = class_meta.base()?;
let metaclass = class_meta.metaclass()?;
let unhashable = class_meta.unhashable()?;
let manual_payload = class_meta.manual_payload()?;

let is_pystruct = attrs.iter().any(|attr| {
attr.path().is_ident("derive")
&& if let Ok(Meta::List(l)) = attr.parse_meta() {
l.nested
.into_iter()
.any(|n| n.get_ident().is_some_and(|p| p == "PyStructSequence"))
} else {
false
}
});

let class_def = generate_class_def(
ident,
Expand All @@ -435,6 +449,7 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result<Tok
base,
metaclass,
unhashable,
is_pystruct,
attrs,
)?;

Expand Down Expand Up @@ -487,14 +502,23 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result<Tok
}
};

let impl_payload = if let Some(ctx_type_name) = class_meta.ctx_name()? {
let ctx_type_ident = Ident::new(&ctx_type_name, ident.span()); // FIXME span

// We need this to make extend mechanism work:
let impl_payload = if !(is_pystruct || manual_payload) {
let base_ty = base
.as_ref()
.map(|t| quote!(#t))
.unwrap_or_else(|| quote!(::rustpython_vm::builtins::PyBaseObject));
let get_class = if let Some(ctx_type_ident) = class_meta.ctx_name()? {
quote!(ctx.types.#ctx_type_ident)
} else if let Some(ctx_type_ident) = class_meta.exception_ctx_name()? {
quote!(ctx.exceptions.#ctx_type_ident)
} else {
quote!(<Self as ::rustpython_vm::class::StaticType>::static_type())
};
quote! {
impl ::rustpython_vm::PyPayload for #ident {
type Super = #base_ty;
fn class(ctx: &::rustpython_vm::vm::Context) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
ctx.types.#ctx_type_ident
#get_class
}
}
}
Expand Down Expand Up @@ -537,21 +561,12 @@ pub(crate) fn impl_pyexception(attr: PunctuatedNestedMeta, item: Item) -> Result
let class_meta = ExceptionItemMeta::from_nested(ident.clone(), fake_ident, attr.into_iter())?;
let class_name = class_meta.class_name()?;

let base_class_name = class_meta.base()?;
let impl_payload = if let Some(ctx_type_name) = class_meta.ctx_name()? {
let ctx_type_ident = Ident::new(&ctx_type_name, ident.span()); // FIXME span

// We need this to make extend mechanism work:
quote! {
impl ::rustpython_vm::PyPayload for #ident {
fn class(ctx: &::rustpython_vm::vm::Context) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
ctx.exceptions.#ctx_type_ident
}
}
}
} else {
quote! {}
};
let base_class_name = class_meta
.base()?
.ok_or_else(|| syn::Error::new(Span::call_site(), "pyexception must specify base"))?;
let ctx_type_ident = class_meta
.ctx_name()?
.ok_or_else(|| syn::Error::new(Span::call_site(), "pyexception must specify ctx"))?;
let impl_pyclass = if class_meta.has_impl()? {
quote! {
#[pyexception]
Expand All @@ -562,9 +577,8 @@ pub(crate) fn impl_pyexception(attr: PunctuatedNestedMeta, item: Item) -> Result
};

let ret = quote! {
#[pyclass(module = false, name = #class_name, base = #base_class_name)]
#[pyclass(module = false, name = #class_name, base = #base_class_name, exception_ctx = #ctx_type_ident)]
#item
#impl_payload
#impl_pyclass
};
Ok(ret)
Expand Down
1 change: 1 addition & 0 deletions derive-impl/src/pypayload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(crate) fn impl_pypayload(input: DeriveInput) -> Result<TokenStream> {

let ret = quote! {
impl ::rustpython_vm::PyPayload for #ty {
type Super = ::rustpython_vm::builtins::PyBaseObject;
fn class(_ctx: &::rustpython_vm::vm::Context) -> &'static rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
<Self as ::rustpython_vm::class::StaticType>::static_type()
}
Expand Down
63 changes: 59 additions & 4 deletions derive-impl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,51 @@
Ok(value)
}

pub fn _optional_path(&self, key: &str) -> Result<Option<&syn::ExprPath>> {
let value = if let Some((_, meta)) = self.meta_map.get(key) {
let Meta::NameValue(syn::MetaNameValue {
value: syn::Expr::Path(path),
..
}) = meta
else {
bail_span!(
meta,
"#[{}({} = ...)] must exist as a path",
self.meta_name(),
key
)
};
Some(path)
} else {
None
};
Ok(value)
}

pub fn _optional_ident(&self, key: &str) -> Result<Option<&Ident>> {
let Some((_, meta)) = self.meta_map.get(key) else {
return Ok(None);
};
if let Meta::NameValue(syn::MetaNameValue {
value:
syn::Expr::Path(syn::ExprPath {
qself: None, path, ..

Check warning on line 218 in derive-impl/src/util.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (qself)
Copy link
Member

Choose a reason for hiding this comment

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

please add qself to rust-more.txt

}),
..
}) = meta
{
if let Some(ident) = path.get_ident() {
return Ok(Some(ident));
}
}
bail_span!(
meta,
"#[{}({} = ...)] must exist as an ident",
self.meta_name(),
key
)
}

pub fn _has_key(&self, key: &str) -> Result<bool> {
Ok(matches!(self.meta_map.get(key), Some((_, _))))
}
Expand Down Expand Up @@ -336,7 +381,9 @@
"base",
"metaclass",
"unhashable",
"manual_payload",
"ctx",
"exception_ctx",
"impl",
"traverse",
];
Expand Down Expand Up @@ -375,18 +422,26 @@
)
}

pub fn ctx_name(&self) -> Result<Option<String>> {
self.inner()._optional_str("ctx")
pub fn ctx_name(&self) -> Result<Option<&Ident>> {
self.inner()._optional_ident("ctx")
}

pub fn base(&self) -> Result<Option<String>> {
self.inner()._optional_str("base")
pub fn exception_ctx_name(&self) -> Result<Option<&Ident>> {
self.inner()._optional_ident("exception_ctx")
}

pub fn base(&self) -> Result<Option<&syn::ExprPath>> {
self.inner()._optional_path("base")
}

pub fn unhashable(&self) -> Result<bool> {
self.inner()._bool("unhashable")
}

pub fn manual_payload(&self) -> Result<bool> {
self.inner()._bool("manual_payload")
}

pub fn metaclass(&self) -> Result<Option<String>> {
self.inner()._optional_str("metaclass")
}
Expand Down
2 changes: 1 addition & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn derive_from_args(input: TokenStream) -> TokenStream {
/// - `IMMUTABLETYPE`: class attributes are immutable.
/// - `with`: which trait implementations are to be included in the python class.
/// ```rust, ignore
/// #[pyclass(module = "my_module", name = "MyClass", base = "BaseClass")]
/// #[pyclass(module = "my_module", name = "MyClass", base = BaseClass)]
/// struct MyStruct {
/// x: i32,
/// }
Expand Down
4 changes: 2 additions & 2 deletions examples/call_between_rust_and_python.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustpython::vm::{
PyObject, PyPayload, PyResult, TryFromBorrowedObject, VirtualMachine, pyclass, pymodule,
PyObject, PyResult, TryFromBorrowedObject, VirtualMachine, pyclass, pymodule,
};

pub fn main() {
Expand Down Expand Up @@ -64,7 +64,7 @@ python_person.name: {}",

#[pyattr]
#[pyclass(module = "rust_py_module", name = "RustStruct")]
#[derive(Debug, PyPayload)]
#[derive(Debug)]
struct RustStruct {
numbers: NumVec,
}
Expand Down
8 changes: 4 additions & 4 deletions extra_tests/snippets/stdlib_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ def LoadLibrary(self, name):

cdll = LibraryLoader(CDLL)

test_byte_array = create_string_buffer(b"Hello, World!\n")
assert test_byte_array._length_ == 15
# test_byte_array = create_string_buffer(b"Hello, World!\n")
Copy link
Member

Choose a reason for hiding this comment

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

is this intended?

# assert test_byte_array._length_ == 15

if _os.name == "posix" or _sys.platform == "darwin":
pass
Expand All @@ -279,8 +279,8 @@ def LoadLibrary(self, name):
i = c_int(1)
print("start srand")
print(libc.srand(i))
print(test_byte_array)
print(test_byte_array._type_)
# print(test_byte_array)
# print(test_byte_array._type_)
# print("start printf")
# libc.printf(test_byte_array)

Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ mod array {
#[pyattr]
#[pyattr(name = "ArrayType")]
#[pyclass(name = "array")]
#[derive(Debug, PyPayload)]
#[derive(Debug)]
pub struct PyArray {
array: PyRwLock<ArrayContentType>,
exports: AtomicUsize,
Expand Down Expand Up @@ -1387,7 +1387,7 @@ mod array {

#[pyattr]
#[pyclass(name = "arrayiterator", traverse)]
#[derive(Debug, PyPayload)]
#[derive(Debug)]
pub struct PyArrayIter {
internal: PyMutex<PositionIterInternal<PyArrayRef>>,
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/bz2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod _bz2 {

#[pyattr]
#[pyclass(name = "BZ2Decompressor")]
#[derive(PyPayload)]
#[derive()]
struct BZ2Decompressor {
state: PyMutex<DecompressorState>,
}
Expand Down Expand Up @@ -172,7 +172,7 @@ mod _bz2 {

#[pyattr]
#[pyclass(name = "BZ2Compressor")]
#[derive(PyPayload)]
#[derive()]
Copy link
Member

Choose a reason for hiding this comment

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

empty derive?

struct BZ2Compressor {
state: PyMutex<CompressorState>,
}
Expand Down
Loading
Loading