Skip to content

Add list type safety, helpers; remove cruft #157

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
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions py/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,9 @@ func (a StringDict) M__contains__(other Object) (Object, error) {
}
return False, nil
}

func (d StringDict) GetDict() StringDict {
return d
}

var _ IGetDict = (*StringDict)(nil)
21 changes: 18 additions & 3 deletions py/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func init() {

ListType.Dict["sort"] = MustNewMethod("sort", func(self Object, args Tuple, kwargs StringDict) (Object, error) {
const funcName = "sort"
var l *List
if self == None {
l, isList := self.(*List)
if !isList {
// method called using `list.sort([], **kwargs)`
var o Object
err := UnpackTuple(args, nil, funcName, 1, 1, &o)
Expand All @@ -60,7 +60,6 @@ func init() {
if err != nil {
return nil, err
}
l = self.(*List)
}
err := SortInPlace(l, kwargs, funcName)
if err != nil {
Expand Down Expand Up @@ -121,6 +120,15 @@ func NewListFromItems(items []Object) *List {
return l
}

// Makes an argv into a tuple
func NewListFromStrings(items []string) *List {
l := NewListSized(len(items))
for i, v := range items {
l.Items[i] = String(v)
}
return l
}

// Copy a list object
func (l *List) Copy() *List {
return NewListFromItems(l.Items)
Expand All @@ -141,6 +149,13 @@ func (l *List) Extend(items []Object) {
l.Items = append(l.Items, items...)
}

// Extend the list with strings
func (l *List) ExtendWithStrings(items []string) {
for _, item := range items {
l.Items = append(l.Items, Object(String(item)))
}
}

// Extends the list with the sequence passed in
func (l *List) ExtendSequence(seq Object) error {
return Iterate(seq, func(item Object) bool {
Expand Down
2 changes: 1 addition & 1 deletion py/traceback.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ RuntimeError: this is the error message
func (tb *Traceback) TracebackDump(w io.Writer) {
for ; tb != nil; tb = tb.Next {
fmt.Fprintf(w, " File %q, line %d, in %s\n", tb.Frame.Code.Filename, tb.Lineno, tb.Frame.Code.Name)
fmt.Fprintf(w, " %s\n", "FIXME line of source goes here")
//fmt.Fprintf(w, " %s\n", "FIXME line of source goes here")
}
}

Expand Down