Skip to content

Commit d60fc6f

Browse files
committed
Implement tuple.__new__
1 parent 4eda964 commit d60fc6f

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

py/tuple.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package py
44

5-
var TupleType = NewType("tuple", "tuple() -> empty tuple\ntuple(iterable) -> tuple initialized from iterable's items\n\nIf the argument is a tuple, the return value is the same object.")
5+
var TupleType = ObjectType.NewType("tuple", "tuple() -> empty tuple\ntuple(iterable) -> tuple initialized from iterable's items\n\nIf the argument is a tuple, the return value is the same object.", TupleNew, nil)
66

77
type Tuple []Object
88

@@ -11,6 +11,31 @@ func (o Tuple) Type() *Type {
1111
return TupleType
1212
}
1313

14+
// TupleNew
15+
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+
}()
27+
var iterable Object
28+
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)
36+
}
37+
}
38+
1439
// Copy a tuple object
1540
func (t Tuple) Copy() Tuple {
1641
newT := make(Tuple, len(t))

0 commit comments

Comments
 (0)