From 0203481f3655b21918e901ce518084f1df754519 Mon Sep 17 00:00:00 2001 From: HyeockJinKim Date: Wed, 25 Sep 2019 20:28:14 +0900 Subject: [PATCH 1/3] Add Slice function for range type Crate a new range object by calculating start, stop, and step when slice is entered as argument to the __getitem__ function of the range Fixes #77 --- py/range.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/py/range.go b/py/range.go index 31e85963..fe31497e 100644 --- a/py/range.go +++ b/py/range.go @@ -79,18 +79,18 @@ func RangeNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) { } func (r *Range) M__getitem__(key Object) (Object, error) { + if slice, ok := key.(*Slice); ok { + return computeRangeSlice(r, slice) + } + index, err := Index(key) if err != nil { return nil, err } - // TODO(corona10): Support slice case - length := computeRangeLength(r.Start, r.Stop, r.Step) - if index < 0 { - index += length - } + index = computeNegativeIndex(index, r.Length) - if index < 0 || index >= length { - return nil, ExceptionNewf(TypeError, "range object index out of range") + if index < 0 || index >= r.Length { + return nil, ExceptionNewf(IndexError, "range object index out of range") } result := computeItem(r, index) return result, nil @@ -160,6 +160,69 @@ func computeRangeLength(start, stop, step Int) Int { return res } +func computeNegativeIndex(index, length Int) Int { + if index < 0 { + index += length + } + return index +} + +func computeBoundIndex(index, length Int) Int { + if index < 0 { + index = 0 + } else if index > length { + index = length + } + return index +} + +func computeRangeSlice(r *Range, s *Slice) (Object, error) { + start, err := Index(s.Start) + if err != nil { + start = 0 + } + stop, err := Index(s.Stop) + if err != nil { + stop = r.Length + } + + step, err := Index(s.Step) + if err != nil { + step = 1 + } + if step == 0 { + return nil, ExceptionNewf(ValueError, "slice step cannot be zero") + } + start = computeNegativeIndex(start, r.Length) + stop = computeNegativeIndex(stop, r.Length) + + start = computeBoundIndex(start, r.Length) + stop = computeBoundIndex(stop, r.Length) + + startIndex := computeItem(r, start) + stopIndex := computeItem(r, stop) + stepIndex := step * r.Step + + var sliceLength Int + if start < stop { + if stepIndex < 0 { + startIndex, stopIndex = stopIndex-1, startIndex-1 + } + } else { + if stepIndex < 0 { + startIndex, stopIndex = stopIndex+1, startIndex+1 + } + } + sliceLength = computeRangeLength(startIndex, stopIndex, stepIndex) + + return &Range{ + Start: startIndex, + Stop: stopIndex, + Step: stepIndex, + Length: sliceLength, + }, nil +} + // Check interface is satisfied var _ I__getitem__ = (*Range)(nil) var _ I__iter__ = (*Range)(nil) From bedc71c4e861ecee8f277d749bc127b356b82883 Mon Sep 17 00:00:00 2001 From: HyeockJinKim Date: Thu, 26 Sep 2019 23:30:22 +0900 Subject: [PATCH 2/3] Add typecall for __index__ in Index function If __index__ is defined when class is used as index value, __index__ value is used. --- py/internal.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/py/internal.go b/py/internal.go index 7c3e999a..bf654631 100644 --- a/py/internal.go +++ b/py/internal.go @@ -87,10 +87,21 @@ func MakeGoInt64(a Object) (int64, error) { // // Will raise TypeError if Index can't be run on this object func Index(a Object) (Int, error) { - A, ok := a.(I__index__) - if ok { + if A, ok := a.(I__index__); ok { return A.M__index__() } + + if A, ok, err := TypeCall0(a, "__index__"); ok { + if err != nil { + return 0, err + } + if res, ok := A.(Int); ok { + return res, nil + } + + return 0, err + } + return 0, ExceptionNewf(TypeError, "unsupported operand type(s) for index: '%s'", a.Type().Name) } From 2da2efab0c894f8f9803e6c6275bf8d33c719cc2 Mon Sep 17 00:00:00 2001 From: HyeockJinKim Date: Thu, 26 Sep 2019 23:54:23 +0900 Subject: [PATCH 3/3] Add tests for range --- py/tests/range.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/py/tests/range.py b/py/tests/range.py index 0d3a54fc..44eca5db 100644 --- a/py/tests/range.py +++ b/py/tests/range.py @@ -76,4 +76,44 @@ assert str(range(0, 3)) == 'range(0, 3)' assert str(range(10, 3, -2)) == 'range(10, 3, -2)' +doc="range_slice" +a = range(10) +assert a[::-1][0] == 9 +assert a[::-1][9] == 0 +assert a[0:3][0] == 0 +assert a[0:3][2] == 2 +assert a[-3:10][0] == 7 +assert a[-100:13][0] == 0 +assert a[-100:13][9] == 9 + +try: + a[0:3][3] +except IndexError: + pass +else: + assert False, "IndexError not raised" +try: + a[100:13][0] +except IndexError: + pass +else: + assert False, "IndexError not raised" +try: + a[0:3:0] +except ValueError: + pass +else: + assert False, "ValueError not raised" + +doc="range_index" +class Index: + def __index__(self): + return 1 + +a = range(10) +b = Index() +assert a[b] == 1 +assert a[b:10] == a[1:10] +assert a[10:b:-1] == a[10:1:-1] + doc="finished" 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