-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtype.go
95 lines (76 loc) · 2.49 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
// 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 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);
// }
import "C"
import "unsafe"
type Type struct {
AbstractObject
o C.PyTypeObject
}
// TypeType is the Type object that represents the Type type.
var TypeType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyType_Type)))
func typeCheck(obj Object) bool {
if obj == nil {
return false
}
return C.typeCheck(c(obj)) != 0
}
func newType(obj *C.PyObject) *Type {
return (*Type)(unsafe.Pointer(obj))
}
func (t *Type) Alloc(n int64) (Object, error) {
ret := C.typeAlloc(c(t), C.Py_ssize_t(n))
return obj2ObjErr(ret)
}
func (t *Type) Init(obj Object, args *Tuple, kw *Dict) error {
ret := C.typeInit(c(t), c(obj), c(args), c(kw))
if ret < 0 {
return exception()
}
return nil
}
func (t *Type) String() string {
pyS := C.PyObject_Str(c(t))
if pyS == nil {
return "<unknown type>"
}
return stringify(t)
}
// CheckExact returns true when "t" is an actual Type object, and not some form
// of subclass.
func (t *Type) CheckExact() bool {
return C.typeCheckE(c(t)) == 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.ulong(feature)) != 0
}
// IsGc returns true if the type "t" supports Cyclic Garbage Collection.
func (t *Type) IsGc() bool {
return t.HasFeature(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.