Merge pull request #1689 from youknowone/pymethod-magic

#[pymethod(magic)]: pyname as __{sig.ident}__ form
This commit is contained in:
Jeong YunWon
2020-01-26 12:04:39 +09:00
committed by GitHub
2 changed files with 21 additions and 7 deletions

View File

@@ -68,14 +68,28 @@ impl Class {
NestedMeta::Meta(meta) => meta,
NestedMeta::Lit(_) => continue,
};
if let Meta::NameValue(name_value) = meta {
if path_eq(&name_value.path, "name") {
if let Lit::Str(s) = &name_value.lit {
py_name = Some(s.value());
} else {
bail_span!(&sig.ident, "#[pymethod(name = ...)] must be a string");
match meta {
Meta::NameValue(name_value) => {
if path_eq(&name_value.path, "name") {
if let Lit::Str(s) = &name_value.lit {
py_name = Some(s.value());
} else {
bail_span!(&sig.ident, "#[pymethod(name = ...)] must be a string");
}
}
}
Meta::Path(path) => {
if path.get_ident().map_or(false, |v| v == "magic") {
py_name = Some(format!("__{}__", sig.ident.to_string()));
} else {
bail_span!(
&sig.ident,
"#[pymethod(magic)] or #[pymethod(name = ...)] is expected"
);
}
}
_ => (),
}
}

View File

@@ -242,7 +242,7 @@ impl PyValue for PyFunction {
#[pyimpl(with(PyBuiltinDescriptor))]
impl PyFunction {
#[pymethod(name = "__call__")]
#[pymethod(magic)]
fn call(zelf: PyObjectRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
vm.invoke(&zelf, args)
}