Expand #[pyexception] macro working with module attr

This commit is contained in:
Jeong YunWon authored and Jeong, YunWon committed 2025-10-27 18:37:01 +09:00
1 parent 2e7a8b4735
commit 37915336ea
3 files changed
+16 -5

No files matched your search

+7 -1
View File
@@ -551,7 +551,13 @@ pub(crate) fn impl_pyexception(attr: PunctuatedNestedMeta, item: Item) -> Result
}
}
} else {
quote! {}
quote! {
impl ::rustpython_vm::PyPayload for #ident {
fn class(_ctx: &::rustpython_vm::vm::Context) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
<Self as ::rustpython_vm::class::StaticType>::static_type()
}
}
}
};
let impl_pyclass = if class_meta.has_impl()? {
quote! {
+7 -3
View File
@@ -16,6 +16,7 @@ enum AttrName {
Function,
Attr,
Class,
Exception,
}
impl std::fmt::Display for AttrName {
@@ -24,6 +25,7 @@ impl std::fmt::Display for AttrName {
Self::Function => "pyfunction",
Self::Attr => "pyattr",
Self::Class => "pyclass",
Self::Exception => "pyexception",
};
s.fmt(f)
}
@@ -37,6 +39,7 @@ impl FromStr for AttrName {
"pyfunction" => Self::Function,
"pyattr" => Self::Attr,
"pyclass" => Self::Class,
"pyexception" => Self::Exception,
s => {
return Err(s.to_owned());
}
@@ -232,7 +235,8 @@ fn module_item_new(
inner: ContentItemInner { index, attr_name },
py_attrs,
}),
AttrName::Class => Box::new(ClassItem {
// pyexception is treated like pyclass - both define types
AttrName::Class | AttrName::Exception => Box::new(ClassItem {
inner: ContentItemInner { index, attr_name },
py_attrs,
}),
@@ -302,13 +306,13 @@ where
result.push(item_new(i, attr_name, Vec::new()));
} else {
match attr_name {
AttrName::Class | AttrName::Function => {
AttrName::Class | AttrName::Function | AttrName::Exception => {
result.push(item_new(i, attr_name, py_attrs.clone()));
}
_ => {
bail_span!(
attr,
"#[pyclass] or #[pyfunction] only can follow #[pyattr]",
"#[pyclass], #[pyfunction], or #[pyexception] can follow #[pyattr]",
)
}
}
+2 -1
View File
@@ -446,7 +446,8 @@ impl ClassItemMeta {
pub(crate) struct ExceptionItemMeta(ClassItemMeta);
impl ItemMeta for ExceptionItemMeta {
const ALLOWED_NAMES: &'static [&'static str] = &["name", "base", "unhashable", "ctx", "impl"];
const ALLOWED_NAMES: &'static [&'static str] =
&["module", "name", "base", "unhashable", "ctx", "impl"];
fn from_inner(inner: ItemMetaInner) -> Self {
Self(ClassItemMeta(inner))