Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Properly handles del val.__dict__. #5576

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +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: PyDictRef, vm: &VirtualMachine) -> PyResult<()> {

pub fn object_set_dict(
obj: PyObjectRef,
dict: PySetterValue<PyDictRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
obj.set_dict(dict)
.map_err(|_| vm.new_attribute_error("This object has no __dict__".to_owned()))
.ok_or_else(|| vm.new_type_error("cannot delete __dict__".to_owned()))
}

pub fn init(ctx: &Context) {
Expand Down
11 changes: 8 additions & 3 deletions vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,11 @@ fn subtype_get_dict(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Ok(ret)
}

fn subtype_set_dict(obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
fn subtype_set_dict(
obj: PyObjectRef,
value: PySetterValue<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
let cls = obj.class();
match find_base_dict_descr(cls, vm) {
Some(descr) => {
Expand All @@ -1285,10 +1289,11 @@ fn subtype_set_dict(obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -
cls.name()
))
})?;
descr_set(&descr, obj, PySetterValue::Assign(value), vm)
descr_set(&descr, obj, value, vm)
}
None => {
object::object_set_dict(obj, value.try_into_value(vm)?, vm)?;
let dict = value.map(|s| s.try_into_value(vm)).transpose()?;
object::object_set_dict(obj, dict, vm)?;
Ok(())
}
}
Expand Down
36 changes: 30 additions & 6 deletions vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
lock::{PyMutex, PyMutexGuard, PyRwLock},
refcount::RefCount,
},
function::PySetterValue,
vm::VirtualMachine,
};
use itertools::Itertools;
Expand Down Expand Up @@ -711,14 +712,37 @@ impl PyObject {

/// Set the dict field. Returns `Err(dict)` if this object does not have a dict field
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc-comment doesn't fit in actual implementation anymore

/// in the first place.
pub fn set_dict(&self, dict: PyDictRef) -> Result<(), PyDictRef> {
match self.instance_dict() {
Some(d) => {
pub fn set_dict(&self, dict: PySetterValue<PyDictRef>) -> Option<()> {
// NOTE: So far, this is the only error condition that I know of so we can use Option
// for now.
if self.payload_is::<crate::builtins::function::PyFunction>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry. I don't get how PyFunction specifically requires to be checked yet. Is this same in CPython?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because deleting dict of a function is not allowed.
I can't find the equivalent check inside of cpython...

class A:
    pass


def ff():
    return 0


# Deleting __dict__ of class is fine
a = A()
print(a.__dict__)
del a.__dict__

# Deleting __dict__ of function is not allowed
print(ff.__dict__)
del ff.__dict__

Yields

hbina085@akarin-thinkpad ~/g/RustPython (hbina-fix-issue-5355) [1]> python ggg_issue_5355.py                                                                                                                      (numbadev) 
{}
{}
Traceback (most recent call last):
  File "/home/hbina085/git/RustPython/ggg_issue_5355.py", line 16, in <module>
    del ff.__dict__
        ^^^^^^^^^^^
TypeError: cannot delete __dict__

Perhaps I should have added comments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of PyFunction, isn't it a similar, but different rule? e.g. static types, immutable types etc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instant_dict() can be simple None for those types.

return None;
}

match (self.instance_dict(), dict) {
(Some(d), PySetterValue::Assign(dict)) => {
d.set(dict);
Ok(())
}
None => Err(dict),
}
(None, PySetterValue::Assign(dict)) => {
// self.0.dict = Some(InstanceDict::new(dict));
unsafe {
let ptr = self as *const _ as *mut PyObject;
(*ptr).0.dict = Some(InstanceDict::new(dict));
}
Comment on lines +727 to +731
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessarily to be unsafe? Is this guaranteed to be actually safe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea how to implement this without unsafe...All the methods only have immutable access to self. Can't figure out the head or tails of PyObjectRef either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it matched to None, I guess this object never can have __dict__. Then returning None will fit to it.

}
(Some(_), PySetterValue::Delete) => {
// self.0.dict = None;
unsafe {
let ptr = self as *const _ as *mut PyObject;
(*ptr).0.dict = None;
}
}
Comment on lines +733 to +739
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(Some(_), PySetterValue::Delete) => {
// self.0.dict = None;
unsafe {
let ptr = self as *const _ as *mut PyObject;
(*ptr).0.dict = None;
}
}
(Some(dict), PySetterValue::Delete) => {
dict.write().clear();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't do this because it only have &self.

(None, PySetterValue::Delete) => {
// NOTE(hanif) - noop?
}
};

Some(())
}

#[inline(always)]
Expand Down
Loading