Skip to content

Commit fc9465a

Browse files
committed
Fix default arguments
1 parent 40e3f41 commit fc9465a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

py/function.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,25 @@ func NewFunction(code *Code, globals StringDict, qualname string) *Function {
8686
// Setup locals for calling the function with the given arguments
8787
func (f *Function) LocalsForCall(args Tuple) StringDict {
8888
// fmt.Printf("call f %#v with %v\n", f, args)
89-
if len(args) != int(f.Code.Argcount) {
90-
// FIXME don't know how to deal with default args
91-
panic(fmt.Sprintf("Wrong number of arguments: expecting %d but got %d: %#v", f.Code.Argcount, len(args), args))
89+
max := int(f.Code.Argcount)
90+
min := max - len(f.Defaults)
91+
if len(args) > max || len(args) < min {
92+
if min == max {
93+
panic(fmt.Sprintf("TypeError: %s() takes %d positional arguments but %d were given", f.Name, max))
94+
} else {
95+
panic(fmt.Sprintf("TypeError: %s() takes from %d to %d positional arguments but %d were given", f.Name, min, max))
96+
}
9297
}
98+
9399
// FIXME not sure this is right!
94100
// Copy the args into the local variables
95101
locals := NewStringDict()
96102
for i := range args {
97103
locals[f.Code.Varnames[i]] = args[i]
98104
}
105+
for i := len(args); i < max; i++ {
106+
locals[f.Code.Varnames[i]] = f.Defaults[i-min]
107+
}
99108
// fmt.Printf("locals = %v\n", locals)
100109
return locals
101110
}

0 commit comments

Comments
 (0)