-
Notifications
You must be signed in to change notification settings - Fork 9
/
cfunction.go
172 lines (140 loc) · 3.58 KB
/
cfunction.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
171
172
// 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 cfunctionCheck(PyObject *o) { return PyCFunction_Check(o); }
import "C"
import (
"sync"
"unsafe"
)
type CFunction struct {
AbstractObject
o C.PyCFunctionObject
}
func cfunctionCheck(obj Object) bool {
if obj == nil {
return false
}
return C.cfunctionCheck(c(obj)) != 0
}
func newCFunction(obj *C.PyObject) *CFunction {
return (*CFunction)(unsafe.Pointer(obj))
}
func NewCFunction(name string, fn interface{}, doc string) (*CFunction, error) {
return makeCFunction(name, fn, doc, nil)
}
func makeCFunction(name string, fn interface{}, doc string, mod_name *C.PyObject) (*CFunction, error) {
ml := C.newMethodDef()
switch fn.(type) {
case func() (Object, error):
C.set_call_noargs(&ml.ml_meth)
ml.ml_flags = C.METH_NOARGS
case func(a *Tuple) (Object, error):
C.set_call_args(&ml.ml_meth)
ml.ml_flags = C.METH_VARARGS
case func(a *Tuple, k *Dict) (Object, error):
C.set_call_keywords(&ml.ml_meth)
ml.ml_flags = C.METH_VARARGS | C.METH_KEYWORDS
default:
C.free(unsafe.Pointer(ml))
return nil, TypeError.Err("CFunction_New: unknown func type for %s", name)
}
ret := C.PyCFunction_NewEx(ml, saveFunc(fn), mod_name)
if ret == nil {
C.free(unsafe.Pointer(ml))
return nil, exception()
}
ml.ml_name = C.CString(name)
ml.ml_doc = C.CString(doc)
return newCFunction(ret), nil
}
// PyCFunction_GetFunction
func (f *CFunction) Self() (Object, error) {
ret := C.PyCFunction_GetSelf(c(f))
if ret == nil {
return nil, exception()
}
return newObject(ret), nil
}
func (f *CFunction) Flags() (int, error) {
ret := C.PyCFunction_GetFlags(c(f))
return int(ret), exception()
}
func (f *CFunction) Call(args *Tuple, kw *Dict) (Object, error) {
ret := C.PyCFunction_Call(c(f), c(args), c(kw))
if ret == nil {
return nil, exception()
}
return newObject(ret), nil
}
type Method struct {
Name string
Func interface{}
Doc string
}
var (
funcLock sync.RWMutex
funcs []interface{}
)
func saveFunc(f interface{}) *C.PyObject {
funcLock.Lock()
defer funcLock.Unlock()
funcs = append(funcs, f)
return C.PyLong_FromLong(C.long(len(funcs) - 1))
}
func getFunc(self unsafe.Pointer) interface{} {
funcLock.RLock()
defer funcLock.RUnlock()
idx := int(C.PyLong_AsLong((*C.PyObject)(self)))
if idx >= len(funcs) {
return nil
}
return funcs[idx]
}
//export callWithoutArgs
func callWithoutArgs(self, args unsafe.Pointer) unsafe.Pointer {
f, ok := getFunc(self).(func() (Object, error))
if !ok {
raise(AssertionError.Err("callWithoutArgs: wrong function type!!!"))
return nil
}
ret, err := f()
if err != nil {
raise(err)
return nil
}
return unsafe.Pointer(c(ret))
}
//export callWithArgs
func callWithArgs(self, args unsafe.Pointer) unsafe.Pointer {
f, ok := getFunc(self).(func(a *Tuple) (Object, error))
if !ok {
raise(AssertionError.Err("callWithArgs: wrong function type!!!"))
return nil
}
a := newTuple((*C.PyObject)(args))
ret, err := f(a)
if err != nil {
raise(err)
return nil
}
return unsafe.Pointer(c(ret))
}
//export callWithKeywords
func callWithKeywords(self, args, kw unsafe.Pointer) unsafe.Pointer {
f, ok := getFunc(self).(func(a *Tuple, k *Dict) (Object, error))
if !ok {
raise(AssertionError.Err("callWithKeywords: wrong function type!!!"))
return nil
}
a := newTuple((*C.PyObject)(args))
k := newDict((*C.PyObject)(kw))
ret, err := f(a, k)
if err != nil {
raise(err)
return nil
}
return unsafe.Pointer(c(ret))
}