forked from Rust-related/RustPython
Add generic_getattribute{,_opt}
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user