Fix weakproxy getattr

This commit is contained in:
Jeong Yunwon
2022-05-26 11:06:04 +09:00
parent 8ab4e77035
commit 491fde8f7f

View File

@@ -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,
};
@@ -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() {
@@ -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>,