diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index 1cafd20930..396bb204ff 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -545,6 +545,29 @@ impl PyType { )) } + #[pyproperty(magic, setter)] + fn set_name(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + if !self.slots.flags.has_feature(PyTypeFlags::HEAPTYPE) { + Err(vm.new_type_error(format!( + "cannot set '{}' attribute of immutable type '{}'", + "__name__", + self.name() + ))) + } else { + match value.downcast_ref::().map(|m| m.as_str()) { + Some(name) => { + *self.slots.name.write() = Some(name.to_string()); + Ok(()) + } + None => Err(vm.new_type_error(format!( + "can only assign string to {}.__name__, not '{}'", + self.name(), + value.class().name() + ))), + } + } + } + #[pyproperty(magic)] fn text_signature(&self) -> Option { self.slots