Skip to content

Commit 137c43f

Browse files
drew-512Drew O'Meara
and
Drew O'Meara
authored
py: introduce NewListFromStrings, apply govet fixes
This CL introduces NewListFromStrings to easily create a py.List from a slice of strings and applies various govet fixes. Co-authored-by: Drew O'Meara <[email protected]>
1 parent cd69d37 commit 137c43f

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

Diff for: py/dict.go

+6
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,9 @@ func (a StringDict) M__contains__(other Object) (Object, error) {
217217
}
218218
return False, nil
219219
}
220+
221+
func (d StringDict) GetDict() StringDict {
222+
return d
223+
}
224+
225+
var _ IGetDict = (*StringDict)(nil)

Diff for: py/list.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func init() {
4141

4242
ListType.Dict["sort"] = MustNewMethod("sort", func(self Object, args Tuple, kwargs StringDict) (Object, error) {
4343
const funcName = "sort"
44-
var l *List
45-
if self == None {
44+
l, isList := self.(*List)
45+
if !isList {
4646
// method called using `list.sort([], **kwargs)`
4747
var o Object
4848
err := UnpackTuple(args, nil, funcName, 1, 1, &o)
@@ -60,7 +60,6 @@ func init() {
6060
if err != nil {
6161
return nil, err
6262
}
63-
l = self.(*List)
6463
}
6564
err := SortInPlace(l, kwargs, funcName)
6665
if err != nil {
@@ -121,6 +120,15 @@ func NewListFromItems(items []Object) *List {
121120
return l
122121
}
123122

123+
// Makes an argv into a tuple
124+
func NewListFromStrings(items []string) *List {
125+
l := NewListSized(len(items))
126+
for i, v := range items {
127+
l.Items[i] = String(v)
128+
}
129+
return l
130+
}
131+
124132
// Copy a list object
125133
func (l *List) Copy() *List {
126134
return NewListFromItems(l.Items)
@@ -141,6 +149,13 @@ func (l *List) Extend(items []Object) {
141149
l.Items = append(l.Items, items...)
142150
}
143151

152+
// Extend the list with strings
153+
func (l *List) ExtendWithStrings(items []string) {
154+
for _, item := range items {
155+
l.Items = append(l.Items, Object(String(item)))
156+
}
157+
}
158+
144159
// Extends the list with the sequence passed in
145160
func (l *List) ExtendSequence(seq Object) error {
146161
return Iterate(seq, func(item Object) bool {

Diff for: py/traceback.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ RuntimeError: this is the error message
5252
func (tb *Traceback) TracebackDump(w io.Writer) {
5353
for ; tb != nil; tb = tb.Next {
5454
fmt.Fprintf(w, " File %q, line %d, in %s\n", tb.Frame.Code.Filename, tb.Lineno, tb.Frame.Code.Name)
55-
fmt.Fprintf(w, " %s\n", "FIXME line of source goes here")
55+
//fmt.Fprintf(w, " %s\n", "FIXME line of source goes here")
5656
}
5757
}
5858

0 commit comments

Comments
 (0)