-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomodule.go
38 lines (28 loc) · 848 Bytes
/
gomodule.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
package py
// #include <Python.h>
// static inline void decref(PyObject *obj) { Py_DECREF(obj); }
import "C"
import "unsafe"
// ------------------------------------------------------------------------------------------
type GoModule struct {
*Module
Ctx RegisterCtx
}
func NewGoModule(name string, doc string, self interface{}) (mod GoModule, err error) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
var mdoc *C.char
if doc != "" {
mdoc = C.CString(doc)
defer C.free(unsafe.Pointer(mdoc))
}
m := C.Py_InitModule4(cName, nil, mdoc, nil, C.PYTHON_API_VERSION)
if m == nil {
err = exception()
return
}
mod.Module = (*Module)(unsafe.Pointer(m))
mod.Ctx = Register(mod.Module.Dict(), name + ".", self)
return
}
// ------------------------------------------------------------------------------------------