Skip to content

Commit 87be7f7

Browse files
Randall C. O'Reillysbinet
authored andcommitted
bind: add no-warn flag; use underlying type for Named types for struct fields; use overall package name for DecRef etc.
Fixes #219 Fixes #220 Fixes #221
1 parent 299c7e9 commit 87be7f7

File tree

10 files changed

+58
-27
lines changed

10 files changed

+58
-27
lines changed

bind/gen.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ func main() {
452452
// for some reason, it could be done.
453453
var thePyGen *pyGen
454454

455+
// NoWarn turns off warnings -- this must be a global as it is relevant during initial package parsing
456+
// before e.g., thePyGen is present.
457+
var NoWarn = false
458+
455459
// GenPyBind generates a .go file, build.py file to enable pybindgen to create python bindings,
456460
// and wrapper .py file(s) that are loaded as the interface to the package with shadow
457461
// python-side classes
@@ -460,6 +464,7 @@ func GenPyBind(mode BuildMode, odir, outname, cmdstr, vm, mainstr, libext, extra
460464
gen := &pyGen{
461465
mode: mode,
462466
odir: odir,
467+
pypkgname: outname,
463468
outname: outname,
464469
cmdstr: cmdstr,
465470
vm: vm,
@@ -489,12 +494,14 @@ type pyGen struct {
489494
err ErrorList
490495
pkgmap map[string]struct{} // map of package paths
491496

492-
mode BuildMode // mode: gen, build, pkg, exe
493-
odir string // output directory
494-
outname string // overall output (package) name
495-
cmdstr string // overall command (embedded in generated files)
496-
vm string // python interpreter
497-
mainstr string // main function code string
497+
mode BuildMode // mode: gen, build, pkg, exe
498+
odir string // output directory
499+
pypkgname string // python package name, for pkg and exe build modes (--name arg there), else = outname
500+
// all global functions in goPreamble are accessed by: pypkgname.FuncName, e.g., IncRef, DecRef
501+
outname string // output (package) name -- for specific current go package
502+
cmdstr string // overall command (embedded in generated files)
503+
vm string // python interpreter
504+
mainstr string // main function code string
498505
libext string
499506
extraGccArgs string
500507
lang int // c-python api version (2,3)

bind/gen_map.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ otherwise parameter is a python list that we copy from
9999
g.pywrap.Printf("if len(kwargs) == 1 and 'handle' in kwargs:\n")
100100
g.pywrap.Indent()
101101
g.pywrap.Printf("self.handle = kwargs['handle']\n")
102-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
102+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
103103
g.pywrap.Outdent()
104104
g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], %sGoClass):\n", gocl)
105105
g.pywrap.Indent()
106106
g.pywrap.Printf("self.handle = args[0].handle\n")
107-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
107+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
108108
g.pywrap.Outdent()
109109
g.pywrap.Printf("else:\n")
110110
g.pywrap.Indent()
111111
g.pywrap.Printf("self.handle = _%s_CTor()\n", qNm)
112-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
112+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
113113
g.pywrap.Printf("if len(args) > 0:\n")
114114
g.pywrap.Indent()
115115
g.pywrap.Printf("if not isinstance(args[0], collections.Mapping):\n")
@@ -126,7 +126,7 @@ otherwise parameter is a python list that we copy from
126126

127127
g.pywrap.Printf("def __del__(self):\n")
128128
g.pywrap.Indent()
129-
g.pywrap.Printf("_%s.DecRef(self.handle)\n", g.outname)
129+
g.pywrap.Printf("_%s.DecRef(self.handle)\n", g.pypkgname)
130130
g.pywrap.Outdent()
131131

132132
if mpob != nil && mpob.prots&ProtoStringer != 0 {

bind/gen_slice.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ otherwise parameter is a python list that we copy from
8787
g.pywrap.Printf("if len(kwargs) == 1 and 'handle' in kwargs:\n")
8888
g.pywrap.Indent()
8989
g.pywrap.Printf("self.handle = kwargs['handle']\n")
90-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
90+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
9191
g.pywrap.Outdent()
9292
g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], %sGoClass):\n", gocl)
9393
g.pywrap.Indent()
9494
g.pywrap.Printf("self.handle = args[0].handle\n")
95-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
95+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
9696
g.pywrap.Outdent()
9797
if slc.isSlice() {
9898
g.pywrap.Printf("else:\n")
9999
g.pywrap.Indent()
100100
g.pywrap.Printf("self.handle = _%s_CTor()\n", qNm)
101-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
101+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
102102
g.pywrap.Printf("if len(args) > 0:\n")
103103
g.pywrap.Indent()
104104
g.pywrap.Printf("if not isinstance(args[0], collections.Iterable):\n")
@@ -116,7 +116,7 @@ otherwise parameter is a python list that we copy from
116116

117117
g.pywrap.Printf("def __del__(self):\n")
118118
g.pywrap.Indent()
119-
g.pywrap.Printf("_%s.DecRef(self.handle)\n", g.outname)
119+
g.pywrap.Printf("_%s.DecRef(self.handle)\n", g.pypkgname)
120120
g.pywrap.Outdent()
121121

122122
if slob != nil && slob.prots&ProtoStringer != 0 {

bind/gen_struct.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ in which case a new Go object is constructed first
5353
g.pywrap.Printf("if len(kwargs) == 1 and 'handle' in kwargs:\n")
5454
g.pywrap.Indent()
5555
g.pywrap.Printf("self.handle = kwargs['handle']\n")
56-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
56+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
5757
g.pywrap.Outdent()
5858
g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], go.GoClass):\n")
5959
g.pywrap.Indent()
6060
g.pywrap.Printf("self.handle = args[0].handle\n")
61-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
61+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
6262
g.pywrap.Outdent()
6363
g.pywrap.Printf("else:\n")
6464
g.pywrap.Indent()
6565
g.pywrap.Printf("self.handle = _%s.%s_CTor()\n", pkgname, s.ID())
66-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
66+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
6767

6868
for i := 0; i < numFields; i++ {
6969
f := s.Struct().Field(i)
@@ -91,7 +91,7 @@ in which case a new Go object is constructed first
9191

9292
g.pywrap.Printf("def __del__(self):\n")
9393
g.pywrap.Indent()
94-
g.pywrap.Printf("_%s.DecRef(self.handle)\n", s.Package().Name())
94+
g.pywrap.Printf("_%s.DecRef(self.handle)\n", g.pypkgname)
9595
g.pywrap.Outdent()
9696

9797
if s.prots&ProtoStringer != 0 {
@@ -237,7 +237,11 @@ func (g *pyGen) genStructMemberSetter(s *Struct, i int, f types.Object) {
237237
g.pywrap.Indent()
238238
// See comment in genStructInit about ensuring that gopy managed
239239
// objects are only assigned to from gopy managed objects.
240-
switch f.Type().(type) {
240+
utyp := f.Type()
241+
if _, isNamed := utyp.(*types.Named); isNamed {
242+
utyp = utyp.Underlying()
243+
}
244+
switch utyp.(type) {
241245
case *types.Basic:
242246
g.pywrap.Printf("_%s.%s(self.handle, value)\n", pkgname, cgoFn)
243247
default:
@@ -298,12 +302,12 @@ handle=A Go-side object is always initialized with an explicit handle=arg
298302
g.pywrap.Printf("if len(kwargs) == 1 and 'handle' in kwargs:\n")
299303
g.pywrap.Indent()
300304
g.pywrap.Printf("self.handle = kwargs['handle']\n")
301-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
305+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
302306
g.pywrap.Outdent()
303307
g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], go.GoClass):\n")
304308
g.pywrap.Indent()
305309
g.pywrap.Printf("self.handle = args[0].handle\n")
306-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
310+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
307311
g.pywrap.Outdent()
308312
g.pywrap.Printf("else:\n")
309313
g.pywrap.Indent()

bind/gen_type.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,17 @@ handle=A Go-side object is always initialized with an explicit handle=arg
145145
g.pywrap.Printf("if len(kwargs) == 1 and 'handle' in kwargs:\n")
146146
g.pywrap.Indent()
147147
g.pywrap.Printf("self.handle = kwargs['handle']\n")
148-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
148+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
149149
g.pywrap.Outdent()
150150
g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], GoClass):\n")
151151
g.pywrap.Indent()
152152
g.pywrap.Printf("self.handle = args[0].handle\n")
153-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
153+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
154154
g.pywrap.Outdent()
155155
g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], int):\n")
156156
g.pywrap.Indent()
157157
g.pywrap.Printf("self.handle = args[0]\n")
158-
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.outname)
158+
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
159159
g.pywrap.Outdent()
160160
g.pywrap.Printf("else:\n")
161161
g.pywrap.Indent()
@@ -165,7 +165,7 @@ handle=A Go-side object is always initialized with an explicit handle=arg
165165

