Skip to content

Commit 050f0bd

Browse files
authored
weakref.__reversed__ fixed (RustPython#4780)
1 parent 7c56313 commit 050f0bd

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

Lib/test/test_weakref.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,6 @@ def __iter__(self):
464464
with self.assertRaisesRegex(TypeError, msg):
465465
list(a)
466466

467-
# TODO: RUSTPYTHON
468-
@unittest.expectedFailure
469467
def test_proxy_reversed(self):
470468
class MyObj:
471469
def __len__(self):

vm/src/builtins/weakproxy.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
common::hash::PyHash,
66
function::{OptionalArg, PyComparisonValue, PySetterValue},
77
protocol::{PyIter, PyIterReturn, PyMappingMethods, PySequenceMethods},
8+
stdlib::builtins::reversed,
89
types::{
910
AsMapping, AsSequence, Comparable, Constructor, GetAttr, Hashable, IterNext, Iterable,
1011
PyComparisonOp, Representable, SetAttr,
@@ -98,6 +99,11 @@ impl PyWeakProxy {
9899
self.try_upgrade(vm)?.bytes(vm)
99100
}
100101

102+
#[pymethod(magic)]
103+
fn reversed(&self, vm: &VirtualMachine) -> PyResult {
104+
let obj = self.try_upgrade(vm)?;
105+
reversed(obj, vm)
106+
}
101107
#[pymethod(magic)]
102108
fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
103109
self.try_upgrade(vm)?.to_sequence(vm).contains(&needle, vm)

vm/src/stdlib/builtins.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Implements the list of [builtin Python functions](https://docs.python.org/3/library/builtins.html).
44
use crate::{builtins::PyModule, class::PyClassImpl, Py, VirtualMachine};
55
pub(crate) use builtins::{__module_def, DOC};
6-
pub use builtins::{ascii, print};
6+
pub use builtins::{ascii, print, reversed};
77

88
#[pymodule]
99
mod builtins {
@@ -686,7 +686,7 @@ mod builtins {
686686
}
687687

688688
#[pyfunction]
689-
fn reversed(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
689+
pub fn reversed(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
690690
if let Some(reversed_method) = vm.get_method(obj.clone(), identifier!(vm, __reversed__)) {
691691
reversed_method?.call((), vm)
692692
} else {

0 commit comments

Comments
 (0)