Skip to content

Commit 6364ad4

Browse files
committed
Reflect feedbacks
1 parent 3cdda75 commit 6364ad4

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

Lib/test/test_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,8 @@ def test_copy(self):
11731173
self.assertEqual(view['key1'], 70)
11741174
self.assertEqual(copy['key1'], 27)
11751175

1176+
# TODO: RUSTPYTHON
1177+
@unittest.expectedFailure
11761178
def test_union(self):
11771179
mapping = {'a': 0, 'b': 1, 'c': 2}
11781180
view = self.mappingproxy(mapping)
@@ -1185,8 +1187,7 @@ def test_union(self):
11851187
other = {'c': 3, 'p': 0}
11861188
self.assertDictEqual(view | other, {'a': 0, 'b': 1, 'c': 3, 'p': 0})
11871189
self.assertDictEqual(other | view, {'c': 2, 'p': 0, 'a': 0, 'b': 1})
1188-
# Should be test after implementing MappingProxy's Comparable trait
1189-
# self.assertEqual(view, {'a': 0, 'b': 1, 'c': 2})
1190+
self.assertEqual(view, {'a': 0, 'b': 1, 'c': 2})
11901191
self.assertDictEqual(mapping, {'a': 0, 'b': 1, 'c': 2})
11911192
self.assertDictEqual(other, {'c': 3, 'p': 0})
11921193

vm/src/builtins/mappingproxy.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::{
33
class::PyClassImpl,
44
convert::ToPyObject,
55
function::{ArgMapping, OptionalArg},
6-
protocol::{PyMapping, PyMappingMethods, PySequence, PySequenceMethods},
7-
types::{AsMapping, AsSequence, Constructor, Iterable},
6+
protocol::{PyMapping, PyMappingMethods, PyNumberMethods, PySequence, PySequenceMethods},
7+
types::{AsMapping, AsNumber, AsSequence, Constructor, Iterable},
88
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
99
};
1010

@@ -152,25 +152,26 @@ impl PyMappingProxy {
152152

153153
#[pymethod(magic)]
154154
fn reversed(&self, vm: &VirtualMachine) -> PyResult {
155-
let obj = self.to_object(vm)?;
156-
let reversed_method = vm.get_method(obj, identifier!(vm, __reversed__)).unwrap();
157-
vm.invoke(&reversed_method?, ())
155+
vm.call_method(
156+
self.to_object(vm)?.as_object(),
157+
identifier!(vm, __reversed__).as_str(),
158+
(),
159+
)
158160
}
159161

160162
#[pymethod(magic)]
161163
fn ior(&self, _args: PyObjectRef, vm: &VirtualMachine) -> PyResult {
162-
Err(vm.new_type_error("\"'|=' is not supported by %s; use '|' instead\"".to_owned()))
164+
Err(vm.new_type_error(format!(
165+
"\"'|=' is not supported by {}; use '|' instead\"",
166+
Self::class(vm)
167+
)))
163168
}
164169

170+
#[pymethod(name = "__ror__")]
165171
#[pymethod(magic)]
166172
fn or(&self, args: PyObjectRef, vm: &VirtualMachine) -> PyResult {
167173
vm._or(self.copy(vm)?.as_ref(), args.as_ref())
168174
}
169-
170-
#[pymethod(magic)]
171-
fn ror(&self, args: PyObjectRef, vm: &VirtualMachine) -> PyResult {
172-
vm._or(args.as_ref(), self.copy(vm)?.as_ref())
173-
}
174175
}
175176

176177
impl AsMapping for PyMappingProxy {
@@ -186,11 +187,18 @@ impl AsMapping for PyMappingProxy {
186187
impl AsSequence for PyMappingProxy {
187188
const AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
188189
contains: Some(|seq, target, vm| Self::sequence_downcast(seq)._contains(target, vm)),
189-
length: Some(|seq, vm| Self::sequence_downcast(seq).len(vm)),
190190
..PySequenceMethods::NOT_IMPLEMENTED
191191
};
192192
}
193193

194+
impl AsNumber for PyMappingProxy {
195+
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
196+
or: Some(|num, args, vm| Self::number_downcast(num).or(args.to_pyobject(vm), vm)),
197+
inplace_or: Some(|num, args, vm| Self::number_downcast(num).ior(args.to_pyobject(vm), vm)),
198+
..PyNumberMethods::NOT_IMPLEMENTED
199+
};
200+
}
201+
194202
impl Iterable for PyMappingProxy {
195203
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
196204
let obj = zelf.to_object(vm)?;

0 commit comments

Comments
 (0)