pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/go-python/gpython/pull/67/files

rel="stylesheet" href="https://github.githubassets.com/assets/code-34e10031edc15af1.css" /> py: Fix __mul__ of list and tuple on negative case by corona10 · Pull Request #67 · go-python/gpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions py/list.go
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
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
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
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
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

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