bind: declare/implement converters for basic types

Change-Id: If570b3d119a1ee8e7288915a4db8e4b18b6285ea
This commit is contained in:
Sebastien Binet 2015-08-05 17:40:41 +02:00
parent 03c7549090
commit 9b92f7de5d
3 changed files with 72 additions and 2 deletions

View File

@ -60,6 +60,43 @@ cgopy_cnv_c2py_string(GoString *addr) {
free((void*)str); free((void*)str);
return pystr; return pystr;
} }
static int
cgopy_cnv_py2c_f32(PyObject *o, GoFloat32 *addr) {
GoFloat32 v = PyFloat_AsDouble(o);
*addr = v;
return 1;
}
static PyObject*
cgopy_cnv_c2py_f32(GoFloat32 *addr) {
GoFloat64 v = *addr;
return PyFloat_FromDouble(v);
}
static int
cgopy_cnv_py2c_complex64(PyObject *o, GoComplex64 *addr) {
Py_complex v = PyComplex_AsCComplex(o);
*addr = v.real + v.imag * _Complex_I;
return 1;
}
static PyObject*
cgopy_cnv_c2py_complex64(GoComplex64 *addr) {
return PyComplex_FromDoubles(creal(*addr), cimag(*addr));
}
static int
cgopy_cnv_py2c_complex128(PyObject *o, GoComplex128 *addr) {
Py_complex v = PyComplex_AsCComplex(o);
*addr = v.real + v.imag * _Complex_I;
return 1;
}
static PyObject*
cgopy_cnv_c2py_complex128(GoComplex128 *addr) {
return PyComplex_FromDoubles(creal(*addr), cimag(*addr));
}
` `
) )

View File

