mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Add has_attr to AttributeProtocol.
This commit is contained in:
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user