-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopytest.go
85 lines (76 loc) · 1.9 KB
/
gopytest.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"py";
"fmt";
)
func runSimpleString() {
fmt.Printf("--- runSimpleString ------------------------------\n");
result := py.Run_SimpleString("print 'hello'");
fmt.Printf("result %d\n\n", result);
fmt.Printf("--- runSimpleString done ---\n\n");
}
func setArgs(pArgs *py.Object, index int, value int) bool {
pValue := py.Int_FromInt(value);
if pValue == nil {
fmt.Printf("Value : %d\n", value);
py.Err_Print();
return false;
}
pArgs.Tuple_SetItem(index, pValue);
return true;
}
func runFunc(pythonfile string, funcname string, arg []int) {
fmt.Printf("--- runFunc ------------------------------\n");
pName := py.String_FromString(pythonfile);
pModule := py.Import_Import(pName);
fmt.Printf("import done \n");
pName.DecRef();
if pModule != nil {
fmt.Printf("pModule %t\n", pModule);
pFunc := pModule.GetAttrString(funcname);
if pFunc != nil && pFunc.Callable_Check() != 0 {
pArgs := py.Tuple_New(2);
if setArgs(pArgs, 0, arg[0]) == false {
pArgs.DecRef();
pModule.DecRef();
fmt.Printf("Cannot Convert Argument\n");
return;
}
if setArgs(pArgs, 1, arg[1]) == false {
pArgs.DecRef();
pModule.DecRef();
fmt.Printf("Cannot Convert Argument\n");
return;
}
pValue := pFunc.CallObject(pArgs);
pArgs.DecRef();
if pValue != nil {
fmt.Printf("Result of call: %d\n", pValue.Int_AsInt64());
pValue.DecRef();
} else {
pFunc.DecRef();
pModule.DecRef();
py.Err_Print();
fmt.Printf("Call Failed\n");
}
} else {
if py.Err_Occurred() != nil {
py.Err_Print();
}
fmt.Printf("Cannot find function %s\n", funcname);
}
py.XDecRef(pFunc);
pModule.DecRef();
} else {
py.Err_Print();
fmt.Print("error\n\n");
}
fmt.Printf("--- runFunc done ---\n\n");
}
func main() {
py.Initialize();
runSimpleString();
runFunc("multiply", "multiply", []int{4, 5});
py.Finalize();
fmt.Print("finalize done\n");
}