-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Implement del object.__dict__
#5509
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,10 @@ where | |
{ | ||
#[inline] | ||
fn from_setter_value(vm: &VirtualMachine, obj: PySetterValue) -> PyResult<Self> { | ||
let obj = obj.ok_or_else(|| vm.new_type_error("can't delete attribute".to_owned()))?; | ||
T::try_from_object(vm, obj) | ||
match obj { | ||
PySetterValue::Assign(obj) => T::try_from_object(vm, obj), | ||
PySetterValue::Delete => T::try_from_object(vm, vm.ctx.none()), | ||
} | ||
Comment on lines
+39
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
def testInvalidAttrs(self):
self.assertRaises(TypeError, setattr, Exception(), '__cause__', 1)
self.assertRaises(TypeError, delattr, Exception(), '__cause__')
self.assertRaises(TypeError, setattr, Exception(), '__context__', 1)
self.assertRaises(TypeError, delattr, Exception(), '__context__') This is because FromPySetterValue trait affect not only dict but also other attributes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found only one way to restrict deletion of attribute except // vm/src/builtins/getset.rs: 97
#[pyslot]
fn descr_set(
zelf: &PyObject,
obj: PyObjectRef,
value: PySetterValue<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
let zelf = zelf.try_to_ref::<Self>(vm)?;
if let Some(ref f) = zelf.setter {
match value {
PySetterValue::Assign(v) => f(vm, obj, PySetterValue::Assign(v)),
PySetterValue::Delete if zelf.name == "__dict__" => {
f(vm, obj, PySetterValue::Delete)
}
_ => Err(vm.new_type_error("can't delete attribute".to_owned())),
}
} else {
Err(vm.new_attribute_error(format!(
"attribute '{}' of '{}' objects is not writable",
zelf.name,
obj.class().name()
)))
}
} |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lib/test/test_descr.py:3399 test_set_dict
If I don't change code like this,
object_set_dict
fails to be compiled because now value must be the typePySetterValue<PyDictRef>
.There are two option for it, pass the value
PySetterValue::Assign
orPySetterValue::Delete
For the case of Assign I get
For Delete case,
Test function
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarified problem is this.
If I try to delete
__dict__
, following methods are called.PySetterValue::Delete
becomesNone
Since the first
V::from_setter_value(vm, value)
give same result forvalue = PySetterValue::Assign(None)
andvalue = PySetterValue::Delete
to None, This kind of error occurs.