Skip to content

Commit 4783df5

Browse files
authored
Merge pull request RustPython#4767 from jyj0816/weakref
Add Hash and Iter of WeakProxy.rs for test\testWeakref.py
2 parents b00e63e + afffb13 commit 4783df5

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

Lib/test/test_weakref.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,6 @@ def __iter__(self):
432432
# can be killed in the middle of the call
433433
"blech" in p
434434

435-
# TODO: RUSTPYTHON
436-
@unittest.expectedFailure
437435
def test_proxy_next(self):
438436
arr = [4, 5, 6]
439437
def iterator_func():
@@ -478,8 +476,6 @@ def __reversed__(self):
478476
obj = MyObj()
479477
self.assertEqual("".join(reversed(weakref.proxy(obj))), "cba")
480478

481-
# TODO: RUSTPYTHON
482-
@unittest.expectedFailure
483479
def test_proxy_hash(self):
484480
class MyObj:
485481
def __hash__(self):

vm/src/builtins/weakproxy.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
use once_cell::sync::Lazy;
2-
31
use super::{PyStr, PyStrRef, PyType, PyTypeRef, PyWeak};
42
use crate::{
53
atomic_func,
64
class::PyClassImpl,
5+
common::hash::PyHash,
76
function::{OptionalArg, PyComparisonValue, PySetterValue},
8-
protocol::{PyMappingMethods, PySequenceMethods},
7+
protocol::{PyIter, PyIterReturn, PyMappingMethods, PySequenceMethods},
98
types::{
10-
AsMapping, AsSequence, Comparable, Constructor, GetAttr, PyComparisonOp, Representable,
11-
SetAttr,
9+
AsMapping, AsSequence, Comparable, Constructor, GetAttr, Hashable, IterNext, Iterable,
10+
PyComparisonOp, Representable, SetAttr,
1211
},
1312
Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1413
};
14+
use once_cell::sync::Lazy;
1515

16-
#[pyclass(module = false, name = "weakproxy")]
16+
#[pyclass(module = false, name = "weakproxy", unhashable = true)]
1717
#[derive(Debug)]
1818
pub struct PyWeakProxy {
1919
weak: PyRef<PyWeak>,
@@ -71,7 +71,8 @@ crate::common::static_cell! {
7171
Comparable,
7272
AsSequence,
7373
AsMapping,
74-
Representable
74+
Representable,
75+
IterNext
7576
))]
7677
impl PyWeakProxy {
7778
fn try_upgrade(&self, vm: &VirtualMachine) -> PyResult {
@@ -123,6 +124,20 @@ impl PyWeakProxy {
123124
}
124125
}
125126

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+
126141
fn new_reference_error(vm: &VirtualMachine) -> PyRef<super::PyBaseException> {
127142
vm.new_exception_msg(
128143
vm.ctx.exceptions.reference_error.to_owned(),
@@ -212,3 +227,9 @@ impl Representable for PyWeakProxy {
212227
pub fn init(context: &Context) {
213228
PyWeakProxy::extend_class(context, context.types.weakproxy_type);
214229
}
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

Comments
 (0)