Skip to content

Call __set_name__ on descriptors when initializing types #1835

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

Merged
merged 1 commit into from
Apr 3, 2020
Merged
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
3 changes: 2 additions & 1 deletion Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

__all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES',
'total_ordering', 'cmp_to_key', 'lru_cache', 'reduce', 'partial',
'partialmethod', 'singledispatch', 'singledispatchmethod']
'partialmethod', 'singledispatch', 'singledispatchmethod',
"cached_property"]

from abc import get_cache_token
from collections import namedtuple
Expand Down
31 changes: 25 additions & 6 deletions vm/src/obj/objtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,33 @@ impl PyClassRef {
}
}

match new(metatype, name.as_str(), base.clone(), bases, attributes) {
Ok(typ) => {
typ.slots.borrow_mut().flags = base.slots.borrow().flags;
vm.ctx.add_tp_new_wrapper(&typ);
Ok(typ.into())
let typ = new(metatype, name.as_str(), base.clone(), bases, attributes)
.map_err(|e| vm.new_type_error(e))?;

typ.slots.borrow_mut().flags = base.slots.borrow().flags;
vm.ctx.add_tp_new_wrapper(&typ);

for (name, obj) in typ.attributes.borrow().iter() {
if let Some(meth) = vm.get_method(obj.clone(), "__set_name__") {
let set_name = meth?;
vm.invoke(
&set_name,
vec![typ.clone().into_object(), vm.new_str(name.clone())],
)
.map_err(|e| {
let err = vm.new_runtime_error(format!(
"Error calling __set_name__ on '{}' instance {} in '{}'",
obj.class().name,
name,
typ.name
));
err.set_cause(Some(e));
err
})?;
}
Err(string) => Err(vm.new_type_error(string)),
}

Ok(typ.into_object())
}

#[pyslot]
Expand Down