-
Notifications
You must be signed in to change notification settings - Fork 9
/
module.go
170 lines (137 loc) · 3.49 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.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 {
AbstractObject
o C.PyObject
}
// ModuleType is the Type object that represents the Module type.
var ModuleType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyModule_Type)))
func moduleCheck(obj Object) bool {
if obj == nil {
return false
}
return C.moduleCheck(c(obj)) != 0
}
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.PyUnicode_FromString(s)
defer C.decref(pyName)
obj := C.PyImport_Import(pyName)
if obj == nil {
return nil, exception()
}
return newModule(obj), nil
}
func InitModule(name string, methods []Method) (*Module, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
cm := C.PyImport_AddModule(cname)
if cm == nil {
return nil, exception()
}
m := newModule(cm)
if len(methods) == 0 {
return m, nil
}
defer m.Decref()
n := C.PyUnicode_FromString(cname)
if n == nil {
return nil, exception()
}
for _, method := range methods {
pyF, err := makeCFunction(method.Name, method.Func, method.Doc, n)
if err != nil {
return nil, err
}
if m.AddObject(method.Name, pyF); err != nil {
return nil, err
}
}
m.Incref()
return m, nil
}
func ExecCodeModule(name string, code Object) (*Module, error) {
s := C.CString(name)
defer C.free(unsafe.Pointer(s))
ret := C.PyImport_ExecCodeModule(s, c(code))
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 (mod *Module) CheckExact() bool {
return C.moduleCheckE(c(mod)) != 0
}
func (mod *Module) Dict() *Dict {
ret := C.PyModule_GetDict(c(mod))
return newDict(ret)
}
func (mod *Module) Name() (string, error) {
ret := C.PyModule_GetName(c(mod))
if ret == nil {
return "", exception()
}
return C.GoString(ret), nil
}
// func (mod *Module) Filename() (string, error) {
// ret := C.PyModule_GetFilename(c(mod))
// if ret == nil {
// return "", exception()
// }
// return C.GoString(ret), nil
// }
func (mod *Module) AddObject(name string, obj Object) error {
if obj == nil {
return AssertionError.Err("ValueError: obj == nil!")
}
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ret := C.PyModule_AddObject(c(mod), cname, c(obj))
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(c(mod), 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(c(mod), cname, cvalue)
if ret < 0 {
return exception()
}
return nil
}