|
1 | | -use once_cell::sync::Lazy; |
2 | | - |
3 | 1 | use super::{PyStr, PyStrRef, PyType, PyTypeRef, PyWeak}; |
4 | 2 | use crate::{ |
5 | 3 | atomic_func, |
6 | 4 | class::PyClassImpl, |
| 5 | + common::hash::PyHash, |
7 | 6 | function::{OptionalArg, PyComparisonValue, PySetterValue}, |
8 | | - protocol::{PyMappingMethods, PySequenceMethods}, |
| 7 | + protocol::{PyIter, PyIterReturn, PyMappingMethods, PySequenceMethods}, |
9 | 8 | types::{ |
10 | | - AsMapping, AsSequence, Comparable, Constructor, GetAttr, PyComparisonOp, Representable, |
11 | | - SetAttr, |
| 9 | + AsMapping, AsSequence, Comparable, Constructor, GetAttr, Hashable, IterNext, Iterable, |
| 10 | + PyComparisonOp, Representable, SetAttr, |
12 | 11 | }, |
13 | 12 | Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, |
14 | 13 | }; |
| 14 | +use once_cell::sync::Lazy; |
15 | 15 |
|
16 | | -#[pyclass(module = false, name = "weakproxy")] |
| 16 | +#[pyclass(module = false, name = "weakproxy", unhashable = true)] |
17 | 17 | #[derive(Debug)] |
18 | 18 | pub struct PyWeakProxy { |
19 | 19 | weak: PyRef<PyWeak>, |
@@ -71,7 +71,8 @@ crate::common::static_cell! { |
71 | 71 | Comparable, |
72 | 72 | AsSequence, |
73 | 73 | AsMapping, |
74 | | - Representable |
| 74 | + Representable, |
| 75 | + IterNext |
75 | 76 | ))] |
76 | 77 | impl PyWeakProxy { |
77 | 78 | fn try_upgrade(&self, vm: &VirtualMachine) -> PyResult { |
@@ -123,6 +124,20 @@ impl PyWeakProxy { |
123 | 124 | } |
124 | 125 | } |
125 | 126 |
|
| 127 | +impl Iterable for PyWeakProxy { |
| 128 | + fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult { |
| 129 | + let obj = zelf.try_upgrade(vm)?; |
| 130 | + Ok(obj.get_iter(vm)?.into()) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl IterNext for PyWeakProxy { |
| 135 | + fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> { |
| 136 | + let obj = zelf.try_upgrade(vm)?; |
| 137 | + PyIter::new(obj).next(vm) |
| 138 | + } |
| 139 | +} |
| 140 | + |
126 | 141 | fn new_reference_error(vm: &VirtualMachine) -> PyRef<super::PyBaseException> { |
127 | 142 | vm.new_exception_msg( |
128 | 143 | vm.ctx.exceptions.reference_error.to_owned(), |
@@ -212,3 +227,9 @@ impl Representable for PyWeakProxy { |
212 | 227 | pub fn init(context: &Context) { |
213 | 228 | PyWeakProxy::extend_class(context, context.types.weakproxy_type); |
214 | 229 | } |
| 230 | + |
| 231 | +impl Hashable for PyWeakProxy { |
| 232 | + fn hash(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> { |
| 233 | + zelf.try_upgrade(vm)?.hash(vm) |
| 234 | + } |
| 235 | +} |
0 commit comments