Add has_attr to AttributeProtocol.

This commit is contained in:
Adam Kelly
2018-08-12 09:47:21 +01:00
parent 6889b34515
commit 0597cff6c6

View File

@@ -238,11 +238,13 @@ impl ParentProtocol for PyObjectRef {
pub trait AttributeProtocol {
fn get_attr(&self, attr_name: &String) -> PyObjectRef;
fn set_attr(&self, attr_name: &String, value: PyObjectRef);
fn has_attr(&self, attr_name: &String) -> bool;
}
impl AttributeProtocol for PyObjectRef {
fn get_attr(&self, attr_name: &String) -> PyObjectRef {
match self.borrow().kind {
let obj = self.borrow();
match obj.kind {
PyObjectKind::Module { name: _, ref dict } => dict.get_item(attr_name),
PyObjectKind::Class { name: _, ref dict } => dict.get_item(attr_name),
PyObjectKind::Instance { ref dict } => dict.get_item(attr_name),
@@ -250,6 +252,16 @@ impl AttributeProtocol for PyObjectRef {
}
}
fn has_attr(&self, attr_name: &String) -> bool {
let obj = self.borrow();
match obj.kind {
PyObjectKind::Module { name: _, ref dict } => dict.contains_key(attr_name),
PyObjectKind::Class { name: _, ref dict } => dict.contains_key(attr_name),
PyObjectKind::Instance { ref dict } => dict.contains_key(attr_name),
ref kind => unimplemented!("load_attr unimplemented for: {:?}", kind),
}
}
fn set_attr(&self, attr_name: &String, value: PyObjectRef) {
match self.borrow_mut().kind {
PyObjectKind::Instance { ref mut dict } => dict.set_item(attr_name, value),