Skip to content

Commit 641997e

Browse files
committed
py/bind: bridge in go/doc.Package, add docstrings - fixes #3
1 parent 7a924b9 commit 641997e

File tree

3 files changed

+84
-18
lines changed

3 files changed

+84
-18
lines changed

bind/bind.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package bind
77
import (
88
"bytes"
99
"fmt"
10+
"go/doc"
1011
"go/token"
1112
"io"
1213
"os"
@@ -28,8 +29,71 @@ func (list ErrorList) Error() string {
2829
return buf.String()
2930
}
3031

32+
// Package ties types.Package and ast.Package together
33+
type Package struct {
34+
pkg *types.Package
35+
doc *doc.Package
36+
}
37+
38+
// NewPackage creates a new Package, tying types.Package and ast.Package together.
39+
func NewPackage(pkg *types.Package, doc *doc.Package) *Package {
40+
return &Package{
41+
pkg: pkg,
42+
doc: doc,
43+
}
44+
}
45+
46+
// Name returns the package name.
47+
func (p *Package) Name() string {
48+
return p.pkg.Name()
49+
}
50+
51+
// getDoc returns the doc string associated with types.Object
52+
func (p *Package) getDoc(o types.Object) string {
53+
n := o.Name()
54+
switch o.(type) {
55+
case *types.Const:
56+
for _, c := range p.doc.Consts {
57+
for _, cn := range c.Names {
58+
if n == cn {
59+
return c.Doc
60+
}
61+
}
62+
}
63+
64+
case *types.Var:
65+
for _, v := range p.doc.Vars {
66+
for _, vn := range v.Names {
67+
if n == vn {
68+
return v.Doc
69+
}
70+
}
71+
}
72+
73+
case *types.Func:
74+
for _, f := range p.doc.Funcs {
75+
if n == f.Name {
76+
return f.Doc
77+
}
78+
}
79+
80+
case *types.TypeName:
81+
for _, t := range p.doc.Types {
82+
if n == t.Name {
83+
return t.Doc
84+
}
85+
}
86+
87+
default:
88+
// TODO(sbinet)
89+
panic(fmt.Errorf("not yet supported: %v (%T)", o, o))
90+
}
91+
92+
return ""
93+
}
94+
3195
// GenCPython generates a (C)Python package from a Go package
32-
func GenCPython(w io.Writer, fset *token.FileSet, pkg *types.Package) error {
96+
func GenCPython(w io.Writer, fset *token.FileSet, pkg *Package) error {
3397
buf := new(bytes.Buffer)
3498
gen := &cpyGen{
3599
printer: &printer{buf: buf, indentEach: []byte("\t")},
@@ -47,7 +111,7 @@ func GenCPython(w io.Writer, fset *token.FileSet, pkg *types.Package) error {
47111
}
48112

49113
// GenGo generates a cgo package from a Go package
50-
func GenGo(w io.Writer, fset *token.FileSet, pkg *types.Package) error {
114+
func GenGo(w io.Writer, fset *token.FileSet, pkg *Package) error {
51115
buf := new(bytes.Buffer)
52116
gen := &goGen{
53117
printer: &printer{buf: buf, indentEach: []byte("\t")},

bind/gencpy.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ type cpyGen struct {
9191
*printer
9292

9393
fset *token.FileSet
94-
pkg *types.Package
94+
pkg *Package
9595
err ErrorList
9696
}
9797

@@ -100,8 +100,9 @@ func (g *cpyGen) gen() error {
100100
g.genPreamble()
101101

102102
var funcs []string
103+
docs := make(map[string]string)
103104

104-
scope := g.pkg.Scope()
105+
scope := g.pkg.pkg.Scope()
105106
names := scope.Names()
106107
for _, name := range names {
107108
obj := scope.Lookup(name)
@@ -122,6 +123,7 @@ func (g *cpyGen) gen() error {
122123
case *types.Func:
123124
funcs = append(funcs, obj.Name())
124125
g.genFunc(obj)
126+
docs[obj.Name()] = g.pkg.getDoc(obj)
125127

126128
case *types.TypeName:
127129
named := obj.Type().(*types.Named)
@@ -144,23 +146,23 @@ func (g *cpyGen) gen() error {
144146
}
145147
}
146148

147-
g.Printf("static PyMethodDef GoPy_%s_Methods[] = {\n", g.pkg.Name())
149+
g.Printf("static PyMethodDef GoPy_%s_Methods[] = {\n", g.pkg.pkg.Name())
148150
g.Indent()
149151
for _, name := range funcs {
150152
//obj := scope.Lookup(name)
151153
g.Printf("{%[1]q, %[2]s, METH_VARARGS, %[3]q},\n",
152-
name, "gopy_"+name, "doc for: "+g.pkg.Name()+"."+name,
154+
name, "gopy_"+name, docs[name],
153155
)
154156
}
155157
g.Printf("{NULL, NULL, 0, NULL} /* Sentinel */\n")
156158
g.Outdent()
157159
g.Printf("};\n\n")
158160

159-
g.Printf("PyMODINIT_FUNC\ninit%[1]s(void)\n{\n", g.pkg.Name())
161+
g.Printf("PyMODINIT_FUNC\ninit%[1]s(void)\n{\n", g.pkg.pkg.Name())
160162
g.Indent()
161163
g.Printf("(void) Py_InitModule3(%[1]q, GoPy_%[1]s_Methods, %[2]q);\n",
162-
g.pkg.Name(),
163-
"FIXME(sbinet): documentation for package "+g.pkg.Name(),
164+
g.pkg.pkg.Name(),
165+
g.pkg.doc.Doc,
164166
)
165167
g.Outdent()
166168
g.Printf("}\n\n")
@@ -179,7 +181,7 @@ func (g *cpyGen) genFunc(o *types.Func) {
179181
static PyObject*
180182
gopy_%[1]s(PyObject *self, PyObject *args) {
181183
`,
182-
o.Name(), g.pkg.Name(),
184+
o.Name(), g.pkg.pkg.Name(),
183185
)
184186

185187
g.Indent()
@@ -283,6 +285,6 @@ func (g *cpyGen) genFuncBody(o *types.Func) {
283285
}
284286

285287
func (g *cpyGen) genPreamble() {
286-
n := g.pkg.Name()
287-
g.Printf(cPreamble, n, g.pkg.Path())
288+
n := g.pkg.pkg.Name()
289+
g.Printf(cPreamble, n, g.pkg.pkg.Path())
288290
}

bind/gengo.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type goGen struct {
3333
*printer
3434

3535
fset *token.FileSet
36-
pkg *types.Package
36+
pkg *Package
3737
err ErrorList
3838
}
3939

@@ -43,7 +43,7 @@ func (g *goGen) gen() error {
4343

4444
var funcs []string
4545

46-
scope := g.pkg.Scope()
46+
scope := g.pkg.pkg.Scope()
4747
names := scope.Names()
4848
for _, name := range names {
4949
obj := scope.Lookup(name)
@@ -88,7 +88,7 @@ func (g *goGen) gen() error {
8888

8989
g.Printf("// Register registers the generated CPython module with the CPython runtime\nfunc Register() {\n")
9090
g.Indent()
91-
g.Printf("C.init%[1]s()\n", g.pkg.Name())
91+
g.Printf("C.init%[1]s()\n", g.pkg.pkg.Name())
9292
g.Outdent()
9393
g.Printf("}\n")
9494

@@ -127,7 +127,7 @@ func (g *goGen) genFuncBody(o *types.Func) {
127127

128128
g.Printf(
129129
"%[3]s%[1]s.%[2]s(",
130-
g.pkg.Name(),
130+
g.pkg.pkg.Name(),
131131
o.Name(),
132132
ret,
133133
)
@@ -146,6 +146,6 @@ func (g *goGen) genFuncBody(o *types.Func) {
146146
}
147147

148148
func (g *goGen) genPreamble() {
149-
n := g.pkg.Name()
150-
g.Printf(goPreamble, n, g.pkg.Path())
149+
n := g.pkg.pkg.Name()
150+
g.Printf(goPreamble, n, g.pkg.pkg.Path())
151151
}

0 commit comments

Comments
 (0)