Add generic_getattribute{,_opt}

This commit is contained in:
Noah
2020-02-16 16:02:26 -06:00
parent dfe8d8effa
commit 03c2042bd8
3 changed files with 9 additions and 5 deletions

View File

@@ -64,7 +64,7 @@ impl PyModuleRef {
}
fn name(self, vm: &VirtualMachine) -> Option<String> {
vm.generic_getattribute(
vm.generic_getattribute_opt(
self.as_object().clone(),
PyString::from("__name__").into_ref(vm),
)
@@ -74,7 +74,7 @@ impl PyModuleRef {
#[pymethod(magic)]
fn getattribute(self, name: PyStringRef, vm: &VirtualMachine) -> PyResult {
vm.generic_getattribute(self.as_object().clone(), name.clone())?
vm.generic_getattribute_opt(self.as_object().clone(), name.clone())?
.ok_or_else(|| {
let module_name = if let Some(name) = self.name(vm) {
format!(" '{}'", name)

View File

@@ -210,8 +210,7 @@ impl PyBaseObject {
#[pymethod(magic)]
fn getattribute(obj: PyObjectRef, name: PyStringRef, vm: &VirtualMachine) -> PyResult {
vm_trace!("object.__getattribute__({:?}, {:?})", obj, name);
vm.generic_getattribute(obj.clone(), name.clone())?
.ok_or_else(|| vm.new_attribute_error(format!("{} has no attribute '{}'", obj, name)))
vm.generic_getattribute(obj, name)
}
#[pymethod(magic)]

View File

@@ -869,8 +869,13 @@ impl VirtualMachine {
})
}
pub fn generic_getattribute(&self, obj: PyObjectRef, name: PyStringRef) -> PyResult {
self.generic_getattribute_opt(obj.clone(), name.clone())?
.ok_or_else(|| self.new_attribute_error(format!("{} has no attribute '{}'", obj, name)))
}
/// CPython _PyObject_GenericGetAttrWithDict
pub fn generic_getattribute(
pub fn generic_getattribute_opt(
&self,
obj: PyObjectRef,
name_str: PyStringRef,