-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPy.go
60 lines (54 loc) · 1.17 KB
/
Py.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
package py3
import (
"fmt"
"github.com/aadog/py3-go/cpy3"
"sync"
)
var SystemModuleMap = sync.Map{}
var GoModule *PyModule
func InitGoModuleName(name string, doc string) {
PyImport_AppendInittab(name, func() *PyObject {
GoModule = CreateModule(name, doc)
cls := CreateClass("MyClass", "")
cls.AddFunction("z", func(self *PyObject) {
fmt.Println("z")
})
GoModule.AddClass(cls)
return GoModule.AsObj()
})
}
func Initialize() {
//init python3
cpy3.Py_Initialize()
//new userexception
_UserException = PyErr_NewException("gofunction.error", PyNil, PyNil)
}
func IsInitialized() int {
return cpy3.Py_IsInitialized()
}
func Finalize() {
SystemModuleMap.Range(func(key, value any) bool {
//SystemModuleMap.Delete(key)
m := value.(*PyModule)
m.DecRef()
return true
})
_UserException.DecRef()
cpy3.Py_Finalize()
}
func FinalizeEx() int {
SystemModuleMap.Range(func(key, value any) bool {
//SystemModuleMap.Delete(key)
m := value.(*PyModule)
m.DecRef()
return true
})
_UserException.DecRef()
return cpy3.Py_FinalizeEx()
}
func SetProgramName(name string) {
cpy3.Py_SetProgramName(name)
}
func SetPythonHome(home string) {
cpy3.Py_SetPythonHome(home)
}