forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweakproxy.rs
More file actions
196 lines (172 loc) · 5.81 KB
/
weakproxy.rs
File metadata and controls
196 lines (172 loc) · 5.81 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use once_cell::sync::Lazy;
use super::{PyStrRef, PyType, PyTypeRef, PyWeak};
use crate::{
atomic_func,
class::PyClassImpl,
function::{OptionalArg, PyComparisonValue, PySetterValue},
protocol::{PyMappingMethods, PySequenceMethods},
types::{AsMapping, AsSequence, Comparable, Constructor, GetAttr, PyComparisonOp, SetAttr},
Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};
#[pyclass(module = false, name = "weakproxy")]
#[derive(Debug)]
pub struct PyWeakProxy {
weak: PyRef<PyWeak>,
}
impl PyPayload for PyWeakProxy {
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
vm.ctx.types.weakproxy_type
}
}
#[derive(FromArgs)]
pub struct WeakProxyNewArgs {
#[pyarg(positional)]
referent: PyObjectRef,
#[pyarg(positional, optional)]
callback: OptionalArg<PyObjectRef>,
}
impl Constructor for PyWeakProxy {
type Args = WeakProxyNewArgs;
fn py_new(
cls: PyTypeRef,
Self::Args { referent, callback }: Self::Args,
vm: &VirtualMachine,
) -> PyResult {
// using an internal subclass as the class prevents us from getting the generic weakref,
// which would mess up the weakref count
let weak_cls = WEAK_SUBCLASS.get_or_init(|| {
vm.ctx.new_class(
None,
"__weakproxy",
vm.ctx.types.weakref_type.to_owned(),
super::PyWeak::make_slots(),
)
});
// TODO: PyWeakProxy should use the same payload as PyWeak
PyWeakProxy {
weak: referent.downgrade_with_typ(callback.into_option(), weak_cls.clone(), vm)?,
}
.into_ref_with_type(vm, cls)
.map(Into::into)
}
}
crate::common::static_cell! {
static WEAK_SUBCLASS: PyTypeRef;
}
#[pyclass(with(GetAttr, SetAttr, Constructor, Comparable, AsSequence, AsMapping))]
impl PyWeakProxy {
fn try_upgrade(&self, vm: &VirtualMachine) -> PyResult {
self.weak.upgrade().ok_or_else(|| new_reference_error(vm))
}
#[pymethod(magic)]
fn str(&self, vm: &VirtualMachine) -> PyResult<PyStrRef> {
self.try_upgrade(vm)?.str(vm)
}
fn len(&self, vm: &VirtualMachine) -> PyResult<usize> {
self.try_upgrade(vm)?.length(vm)
}
#[pymethod(magic)]
fn bool(&self, vm: &VirtualMachine) -> PyResult<bool> {
self.try_upgrade(vm)?.is_true(vm)
}
#[pymethod(magic)]
fn bytes(&self, vm: &VirtualMachine) -> PyResult {
self.try_upgrade(vm)?.bytes(vm)
}
#[pymethod(magic)]
fn repr(&self, vm: &VirtualMachine) -> PyResult<PyStrRef> {
self.try_upgrade(vm)?.repr(vm)
}
#[pymethod(magic)]
fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
self.try_upgrade(vm)?.to_sequence(vm).contains(&needle, vm)
}
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let obj = self.try_upgrade(vm)?;
obj.get_item(&*needle, vm)
}
fn setitem(
&self,
needle: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<()> {
let obj = self.try_upgrade(vm)?;
obj.set_item(&*needle, value, vm)
}
fn delitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let obj = self.try_upgrade(vm)?;
obj.del_item(&*needle, vm)
}
}
fn new_reference_error(vm: &VirtualMachine) -> PyRef<super::PyBaseException> {
vm.new_exception_msg(
vm.ctx.exceptions.reference_error.to_owned(),
"weakly-referenced object no longer exists".to_owned(),
)
}
impl GetAttr for PyWeakProxy {
// TODO: callbacks
fn getattro(zelf: &Py<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
let obj = zelf.try_upgrade(vm)?;
obj.get_attr(name, vm)
}
}
impl SetAttr for PyWeakProxy {
fn setattro(
zelf: &crate::Py<Self>,
attr_name: PyStrRef,
value: PySetterValue,
vm: &VirtualMachine,
) -> PyResult<()> {
let obj = zelf.try_upgrade(vm)?;
obj.call_set_attr(vm, attr_name, value)
}
}
impl Comparable for PyWeakProxy {
fn cmp(
zelf: &crate::Py<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
let obj = zelf.try_upgrade(vm)?;
Ok(PyComparisonValue::Implemented(
obj.rich_compare_bool(other, op, vm)?,
))
}
}
impl AsSequence for PyWeakProxy {
fn as_sequence() -> &'static PySequenceMethods {
static AS_SEQUENCE: Lazy<PySequenceMethods> = Lazy::new(|| PySequenceMethods {
length: atomic_func!(|seq, vm| PyWeakProxy::sequence_downcast(seq).len(vm)),
contains: atomic_func!(|seq, needle, vm| {
PyWeakProxy::sequence_downcast(seq).contains(needle.to_owned(), vm)
}),
..PySequenceMethods::NOT_IMPLEMENTED
});
&AS_SEQUENCE
}
}
impl AsMapping for PyWeakProxy {
fn as_mapping() -> &'static PyMappingMethods {
static AS_MAPPING: PyMappingMethods = PyMappingMethods {
length: atomic_func!(|mapping, vm| PyWeakProxy::mapping_downcast(mapping).len(vm)),
subscript: atomic_func!(|mapping, needle, vm| {
PyWeakProxy::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: atomic_func!(|mapping, needle, value, vm| {
let zelf = PyWeakProxy::mapping_downcast(mapping);
if let Some(value) = value {
zelf.setitem(needle.to_owned(), value, vm)
} else {
zelf.delitem(needle.to_owned(), vm)
}
}),
};
&AS_MAPPING
}
}
pub fn init(context: &Context) {
PyWeakProxy::extend_class(context, context.types.weakproxy_type);
}