-
Notifications
You must be signed in to change notification settings - Fork 56
/
type.go
102 lines (83 loc) · 2.73 KB
/
type.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
package py
// #include <Python.h>
// static inline int typeCheck(PyObject *o) { return PyType_Check(o); }
// static inline int typeCheckE(PyObject *o) { return PyType_CheckExact(o); }
// static inline PyObject* typeAlloc(PyObject *t, Py_ssize_t n) {
// return ((PyTypeObject*)t)->tp_alloc((PyTypeObject *)t, n);
// }
// static inline int typeInit(PyObject *t, PyObject *o, PyObject *a, PyObject *k) {
// return ((PyTypeObject*)t)->tp_init(o, a, k);
// }
// static inline PyObject* typeNew(PyObject *t, PyObject *a, PyObject *k) {
// return ((PyTypeObject*)t)->tp_new((PyTypeObject*)t, a, k);
// }
import "C"
import "unsafe"
type Type struct {
Base
o C.PyTypeObject
}
// TypeType is the Type object that represents the Type type.
var TypeType = (*Type)(unsafe.Pointer(&C.PyType_Type))
func newType(obj *C.PyObject) *Type {
return (*Type)(unsafe.Pointer(obj))
}
func AsType(o *Base) (v *Type, ok bool) {
if ok = C.typeCheck(o.c()) != 0; ok {
v = newType(o.c())
}
return
}
func (t *Type) NewNoArgs() (ret *Base, err error) {
args := NewTuple(0)
defer args.Decref()
return t.New(args, nil)
}
func (t *Type) New(args *Tuple, kw *Dict) (ret *Base, err error) {
ret1 := C.typeNew(t.c(), args.c(), kw.c())
return obj2ObjErr(ret1)
}
func (t *Type) NewObjArgs(args ...*Base) (ret *Base, err error) {
args1 := PackTuple(args...)
defer args1.Decref()
return t.New(args1, nil)
}
func (t *Type) Alloc(n int64) (*Base, error) {
ret := C.typeAlloc(t.c(), C.Py_ssize_t(n))
return obj2ObjErr(ret)
}
func (t *Type) Init(obj *Base, args *Tuple, kw *Dict) error {
ret := C.typeInit(t.c(), obj.c(), args.c(), kw.c())
if ret < 0 {
return exception()
}
return nil
}
// CheckExact returns true when "t" is an actual Type object, and not some form
// of subclass.
func (t *Type) CheckExact() bool {
return C.typeCheckE(t.c()) == 1
}
// PyType_ClearCache : TODO - ???
// Modified should be called after the attributes or base class of a Type have
// been changed.
func (t *Type) Modified() {
C.PyType_Modified(&t.o)
}
// HasFeature returns true when "t" has the feature in question.
func (t *Type) HasFeature(feature uint32) bool {
return (t.o.tp_flags & C.long(feature)) != 0
}
// IsGc returns true if the type "t" supports Cyclic Garbage Collection.
func (t *Type) IsGc() bool {
return t.HasFeature(C.Py_TPFLAGS_HAVE_GC)
}
// IsSubtype returns true if "t" is a subclass of "t2".
func (t *Type) IsSubtype(t2 *Type) bool {
return C.PyType_IsSubtype(&t.o, &t2.o) == 1
}
// PyType_GenericAlloc : This is an internal function, which we should not need
// to expose.
// PyType_GenericNew : Another internal function we don't need to expose.
// PyType_Ready : This function is wrapped (along with a lot of other
// functionality) in the Create method of the Class stuct.