Skip to content

Implement delattr for type object #1496

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

Merged
merged 1 commit into from
Oct 11, 2019
Merged
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: 9 additions & 0 deletions tests/snippets/attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
class A:
pass

class B:
x = 50

a = A()
a.b = 10
assert hasattr(a, 'b')
assert a.b == 10

assert B.x == 50

# test delete class attribute with del keyword
del B.x
with assert_raises(AttributeError):
_ = B.x

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add the snippet the from the explanation in the pull request as well? Class attributes probably take another code path as instance attributes.

I mean this snippet:

class C:
    x = 10

...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I added it.

# test override attribute
setattr(a, 'b', 12)
assert a.b == 12
Expand Down
19 changes: 18 additions & 1 deletion vm/src/obj/objtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,23 @@ impl PyClassRef {
Ok(())
}

fn del_attr(self, attr_name: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
if let Some(attr) = class_get_attr(&self.class(), attr_name.as_str()) {
if let Some(ref descriptor) = class_get_attr(&attr.class(), "__delete__") {
return vm
.invoke(descriptor, vec![attr, self.into_object()])
.map(|_| ());
}
}

if class_get_attr(&self, attr_name.as_str()).is_some() {
self.attributes.borrow_mut().remove(attr_name.as_str());
Ok(())
} else {
Err(vm.new_attribute_error(attr_name.as_str().to_string()))
}
}

// This is used for class initialisation where the vm is not yet available.
pub fn set_str_attr<V: Into<PyObjectRef>>(&self, attr_name: &str, value: V) {
self.attributes
Expand Down Expand Up @@ -256,8 +273,8 @@ pub fn init(ctx: &PyContext) {
"__prepare__" => ctx.new_rustfunc(PyClassRef::prepare),
"__getattribute__" => ctx.new_rustfunc(PyClassRef::getattribute),
"__setattr__" => ctx.new_rustfunc(PyClassRef::set_attr),
"__delattr__" => ctx.new_rustfunc(PyClassRef::del_attr),
"__subclasses__" => ctx.new_rustfunc(PyClassRef::subclasses),
"__getattribute__" => ctx.new_rustfunc(PyClassRef::getattribute),
"__instancecheck__" => ctx.new_rustfunc(PyClassRef::instance_check),
"__subclasscheck__" => ctx.new_rustfunc(PyClassRef::subclass_check),
"__doc__" => ctx.new_str(type_doc.to_string()),
Expand Down