Skip to content

Commit 691b7e0

Browse files
author
Randall C. O'Reilly
committed
major fix: create an import alias for package name conflicts -- e.g., adding goplot font package conflicts with image/font.. added pkgconflict test case
1 parent a9475fd commit 691b7e0

File tree

13 files changed

+194
-71
lines changed

13 files changed

+194
-71
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ release:
6666
@echo " VersionDate = \"$(VERS_DATE)\" // UTC" >> $(VERS_FILE)
6767
@echo ")" >> $(VERS_FILE)
6868
@echo "" >> $(VERS_FILE)
69+
goimports -w $(VERS_FILE)
6970
/bin/cat $(VERS_FILE)
7071
git commit -am "$(VERS) release"
7172
git tag -a $(VERS) -m "$(VERS) release"

SUPPORT_MATRIX.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ _examples/lot | yes | yes
1919
_examples/maps | yes | yes
2020
_examples/named | yes | yes
2121
_examples/osfile | yes | yes
22+
_examples/pkgconflict | yes | yes
2223
_examples/pointers | yes | yes
2324
_examples/pyerrors | yes | yes
2425
_examples/rename | yes | yes

_examples/pkgconflict/html.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2021 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 pkgconflict
6+
7+
import (
8+
"html/template"
9+
"os"
10+
)
11+
12+
func HtmlTempl(nm string) *template.Template {
13+
sweaters := Inventory{"hyperlinks", 42}
14+
t, err := template.New(nm).Parse("{{.Count}} items are made of {{.Material}}\n")
15+
if err != nil {
16+
panic(err)
17+
}
18+
err = t.Execute(os.Stdout, sweaters)
19+
if err != nil {
20+
panic(err)
21+
}
22+
return t
23+
}

_examples/pkgconflict/test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2015 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+
# py2/py3 compat
6+
from __future__ import print_function
7+
8+
import pkgconflict
9+
10+
txt = pkgconflict.TextTempl("text")
11+
# note: print is for confirming correct return type, but has addr so not good for testing
12+
# print("txt = %s" % txt)
13+
14+
htm = pkgconflict.HtmlTempl("html")
15+
# print("htm = %s" % htm)
16+
17+
htm2 = pkgconflict.HtmlTemplSame("html2")
18+
# print("htm2 = %s" % htm2)
19+

_examples/pkgconflict/text.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2021 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 pkgconflict
6+
7+
import (
8+
htemplate "html/template"
9+
"os"
10+
"text/template"
11+
)
12+
13+
type Inventory struct {
14+
Material string
15+
Count uint
16+
}
17+
18+
func TextTempl(nm string) *template.Template {
19+
sweaters := Inventory{"wool", 17}
20+
t, err := template.New(nm).Parse("{{.Count}} items are made of {{.Material}}\n")
21+
if err != nil {
22+
panic(err)
23+
}
24+
err = t.Execute(os.Stdout, sweaters)
25+
if err != nil {
26+
panic(err)
27+
}
28+
return t
29+
}
30+
31+
func HtmlTemplSame(nm string) *htemplate.Template {
32+
sweaters := Inventory{"fuzz", 2}
33+
t, err := htemplate.New(nm).Parse("{{.Count}} items are made of {{.Material}}\n")
34+
if err != nil {
35+
panic(err)
36+
}
37+
err = t.Execute(os.Stdout, sweaters)
38+
if err != nil {
39+
panic(err)
40+
}
41+
return t
42+
}

bind/gen.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,13 @@ func (g *pyGen) genPkg(p *Package) {
565565

566566
func (g *pyGen) genGoPreamble() {
567567
pkgimport := ""
568-
for pi, _ := range current.imports {
569-
pkgimport += fmt.Sprintf("\n\t%q", pi)
568+
for pp, pnm := range current.imports {
569+
_, psfx := filepath.Split(pp)
570+
if psfx != pnm {
571+
pkgimport += fmt.Sprintf("\n\t%s %q", pnm, pp)
572+
} else {
573+
pkgimport += fmt.Sprintf("\n\t%q", pp)
574+
}
570575
}
571576
libcfg := func() string {
572577
pycfg, err := GetPythonConfig(g.cfg.VM)

bind/gen_func.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (g *pyGen) genFuncBody(sym *symbol, fsym *Func) {
212212
isIface := false
213213
symNm := ""
214214
if isMethod {
215-
symNm = sym.gofmt()
215+
symNm = sym.goname
216216
isIface = sym.isInterface()
217217
if !isIface {
218218
symNm = "*" + symNm

bind/gen_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ otherwise parameter is a python list that we copy from
271271
g.gofile.Printf("//export %s\n", ctNm)
272272
g.gofile.Printf("func %s() CGoHandle {\n", ctNm)
273273
g.gofile.Indent()
274-
g.gofile.Printf("return CGoHandle(handleFromPtr_%[1]s(&%[2]s{}))\n", slNm, slc.gofmt())
274+
g.gofile.Printf("return CGoHandle(handleFromPtr_%[1]s(&%[2]s{}))\n", slNm, slc.goname)
275275
g.gofile.Outdent()
276276
g.gofile.Printf("}\n\n")
277277

bind/gen_slice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ otherwise parameter is a python list that we copy from
282282
g.gofile.Printf("//export %s\n", ctNm)
283283
g.gofile.Printf("func %s() CGoHandle {\n", ctNm)
284284
g.gofile.Indent()
285-
g.gofile.Printf("return CGoHandle(handleFromPtr_%[1]s(&%[2]s{}))\n", slNm, slc.gofmt())
285+
g.gofile.Printf("return CGoHandle(handleFromPtr_%[1]s(&%[2]s{}))\n", slNm, slc.goname)
286286
g.gofile.Outdent()
287287
g.gofile.Printf("}\n\n")
288288

bind/gen_type.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (g *pyGen) genTypeHandlePtr(sym *symbol) {
6363
if sym.goname == "interface{}" {
6464
return
6565
}
66-
gonm := sym.gofmt()
66+
gonm := sym.goname
6767
g.gofile.Printf("\n// Converters for pointer handles for type: %s\n", gonm)
6868
g.gofile.Printf("func %s(h CGoHandle) %s {\n", sym.py2go, gonm)
6969
g.gofile.Indent()
@@ -89,7 +89,7 @@ func (g *pyGen) genTypeHandlePtr(sym *symbol) {
8989

9090
// implicit pointer types: slice, map, array
9191
func (g *pyGen) genTypeHandleImplPtr(sym *symbol) {
92-
gonm := sym.gofmt()
92+
gonm := sym.goname
9393
ptrnm := gonm
9494
nptrnm := gonm
9595
if ptrnm[0] != '*' {
@@ -137,7 +137,7 @@ func nonPtrName(nm string) string {
137137
}
138138

139139
func (g *pyGen) genTypeHandle(sym *symbol) {
140-
gonm := sym.gofmt()
140+
gonm := sym.goname
141141
ptrnm := gonm
142142
if ptrnm[0] != '*' {
143143
ptrnm = "*" + ptrnm

0 commit comments

Comments
 (0)