Skip to content

Commit 8ca157b

Browse files
committed
py: improve ParseTuple{,AndKeywords} to handle 'y{,*,#}'
Signed-off-by: Sebastien Binet <binet@cern.ch>
1 parent af8341e commit 8ca157b

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

py/args.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,23 @@ func ParseTupleAndKeywords(args Tuple, kwargs StringDict, format string, kwlist
519519
}
520520
}
521521
*result = arg
522+
case 'y':
523+
switch op.modifier {
524+
default:
525+
if _, ok := arg.(Bytes); !ok {
526+
return ExceptionNewf(TypeError, "%s() argument %d must be bytes-like, not %s", name, i+1, arg.Type().Name)
527+
}
528+
case '#':
529+
fallthrough // FIXME(sbinet): check for read-only?
530+
case '*':
531+
switch arg := arg.(type) {
532+
case Bytes:
533+
// ok.
534+
default:
535+
return ExceptionNewf(TypeError, "%s() argument %d must be bytes-like, not %s", name, i+1, arg.Type().Name)
536+
}
537+
}
538+
*result = arg
522539
case 'i', 'n':
523540
if _, ok := arg.(Int); !ok {
524541
return ExceptionNewf(TypeError, "%s() argument %d must be int, not %s", name, i+1, arg.Type().Name)

py/args_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,57 @@ func TestParseTupleAndKeywords(t *testing.T) {
174174
results: []Object{nil},
175175
err: fmt.Errorf("TypeError: 'func() argument 1 must be str or bytes-like, not NoneType'"),
176176
},
177+
{
178+
args: Tuple{Bytes("a")},
179+
format: "y:func",
180+
results: []Object{Bytes("a")},
181+
},
182+
{
183+
args: Tuple{None},
184+
format: "y:func",
185+
results: []Object{nil},
186+
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not NoneType'"),
187+
},
188+
{
189+
args: Tuple{String("a")},
190+
format: "y:func",
191+
results: []Object{nil},
192+
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not str'"),
193+
},
194+
{
195+
args: Tuple{Bytes("a")},
196+
format: "y#:func",
197+
results: []Object{Bytes("a")},
198+
},
199+
{
200+
args: Tuple{String("a")},
201+
format: "y#:func",
202+
results: []Object{nil},
203+
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not str'"),
204+
},
205+
{
206+
args: Tuple{None},
207+
format: "y#:func",
208+
results: []Object{nil},
209+
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not NoneType'"),
210+
},
211+
{
212+
args: Tuple{Bytes("a")},
213+
format: "y*:func",
214+
results: []Object{Bytes("a")},
215+
},
216+
{
217+
args: Tuple{String("a")},
218+
format: "y*:func",
219+
results: []Object{nil},
220+
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not str'"),
221+
},
222+
{
223+
args: Tuple{None},
224+
format: "y*:func",
225+
results: []Object{nil},
226+
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not NoneType'"),
227+
},
177228
{
178229
args: Tuple{String("a")},
179230
format: "U:func",

0 commit comments

Comments
 (0)