diff --git a/py/list.go b/py/list.go index 0c7a7cc2..9e8e3dca 100644 --- a/py/list.go +++ b/py/list.go @@ -259,6 +259,9 @@ func (l *List) M__mul__(other Object) (Object, error) { if b, ok := convertToInt(other); ok { m := len(l.Items) n := int(b) * m + if n < 0 { + n = 0 + } newList := NewListSized(n) for i := 0; i < n; i += m { copy(newList.Items[i:i+m], l.Items) diff --git a/py/tests/list.py b/py/tests/list.py index f959fed2..4fb1066c 100644 --- a/py/tests/list.py +++ b/py/tests/list.py @@ -33,4 +33,10 @@ assert repr(a) == "['a', 'b', 'c', 'd', 'e', 'f']" assertRaises(TypeError, lambda: [].append()) +doc="mul" +a = [1, 2, 3] +assert a * 2 == [1, 2, 3, 1, 2, 3] +assert a * 0 == [] +assert a * -1 == [] + doc="finished" diff --git a/py/tests/tuple.py b/py/tests/tuple.py index 49bb2ed5..609a687c 100644 --- a/py/tests/tuple.py +++ b/py/tests/tuple.py @@ -14,4 +14,10 @@ assert repr((1,(2,3),4)) == "(1, (2, 3), 4)" assert repr(("1",(2.5,17,()))) == "('1', (2.5, 17, ()))" +doc="mul" +a = (1, 2, 3) +assert a * 2 == (1, 2, 3, 1, 2, 3) +assert a * 0 == () +assert a * -1 == () + doc="finished" diff --git a/py/tuple.go b/py/tuple.go index d4292ed9..e65cf765 100644 --- a/py/tuple.go +++ b/py/tuple.go @@ -113,6 +113,7 @@ func (a Tuple) M__add__(other Object) (Object, error) { copy(newTuple[len(b):], b) return newTuple, nil } + return NotImplemented, nil } @@ -131,6 +132,9 @@ func (l Tuple) M__mul__(other Object) (Object, error) { if b, ok := convertToInt(other); ok { m := len(l) n := int(b) * m + if n < 0 { + n = 0 + } newTuple := make(Tuple, n) for i := 0; i < n; i += m { copy(newTuple[i:i+m], l)