-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.go
43 lines (33 loc) · 879 Bytes
/
int.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
// 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 <Python.h>
// static inline int intCheck(PyObject *o) { return PyInt_Check(o); }
import "C"
import "unsafe"
type Int struct {
Base
NumberProtocol
o C.PyIntObject
}
// IntType is the Type object that represents the Int type.
var IntType = (*Type)(unsafe.Pointer(&C.PyInt_Type))
func newInt(obj *C.PyObject) *Int {
return (*Int)(unsafe.Pointer(obj))
}
func NewInt(i int) *Int {
return newInt(C.PyInt_FromLong(C.long(i)))
}
func NewInt64(i int64) *Int {
return newInt(C.PyInt_FromSsize_t(C.Py_ssize_t(i)))
}
func AsInt(o *Base) (v *Int, ok bool) {
if ok = C.intCheck(o.c()) != 0; ok {
v = newInt(o.c())
}
return
}
func (i *Int) Int() int {
return int(C.PyInt_AsLong(i.c()))
}