FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

py: Fix __mul__ of list and tuple on negative case by corona10 · Pull Request #67 · go-python/gpython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (2) .py  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions py/list.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions py/tests/list.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 6 additions & 0 deletions py/tests/tuple.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions py/tuple.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (a Tuple) M__add__(other Object) (Object, error) {
copy(newTuple[len(b):], b)
return newTuple, nil
}

return NotImplemented, nil
}

Expand All @@ -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)
Expand Down

Back | FazBrowse Home | New Git URL