Skip to content

Commit 08c7344

Browse files
committed
ArgSize for tuple
1 parent e686b64 commit 08c7344

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

vm/src/builtins/tuple.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
atomic_func,
77
class::PyClassImpl,
88
convert::{ToPyObject, TransmuteFromObject},
9-
function::{OptionalArg, PyArithmeticValue, PyComparisonValue},
9+
function::{ArgSize, OptionalArg, PyArithmeticValue, PyComparisonValue},
1010
iter::PyExactSizeIterator,
1111
protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods},
1212
recursion::ReprGuard,
@@ -169,6 +169,22 @@ impl PyTuple {
169169
pub fn as_slice(&self) -> &[PyObjectRef] {
170170
&self.elements
171171
}
172+
173+
fn repeat(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
174+
Ok(if zelf.elements.is_empty() || value == 0 {
175+
vm.ctx.empty_tuple.clone()
176+
} else if value == 1 && zelf.class().is(vm.ctx.types.tuple_type) {
177+
// Special case: when some `tuple` is multiplied by `1`,
178+
// nothing really happens, we need to return an object itself
179+
// with the same `id()` to be compatible with CPython.
180+
// This only works for `tuple` itself, not its subclasses.
181+
zelf
182+
} else {
183+
let v = zelf.elements.mul(vm, value)?;
184+
let elements = v.into_boxed_slice();
185+
Self { elements }.into_ref(vm)
186+
})
187+
}
172188
}
173189

174190
#[pyclass(
@@ -244,20 +260,8 @@ impl PyTuple {
244260

245261
#[pymethod(name = "__rmul__")]
246262
#[pymethod(magic)]
247-
fn mul(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
248-
Ok(if zelf.elements.is_empty() || value == 0 {
249-
vm.ctx.empty_tuple.clone()
250-
} else if value == 1 && zelf.class().is(vm.ctx.types.tuple_type) {
251-
// Special case: when some `tuple` is multiplied by `1`,
252-
// nothing really happens, we need to return an object itself
253-
// with the same `id()` to be compatible with CPython.
254-
// This only works for `tuple` itself, not its subclasses.
255-
zelf
256-
} else {
257-
let v = zelf.elements.mul(vm, value)?;
258-
let elements = v.into_boxed_slice();
259-
Self { elements }.into_ref(vm)
260-
})
263+
fn mul(zelf: PyRef<Self>, value: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
264+
Self::repeat(zelf, value.into(), vm)
261265
}
262266

263267
fn _getitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
@@ -353,7 +357,7 @@ impl AsSequence for PyTuple {
353357
}),
354358
repeat: atomic_func!(|seq, n, vm| {
355359
let zelf = PyTuple::sequence_downcast(seq);
356-
PyTuple::mul(zelf.to_owned(), n, vm).map(|x| x.into())
360+
PyTuple::repeat(zelf.to_owned(), n, vm).map(|x| x.into())
357361
}),
358362
item: atomic_func!(|seq, i, vm| {
359363
let zelf = PyTuple::sequence_downcast(seq);

0 commit comments

Comments
 (0)