forked from panchengtao/gopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlevel.go
68 lines (57 loc) · 1.67 KB
/
highlevel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package gopython
//#cgo pkg-config: python-3.6
//#include "go-python.h"
import "C"
import (
"fmt"
)
var PyStr = PyString_FromString
var GoStr = PyString_AsString
var GoInt = PyInt_AsLong
func InsertPackagePath(path string) error {
interr := PyRun_SimpleString("import sys")
if interr == 0 {
interr = PyRun_SimpleString(fmt.Sprintf("sys.path.append('%s')", path))
if interr == 0 {
return nil
}
}
return fmt.Errorf("python: could not invoke PyRun_SimpleString to append extra path %s", path)
}
// CallFunc 调用 Python 中的方法
// 指定模块名称,方法名称,排列参数
func CallFunc(modulename string, funcname string, args ...interface{}) (*PyObject, error) {
module, err := getModule(modulename)
if err == nil {
if len(args) > 0 {
funcArgs := PyTuple_New(len(args))
for i := 0; i < len(args); i++ {
switch args[i].(type) {
case int:
PyTuple_SetItem(funcArgs, i, PyInt_FromLong(args[i].(int)))
case string:
PyTuple_SetItem(funcArgs, i, PyStr(args[i].(string)))
}
}
var attr = module.GetAttrString(funcname)
if attr != nil {
res := attr.CallFunction(funcArgs)
return res, nil
}
} else {
var attr = module.GetAttrString(funcname)
if attr != nil {
res := module.GetAttrString(funcname).CallFunction()
return res, nil
}
}
return nil, fmt.Errorf("python: could not get func named %s after importing module named %s", funcname, modulename)
}
return nil, err
}
func getModule(modulename string) (*PyObject, error) {
if obj := PyImport_ImportModule(modulename); obj != nil {
return obj, nil
}
return nil, fmt.Errorf("python: could not import The specified module named %s", modulename)
}