Skip to content

Handle the non-integer return of __index__ #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 1, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Handle error of slice in range type
When an error occurs, the error is returned and
when the value is none the slice's values have a default value.
  • Loading branch information
HyeockJinKim committed Sep 29, 2019
commit 22ccb4a1288c395cbdb5e6ee2657ffc4de0116a9
24 changes: 17 additions & 7 deletions py/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ func computeRangeLength(start, stop, step Int) Int {
return res
}

func getIndexWithDefault(i Object, d Int) (Int, error) {
if i == None {
return d, nil
} else if res, err := Index(i); err != nil {
return 0, err
} else {
return res, nil
}
}

func computeNegativeIndex(index, length Int) Int {
if index < 0 {
index += length
Expand All @@ -177,19 +187,19 @@ func computeBoundIndex(index, length Int) Int {
}

func computeRangeSlice(r *Range, s *Slice) (Object, error) {
start, err := Index(s.Start)
start, err := getIndexWithDefault(s.Start, 0)
if err != nil {
start = 0
return nil, err
}
stop, err := Index(s.Stop)
stop, err := getIndexWithDefault(s.Stop, r.Length)
if err != nil {
stop = r.Length
return nil, err
}

step, err := Index(s.Step)
step, err := getIndexWithDefault(s.Step, 1)
if err != nil {
step = 1
return nil, err
}

if step == 0 {
return nil, ExceptionNewf(ValueError, "slice step cannot be zero")
}
Expand Down