Skip to content

Commit d9fc95c

Browse files
committed
Fix sequence protocol concat
1 parent 788ccff commit d9fc95c

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

Lib/test/test_mmap.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,6 @@ def test_resize_past_pos(self):
721721
self.assertRaises(ValueError, m.write_byte, 42)
722722
self.assertRaises(ValueError, m.write, b'abc')
723723

724-
@unittest.skip('TODO: RUSTPYTHON, Number Protocol is not properly implemented yet')
725724
def test_concat_repeat_exception(self):
726725
m = mmap.mmap(-1, 16)
727726
with self.assertRaises(TypeError):

vm/src/protocol/number.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ impl PyNumberMethods {
232232
}
233233
}
234234

235+
#[derive(Copy, Clone)]
235236
pub enum PyNumberBinaryOpSlot {
236237
Add,
237238
Subtract,

vm/src/protocol/sequence.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
builtins::{type_::PointerSlot, PyList, PyListRef, PySlice, PyTuple, PyTupleRef},
33
convert::ToPyObject,
44
function::PyArithmeticValue,
5-
protocol::PyMapping,
5+
protocol::{PyMapping, PyNumberBinaryOpSlot},
66
AsObject, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
77
};
88
use crossbeam_utils::atomic::AtomicCell;
@@ -118,7 +118,7 @@ impl PySequence<'_> {
118118

119119
// if both arguments apear to be sequences, try fallback to __add__
120120
if self.check() && other.to_sequence(vm).check() {
121-
let ret = vm._add(self.obj, other)?;
121+
let ret = vm.binary_op1(self.obj, other, &PyNumberBinaryOpSlot::Add)?;
122122
if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) {
123123
return Ok(ret);
124124
}
@@ -137,7 +137,11 @@ impl PySequence<'_> {
137137

138138
// fallback to __mul__
139139
if self.check() {
140-
let ret = vm._mul(self.obj, &n.to_pyobject(vm))?;
140+
let ret = vm.binary_op1(
141+
self.obj,
142+
&n.to_pyobject(vm),
143+
&PyNumberBinaryOpSlot::Multiply,
144+
)?;
141145
if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) {
142146
return Ok(ret);
143147
}

vm/src/vm/vm_ops.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ impl VirtualMachine {
131131
/// b.rop(b,a)[*], a.op(a,b), b.rop(b,a)
132132
///
133133
/// [*] only when Py_TYPE(a) != Py_TYPE(b) && Py_TYPE(b) is a subclass of Py_TYPE(a)
134-
fn binary_op1(&self, a: &PyObject, b: &PyObject, op_slot: &PyNumberBinaryOpSlot) -> PyResult {
134+
pub fn binary_op1(
135+
&self,
136+
a: &PyObject,
137+
b: &PyObject,
138+
op_slot: &PyNumberBinaryOpSlot,
139+
) -> PyResult {
135140
let slot_a = a.class().slots.number.get_left_binary_op(op_slot)?;
136141
let mut slot_b = if b.class().is(a.class()) {
137142
None

0 commit comments

Comments
 (0)