forked from Rust-related/RustPython
Compare commits
2 Commits
main
...
impl_del_o
| Author | SHA1 | Date | |
|---|---|---|---|
| 20c053a3c0 | |||
|
|
6181bf4a7a |
@@ -495,9 +495,18 @@ pub fn object_get_dict(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyDict
|
||||
obj.dict()
|
||||
.ok_or_else(|| vm.new_attribute_error("This object has no __dict__".to_owned()))
|
||||
}
|
||||
pub fn object_set_dict(obj: PyObjectRef, dict: PyDictRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
obj.set_dict(dict)
|
||||
.map_err(|_| vm.new_attribute_error("This object has no __dict__".to_owned()))
|
||||
|
||||
pub fn object_set_dict(obj: PyObjectRef, dict: PySetterValue<PyDictRef>, vm: &VirtualMachine) -> PyResult<()> {
|
||||
match dict {
|
||||
PySetterValue::Assign(dict) => {
|
||||
obj.set_dict(dict)
|
||||
.map_err(|_| vm.new_attribute_error("This object has no __dict__".to_owned()))
|
||||
}
|
||||
PySetterValue::Delete => {
|
||||
obj.delete_dict()
|
||||
.map_err(|_| vm.new_attribute_error("This object has no deletable __dict__".to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(ctx: &Context) {
|
||||
|
||||
@@ -1288,7 +1288,7 @@ fn subtype_set_dict(obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -
|
||||
descr_set(&descr, obj, PySetterValue::Assign(value), vm)
|
||||
}
|
||||
None => {
|
||||
object::object_set_dict(obj, value.try_into_value(vm)?, vm)?;
|
||||
object::object_set_dict(obj, PySetterValue::Delete, vm)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -717,6 +717,19 @@ impl PyObject {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_dict(&self) -> Result<(), ()> {
|
||||
match self.instance_dict() {
|
||||
Some(_) => {
|
||||
unsafe {
|
||||
let ptr = self as *const _ as *mut PyObject;
|
||||
(*ptr).0.dict = None;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
None => Err(()),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn payload_if_subclass<T: crate::PyPayload>(&self, vm: &VirtualMachine) -> Option<&T> {
|
||||
if self.class().fast_issubclass(T::class(&vm.ctx)) {
|
||||
|
||||
Reference in New Issue
Block a user