-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcfunction.go
More file actions
179 lines (142 loc) · 4.17 KB
/
cfunction.go
File metadata and controls
179 lines (142 loc) · 4.17 KB
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
173
174
175
176
177
178
179
package py
// #include "utils.h"
import "C"
import (
"sync"
)
// NewCFunction creates a CFunction object from the supplied function.
//
// The supplied function can take one of several forms, which map to different
// calling conventions supported by Python:
//
// func() (py.Object, error)
// func(py.Object) (py.Object, error)
// func(*py.Tuple) (py.Object, error)
// func(*py.Tuple, *py.Dict) (py.Object, error)
//
// These map to the following PYTHON calling conventions:
//
// - METH_NOARGS: A function with no arguments
// - METH_O: A function with a single argument, passed directly
// - METH_VARARGS: A function with positional arguments only, passed as a
// Tuple
// - METH_VARARGS | METH_KEYWORDS: A function with positional and keyword
// arguments, passed as a Tuple and Dict
//
// Return value: New Reference.
func NewCFunction(name string, fn interface{}, doc string) (*CFunction, error) {
return makeCFunction(name, fn, doc, nil)
}
func makeCFunction(name string, fn interface{}, doc string, modName *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(Object) (Object, error):
C.set_call_single(&ml.ml_meth)
ml.ml_flags = C.METH_O
case func(*Tuple) (Object, error):
C.set_call_args(&ml.ml_meth)
ml.ml_flags = C.METH_VARARGS
case func(*Tuple, *Dict) (Object, error):
C.set_call_keywords(&ml.ml_meth)
ml.ml_flags = C.METH_VARARGS | C.METH_KEYWORDS
default:
cfree(ml)
return nil, TypeError.Err("CFunction_New: unknown func type for %s", name)
}
ret := C.PyCFunction_NewEx(ml, saveFunc(fn), modName)
if ret == nil {
cfree(ml)
return nil, exception()
}
ml.ml_name = C.CString(name)
ml.ml_doc = C.CString(doc)
return newCFunction(ret), nil
}
// PyCFunction_GetFunction
// Self returns the self Object stored in the CFunction. May return nil, e.g. if
// the CFunction is a static method.
//
// Return Value: Borrowed Reference.
func (cf *CFunction) Self() (Object, error) {
ret := C.PyCFunction_GetSelf(c(cf))
if ret == nil {
return nil, exception()
}
return newObject(ret), nil
}
// Flags returns the flags stored in the CFunction. This will include the
// calling convention to be used.
//
// Return Value: Borrowed Reference.
func (cf *CFunction) Flags() (int, error) {
ret := C.PyCFunction_GetFlags(c(cf))
return int(ret), exception()
}
// GoMethod describes a Go function to be used in Python. Name is the name of
// the function in Python, Doc is the docstring to be used, and Func is the
// function to be called.
//
// See NewCFunction for details of the supported function signatures.
type GoMethod 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 *C.PyObject) interface{} {
funcLock.RLock()
defer funcLock.RUnlock()
idx := int(C.PyLong_AsLong(self))
if idx >= len(funcs) {
return nil
}
return funcs[idx]
}
//export callWithoutArgs
func callWithoutArgs(self, _ *C.PyObject) *C.PyObject {
f, ok := getFunc(self).(func() (Object, error))
if !ok {
raise(AssertionError.Err("callWithoutArgs: wrong function type!!!"))
return nil
}
return ce(f())
}
//export callWithSingle
func callWithSingle(self, arg *C.PyObject) *C.PyObject {
f, ok := getFunc(self).(func(a Object) (Object, error))
if !ok {
raise(AssertionError.Err("callWithArgs: wrong function type!!!"))
return nil
}
return ce(f(newObject(arg)))
}
//export callWithArgs
func callWithArgs(self, args *C.PyObject) *C.PyObject {
f, ok := getFunc(self).(func(a *Tuple) (Object, error))
if !ok {
raise(AssertionError.Err("callWithArgs: wrong function type!!!"))
return nil
}
return ce(f(newTuple(args)))
}
//export callWithKeywords
func callWithKeywords(self, args, kw *C.PyObject) *C.PyObject {
f, ok := getFunc(self).(func(a *Tuple, k *Dict) (Object, error))
if !ok {
raise(AssertionError.Err("callWithKeywords: wrong function type!!!"))
return nil
}
return ce(f(newTuple(args), newDict(kw)))
}