From b4d99f5337357941e7dfa2c22bbf0c05e093282f Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 27 Jul 2019 22:29:42 +0900 Subject: [PATCH] py: Fix __mul__ of list and tuple on negative case --- py/list.go | 3 +++ py/tests/list.py | 6 ++++++ py/tests/tuple.py | 6 ++++++ py/tuple.go | 4 ++++ 4 files changed, 19 insertions(+) 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) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy