Skip to content

Commit 63fe344

Browse files
committed
Implement ParseTuple and "d" option
1 parent 47c8d26 commit 63fe344

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

py/args.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ package py
409409

410410
// ParseTupleAndKeywords
411411
func ParseTupleAndKeywords(args Tuple, kwargs StringDict, format string, kwlist []string, results ...*Object) {
412-
if len(results) != len(kwlist) {
412+
if kwlist != nil && len(results) != len(kwlist) {
413413
panic("Internal error: supply the same number of results and kwlist")
414414
}
415415
min, max, name, ops := parseFormat(format)
@@ -455,12 +455,27 @@ func ParseTupleAndKeywords(args Tuple, kwargs StringDict, format string, kwlist
455455
panic(ExceptionNewf(TypeError, "%s() argument %d must be int, not %s", name, i+1, arg.Type().Name))
456456
}
457457
*result = arg
458+
case "d":
459+
switch x := arg.(type) {
460+
case Int:
461+
*result = Float(x)
462+
case Float:
463+
*result = x
464+
default:
465+
panic(ExceptionNewf(TypeError, "%s() argument %d must be float, not %s", name, i+1, arg.Type().Name))
466+
}
467+
458468
default:
459469
panic(ExceptionNewf(TypeError, "Unknown/Unimplemented format character %q in ParseTupleAndKeywords called from %s", op, name))
460470
}
461471
}
462472
}
463473

474+
// Parse tuple only
475+
func ParseTuple(args Tuple, format string, results ...*Object) {
476+
ParseTupleAndKeywords(args, nil, format, nil, results...)
477+
}
478+
464479
// Parse the format
465480
func parseFormat(format string) (min, max int, name string, ops []string) {
466481
name = "function"

0 commit comments

Comments
 (0)