@ -23,6 +23,7 @@ package main
//#cgo pkg-config: python2 --cflags --libs //#cgo pkg-config: python2 --cflags --libs
//#include <stdlib.h> //#include <stdlib.h>
//#include <string.h> //#include <string.h>
//#include <complex.h>
import "C" import "C"
import ( import (

View File

@ -81,7 +81,7 @@ func (s symbol) isStruct() bool {
} }
func (s symbol) hasConverter() bool { func (s symbol) hasConverter() bool {
return s.c2py != "" || s.py2c != "" return s.pyfmt == "O&" && (s.c2py != "" || s.py2c != "")
} }
func (s symbol) cgotypename() string { func (s symbol) cgotypename() string {
@ -356,6 +356,8 @@ func init() {
cgoname: "GoInt", cgoname: "GoInt",
pyfmt: "i", pyfmt: "i",
pysig: "int", pysig: "int",
c2py: "PyInt_FromLong",
py2c: "PyInt_AsLong",
}, },
"int8": { "int8": {
@ -366,6 +368,8 @@ func init() {
cgoname: "GoInt8", cgoname: "GoInt8",
pyfmt: "c", pyfmt: "c",
pysig: "int", pysig: "int",
c2py: "PyInt_FromLong",
py2c: "PyInt_AsLong",
}, },
"int16": { "int16": {
@ -376,6 +380,8 @@ func init() {
cgoname: "GoInt16", cgoname: "GoInt16",
pyfmt: "h", pyfmt: "h",
pysig: "int", pysig: "int",
c2py: "PyInt_FromLong",
py2c: "PyInt_AsLong",
}, },
"int32": { "int32": {
@ -385,7 +391,9 @@ func init() {
cpyname: "int32_t", cpyname: "int32_t",
cgoname: "GoInt32", cgoname: "GoInt32",
pyfmt: "i", pyfmt: "i",
pysig: "long", pysig: "int",
c2py: "PyInt_FromLong",
py2c: "PyInt_AsLong",
}, },
"int64": { "int64": {
@ -396,6 +404,8 @@ func init() {
cgoname: "GoInt64", cgoname: "GoInt64",
pyfmt: "k", pyfmt: "k",
pysig: "long", pysig: "long",
c2py: "PyLong_FromLong",
py2c: "PyLong_AsLong",
}, },
"uint": { "uint": {
@ -406,6 +416,8 @@ func init() {
cgoname: "GoUint", cgoname: "GoUint",
pyfmt: "I", pyfmt: "I",
pysig: "int", pysig: "int",
c2py: "PyInt_FromUnsignedLong",
py2c: "PyInt_AsUnsignedLong",
}, },
"uint8": { "uint8": {
@ -416,6 +428,8 @@ func init() {
cgoname: "GoUint8", cgoname: "GoUint8",
pyfmt: "b", pyfmt: "b",
pysig: "int", pysig: "int",
c2py: "PyInt_FromUnsignedLong",
py2c: "PyInt_AsUnsignedLong",
}, },
"uint16": { "uint16": {
goobj: look("uint16"), goobj: look("uint16"),
@ -425,6 +439,8 @@ func init() {
cgoname: "GoUint16", cgoname: "GoUint16",
pyfmt: "H", pyfmt: "H",
pysig: "int", pysig: "int",
c2py: "PyInt_FromUnsignedLong",
py2c: "PyInt_AsUnsignedLong",
}, },
"uint32": { "uint32": {
goobj: look("uint32"), goobj: look("uint32"),
@ -434,6 +450,8 @@ func init() {
cgoname: "GoUint32", cgoname: "GoUint32",
pyfmt: "I", pyfmt: "I",
pysig: "long", pysig: "long",
c2py: "PyInt_FromUnsignedLong",
py2c: "PyInt_AsUnsignedLong",
}, },
"uint64": { "uint64": {
@ -444,6 +462,8 @@ func init() {
cgoname: "GoUint64", cgoname: "GoUint64",
pyfmt: "K", pyfmt: "K",
pysig: "long", pysig: "long",
c2py: "PyLong_FromUnsignedLong",
py2c: "PyLong_AsUnsignedLong",
}, },
"float32": { "float32": {
@ -454,6 +474,8 @@ func init() {
cgoname: "GoFloat32", cgoname: "GoFloat32",
pyfmt: "f", pyfmt: "f",
pysig: "float", pysig: "float",
c2py: "cgopy_cnv_c2py_f32",
py2c: "cgopy_cnv_py2c_f32",
}, },
"float64": { "float64": {
goobj: look("float64"), goobj: look("float64"),
@ -463,6 +485,8 @@ func init() {
cgoname: "GoFloat64", cgoname: "GoFloat64",
pyfmt: "d", pyfmt: "d",
pysig: "float", pysig: "float",
c2py: "PyFloat_FromDouble",
py2c: "PyFloat_AsDouble",
}, },
"complex64": { "complex64": {
goobj: look("complex64"), goobj: look("complex64"),
@ -472,6 +496,8 @@ func init() {
cgoname: "GoComplex64", cgoname: "GoComplex64",
pyfmt: "D", pyfmt: "D",
pysig: "float", pysig: "float",
c2py: "cgopy_cnv_c2py_complex64",
py2c: "cgopy_cnv_py2c_complex64",
}, },
"complex128": { "complex128": {
goobj: look("complex128"), goobj: look("complex128"),
@ -481,6 +507,8 @@ func init() {
cgoname: "GoComplex128", cgoname: "GoComplex128",
pyfmt: "D", pyfmt: "D",
pysig: "float", pysig: "float",
c2py: "cgopy_cnv_c2py_complex128",
py2c: "cgopy_cnv_py2c_complex128",
}, },
"string": { "string": {
@ -529,6 +557,8 @@ func init() {
cgoname: "GoInt", cgoname: "GoInt",
pyfmt: "k", pyfmt: "k",
pysig: "int", pysig: "int",
c2py: "PyLong_FromLong",
py2c: "PyLong_AsLong",
} }
syms["uint"] = &symbol{ syms["uint"] = &symbol{
goobj: look("uint"), goobj: look("uint"),
@ -538,6 +568,8 @@ func init() {
cgoname: "GoUint", cgoname: "GoUint",
pyfmt: "K", pyfmt: "K",
pysig: "int", pysig: "int",
c2py: "PyLong_FromUnsignedLong",
py2c: "PyLong_AsUnsignedLong",
} }
} }