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
Next Next commit
py: improve ParseTuple{,AndKeywords} to handle 's{,*,#}'
Signed-off-by: Sebastien Binet <binet@cern.ch>
  • Loading branch information
sbinet committed May 9, 2022
commit af8341ee071138455dc92592a5368243c873bcba
55 changes: 49 additions & 6 deletions py/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,60 @@ func ParseTupleAndKeywords(args Tuple, kwargs StringDict, format string, kwlist
switch op.code {
case 'O':
*result = arg
case 'Z', 'z':
if _, ok := arg.(NoneType); ok {
*result = arg
break
case 'Z':
switch op.modifier {
default:
return ExceptionNewf(TypeError, "%s() argument %d must be str or None, not %s", name, i+1, arg.Type().Name)
case '#', 0:
switch arg := arg.(type) {
case String, NoneType:
default:
return ExceptionNewf(TypeError, "%s() argument %d must be str or None, not %s", name, i+1, arg.Type().Name)
}
}
*result = arg
case 'z':
switch op.modifier {
default:
switch arg := arg.(type) {
case String, NoneType:
// ok
default:
return ExceptionNewf(TypeError, "%s() argument %d must be str or None, not %s", name, i+1, arg.Type().Name)
}
case '#':
fallthrough // FIXME(sbinet): check for read-only?
case '*':
switch arg := arg.(type) {
case String, Bytes, NoneType:
// ok.
default:
return ExceptionNewf(TypeError, "%s() argument %d must be str, bytes-like or None, not %s", name, i+1, arg.Type().Name)
}
}
fallthrough
case 'U', 's':
*result = arg
case 'U':
if _, ok := arg.(String); !ok {
return ExceptionNewf(TypeError, "%s() argument %d must be str, not %s", name, i+1, arg.Type().Name)
}
*result = arg
case 's':
switch op.modifier {
default:
if _, ok := arg.(String); !ok {
return ExceptionNewf(TypeError, "%s() argument %d must be str, not %s", name, i+1, arg.Type().Name)
}
case '#':
fallthrough // FIXME(sbinet): check for read-only?
case '*':
switch arg := arg.(type) {
case String, Bytes:
// ok.
default:
return ExceptionNewf(TypeError, "%s() argument %d must be str or 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
277 changes: 277 additions & 0 deletions py/args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package py

import (
"fmt"
"testing"
)

func TestParseTupleAndKeywords(t *testing.T) {
for _, tc := range []struct {
args Tuple
kwargs StringDict
format string
kwlist []string
results []Object
err error
}{
{
args: Tuple{String("a")},
format: "O:func",
results: []Object{String("a")},
},
{
args: Tuple{None},
format: "Z:func",
results: []Object{None},
},
{
args: Tuple{String("a")},
format: "Z:func",
results: []Object{String("a")},
},
{
args: Tuple{Int(42)},
format: "Z:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str or None, not int'"),
},
{
args: Tuple{None},
format: "Z*:func", // FIXME(sbinet): invalid format.
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str or None, not NoneType'"),
},
{
args: Tuple{None},
format: "Z#:func",
results: []Object{None},
},
{
args: Tuple{String("a")},
format: "Z#:func",
results: []Object{String("a")},
},
{
args: Tuple{Int(42)},
format: "Z#:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str or None, not int'"),
},
{
args: Tuple{None},
format: "z:func",
results: []Object{None},
},
{
args: Tuple{String("a")},
format: "z:func",
results: []Object{String("a")},
},
{
args: Tuple{Int(42)},
format: "z:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str or None, not int'"),
},
{
args: Tuple{Bytes("a")},
format: "z:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str or None, not bytes'"),
},
{
args: Tuple{None},
format: "z*:func",
results: []Object{None},
},
{
args: Tuple{Bytes("a")},
format: "z*:func",
results: []Object{Bytes("a")},
},
{
args: Tuple{String("a")},
format: "z*:func",
results: []Object{String("a")},
},
{
args: Tuple{Int(42)},
format: "z*:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str, bytes-like or None, not int'"),
},
{
args: Tuple{None},
format: "z#:func",
results: []Object{None},
},
{
args: Tuple{Bytes("a")},
format: "z#:func",
results: []Object{Bytes("a")},
},
{
args: Tuple{String("a")},
format: "z#:func",
results: []Object{String("a")},
},
{
args: Tuple{Int(42)},
format: "z#:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str, bytes-like or None, not int'"),
},
{
args: Tuple{String("a")},
format: "s:func",
results: []Object{String("a")},
},
{
args: Tuple{None},
format: "s:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str, not NoneType'"),
},
{
args: Tuple{Bytes("a")},
format: "s:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str, not bytes'"),
},
{
args: Tuple{String("a")},
format: "s#:func",
results: []Object{String("a")},
},
{
args: Tuple{Bytes("a")},
format: "s#:func",
results: []Object{Bytes("a")},
},
{
args: Tuple{None},
format: "s#:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str or bytes-like, not NoneType'"),
},
{
args: Tuple{String("a")},
format: "s*:func",
results: []Object{String("a")},
},
{
args: Tuple{Bytes("a")},
format: "s*:func",
results: []Object{Bytes("a")},
},
{
args: Tuple{None},
format: "s*:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str or bytes-like, not NoneType'"),
},
{
args: Tuple{String("a")},
format: "U:func",
results: []Object{String("a")},
},
{
args: Tuple{None},
format: "U:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str, not NoneType'"),
},
{
args: Tuple{Bytes("a")},
format: "U:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str, not bytes'"),
},
{
args: Tuple{Bytes("a")},
format: "U*:func", // FIXME(sbinet): invalid format
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str, not bytes'"),
},
{
args: Tuple{Bytes("a")},
format: "U#:func", // FIXME(sbinet): invalid format
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be str, not bytes'"),
},
{
args: Tuple{Int(42)},
format: "i:func",
results: []Object{Int(42)},
},
{
args: Tuple{None},
format: "i:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be int, not NoneType'"),
},
{
args: Tuple{Int(42)},
format: "n:func",
results: []Object{Int(42)},
},
{
args: Tuple{None},
format: "n:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be int, not NoneType'"),
},
{
args: Tuple{Bool(true)},
format: "p:func",
results: []Object{Bool(true)},
},
{
args: Tuple{None},
format: "p:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be bool, not NoneType'"),
},
{
args: Tuple{Float(42)},
format: "d:func",
results: []Object{Float(42)},
},
{
args: Tuple{Int(42)},
format: "d:func",
results: []Object{Float(42)},
},
{
args: Tuple{None},
format: "p:func",
results: []Object{nil},
err: fmt.Errorf("TypeError: 'func() argument 1 must be bool, not NoneType'"),
},
} {
t.Run(tc.format, func(t *testing.T) {
results := make([]*Object, len(tc.results))
for i := range tc.results {
results[i] = &tc.results[i]
}
err := ParseTupleAndKeywords(tc.args, tc.kwargs, tc.format, tc.kwlist, results...)
switch {
case err != nil && tc.err != nil:
if got, want := err.Error(), tc.err.Error(); got != want {
t.Fatalf("invalid error:\ngot= %s\nwant=%s", got, want)
}
case err != nil && tc.err == nil:
t.Fatalf("could not parse tuple+kwargs: %+v", err)
case err == nil && tc.err != nil:
t.Fatalf("expected an error (got=nil): %+v", tc.err)
case err == nil && tc.err == nil:
// ok.
}
// FIXME(sbinet): check results
})
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy