Skip to content

Fix getattr to use __getattribute__ correctly #3748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,6 @@ def red(self):
green = 2
blue = 3

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_enum_with_value_name(self):
class Huh(Enum):
name = 1
Expand Down Expand Up @@ -1668,8 +1666,6 @@ class Test(Base):
test = 1
self.assertIs(type(Test.test), Test)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_subclass_duplicate_name_dynamic(self):
from types import DynamicClassAttribute
class Base(Enum):
Expand Down
18 changes: 10 additions & 8 deletions vm/src/builtins/weakproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{PyStrRef, PyType, PyTypeRef, PyWeak};
use crate::{
class::PyClassImpl,
function::OptionalArg,
types::{Constructor, SetAttr},
types::{Constructor, GetAttr, SetAttr},
Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};

Expand Down Expand Up @@ -57,14 +57,8 @@ crate::common::static_cell! {
static WEAK_SUBCLASS: PyTypeRef;
}

#[pyimpl(with(SetAttr, Constructor))]
#[pyimpl(with(GetAttr, SetAttr, Constructor))]
impl PyWeakProxy {
// TODO: callbacks
#[pymethod(magic)]
fn getattr(&self, attr_name: PyStrRef, vm: &VirtualMachine) -> PyResult {
let obj = self.weak.upgrade().ok_or_else(|| new_reference_error(vm))?;
obj.get_attr(attr_name, vm)
}
#[pymethod(magic)]
fn str(&self, vm: &VirtualMachine) -> PyResult<PyStrRef> {
match self.weak.upgrade() {
Expand All @@ -81,6 +75,14 @@ fn new_reference_error(vm: &VirtualMachine) -> PyRef<super::PyBaseException> {
)
}

impl GetAttr for PyWeakProxy {
// TODO: callbacks
fn getattro(zelf: &Py<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
let obj = zelf.weak.upgrade().ok_or_else(|| new_reference_error(vm))?;
obj.get_attr(name, vm)
}
}

impl SetAttr for PyWeakProxy {
fn setattro(
zelf: &crate::Py<Self>,
Expand Down
3 changes: 0 additions & 3 deletions vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ impl PyObject {
}
None => Ok(Some(attr)),
}
} else if let Some(getter) = obj_cls.get_attr(identifier!(vm, __getattr__)) {
drop(obj_cls);
vm.invoke(&getter, (self.to_owned(), name_str)).map(Some)
} else {
Ok(None)
}
Expand Down
12 changes: 10 additions & 2 deletions vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,15 @@ fn call_wrapper(zelf: &PyObject, args: FuncArgs, vm: &VirtualMachine) -> PyResul
}

fn getattro_wrapper(zelf: &PyObject, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
vm.call_special_method(zelf.to_owned(), identifier!(vm, __getattribute__), (name,))
let __getattribute__ = identifier!(vm, __getattribute__);
let __getattr__ = identifier!(vm, __getattr__);
match vm.call_special_method(zelf.to_owned(), __getattribute__, (name.clone(),)) {
Ok(r) => Ok(r),
Err(_) if zelf.class().has_attr(__getattr__) => {
vm.call_special_method(zelf.to_owned(), __getattr__, (name,))
}
Err(e) => Err(e),
}
}

fn setattro_wrapper(
Expand Down Expand Up @@ -393,7 +401,7 @@ impl PyType {
"__call__" => {
update_slot!(call, call_wrapper);
}
"__getattribute__" => {
"__getattr__" | "__getattribute__" => {
update_slot!(getattro, getattro_wrapper);
}
"__setattr__" | "__delattr__" => {
Expand Down