Fixes that id() of tuple was changing on * 1, refs #2840

This commit is contained in:
sobolevn committed 2021-08-11 01:47:16 +03:00
1 parent 08614bfc95
commit 7e2efb4de5
2 files changed
+9 -5

No files matched your search

-2
View File
@@ -311,8 +311,6 @@ class CommonTest(unittest.TestCase):
return str(key) + '!!!'
self.assertEqual(next(iter(T((1,2)))), 1)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_repeat(self):
for m in range(4):
s = tuple(range(m))
+9 -3
View File
@@ -169,11 +169,17 @@ impl PyTuple {
#[pymethod(name = "__mul__")]
#[pymethod(name = "__rmul__")]
fn mul(&self, counter: isize, vm: &VirtualMachine) -> PyRef<Self> {
if self.elements.is_empty() || counter == 0 {
fn mul(zelf: PyRef<Self>, counter: isize, vm: &VirtualMachine) -> PyRef<Self> {
if zelf.elements.is_empty() || counter == 0 {
vm.ctx.empty_tuple.clone()
} else if counter == 1 && zelf.clone_class().is(PyTuple::class(vm)) {
// Special case: when some `tuple` is multiplied by `1`,
// nothing really happens, we need to return an object itself
// with the same `id()` to be compatible with CPython.
// This only works for `tuple` itself, not its subclasses.
zelf
} else {
let elements = sequence::seq_mul(&self.elements, counter)
let elements = sequence::seq_mul(&zelf.elements, counter)
.cloned()
.collect::<Vec<_>>()
.into_boxed_slice();