예제 #1
0
func NewFloat(v float64) (*Float, error) {
	ret := C.PyFloat_FromDouble(C.double(v))
	if ret == nil {
		return nil, exception()
	}
	return newFloat(ret), nil
}
예제 #2
0
파일: numeric.go 프로젝트: remh/go-python
// PyObject* PyFloat_FromDouble(double v)
// Return value: New reference.
// Create a PyFloatObject object from v, or NULL on failure.
func PyFloat_FromDouble(v float32) *PyObject {
	return togo(C.PyFloat_FromDouble(C.double(v)))
}
예제 #3
0
파일: numeric.go 프로젝트: z0mbie42/py3
// PyObject* PyFloat_FromDouble(double v)
// Return value: New reference.
// Create a PyFloatObject object from v, or NULL on failure.
func PyFloat_FromDouble(v float32) *PyFloat {
	return (*PyFloat)(PyObjectToGO(C.PyFloat_FromDouble(C.double(v))))
}
예제 #4
0
파일: float.go 프로젝트: MogeiWang/py
func NewFloat(i float64) *Float {
	return newFloat(C.PyFloat_FromDouble(C.double(i)))
}
예제 #5
0
파일: python.go 프로젝트: tsavola/go-python
// encode translates a Go value (or a wrapped Python object) to a Python
// object.
func encode(x interface{}) (pyValue *C.PyObject, err error) {
	if x == nil {
		pyValue = C.None_INCREF()
		return
	}

	switch value := x.(type) {
	case bool:
		var i C.long
		if value {
			i = 1
		}
		pyValue = C.PyBool_FromLong(i)

	case byte: // alias uint8
		c := C.char(value)
		pyValue = C.PyString_FromStringAndSize(&c, 1)

	case complex64:
		pyValue = C.PyComplex_FromDoubles(C.double(real(value)), C.double(imag(value)))

	case complex128:
		pyValue = C.PyComplex_FromDoubles(C.double(real(value)), C.double(imag(value)))

	case float32:
		pyValue = C.PyFloat_FromDouble(C.double(value))

	case float64:
		pyValue = C.PyFloat_FromDouble(C.double(value))

	case int: // alias rune
		pyValue = C.PyInt_FromLong(C.long(value))

	case int8:
		pyValue = C.PyInt_FromLong(C.long(value))

	case int16:
		pyValue = C.PyInt_FromLong(C.long(value))

	case int32:
		pyValue = C.PyInt_FromLong(C.long(value))

	case int64:
		pyValue = C.Long_FromInt64(C.int64_t(value))

	case string:
		pyValue = C.String_FromGoStringPtr(unsafe.Pointer(&value))

	case uint:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case uint16:
		pyValue = C.PyInt_FromLong(C.long(value))

	case uint32:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case uint64:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case uintptr:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case []interface{}:
		return encodeTuple(value)

	case map[interface{}]interface{}:
		return encodeDict(value)

	case *object:
		pyValue = value.pyObject
		C.INCREF(pyValue)

	default:
		err = fmt.Errorf("unable to translate %t to Python", x)
		return
	}

	if pyValue == nil {
		err = getError()
	}
	return
}