-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: main
Are you sure you want to change the base?
Changes from all commits
2010890
1abd204
c43327e
79d70e3
02ce85a
425fd4f
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 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,6 +25,7 @@ use crate::{ | |||||||||||||||||||||
lock::{PyMutex, PyMutexGuard, PyRwLock}, | ||||||||||||||||||||||
refcount::RefCount, | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
function::PySetterValue, | ||||||||||||||||||||||
vm::VirtualMachine, | ||||||||||||||||||||||
}; | ||||||||||||||||||||||
use itertools::Itertools; | ||||||||||||||||||||||
|
@@ -711,14 +712,37 @@ impl PyObject { | |||||||||||||||||||||
|
||||||||||||||||||||||
/// Set the dict field. Returns `Err(dict)` if this object does not have a dict field | ||||||||||||||||||||||
/// 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>() { | ||||||||||||||||||||||
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 am sorry. I don't get how 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. Because deleting dict of a function is not allowed. 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
Perhaps I should have added comments. 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. Instead of PyFunction, isn't it a similar, but different rule? e.g. static types, immutable types etc 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. Maybe |
||||||||||||||||||||||
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
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. Is this necessarily to be unsafe? Is this guaranteed to be actually safe? 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 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. 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. When it matched to |
||||||||||||||||||||||
} | ||||||||||||||||||||||
(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
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.
Suggested change
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. Can't do this because it only have |
||||||||||||||||||||||
(None, PySetterValue::Delete) => { | ||||||||||||||||||||||
// NOTE(hanif) - noop? | ||||||||||||||||||||||
} | ||||||||||||||||||||||
}; | ||||||||||||||||||||||
|
||||||||||||||||||||||
Some(()) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
#[inline(always)] | ||||||||||||||||||||||
|
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.
The doc-comment doesn't fit in actual implementation anymore