-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.go
51 lines (41 loc) · 1.26 KB
/
class.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
package py
// #include <Python.h>
// static inline int classCheck(PyObject *o) { return PyClass_Check(o); }
import "C"
import "unsafe"
type Class struct {
Base
o C.PyClassObject
}
// ClassType is the Type object that represents the Class type.
var ClassType = (*Type)(unsafe.Pointer(&C.PyClass_Type))
func newClass(obj *C.PyObject) *Class {
return (*Class)(unsafe.Pointer(obj))
}
func AsClass(o *Base) (v *Class, ok bool) {
if ok = C.classCheck(o.c()) != 0; ok {
v = newClass(o.c())
}
return
}
func (t *Class) NewNoArgs() (ret *Base, err error) {
args := NewTuple(0)
defer args.Decref()
return t.New(args, nil)
}
// Return value: New reference.
// Create a new instance of a specific class. The parameters arg and kw are used as
// the positional and keyword parameters to the object’s constructor.
func (t *Class) New(args *Tuple, kw *Dict) (ret *Base, err error) {
ret1 := C.PyInstance_New(t.c(), args.c(), kw.c())
return obj2ObjErr(ret1)
}
func (t *Class) NewObjArgs(args ...*Base) (ret *Base, err error) {
args1 := PackTuple(args...)
defer args1.Decref()
return t.New(args1, nil)
}
// Return true if klass is a subclass of base. Return false in all other cases.
func (t *Class) IsSubclass(base *Base) bool {
return C.PyClass_IsSubclass(t.c(), base.c()) != 0
}