From 0597cff6c687749e4e0f2e4df1ab64dfd2f9f18e Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Sun, 12 Aug 2018 09:47:21 +0100 Subject: [PATCH] Add has_attr to AttributeProtocol. --- vm/src/pyobject.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index b9c98c71f..1b3cee87f 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -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),