forked from goplus/py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
135 lines (107 loc) · 2.88 KB
/
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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package py
// #include <Python.h>
// static inline int moduleCheck(PyObject *o) { return PyModule_Check(o); }
// static inline int moduleCheckE(PyObject *o) { return PyModule_CheckExact(o); }
// static inline void decref(PyObject *obj) { Py_DECREF(obj); }
import "C"
import "unsafe"
type Module struct {
Base
o C.PyObject
}
// ModuleType is the Type object that represents the Module type.
var ModuleType = (*Type)(unsafe.Pointer(&C.PyModule_Type))
func newModule(obj *C.PyObject) *Module {
return (*Module)(unsafe.Pointer(obj))
}
func Import(name string) (*Module, error) {
s := C.CString(name)
defer C.free(unsafe.Pointer(s))
pyName := C.PyString_FromString(s)
defer C.decref(pyName)
obj := C.PyImport_Import(pyName)
if obj == nil {
return nil, exception()
}
return newModule(obj), nil
}
func ExecCodeModule(name string, code *Base) (*Module, error) {
s := C.CString(name)
defer C.free(unsafe.Pointer(s))
ret := C.PyImport_ExecCodeModule(s, code.c())
if ret == nil {
return nil, exception()
}
return newModule(ret), nil
}
func NewModule(name string) (*Module, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ret := C.PyModule_New(cname)
if ret == nil {
return nil, exception()
}
return newModule(ret), nil
}
func AsModule(o *Base) (v *Module, ok bool) {
if ok = C.moduleCheck(o.c()) != 0; ok {
v = newModule(o.c())
}
return
}
func (mod *Module) CheckExact() bool {
return C.moduleCheckE(mod.c()) != 0
}
// Return value: Borrowed reference.
func (mod *Module) Dict() *Dict {
ret := C.PyModule_GetDict(mod.c())
return newDict(ret)
}
// Return module‘s __name__ value. If the module does not provide one, or if it is not a string,
// SystemError is raised and NULL is returned.
func (mod *Module) Name() (string, error) {
ret := C.PyModule_GetName(mod.c())
if ret == nil {
return "", exception()
}
return C.GoString(ret), nil
}
func (mod *Module) Filename() (string, error) {
ret := C.PyModule_GetFilename(mod.c())
if ret == nil {
return "", exception()
}
return C.GoString(ret), nil
}
func (mod *Module) AddObject(name string, obj *Base) error {
if obj == nil {
return AssertionError.Err("ValueError: obj == nil!")
}
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ret := C.PyModule_AddObject(mod.c(), cname, obj.c())
if ret < 0 {
return exception()
}
return nil
}
func (mod *Module) AddIntConstant(name string, value int) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ret := C.PyModule_AddIntConstant(mod.c(), cname, C.long(value))
if ret < 0 {
return exception()
}
return nil
}
func (mod *Module) AddStringConstant(name, value string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
cvalue := C.CString(value)
defer C.free(unsafe.Pointer(cvalue))
ret := C.PyModule_AddStringConstant(mod.c(), cname, cvalue)
if ret < 0 {
return exception()
}
return nil
}