-
Notifications
You must be signed in to change notification settings - Fork 9
/
bool.go
69 lines (58 loc) · 1.77 KB
/
bool.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
// 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 boolCheck(PyObject *o) { return PyBool_Check(o); }
// static inline void *pyTrue(void) { return Py_True; }
// static inline void *pyFalse(void) { return Py_False; }
import "C"
import (
"fmt"
"unsafe"
)
// *Bool is the representation of the Python bool type. There are only two
// possible values for a Bool, True and False. Every True value refers to the
// same instance, and every False value refers to the same value.
type Bool struct {
AbstractObject
o C.PyObject
}
// BoolType is the Type object that represents the Bool type.
var BoolType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyBool_Type)))
// True is the true value of the Bool type. It is a singleton value, all true
// values refer to the same instance.
var True = (*Bool)(C.pyTrue())
// False is the false value of the Bool type. It is a singleton value, all
// false values refer to the same instance.
var False = (*Bool)(C.pyFalse())
func boolCheck(obj Object) bool {
return C.boolCheck(c(obj)) != 0
}
func newBool(obj *C.PyObject) *Bool {
if obj == c(True) {
return True
}
if obj == c(False) {
return False
}
panic(TypeError.Err("not a bool"))
}
// Bool returns the value of "b" as a bool. true for True, false for False. If
// "b" is neither True nor False then this function will panic.
func (b *Bool) Bool() bool {
if b == True {
return true
}
if b == False {
return false
}
panic(TypeError.Err("not a bool"))
}
// String returns a printable representation of the Bool "b".
func (b *Bool) String() string {
if b == nil {
return "<nil>"
}
return fmt.Sprintf("%v", b.Bool())
}