Skip to content

Stdlib binascii #185

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
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
py: improve ParseTuple{,AndKeywords} to handle 'y{,*,#}'
Signed-off-by: Sebastien Binet <binet@cern.ch>
  • Loading branch information
sbinet committed May 9, 2022
commit 8ca157b84c9fdfe4726ff015fc1d6626eb95c991
17 changes: 17 additions & 0 deletions py/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,23 @@ func ParseTupleAndKeywords(args Tuple, kwargs StringDict, format string, kwlist
}
}
*result = arg
case 'y':
switch op.modifier {
default:
if _, ok := arg.(Bytes); !ok {
return ExceptionNewf(TypeError, "%s() argument %d must be bytes-like, not %s", name, i+1, arg.Type().Name)
}
case '#':
fallthrough // FIXME(sbinet): check for read-only?
case '*':
switch arg := arg.(type) {
case Bytes:
// ok.
default:
return ExceptionNewf(TypeError, "%s() argument %d must be bytes-like, not %s", name, i+1, arg.Type().Name)
}
}
*result = arg
case 'i', 'n':
if _, ok := arg.(Int); !ok {
return ExceptionNewf(TypeError, "%s() argument %d must be int, not %s", name, i+1, arg.Type().Name)
Expand Down
51 changes: 51 additions & 0 deletions py/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,57 @@ func TestParseTupleAndKeywords(t *testing.T) {
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str or bytes-like, not NoneType'"),
},
{
args: Tuple{Bytes("a")},
format: "y:func",
results: []Object{Bytes("a")},
},
{
args: Tuple{None},
format: "y:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not NoneType'"),
},
{
args: Tuple{String("a")},
format: "y:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not str'"),
},
{
args: Tuple{Bytes("a")},
format: "y#:func",
results: []Object{Bytes("a")},
},
{
args: Tuple{String("a")},
format: "y#:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not str'"),
},
{
args: Tuple{None},
format: "y#:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not NoneType'"),
},
{
args: Tuple{Bytes("a")},
format: "y*:func",
results: []Object{Bytes("a")},
},
{
args: Tuple{String("a")},
format: "y*:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not str'"),
},
{
args: Tuple{None},
format: "y*:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be bytes-like, not NoneType'"),
},
{
args: Tuple{String("a")},
format: "U:func",
Expand Down