forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweakproxy.rs
More file actions
80 lines (72 loc) · 2.19 KB
/
Copy pathweakproxy.rs
File metadata and controls
80 lines (72 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use super::{PyStrRef, PyTypeRef, PyWeak};
use crate::{
function::OptionalArg,
slots::{SlotConstructor, SlotSetattro},
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};
#[pyclass(module = false, name = "weakproxy")]
#[derive(Debug)]
pub struct PyWeakProxy {
weak: PyWeak,
}
impl PyValue for PyWeakProxy {
fn class(vm: &VirtualMachine) -> &PyTypeRef {
&vm.ctx.types.weakproxy_type
}
}
#[derive(FromArgs)]
pub struct WeakProxyNewArgs {
#[pyarg(positional)]
referent: PyObjectRef,
#[pyarg(positional, optional)]
callback: OptionalArg<PyObjectRef>,
}
impl SlotConstructor for PyWeakProxy {
type Args = WeakProxyNewArgs;
fn py_new(
cls: PyTypeRef,
Self::Args { referent, callback }: Self::Args,
vm: &VirtualMachine,
) -> PyResult {
if callback.is_present() {
panic!("Passed a callback to weakproxy, but weakproxy does not yet support proxies.");
}
PyWeakProxy {
weak: PyWeak::downgrade(&referent),
}
.into_pyresult_with_type(vm, cls)
}
}
#[pyimpl(with(SlotSetattro, SlotConstructor))]
impl PyWeakProxy {
// TODO: callbacks
#[pymethod(magic)]
fn getattr(&self, attr_name: PyObjectRef, vm: &VirtualMachine) -> PyResult {
match self.weak.upgrade() {
Some(obj) => vm.get_attribute(obj, attr_name),
None => Err(vm.new_exception_msg(
vm.ctx.exceptions.reference_error.clone(),
"weakly-referenced object no longer exists".to_owned(),
)),
}
}
}
impl SlotSetattro for PyWeakProxy {
fn setattro(
zelf: &PyRef<Self>,
attr_name: PyStrRef,
value: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
match zelf.weak.upgrade() {
Some(obj) => vm.call_set_attr(&obj, attr_name, value),
None => Err(vm.new_exception_msg(
vm.ctx.exceptions.reference_error.clone(),
"weakly-referenced object no longer exists".to_owned(),
)),
}
}
}
pub fn init(context: &PyContext) {
PyWeakProxy::extend_class(context, &context.types.weakproxy_type);
}