Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.
Open
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions runtime/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,38 @@ func listDelItem(f *Frame, o *Object, key *Object) *BaseException {
return l.DelItem(f, index)
}

func listDelSlice(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
argc := len(args)
if argc != 3 {
return nil, f.RaiseType(TypeErrorType, fmt.Sprintf("function takes exactly 2 arguments (%d given)", argc-1))
}
l := toListUnsafe(args[0])
if args[1].typ.slots.Index == nil || args[2].typ.slots.Index == nil {
return nil, f.RaiseType(TypeErrorType, "an integer is required")
}
l.mutex.Lock()
numListElems := len(l.elems)
start, raised := calcIndex(f, args[1], numListElems)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead construct a slice and call DelSlice?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trotterdylan was thinking same, but some tests are not passing:

--- FAIL: TestListDelSlice (0.00s)
	list_test.go:122: TestListDelSlice([1, 2, 3, 4, 5], -1, 3) = [1, 2, 3, 4, 5], want [4, 5]
	list_test.go:122: TestListDelSlice([1, 2, 3, 4, 5], 1, -3) = [1, 3, 4, 5], want [1, 2, 3, 4, 5]

from CPython:

>>> x=[1,2,3,4,5]
>>> x.__delslice__(-1, 3)
>>> x
[4, 5]
>>> x=[1,2,3,4,5]
>>> x.__delslice__(1, -3)
>>> x
[1, 2, 3, 4, 5]
>>> 

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trotterdylan ok, I will keep index alignment before initiating Slice, it should be enough

if raised != nil {
l.mutex.Unlock()
return nil, raised
}
stop, raised := calcIndex(f, args[2], numListElems)
if raised != nil {
l.mutex.Unlock()
return nil, raised
}
if start == stop || start > stop {
l.mutex.Unlock()
return nil, nil
}
numSliceElems := stop - start
copy(l.elems[start:numListElems-numSliceElems], l.elems[stop:numListElems])
l.elems = l.elems[:numListElems-numSliceElems]
l.mutex.Unlock()
return nil, nil
}

func listRemove(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkMethodArgs(f, "remove", args, ListType, ObjectType); raised != nil {
return nil, raised
Expand Down Expand Up @@ -521,6 +553,7 @@ func listSort(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
}

func initListType(dict map[string]*Object) {
dict["__delslice__"] = newBuiltinFunction("__delslice__", listDelSlice).ToObject()
dict["append"] = newBuiltinFunction("append", listAppend).ToObject()
dict["count"] = newBuiltinFunction("count", listCount).ToObject()
dict["extend"] = newBuiltinFunction("extend", listExtend).ToObject()
Expand Down Expand Up @@ -552,6 +585,20 @@ func initListType(dict map[string]*Object) {
ListType.slots.SetItem = &setItemSlot{listSetItem}
}

func calcIndex(f *Frame, i *Object, numListElems int) (int, *BaseException) {
index, raised := IndexInt(f, i)
if raised != nil {
return 0, raised
}
if index < 0 {
index = 0
}
if index > numListElems {
index = numListElems
}
return index, nil
}

type listIterator struct {
Object
list *List
Expand Down
32 changes: 32 additions & 0 deletions runtime/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ func TestListCount(t *testing.T) {
}
}

func TestListDelSlice(t *testing.T) {
fun := newBuiltinFunction("TestListDelSlice", func(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
delSlice, raised := GetAttr(f, ListType.ToObject(), NewStr("__delslice__"), nil)
if raised != nil {
return nil, raised
}
if _, raised := delSlice.Call(f, args, nil); raised != nil {
return nil, raised
}
return args[0], nil
}).ToObject()
cases := []invokeTestCase{
{args: wrapArgs(NewList(), 1, 3), want: NewList().ToObject()},
{args: wrapArgs(newTestList(1, 2, 3, 4, 5), -1, 3), want: newTestList(4, 5).ToObject()},
{args: wrapArgs(newTestList(1, 2, 3, 4, 5), 1, -3), want: newTestList(1, 2, 3, 4, 5).ToObject()},
{args: wrapArgs(newTestList(1, 2, 3, 4, 5), -1, -3), want: newTestList(1, 2, 3, 4, 5).ToObject()},
{args: wrapArgs(newTestList(1, 2, 3, 4, 5), 3, 3), want: newTestList(1, 2, 3, 4, 5).ToObject()},
{args: wrapArgs(newTestList(1, 2, 3, 4, 5), 1, 3), want: newTestList(1, 4, 5).ToObject()},
{args: wrapArgs(newTestList(1, 2, 3, 4, 5), 1, big.NewInt(3)), want: newTestList(1, 4, 5).ToObject()},
{args: wrapArgs(newTestList(1, 2, 3, 4, 5), 1, 31415), want: newTestList(1).ToObject()},
{args: wrapArgs(newTestList(1, 2, 3, 4, 5), 3, 1), want: newTestList(1, 2, 3, 4, 5).ToObject()},
{args: wrapArgs(newTestRange(10), 1, 8), want: newTestList(0, 8, 9).ToObject()},
{args: wrapArgs(NewList(), 1), wantExc: mustCreateException(TypeErrorType, "function takes exactly 2 arguments (1 given)")},
{args: wrapArgs(newTestList(true), None, None), wantExc: mustCreateException(TypeErrorType, "an integer is required")},
}
for _, cas := range cases {
if err := runInvokeTestCase(fun, &cas); err != "" {
t.Error(err)
}
}
}

func TestListDelItem(t *testing.T) {
badIndexType := newTestClass("badIndex", []*Type{ObjectType}, newStringDict(map[string]*Object{
"__index__": newBuiltinFunction("__index__", func(f *Frame, _ Args, _ KWArgs) (*Object, *BaseException) {
Expand Down