166166
g.pywrap.Printf("def __del__(self):\n")
167167
g.pywrap.Indent()
168-
g.pywrap.Printf("_%s.DecRef(self.handle)\n", g.outname)
168+
g.pywrap.Printf("_%s.DecRef(self.handle)\n", g.pypkgname)
169169
g.pywrap.Outdent()
170170

171171
g.pywrap.Printf("\n")

bind/symbols.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,9 @@ 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)
562+
if !NoWarn {
563+
fmt.Printf("ignoring python incompatible function: %v.%v: %v: %v\n", pkg.Name(), obj.String(), sig.String(), err)
564+
}
563565

564566
case *types.TypeName:
565567
return sym.addType(obj, obj.Type())
@@ -1118,7 +1120,9 @@ func (sym *symtab) addMethod(pkg *types.Package, obj types.Object, t types.Type,
11181120
sig := t.Underlying().(*types.Signature)
11191121
_, _, _, err := isPyCompatFunc(sig)
11201122
if err != nil {
1121-
fmt.Printf("ignoring python incompatible method: %v.%v: %v: %v\n", pkg.Name(), obj.String(), t.String(), err)
1123+
if !NoWarn {
1124+
fmt.Printf("ignoring python incompatible method: %v.%v: %v: %v\n", pkg.Name(), obj.String(), t.String(), err)
1125+
}
11221126
}
11231127
if err == nil {
11241128
fn := types.ObjectString(obj, nil)

cmd_build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ ex:
3838
cmd.Flag.String("name", "", "name of output package (otherwise name of first package is used)")
3939
cmd.Flag.String("main", "", "code string to run in the go main() function in the cgo library")
4040
cmd.Flag.Bool("symbols", true, "include symbols in output")
41+
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
4142
return cmd
4243
}
4344

@@ -54,8 +55,11 @@ func gopyRunCmdBuild(cmdr *commander.Command, args []string) error {
5455
mainstr = cmdr.Flag.Lookup("main").Value.Get().(string)
5556
vm = cmdr.Flag.Lookup("vm").Value.Get().(string)
5657
symbols = cmdr.Flag.Lookup("symbols").Value.Get().(bool)
58+
nowarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
5759
)
5860

61+
bind.NoWarn = nowarn
62+
5963
cmdstr := argStr()
6064

6165
for _, path := range args {

cmd_exe.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ex:
5252
cmd.Flag.String("email", "[email protected]", "author email")
5353
cmd.Flag.String("desc", "", "short description of project (long comes from README.md)")
5454
cmd.Flag.String("url", "https://github.com/go-python/gopy", "home page for project")
55+
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
5556

5657
return cmd
5758
}
@@ -76,10 +77,13 @@ func gopyRunCmdExe(cmdr *commander.Command, args []string) error {
7677
email = cmdr.Flag.Lookup("email").Value.Get().(string)
7778
desc = cmdr.Flag.Lookup("desc").Value.Get().(string)
7879
url = cmdr.Flag.Lookup("url").Value.Get().(string)
80+
nowarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
7981
)
8082

8183
cmdstr := argStr()
8284

85+
bind.NoWarn = nowarn
86+
8387
if name == "" {
8488
path := args[0]
8589
_, name = filepath.Split(path)

cmd_gen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ex:
3232
cmd.Flag.String("output", "", "output directory for bindings")
3333
cmd.Flag.String("name", "", "name of output package (otherwise name of first package is used)")
3434
cmd.Flag.String("main", "", "code string to run in the go main() function in the cgo library")
35+
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
3536
return cmd
3637
}
3738

@@ -51,12 +52,15 @@ func gopyRunCmdGen(cmdr *commander.Command, args []string) error {
5152
vm = cmdr.Flag.Lookup("vm").Value.Get().(string)
5253
name = cmdr.Flag.Lookup("name").Value.Get().(string)
5354
mainstr = cmdr.Flag.Lookup("main").Value.Get().(string)
55+
nowarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
5456
)
5557

5658
if vm == "" {
5759
vm = "python"
5860
}
5961

62+
bind.NoWarn = nowarn
63+
6064
for _, path := range args {
6165
pkg, err := newPackage(path)
6266
if name == "" {

cmd_pkg.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ex:
5050
cmd.Flag.String("email", "[email protected]", "author email")
5151
cmd.Flag.String("desc", "", "short description of project (long comes from README.md)")
5252
cmd.Flag.String("url", "https://github.com/go-python/gopy", "home page for project")
53+
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
5354

5455
return cmd
5556
}
@@ -74,10 +75,13 @@ func gopyRunCmdPkg(cmdr *commander.Command, args []string) error {
7475
email = cmdr.Flag.Lookup("email").Value.Get().(string)
7576
desc = cmdr.Flag.Lookup("desc").Value.Get().(string)
7677
url = cmdr.Flag.Lookup("url").Value.Get().(string)
78+
nowarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
7779
)
7880

7981
cmdstr := argStr()
8082

83+
bind.NoWarn = nowarn
84+
8185
if name == "" {
8286
path := args[0]
8387
_, name = filepath.Split(path)

0 commit comments

Comments
 (0)