Skip to content

Commit 7d197e6

Browse files
committed
Implement list() factoring common parts out of tuple() into SequenceList, SequenceTuple and Iterate.
1 parent d60fc6f commit 7d197e6

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

py/internal.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,23 @@ func Next(self Object) Object {
298298
panic(ExceptionNewf(TypeError, "'%s' object is not iterable", self.Type().Name))
299299
}
300300

301+
// Iterate the iterator until finished calling the function passed in on each object
302+
func Iterate(iterator Object, fn func(Object)) {
303+
defer func() {
304+
if r := recover(); r != nil {
305+
if IsException(StopIteration, r) {
306+
// StopIteration or subclass raised
307+
} else {
308+
panic(r)
309+
}
310+
}
311+
}()
312+
for {
313+
item := Next(iterator)
314+
fn(item)
315+
}
316+
}
317+
301318
// Call send for the python object
302319
func Send(self, value Object) Object {
303320
if I, ok := self.(I_send); ok {

py/list.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package py
44

5-
var ListType = NewType("list", "list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items")
5+
var ListType = ObjectType.NewType("list", "list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items", ListNew, nil)
66

77
// FIXME lists are mutable so this should probably be struct { Tuple } then can use the sub methods on Tuple
88
type List struct {
@@ -14,6 +14,16 @@ func (o *List) Type() *Type {
1414
return ListType
1515
}
1616

17+
// ListNew
18+
func ListNew(metatype *Type, args Tuple, kwargs StringDict) (res Object) {
19+
var iterable Object
20+
UnpackTuple(args, kwargs, "list", 0, 1, &iterable)
21+
if iterable != nil {
22+
return SequenceList(iterable)
23+
}
24+
return NewList()
25+
}
26+
1727
// Make a new empty list
1828
func NewList() *List {
1929
return &List{}

py/sequence.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,32 @@ package py
44

55
// Converts a sequence object v into a Tuple
66
func SequenceTuple(v Object) Tuple {
7-
// FIXME need to support iterable objects etc!
87
switch x := v.(type) {
98
case Tuple:
109
return x
1110
case *List:
1211
return Tuple(x.Items).Copy()
12+
default:
13+
t := Tuple{}
14+
Iterate(Iter(v), func(item Object) {
15+
t = append(t, item)
16+
})
17+
return t
1318
}
14-
panic(ExceptionNewf(TypeError, "SequenceTuple not fully implemented, can't convert %s", v.Type().Name))
1519
}
1620

1721
// Converts a sequence object v into a List
1822
func SequenceList(v Object) *List {
19-
// FIXME need to support iterable objects etc!
2023
switch x := v.(type) {
2124
case Tuple:
2225
return NewListFromItems(x)
2326
case *List:
2427
return x.Copy()
28+
default:
29+
l := NewList()
30+
Iterate(Iter(v), func(item Object) {
31+
l.Append(item)
32+
})
33+
return l
2534
}
26-
panic(ExceptionNewf(TypeError, "SequenceList not fully implemented, can't convert %s", v.Type().Name))
2735
}

py/tuple.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,12 @@ func (o Tuple) Type() *Type {
1313

1414
// TupleNew
1515
func TupleNew(metatype *Type, args Tuple, kwargs StringDict) (res Object) {
16-
t := Tuple{}
17-
defer func() {
18-
if r := recover(); r != nil {
19-
if IsException(StopIteration, r) {
20-
// StopIteration or subclass raised
21-
res = t
22-
} else {
23-
panic(r)
24-
}
25-
}
26-
}()
2716
var iterable Object
2817
UnpackTuple(args, kwargs, "tuple", 0, 1, &iterable)
29-
if iterable == nil {
30-
return t
31-
}
32-
it := Iter(iterable)
33-
for {
34-
item := Next(it)
35-
t = append(t, item)
18+
if iterable != nil {
19+
return SequenceTuple(iterable)
3620
}
21+
return Tuple{}
3722
}
3823

3924
// Copy a tuple object

0 commit comments

Comments
 (0)