Skip to content

Commit 5266c83

Browse files
authored
gopy,bind: improve error reporting for python-incompatible methods and funcs
1 parent 35472c0 commit 5266c83

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

_examples/gopyerrors/gopyerrors.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2020 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// package gopyerrors contains functions that generate error
6+
// messages from gopy itself.
7+
package gopyerrors
8+
9+
func NotErrorMany() (int, int) {
10+
return 0, 0
11+
}
12+
13+
func TooMany() (int, int, string) {
14+
return 0, 1, "Hi"
15+
}
16+
17+
func OK() (int, error) {
18+
return 0, nil
19+
}
20+
21+
type Struct struct{}
22+
23+
func (s *Struct) NotErrorMany() (int, string) {
24+
return 0, "Hi"
25+
}
26+
27+
func (s *Struct) TooMany() (int, int, string) {
28+
return 0, 1, "Hi"
29+
}

_examples/gopyerrors/test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2020 go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
# py2/py3 compat
6+
from __future__ import print_function
7+
8+
import gopyerrors
9+
10+
# This is empty, its only purpose is to have a test that catches
11+
# errors generated by the gopy itself.
12+
13+
print("OK")

bind/symbols.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ func (sym *symtab) addSymbol(obj types.Object) error {
559559
}
560560
return sym.processTuple(sig.Results())
561561
}
562+
fmt.Printf("ignoring python incompatible function: %v.%v: %v: %v\n", pkg.Name(), obj.String(), sig.String(), err)
563+
562564
case *types.TypeName:
563565
return sym.addType(obj, obj.Type())
564566

@@ -1116,7 +1118,7 @@ func (sym *symtab) addMethod(pkg *types.Package, obj types.Object, t types.Type,
11161118
sig := t.Underlying().(*types.Signature)
11171119
_, _, _, err := isPyCompatFunc(sig)
11181120
if err != nil {
1119-
fmt.Printf("ignoring python incompatible method: %v.%v: %v: %v\n", pkg.Name(), obj.Name(), t.String(), err)
1121+
fmt.Printf("ignoring python incompatible method: %v.%v: %v: %v\n", pkg.Name(), obj.String(), t.String(), err)
11201122
}
11211123
if err == nil {
11221124
fn := types.ObjectString(obj, nil)

main_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ func TestGofmt(t *testing.T) {
9191
}
9292
}
9393

94+
func TestGoPyErrors(t *testing.T) {
95+
pyvm := testBackends["py3"]
96+
workdir, err := ioutil.TempDir("", "gopy-")
97+
if err != nil {
98+
t.Fatalf("could not create workdir: %v\n", err)
99+
}
100+
t.Logf("pyvm: %s making work dir: %s\n", pyvm, workdir)
101+
defer os.RemoveAll(workdir)
102+
103+
curPkgPath := reflect.TypeOf(pkg{}).PkgPath()
104+
fpath := filepath.Join(curPkgPath, "_examples/gopyerrors")
105+
cmd := exec.Command("go", "run", ".", "gen", "-vm="+pyvm, "-output="+workdir, fpath)
106+
t.Logf("running: %v\n", cmd.Args)
107+
out, err := cmd.CombinedOutput()
108+
if err != nil {
109+
t.Fatalf("could not run %v: %+v\n", strings.Join(cmd.Args, " "), err)
110+
}
111+
contains := `--- Processing package: github.com/go-python/gopy/_examples/gopyerrors ---
112+
ignoring python incompatible function: gopyerrors.func github.com/go-python/gopy/_examples/gopyerrors.NotErrorMany() (int, int): func() (int, int): gopy: second result value must be of type error: func() (int, int)
113+
ignoring python incompatible method: gopyerrors.func (*github.com/go-python/gopy/_examples/gopyerrors.Struct).NotErrorMany() (int, string): func() (int, string): gopy: second result value must be of type error: func() (int, string)
114+
ignoring python incompatible method: gopyerrors.func (*github.com/go-python/gopy/_examples/gopyerrors.Struct).TooMany() (int, int, string): func() (int, int, string): gopy: too many results to return: func() (int, int, string)
115+
ignoring python incompatible function: gopyerrors.func github.com/go-python/gopy/_examples/gopyerrors.TooMany() (int, int, string): func() (int, int, string): gopy: too many results to return: func() (int, int, string)
116+
`
117+
if got, want := string(out), contains; !strings.Contains(got, want) {
118+
t.Fatalf("%v does not contain %v\n", got, want)
119+
}
120+
}
121+
94122
func TestHi(t *testing.T) {
95123
// t.Parallel()
96124
path := "_examples/hi"

0 commit comments

Comments
 (0)