forked from sbinet/go-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
go-python.go
82 lines (60 loc) · 1.33 KB
/
go-python.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
package python
// #include "go-python.h"
import "C"
import (
"fmt"
"unsafe"
)
// pyfmt returns the python format string for a given go value
func pyfmt(v interface{}) (unsafe.Pointer, string) {
switch v := v.(type) {
case bool:
return unsafe.Pointer(&v), "b"
// case byte:
// return unsafe.Pointer(&v), "b"
case int8:
return unsafe.Pointer(&v), "b"
case int16:
return unsafe.Pointer(&v), "h"
case int32:
return unsafe.Pointer(&v), "i"
case int64:
return unsafe.Pointer(&v), "k"
case int:
switch unsafe.Sizeof(int(0)) {
case 4:
return unsafe.Pointer(&v), "i"
case 8:
return unsafe.Pointer(&v), "k"
}
case uint8:
return unsafe.Pointer(&v), "B"
case uint16:
return unsafe.Pointer(&v), "H"
case uint32:
return unsafe.Pointer(&v), "I"
case uint64:
return unsafe.Pointer(&v), "K"
case uint:
switch unsafe.Sizeof(uint(0)) {
case 4:
return unsafe.Pointer(&v), "I"
case 8:
return unsafe.Pointer(&v), "K"
}
case float32:
return unsafe.Pointer(&v), "f"
case float64:
return unsafe.Pointer(&v), "d"
case complex64:
return unsafe.Pointer(&v), "D"
case complex128:
return unsafe.Pointer(&v), "D"
case string:
cstr := C.CString(v)
return unsafe.Pointer(cstr), "s"
case *PyObject:
return unsafe.Pointer(v.topy()), "O"
}
panic(fmt.Errorf("python: unknown type (%T)", v))
}