-
Notifications
You must be signed in to change notification settings - Fork 9
/
abstract.go
97 lines (80 loc) · 2.63 KB
/
abstract.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
// 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 void incref(PyObject *obj) { Py_INCREF(obj); }
// static inline void decref(PyObject *obj) { Py_DECREF(obj); }
// static inline void typeFree(PyTypeObject *type, PyObject *o) { type->tp_free(o); }
import "C"
import "unsafe"
// AbstractObject is an 0-sized type that can be embedded as the first item in
// concrete types to provide the Object interface functions.
type AbstractObject struct{}
func newAbstractObject(obj *C.PyObject) *AbstractObject {
return (*AbstractObject)(unsafe.Pointer(obj))
}
// Init initialises obj. It is equivalent to "obj.__init__(*args, **kw)" in
// Python.
func (obj *AbstractObject) Init(args *Tuple, kw *Dict) error {
return obj.Type().Init(obj, args, kw)
}
// Base returns a BaseObject pointer that gives access to the generic methods on
// that type for this object.
func (obj *AbstractObject) Base() *BaseObject {
return (*BaseObject)(unsafe.Pointer(obj))
}
// Type returns a pointer to the Type that represents the type of this object in
// Python.
func (obj *AbstractObject) Type() *Type {
o := c(obj).ob_type
return newType((*C.PyObject)(unsafe.Pointer(o)))
}
// Decref decrements obj's reference count, obj may be nil.
func Decref(obj Object) {
if obj != nil {
C.decref(c(obj))
}
}
// Decref decrements obj's reference count, obj may not be nil.
func (obj *AbstractObject) Decref() {
C.decref(c(obj))
}
// Incref increments obj's reference count, obj may be nil.
func Incref(obj Object) {
if obj != nil {
C.incref(c(obj))
}
}
// Incref increments obj's reference count, obj may not be nil.
func (obj *AbstractObject) Incref() {
C.incref(c(obj))
}
// IsTrue returns true if the value of obj is considered to be True. This is
// equivalent to "if obj:" in Python.
func (obj *AbstractObject) IsTrue() bool {
ret := C.PyObject_IsTrue(c(obj))
if ret < 0 {
panic(exception())
}
return ret != 0
}
// Not returns true if the value of obj is considered to be False. This is
// equivalent to "if not obj:" in Python.
func (obj *AbstractObject) Not() bool {
ret := C.PyObject_Not(c(obj))
if ret < 0 {
panic(exception())
}
return ret != 0
}
// Free deallocates the storage (in Python) for obj. After calling this method,
// obj should no longer be used.
func (obj *AbstractObject) Free() {
o := c(obj)
// Make sure this instance isn't registered anymore
clearClassContext(unsafe.Pointer(o))
// Call Python free function
pyType := (*C.PyTypeObject)(unsafe.Pointer(o.ob_type))
C.typeFree(pyType, o)
}