mirror of https://github.com/go-python/gopy.git
gencpy: generate and insert function signatures at start of docstring
This commit is contained in:
parent
c78e0e1a64
commit
06ec70c37d
|
@ -86,7 +86,7 @@ func (g *cpyGen) gen() error {
|
||||||
|
|
||||||
case *types.Func:
|
case *types.Func:
|
||||||
funcs = append(funcs, cpyFunc{typ: obj})
|
funcs = append(funcs, cpyFunc{typ: obj})
|
||||||
docs[obj.Name()] = g.pkg.getDoc(obj)
|
docs[obj.Name()] = g.getPyDoc(obj)
|
||||||
|
|
||||||
case *types.TypeName:
|
case *types.TypeName:
|
||||||
named := obj.Type().(*types.Named)
|
named := obj.Type().(*types.Named)
|
||||||
|
@ -322,7 +322,7 @@ func (g *cpyGen) genStruct(cpy cpyStruct) {
|
||||||
g.impl.Printf("0,\t/*tp_setattro*/\n")
|
g.impl.Printf("0,\t/*tp_setattro*/\n")
|
||||||
g.impl.Printf("0,\t/*tp_as_buffer*/\n")
|
g.impl.Printf("0,\t/*tp_as_buffer*/\n")
|
||||||
g.impl.Printf("Py_TPFLAGS_DEFAULT,\t/*tp_flags*/\n")
|
g.impl.Printf("Py_TPFLAGS_DEFAULT,\t/*tp_flags*/\n")
|
||||||
g.impl.Printf("%q,\t/* tp_doc */\n", g.pkg.getDoc(obj))
|
g.impl.Printf("%q,\t/* tp_doc */\n", g.getPyDoc(obj))
|
||||||
g.impl.Printf("0,\t/* tp_traverse */\n")
|
g.impl.Printf("0,\t/* tp_traverse */\n")
|
||||||
g.impl.Printf("0,\t/* tp_clear */\n")
|
g.impl.Printf("0,\t/* tp_clear */\n")
|
||||||
g.impl.Printf("0,\t/* tp_richcompare */\n")
|
g.impl.Printf("0,\t/* tp_richcompare */\n")
|
||||||
|
@ -549,3 +549,46 @@ func (g *cpyGen) genPreamble() {
|
||||||
n := g.pkg.pkg.Name()
|
n := g.pkg.pkg.Name()
|
||||||
g.decl.Printf(cPreamble, n, g.pkg.pkg.Path(), filepath.Base(n))
|
g.decl.Printf(cPreamble, n, g.pkg.pkg.Path(), filepath.Base(n))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *cpyGen) getPyDoc(o types.Object) string {
|
||||||
|
doc := g.pkg.getDoc(o)
|
||||||
|
|
||||||
|
switch o.(type) {
|
||||||
|
|
||||||
|
case *types.Func:
|
||||||
|
sig := o.Type().(*types.Signature)
|
||||||
|
|
||||||
|
parseFn := func(tup *types.Tuple) []string {
|
||||||
|
params := []string{}
|
||||||
|
if tup == nil {
|
||||||
|
return params
|
||||||
|
}
|
||||||
|
for i := 0; i < tup.Len(); i++ {
|
||||||
|
paramVar := tup.At(i)
|
||||||
|
paramType := pyTypeName(paramVar.Type())
|
||||||
|
if paramVar.Name() != "" {
|
||||||
|
paramType = fmt.Sprintf("%s %s", paramType, paramVar.Name())
|
||||||
|
}
|
||||||
|
params = append(params, paramType)
|
||||||
|
}
|
||||||
|
return params
|
||||||
|
}
|
||||||
|
|
||||||
|
params := parseFn(sig.Params())
|
||||||
|
results := parseFn(sig.Results())
|
||||||
|
|
||||||
|
paramString := strings.Join(params, ", ")
|
||||||
|
resultString := strings.Join(results, ", ")
|
||||||
|
|
||||||
|
docSig := fmt.Sprintf("%s(%s) %s", o.Name(), paramString, resultString)
|
||||||
|
|
||||||
|
if doc != "" {
|
||||||
|
doc = fmt.Sprintf("%s\n\n%s", docSig, doc)
|
||||||
|
} else {
|
||||||
|
doc = docSig
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return doc
|
||||||
|
}
|
||||||
|
|
|
@ -45,10 +45,23 @@ func cgoTypeName(typ types.Type) string {
|
||||||
return typ.String()
|
return typ.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pyTypeName(typ types.Type) string {
|
||||||
|
switch typ := typ.(type) {
|
||||||
|
case *types.Basic:
|
||||||
|
kind := typ.Kind()
|
||||||
|
o, ok := typedescr[kind]
|
||||||
|
if ok {
|
||||||
|
return o.pysig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "object"
|
||||||
|
}
|
||||||
|
|
||||||
type typedesc struct {
|
type typedesc struct {
|
||||||
ctype string
|
ctype string
|
||||||
cgotype string
|
cgotype string
|
||||||
pyfmt string
|
pyfmt string
|
||||||
|
pysig string
|
||||||
}
|
}
|
||||||
|
|
||||||
var typedescr = map[types.BasicKind]typedesc{
|
var typedescr = map[types.BasicKind]typedesc{
|
||||||
|
@ -56,101 +69,118 @@ var typedescr = map[types.BasicKind]typedesc{
|
||||||
ctype: "_Bool",
|
ctype: "_Bool",
|
||||||
cgotype: "GoBool",
|
cgotype: "GoBool",
|
||||||
pyfmt: "b",
|
pyfmt: "b",
|
||||||
|
pysig: "bool",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Int: typedesc{
|
types.Int: typedesc{
|
||||||
ctype: "int",
|
ctype: "int",
|
||||||
cgotype: "GoInt",
|
cgotype: "GoInt",
|
||||||
pyfmt: "i",
|
pyfmt: "i",
|
||||||
|
pysig: "int",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Int8: typedesc{
|
types.Int8: typedesc{
|
||||||
ctype: "int8_t",
|
ctype: "int8_t",
|
||||||
cgotype: "GoInt8",
|
cgotype: "GoInt8",
|
||||||
pyfmt: "c",
|
pyfmt: "c",
|
||||||
|
pysig: "int",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Int16: typedesc{
|
types.Int16: typedesc{
|
||||||
ctype: "int16_t",
|
ctype: "int16_t",
|
||||||
cgotype: "GoInt16",
|
cgotype: "GoInt16",
|
||||||
pyfmt: "h",
|
pyfmt: "h",
|
||||||
|
pysig: "int",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Int32: typedesc{
|
types.Int32: typedesc{
|
||||||
ctype: "int32_t",
|
ctype: "int32_t",
|
||||||
cgotype: "GoInt32",
|
cgotype: "GoInt32",
|
||||||
pyfmt: "i",
|
pyfmt: "i",
|
||||||
|
pysig: "long",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Int64: typedesc{
|
types.Int64: typedesc{
|
||||||
ctype: "int64_t",
|
ctype: "int64_t",
|
||||||
cgotype: "GoInt64",
|
cgotype: "GoInt64",
|
||||||
pyfmt: "k",
|
pyfmt: "k",
|
||||||
|
pysig: "long",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Uint: typedesc{
|
types.Uint: typedesc{
|
||||||
ctype: "unsigned int",
|
ctype: "unsigned int",
|
||||||
cgotype: "GoUint",
|
cgotype: "GoUint",
|
||||||
pyfmt: "I",
|
pyfmt: "I",
|
||||||
|
pysig: "int",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Uint8: typedesc{
|
types.Uint8: typedesc{
|
||||||
ctype: "uint8_t",
|
ctype: "uint8_t",
|
||||||
cgotype: "GoUint8",
|
cgotype: "GoUint8",
|
||||||
pyfmt: "b",
|
pyfmt: "b",
|
||||||
|
pysig: "int",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Uint16: typedesc{
|
types.Uint16: typedesc{
|
||||||
ctype: "uint16_t",
|
ctype: "uint16_t",
|
||||||
cgotype: "GoUint16",
|
cgotype: "GoUint16",
|
||||||
pyfmt: "H",
|
pyfmt: "H",
|
||||||
|
pysig: "int",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Uint32: typedesc{
|
types.Uint32: typedesc{
|
||||||
ctype: "uint32_t",
|
ctype: "uint32_t",
|
||||||
cgotype: "GoUint32",
|
cgotype: "GoUint32",
|
||||||
pyfmt: "I",
|
pyfmt: "I",
|
||||||
|
pysig: "long",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Uint64: typedesc{
|
types.Uint64: typedesc{
|
||||||
ctype: "uint64_t",
|
ctype: "uint64_t",
|
||||||
cgotype: "GoUint64",
|
cgotype: "GoUint64",
|
||||||
pyfmt: "K",
|
pyfmt: "K",
|
||||||
|
pysig: "long",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Float32: typedesc{
|
types.Float32: typedesc{
|
||||||
ctype: "float",
|
ctype: "float",
|
||||||
cgotype: "float",
|
cgotype: "float",
|
||||||
pyfmt: "f",
|
pyfmt: "f",
|
||||||
|
pysig: "float",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Float64: typedesc{
|
types.Float64: typedesc{
|
||||||
ctype: "double",
|
ctype: "double",
|
||||||
cgotype: "double",
|
cgotype: "double",
|
||||||
pyfmt: "d",
|
pyfmt: "d",
|
||||||
|
pysig: "float",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Complex64: typedesc{
|
types.Complex64: typedesc{
|
||||||
ctype: "float complex",
|
ctype: "float complex",
|
||||||
cgotype: "GoComplex64",
|
cgotype: "GoComplex64",
|
||||||
pyfmt: "D",
|
pyfmt: "D",
|
||||||
|
pysig: "float",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.Complex128: typedesc{
|
types.Complex128: typedesc{
|
||||||
ctype: "double complex",
|
ctype: "double complex",
|
||||||
cgotype: "GoComplex128",
|
cgotype: "GoComplex128",
|
||||||
pyfmt: "D",
|
pyfmt: "D",
|
||||||
|
pysig: "float",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.String: typedesc{
|
types.String: typedesc{
|
||||||
ctype: "const char*",
|
ctype: "const char*",
|
||||||
cgotype: "GoString",
|
cgotype: "GoString",
|
||||||
pyfmt: "s",
|
pyfmt: "s",
|
||||||
|
pysig: "str",
|
||||||
},
|
},
|
||||||
|
|
||||||
types.UnsafePointer: typedesc{
|
types.UnsafePointer: typedesc{
|
||||||
ctype: "void*",
|
ctype: "void*",
|
||||||
cgotype: "void*",
|
cgotype: "void*",
|
||||||
pyfmt: "?",
|
pyfmt: "?",
|
||||||
|
pysig: "object",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue