Skip to content

Commit 9cd1691

Browse files
committed
bind/{cpy,cffi}: attached doc of Go vars if available
This CL adds the doc-string of Go variables (if available) to the python getter and setter functions, for both CPython2 and CFFI. Update TestBindVars accordingly.
1 parent 20ca91f commit 9cd1691

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

_examples/vars/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@
4747

4848
print("k1 = %s" % vars.GetKind1())
4949
print("k2 = %s" % vars.GetKind2())
50+
51+
print("vars.GetDoc() = %s" % repr(vars.GetDoc()))
52+
print("doc of vars.GetDoc = %s" % (repr(vars.GetDoc.__doc__),))
53+
print("doc of vars.SetDoc = %s" % (repr(vars.SetDoc.__doc__),))

_examples/vars/vars.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ var (
2424
Kind2 = 2
2525
)
2626

27+
// Doc is a top-level string with some documentation attached.
28+
var Doc = "A variable with some documentation"
29+
2730
// FIXME: also use an unexported type
2831
// type kind int
2932
// var (

bind/gencffi.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ func (g *cffiGen) genConst(c Const) {
290290

291291
func (g *cffiGen) genVar(v Var) {
292292
id := g.pkg.Name() + "_" + v.Name()
293+
get := "returns " + g.pkg.Name() + "." + v.Name()
294+
set := "sets " + g.pkg.Name() + "." + v.Name()
295+
if v.doc != "" {
296+
// if the Go variable had some documentation attached,
297+
// put it there as well.
298+
get += "\n\n" + v.doc
299+
set += "\n\n" + v.doc
300+
}
293301
doc := v.doc
294302
{
295303
res := []*Var{newVar(g.pkg, v.GoType(), "ret", v.Name(), doc)}
@@ -300,7 +308,7 @@ func (g *cffiGen) genVar(v Var) {
300308
typ: nil,
301309
name: v.Name(),
302310
id: id + "_get",
303-
doc: "returns " + g.pkg.Name() + "." + v.Name(),
311+
doc: get,
304312
ret: v.GoType(),
305313
err: false,
306314
}
@@ -315,7 +323,7 @@ func (g *cffiGen) genVar(v Var) {
315323
typ: nil,
316324
name: v.Name(),
317325
id: id + "_set",
318-
doc: "sets " + g.pkg.Name() + "." + v.Name(),
326+
doc: set,
319327
ret: nil,
320328
err: false,
321329
}

bind/gencpy.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,13 @@ func (g *cpyGen) gen() error {
243243

244244
for _, v := range g.pkg.vars {
245245
name := v.Name()
246-
get := v.doc
247-
set := v.doc
248-
if v.doc == "" {
249-
get = "returns " + g.pkg.Name() + "." + name
250-
set = "sets " + g.pkg.Name() + "." + name
246+
get := "returns " + g.pkg.Name() + "." + name
247+
set := "sets " + g.pkg.Name() + "." + name
248+
if v.doc != "" {
249+
// if the Go variable had some documentation attached,
250+
// put it there as well.
251+
get += "\n\n" + v.doc
252+
set += "\n\n" + v.doc
251253
}
252254
g.impl.Printf("{%[1]q, %[2]s, METH_VARARGS, %[3]q},\n",
253255
"Get"+name, "cpy_func_"+v.id+"_get", get,
@@ -322,7 +324,7 @@ func (g *cpyGen) genVar(v Var) {
322324
typ: nil,
323325
name: v.Name(),
324326
id: id + "_get",
325-
doc: "returns " + g.pkg.Name() + "." + v.Name(),
327+
doc: "", // ignored: done during cpyGen.gen()
326328
ret: v.GoType(),
327329
err: false,
328330
}
@@ -337,7 +339,7 @@ func (g *cpyGen) genVar(v Var) {
337339
typ: nil,
338340
name: v.Name(),
339341
id: id + "_set",
340-
doc: "sets " + g.pkg.Name() + "." + v.Name(),
342+
doc: "", // ignored: done during cpyGen.gen()
341343
ret: nil,
342344
err: false,
343345
}

main_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,9 @@ v6 = 50
805805
v7 = 1111.1111
806806
k1 = 123
807807
k2 = 456
808+
vars.GetDoc() = 'A variable with some documentation'
809+
doc of vars.GetDoc = 'returns vars.Doc\n\nDoc is a top-level string with some documentation attached.\n'
810+
doc of vars.SetDoc = 'sets vars.Doc\n\nDoc is a top-level string with some documentation attached.\n'
808811
`),
809812
})
810813

@@ -836,6 +839,9 @@ v6 = 50
836839
v7 = 1111.1111
837840
k1 = 123
838841
k2 = 456
842+
vars.GetDoc() = 'A variable with some documentation'
843+
doc of vars.GetDoc = 'returns vars.Doc\n\nDoc is a top-level string with some documentation attached.\n'
844+
doc of vars.SetDoc = 'sets vars.Doc\n\nDoc is a top-level string with some documentation attached.\n'
839845
`),
840846
})
841847
}

0 commit comments

Comments
 (0)