-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlong.go
61 lines (48 loc) · 1.11 KB
/
long.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
package gp
/*
#include <Python.h>
*/
import "C"
import "unsafe"
type Long struct {
Object
}
func newLong(obj *cPyObject) Long {
return Long{newObject(obj)}
}
func MakeLong(i int64) Long {
return newLong(C.PyLong_FromLongLong(C.longlong(i)))
}
func LongFromFloat64(v float64) Long {
return newLong(C.PyLong_FromDouble(C.double(v)))
}
func LongFromString(s string, base int) Long {
cstr := AllocCStr(s)
o := C.PyLong_FromString(cstr, nil, C.int(base))
C.free(unsafe.Pointer(cstr))
return newLong(o)
}
func LongFromUnicode(u Object, base int) Long {
return newLong(C.PyLong_FromUnicodeObject(u.cpyObj(), C.int(base)))
}
func (l Long) Int() int {
return int(l.Int64())
}
func (l Long) Int64() int64 {
return int64(C.PyLong_AsLongLong(l.obj))
}
func (l Long) Uint() uint {
return uint(l.Uint64())
}
func (l Long) Uint64() uint64 {
return uint64(C.PyLong_AsUnsignedLongLong(l.obj))
}
func (l Long) Uintptr() uintptr {
return uintptr(l.Int64())
}
func (l Long) Float64() float64 {
return float64(C.PyLong_AsDouble(l.obj))
}
func LongFromUintptr(v uintptr) Long {
return newLong(C.PyLong_FromLong(C.long(v)))
}