forked from go-python/cpy3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
267 lines (214 loc) · 9.18 KB
/
object.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Unless explicitly stated otherwise all files in this repository are licensed
under the MIT License.
This product includes software developed at Datadog (https://www.datadoghq.com/).
Copyright 2018 Datadog, Inc.
*/
package python3
//go:generate go run script/variadic.go
/*
#include "Python.h"
#include "macro.h"
#include "variadic.h"
*/
import "C"
import (
"unsafe"
)
//MaxVariadicLength is the maximum number of arguments that can be passed to a variadic C function due to a cgo limitation
const MaxVariadicLength = 20
// Constants used for comparison in PyObject_RichCompareBool
var (
Py_LT = int(C.Py_LT)
Py_LE = int(C.Py_LE)
Py_EQ = int(C.Py_EQ)
Py_NE = int(C.Py_NE)
Py_GT = int(C.Py_GT)
Py_GE = int(C.Py_GE)
)
//None : https://docs.python.org/3/c-api/none.html#c.Py_None
var Py_None = togo(C.Py_None)
//PyObject : https://docs.python.org/3/c-api/structures.html?highlight=pyobject#c.PyObject
type PyObject C.PyObject
//IncRef : https://docs.python.org/3/c-api/refcounting.html#c.Py_INCREF
func (pyObject *PyObject) IncRef() {
C.Py_IncRef(toc(pyObject))
}
//DecRef : https://docs.python.org/3/c-api/refcounting.html#c.Py_DECREF
func (pyObject *PyObject) DecRef() {
C.Py_DecRef(toc(pyObject))
}
//ReprEnter : https://docs.python.org/3/c-api/exceptions.html#c.Py_ReprEnter
func (pyObject *PyObject) ReprEnter() int {
return int(C.Py_ReprEnter(toc(pyObject)))
}
//ReprLeave : https://docs.python.org/3/c-api/exceptions.html#c.Py_ReprLeave
func (pyObject *PyObject) ReprLeave() {
C.Py_ReprLeave(toc(pyObject))
}
//HasAttr : https://docs.python.org/3/c-api/object.html#c.PyObject_HasAttr
func (pyObject *PyObject) HasAttr(attr_name *PyObject) bool {
return C.PyObject_HasAttr(toc(pyObject), toc(attr_name)) == 1
}
//HasAttrString : https://docs.python.org/3/c-api/object.html#c.PyObject_HasAttrString
func (pyObject *PyObject) HasAttrString(attr_name string) bool {
cattr_name := C.CString(attr_name)
defer C.free(unsafe.Pointer(cattr_name))
return C.PyObject_HasAttrString(toc(pyObject), cattr_name) == 1
}
//GetAttr : https://docs.python.org/3/c-api/object.html#c.PyObject_GetAttr
func (pyObject *PyObject) GetAttr(attr_name *PyObject) *PyObject {
return togo(C.PyObject_GetAttr(toc(pyObject), toc(attr_name)))
}
//GetAttrString : https://docs.python.org/3/c-api/object.html#c.PyObject_GetAttrString
func (pyObject *PyObject) GetAttrString(attr_name string) *PyObject {
cattr_name := C.CString(attr_name)
defer C.free(unsafe.Pointer(cattr_name))
return togo(C.PyObject_GetAttrString(toc(pyObject), cattr_name))
}
//SetAttr : https://docs.python.org/3/c-api/object.html#c.PyObject_SetAttr
func (pyObject *PyObject) SetAttr(attr_name *PyObject, v *PyObject) int {
return int(C.PyObject_SetAttr(toc(pyObject), toc(attr_name), toc(v)))
}
//SetAttrString : https://docs.python.org/3/c-api/object.html#c.PyObject_SetAttrString
func (pyObject *PyObject) SetAttrString(attr_name string, v *PyObject) int {
cattr_name := C.CString(attr_name)
defer C.free(unsafe.Pointer(cattr_name))
return int(C.PyObject_SetAttrString(toc(pyObject), cattr_name, toc(v)))
}
//DelAttr : https://docs.python.org/3/c-api/object.html#c.PyObject_DelAttr
func (pyObject *PyObject) DelAttr(attr_name *PyObject) int {
return int(C._go_PyObject_DelAttr(toc(pyObject), toc(attr_name)))
}
//DelAttrString : https://docs.python.org/3/c-api/object.html#c.PyObject_DelAttrString
func (pyObject *PyObject) DelAttrString(attr_name string) int {
cattr_name := C.CString(attr_name)
defer C.free(unsafe.Pointer(cattr_name))
return int(C._go_PyObject_DelAttrString(toc(pyObject), cattr_name))
}
//RichCompare : https://docs.python.org/3/c-api/object.html#c.PyObject_RichCompare
func (pyObject *PyObject) RichCompare(o *PyObject, opid int) *PyObject {
return togo(C.PyObject_RichCompare(toc(pyObject), toc(o), C.int(opid)))
}
//RichCompareBool : https://docs.python.org/3/c-api/object.html#c.PyObject_RichCompareBool
func (pyObject *PyObject) RichCompareBool(o *PyObject, opid int) int {
return int(C.PyObject_RichCompareBool(toc(pyObject), toc(o), C.int(opid)))
}
//Repr : https://docs.python.org/3/c-api/object.html#c.PyObject_Repr
func (pyObject *PyObject) Repr() *PyObject {
return togo(C.PyObject_Repr(toc(pyObject)))
}
//ASCII : https://docs.python.org/3/c-api/object.html#c.PyObject_ASCII
func (pyObject *PyObject) ASCII() *PyObject {
return togo(C.PyObject_ASCII(toc(pyObject)))
}
//Str : https://docs.python.org/3/c-api/object.html#c.PyObject_Str
func (pyObject *PyObject) Str() *PyObject {
return togo(C.PyObject_Str(toc(pyObject)))
}
//Bytes : https://docs.python.org/3/c-api/object.html#c.PyObject_Bytes
func (pyObject *PyObject) Bytes() *PyObject {
return togo(C.PyObject_Bytes(toc(pyObject)))
}
//IsSubclass : https://docs.python.org/3/c-api/object.html#c.PyObject_IsSubclass
func (pyObject *PyObject) IsSubclass(cls *PyObject) int {
return int(C.PyObject_IsSubclass(toc(pyObject), toc(cls)))
}
//IsInstance : https://docs.python.org/3/c-api/object.html#c.PyObject_IsInstance
func (pyObject *PyObject) IsInstance(cls *PyObject) int {
return int(C.PyObject_IsInstance(toc(pyObject), toc(cls)))
}
// PyCallable_Check : https://docs.python.org/3/c-api/object.html#c.PyCallable_Check
func PyCallable_Check(o *PyObject) bool {
return C.PyCallable_Check(toc(o)) == 1
}
//Call : https://docs.python.org/3/c-api/object.html#c.PyObject_Call
func (pyObject *PyObject) Call(args *PyObject, kwargs *PyObject) *PyObject {
return togo(C.PyObject_Call(toc(pyObject), toc(args), toc(kwargs)))
}
//CallObject : https://docs.python.org/3/c-api/object.html#c.PyObject_CallObject
func (pyObject *PyObject) CallObject(args *PyObject) *PyObject {
return togo(C.PyObject_CallObject(toc(pyObject), toc(args)))
}
//CallFunctionObjArgs : https://docs.python.org/3/c-api/object.html#c.PyObject_CallFunctionObjArgs
func (pyObject *PyObject) CallFunctionObjArgs(args ...*PyObject) *PyObject {
if len(args) > MaxVariadicLength {
panic("CallFunctionObjArgs: too many arrguments")
}
if len(args) == 0 {
return togo(C._go_PyObject_CallFunctionObjArgs(toc(pyObject), 0, (**C.PyObject)(nil)))
}
cargs := make([]*C.PyObject, len(args), len(args))
for i, arg := range args {
cargs[i] = toc(arg)
}
return togo(C._go_PyObject_CallFunctionObjArgs(toc(pyObject), C.int(len(args)), (**C.PyObject)(unsafe.Pointer(&cargs[0]))))
}
//CallMethodObjArgs : https://docs.python.org/3/c-api/object.html#c.PyObject_CallMethodObjArgs
func (pyObject *PyObject) CallMethodObjArgs(name *PyObject, args ...*PyObject) *PyObject {
if len(args) > MaxVariadicLength {
panic("CallMethodObjArgs: too many arguments")
}
if len(args) == 0 {
return togo(C._go_PyObject_CallMethodObjArgs(toc(pyObject), toc(name), 0, (**C.PyObject)(nil)))
}
cargs := make([]*C.PyObject, len(args), len(args))
for i, arg := range args {
cargs[i] = toc(arg)
}
return togo(C._go_PyObject_CallMethodObjArgs(toc(pyObject), toc(name), C.int(len(args)), (**C.PyObject)(unsafe.Pointer(&cargs[0]))))
}
//CallMethodArgs : same as CallMethodObjArgs but with name as go string
func (pyObject *PyObject) CallMethodArgs(name string, args ...*PyObject) *PyObject {
pyName := PyUnicode_FromString(name)
defer pyName.DecRef()
return pyObject.CallMethodObjArgs(pyName, args...)
}
//Hash : https://docs.python.org/3/c-api/object.html#c.PyObject_Hash
func (pyObject *PyObject) Hash() int {
return int(C.PyObject_Hash(toc(pyObject)))
}
//HashNotImplemented : https://docs.python.org/3/c-api/object.html#c.PyObject_HashNotImplemented
func (pyObject *PyObject) HashNotImplemented() int {
return int(C.PyObject_HashNotImplemented(toc(pyObject)))
}
//IsTrue : https://docs.python.org/3/c-api/object.html#c.PyObject_IsTrue
func (pyObject *PyObject) IsTrue() int {
return int(C.PyObject_IsTrue(toc(pyObject)))
}
//Not : https://docs.python.org/3/c-api/object.html#c.PyObject_Not
func (pyObject *PyObject) Not() int {
return int(C.PyObject_Not(toc(pyObject)))
}
//Type : https://docs.python.org/3/c-api/object.html#c.PyObject_Type
func (pyObject *PyObject) Type() *PyObject {
return togo(C.PyObject_Type(toc(pyObject)))
}
//Length : https://docs.python.org/3/c-api/object.html#c.PyObject_Length
func (pyObject *PyObject) Length() int {
return int(C.PyObject_Length(toc(pyObject)))
}
//LengthHint : https://docs.python.org/3/c-api/object.html#c.PyObject_LengthHint
func (pyObject *PyObject) LengthHint(pyDefault int) int {
return int(C.PyObject_LengthHint(toc(pyObject), C.Py_ssize_t(pyDefault)))
}
//GetItem : https://docs.python.org/3/c-api/object.html#c.PyObject_GetItem
func (pyObject *PyObject) GetItem(key *PyObject) *PyObject {
return togo(C.PyObject_GetItem(toc(pyObject), toc(key)))
}
//SetItem : https://docs.python.org/3/c-api/object.html#c.PyObject_SetItem
func (pyObject *PyObject) SetItem(key, v *PyObject) int {
return int(C.PyObject_SetItem(toc(pyObject), toc(key), toc(v)))
}
//DelItem : https://docs.python.org/3/c-api/object.html#c.PyObject_DelItem
func (pyObject *PyObject) DelItem(key *PyObject) int {
return int(C.PyObject_DelItem(toc(pyObject), toc(key)))
}
//Dir : https://docs.python.org/3/c-api/object.html#c.PyObject_Dir
func (pyObject *PyObject) Dir() *PyObject {
return togo(C.PyObject_Dir(toc(pyObject)))
}
//GetIter : https://docs.python.org/3/c-api/object.html#c.PyObject_GetIter
func (pyObject *PyObject) GetIter() *PyObject {
return togo(C.PyObject_GetIter(toc(pyObject)))
}