Skip to content

Commit d7085db

Browse files
authored
Merge pull request RustPython#1689 from youknowone/pymethod-magic
#[pymethod(magic)]: pyname as __{sig.ident}__ form
2 parents bc121d7 + d5367d1 commit d7085db

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

derive/src/pyclass.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,28 @@ impl Class {
6868
NestedMeta::Meta(meta) => meta,
6969
NestedMeta::Lit(_) => continue,
7070
};
71-
if let Meta::NameValue(name_value) = meta {
72-
if path_eq(&name_value.path, "name") {
73-
if let Lit::Str(s) = &name_value.lit {
74-
py_name = Some(s.value());
71+
72+
match meta {
73+
Meta::NameValue(name_value) => {
74+
if path_eq(&name_value.path, "name") {
75+
if let Lit::Str(s) = &name_value.lit {
76+
py_name = Some(s.value());
77+
} else {
78+
bail_span!(&sig.ident, "#[pymethod(name = ...)] must be a string");
79+
}
80+
}
81+
}
82+
Meta::Path(path) => {
83+
if path.get_ident().map_or(false, |v| v == "magic") {
84+
py_name = Some(format!("__{}__", sig.ident.to_string()));
7585
} else {
76-
bail_span!(&sig.ident, "#[pymethod(name = ...)] must be a string");
86+
bail_span!(
87+
&sig.ident,
88+
"#[pymethod(magic)] or #[pymethod(name = ...)] is expected"
89+
);
7790
}
7891
}
92+
_ => (),
7993
}
8094
}
8195

vm/src/obj/objfunction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl PyValue for PyFunction {
242242

243243
#[pyimpl(with(PyBuiltinDescriptor))]
244244
impl PyFunction {
245-
#[pymethod(name = "__call__")]
245+
#[pymethod(magic)]
246246
fn call(zelf: PyObjectRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
247247
vm.invoke(&zelf, args)
248248
}

0 commit comments

Comments
 (0)