Skip to content

Commit c8ff4b9

Browse files
committed
Expand #[pyexception] macro working with module attr
1 parent 2e7a8b4 commit c8ff4b9

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

derive-impl/src/pyclass.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,13 @@ pub(crate) fn impl_pyexception(attr: PunctuatedNestedMeta, item: Item) -> Result
551551
}
552552
}
553553
} else {
554-
quote! {}
554+
quote! {
555+
impl ::rustpython_vm::PyPayload for #ident {
556+
fn class(_ctx: &::rustpython_vm::vm::Context) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
557+
<Self as ::rustpython_vm::class::StaticType>::static_type()
558+
}
559+
}
560+
}
555561
};
556562
let impl_pyclass = if class_meta.has_impl()? {
557563
quote! {

derive-impl/src/pymodule.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum AttrName {
1616
Function,
1717
Attr,
1818
Class,
19+
Exception,
1920
}
2021

2122
impl std::fmt::Display for AttrName {
@@ -24,6 +25,7 @@ impl std::fmt::Display for AttrName {
2425
Self::Function => "pyfunction",
2526
Self::Attr => "pyattr",
2627
Self::Class => "pyclass",
28+
Self::Exception => "pyexception",
2729
};
2830
s.fmt(f)
2931
}
@@ -37,6 +39,7 @@ impl FromStr for AttrName {
3739
"pyfunction" => Self::Function,
3840
"pyattr" => Self::Attr,
3941
"pyclass" => Self::Class,
42+
"pyexception" => Self::Exception,
4043
s => {
4144
return Err(s.to_owned());
4245
}
@@ -232,7 +235,8 @@ fn module_item_new(
232235
inner: ContentItemInner { index, attr_name },
233236
py_attrs,
234237
}),
235-
AttrName::Class => Box::new(ClassItem {
238+
// pyexception is treated like pyclass - both define types
239+
AttrName::Class | AttrName::Exception => Box::new(ClassItem {
236240
inner: ContentItemInner { index, attr_name },
237241
py_attrs,
238242
}),
@@ -302,13 +306,13 @@ where
302306
result.push(item_new(i, attr_name, Vec::new()));
303307
} else {
304308
match attr_name {
305-
AttrName::Class | AttrName::Function => {
309+
AttrName::Class | AttrName::Function | AttrName::Exception => {
306310
result.push(item_new(i, attr_name, py_attrs.clone()));
307311
}
308312
_ => {
309313
bail_span!(
310314
attr,
311-
"#[pyclass] or #[pyfunction] only can follow #[pyattr]",
315+
"#[pyclass], #[pyfunction], or #[pyexception] can follow #[pyattr]",
312316
)
313317
}
314318
}

derive-impl/src/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ impl ClassItemMeta {
446446
pub(crate) struct ExceptionItemMeta(ClassItemMeta);
447447

448448
impl ItemMeta for ExceptionItemMeta {
449-
const ALLOWED_NAMES: &'static [&'static str] = &["name", "base", "unhashable", "ctx", "impl"];
449+
const ALLOWED_NAMES: &'static [&'static str] =
450+
&["module", "name", "base", "unhashable", "ctx", "impl"];
450451

451452
fn from_inner(inner: ItemMetaInner) -> Self {
452453
Self(ClassItemMeta(inner))

0 commit comments

Comments
 (0)