Refactor PySetterValue handling in object and type modules
Some checks failed
PR Review / review (pull_request) Failing after 3s
CI / Run snippets and cpython tests (ubuntu-latest) (pull_request) Failing after 5m54s
CI / Check Rust code with rustfmt and clippy (pull_request) Failing after 45s
CI / Run tests under miri (pull_request) Successful in 6m24s
CI / Check the WASM package and demo (pull_request) Failing after 4m44s
CI / Run rust tests (ubuntu-latest) (pull_request) Failing after 4m38s
CI / Ensure compilation on various targets (pull_request) Failing after 1m6s
CI / Run snippets and cpython tests on wasm-wasi (pull_request) Successful in 4m46s
CI / Run rust tests (macos-latest) (pull_request) Has been cancelled
CI / Run rust tests (windows-latest) (pull_request) Has been cancelled
CI / Run snippets and cpython tests (macos-latest) (pull_request) Has been cancelled
CI / Run snippets and cpython tests (windows-latest) (pull_request) Has been cancelled

- Update object_set_dict to use new PySetterValue variants
- Modify getset.rs to handle PySetterValue::Delete
- Add delete_dict method to PyObject
- Adjust type.rs to use new PySetterValue semantics
This commit is contained in:
2025-02-09 23:26:36 +09:00
parent 6181bf4a7a
commit 20c053a3c0
4 changed files with 21 additions and 5 deletions

View File

@@ -495,13 +495,14 @@ 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: PySetterValue<PyDictRef>, vm: &VirtualMachine) -> PyResult<()> {
match dict {
PySetterValue::Some(dict) => {
PySetterValue::Assign(dict) => {
obj.set_dict(dict)
.map_err(|_| vm.new_attribute_error("This object has no __dict__".to_owned()))
}
PySetterValue::None => {
PySetterValue::Delete => {
obj.delete_dict()
.map_err(|_| vm.new_attribute_error("This object has no deletable __dict__".to_owned()))
}

View File

@@ -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(())
}
}

View File

@@ -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()),
}
}
}

View File

@@ -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)) {