Skip to content

Commit afffb13

Browse files
jyj0816youknowone
authored andcommitted
Add Hash of WeakProxy for test\test_Weakref.py's "test_proxy_hash "
Fix Hash of WeakProxy : remove "?" ending of the code Fix weakproxy.rs for test_proxy_hash to solve error
1 parent 95f3c0d commit afffb13

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

Lib/test/test_weakref.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,6 @@ def __reversed__(self):
476476
obj = MyObj()
477477
self.assertEqual("".join(reversed(weakref.proxy(obj))), "cba")
478478

479-
# TODO: RUSTPYTHON
480-
@unittest.expectedFailure
481479
def test_proxy_hash(self):
482480
class MyObj:
483481
def __hash__(self):

vm/src/builtins/weakproxy.rs

Lines changed: 11 additions & 4 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},
87
protocol::{PyIter, PyIterReturn, PyMappingMethods, PySequenceMethods},
98
types::{
10-
AsMapping, AsSequence, Comparable, Constructor, GetAttr, IterNext, Iterable,
9+
AsMapping, AsSequence, Comparable, Constructor, GetAttr, Hashable, IterNext, Iterable,
1110
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>,
@@ -123,6 +123,7 @@ impl PyWeakProxy {
123123
obj.del_item(&*needle, vm)
124124
}
125125
}
126+
126127
impl Iterable for PyWeakProxy {
127128
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
128129
let obj = zelf.try_upgrade(vm)?;
@@ -226,3 +227,9 @@ impl Representable for PyWeakProxy {
226227
pub fn init(context: &Context) {
227228
PyWeakProxy::extend_class(context, context.types.weakproxy_type);
228229
}
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)