-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.go
50 lines (40 loc) · 963 Bytes
/
module.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
package gp
/*
#include <Python.h>
*/
import "C"
import "unsafe"
type Module struct {
Object
}
func newModule(obj *cPyObject) Module {
return Module{newObject(obj)}
}
func ImportModule(name string) Module {
cname := AllocCStr(name)
mod := C.PyImport_ImportModule(cname)
C.free(unsafe.Pointer(cname))
return newModule(mod)
}
func GetModule(name string) Module {
return newModule(C.PyImport_GetModule(MakeStr(name).obj))
}
func (m Module) Dict() Dict {
return newDict(C.PyModule_GetDict(m.obj))
}
func (m Module) AddObject(name string, obj Object) int {
cname := AllocCStr(name)
r := int(C.PyModule_AddObjectRef(m.obj, cname, obj.obj))
C.free(unsafe.Pointer(cname))
return r
}
func (m Module) Name() string {
return C.GoString(C.PyModule_GetName(m.obj))
}
func CreateModule(name string) Module {
mod := C.PyModule_New(AllocCStrDontFree(name))
return newModule(mod)
}
func GetModuleDict() Dict {
return newDict(C.PyImport_GetModuleDict())
}