8000 add immutable flag to heaptype, check immutable flag in SetAttr · RustPython/RustPython@076e8bc · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 076e8bc

Browse files
committed
add immutable flag to heaptype, check immutable flag in SetAttr
1 parent bcd2331 commit 076e8bc

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

vm/src/builtins/type.rs

Lines changed: 12 additions & 0 deletions
< 8000 /div>
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ impl PyType {
104104

105105
*slots.name.get_mut() = Some(String::from(name));
106106

107+
if !slots.flags.has_feature(PyTypeFlags::HEAPTYPE) {
108+
slots.flags |= PyTypeFlags::IMMUTABLETYPE
109+
}
110+
107111
let new_type = PyRef::new_ref(
108112
PyType {
109113
base: Some(base),
@@ -707,6 +711,14 @@ impl SetAttr for PyType {
707711
value: Option<PyObjectRef>,
708712
vm: &VirtualMachine,
709713
) -> PyResult<()> {
714+
if zelf.slots.flags.has_feature(PyTypeFlags::IMMUTABLETYPE) {
715+
return Err(vm.new_type_error(format!(
716+
"cannot set '{}' attribute of immutable type '{}'",
717+
attr_name,
718+
zelf.name()
719+
)));
720+
}
721+
710722
// TODO: pass PyRefExact instead of &str
711723
let attr_name = vm.ctx.intern_str(attr_name.as_str());
712724
if let Some(attr) = zelf.get_class_attr(attr_name) {

0 commit comments

Comments
 (0)
0