当前位置: 首页>>代码示例>>Golang>>正文


Golang C.Py_ssize_t函数代码示例

本文整理汇总了Golang中C.Py_ssize_t函数的典型用法代码示例。如果您正苦于以下问题:Golang Py_ssize_t函数的具体用法?Golang Py_ssize_t怎么用?Golang Py_ssize_t使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Py_ssize_t函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: encodeTuple

// encodeTuple translates a Go array to a Python object.
func encodeTuple(array []interface{}) (pyTuple *C.PyObject, err error) {
	if len(array) == 0 {
		pyTuple = pyEmptyTuple
		C.INCREF(pyTuple)
	} else {
		pyTuple = C.PyTuple_New(C.Py_ssize_t(len(array)))

		var ok bool

		defer func() {
			if !ok {
				C.DECREF(pyTuple)
				pyTuple = nil
			}
		}()

		for i, item := range array {
			var pyItem *C.PyObject

			if pyItem, err = encode(item); err != nil {
				return
			}

			C.Tuple_SET_ITEM(pyTuple, C.Py_ssize_t(i), pyItem)
		}

		ok = true
	}

	return
}
开发者ID:tsavola,项目名称:go-python,代码行数:32,代码来源:python.go

示例2: pyObjToInterface

func pyObjToInterface(o *C.PyObject) interface{} {
	if C.myPyString_Check(o) != 0 {
		return C.GoStringN(C.PyString_AsString(o), C.int(C.PyString_Size(o)))
	} else if C.myPyInt_Check(o) != 0 {
		return int64(C.PyInt_AsLong(o))
	} else if C.myPyDict_Check(o) != 0 {
		v := make(map[interface{}]interface{})
		items := C.PyDict_Items(o)
		for i := 0; i < int(C.PyTuple_Size(items)); i++ {
			item := C.PyTuple_GetItem(items, C.Py_ssize_t(i))
			key := C.PyTuple_GetItem(item, 0)
			value := C.PyTuple_GetItem(item, 1)
			v[pyObjToInterface(key)] = pyObjToInterface(value)
		}
		C.Py_DecRef(items)
		return v
	} else if C.myPyTuple_Check(o) != 0 {
		length := int(C.PyTuple_Size(o))
		list := make([]interface{}, length)
		for i := 0; i < length; i++ {
			list[i] = pyObjToInterface(C.PyTuple_GetItem(o, C.Py_ssize_t(i)))
		}
		return list
	}
	return nil
}
开发者ID:redbo,项目名称:goswiftobj,代码行数:26,代码来源:pickle.go

示例3: GetSlice

func (l *List) GetSlice(low, high int64) (*List, error) {
	ret := C.PyList_GetSlice(c(l), C.Py_ssize_t(low), C.Py_ssize_t(high))
	if ret == nil {
		return nil, exception()
	}
	return newList(ret), nil
}
开发者ID:ericsnowcurrently,项目名称:qur-gopy,代码行数:7,代码来源:list.go

示例4: GetSlice

func (t *Tuple) GetSlice(low, high int64) (*Tuple, error) {
	ret := C.PyTuple_GetSlice(c(t), C.Py_ssize_t(low), C.Py_ssize_t(high))
	if ret == nil {
		return nil, exception()
	}
	return newTuple(ret), nil
}
开发者ID:xushiwei,项目名称:gopy,代码行数:7,代码来源:tuple.go

示例5: Find

func (u *Unicode) Find(substr Object, start, end int64, direction int) (int64, bool, error) {
	ret := C.PyUnicode_Find(c(u), c(substr), C.Py_ssize_t(start), C.Py_ssize_t(end), C.int(direction))
	if ret >= 0 {
		return int64(ret), true, nil
	} else if ret == -1 {
		return 0, false, nil
	}
	return 0, false, exception()
}
开发者ID:ericsnowcurrently,项目名称:qur-gopy,代码行数:9,代码来源:unicode.go

示例6: PySlice_GetIndices

// int PySlice_GetIndices(PySliceObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
// Retrieve the start, stop and step indices from the slice object slice, assuming a sequence of length length. Treats indices greater than length as errors.
//
// Returns 0 on success and -1 on error with no exception set (unless one of the indices was not None and failed to be converted to an integer, in which case -1 is returned with an exception set).
//
// You probably do not want to use this function. If you want to use slice objects in versions of Python prior to 2.3, you would probably do well to incorporate the source of PySlice_GetIndicesEx(), suitably renamed, in the source of your extension.
//
// Changed in version 2.5: This function used an int type for length and an int * type for start, stop, and step. This might require changes in your code for properly supporting 64-bit systems.
func PySlice_GetIndices(slice *PySliceObject, length int) (start, stop, step int, err error) {
	c_start := C.Py_ssize_t(0)
	c_stop := C.Py_ssize_t(0)
	c_step := C.Py_ssize_t(0)

	err = int2err(C.PySlice_GetIndices(slice.ptr, C.Py_ssize_t(length),
		&c_start, &c_stop, &c_step))

	start = int(c_start)
	stop = int(c_stop)
	step = int(c_step)

	return
}
开发者ID:gward,项目名称:go-python,代码行数:22,代码来源:otherobjects.go

示例7: NewTuple

// NewTuple returns a new *Tuple of the specified size.  However the entries are
// all set to NULL, so the tuple should not be shared, especially with Python
// code, until the entries have all been set.
//
// Return value: New Reference.
func NewTuple(size int64) (*Tuple, error) {
	ret := C.PyTuple_New(C.Py_ssize_t(size))
	if ret == nil {
		return nil, exception()
	}
	return newTuple(ret), nil
}
开发者ID:xushiwei,项目名称:gopy,代码行数:12,代码来源:tuple.go

示例8: PackTuple

// PackTuple returns a new *Tuple which contains the arguments.  This tuple is
// ready to use.
//
// Return value: New Reference.
func PackTuple(items ...Object) (*Tuple, error) {
	ret := C.PyTuple_New(C.Py_ssize_t(len(items)))
	if ret == nil {
		return nil, exception()
	}

	// Since the ob_item array has a size of 1, Go won't let us index more than
	// a single entry, and if we try and use our own local type definition with
	// a flexible array member then cgo converts it to [0]byte which is even
	// less useful.  So, we resort to pointer manipulation - which is
	// unfortunate, as it's messy in Go.

	// base is a pointer to the first item in the array of PyObject pointers.
	// step is the size of a PyObject * (i.e. the number of bytes we need to add
	// to get to the next item).
	base := unsafe.Pointer(&(*C.PyTupleObject)(unsafe.Pointer(ret)).ob_item[0])
	step := uintptr(C.tupleItemSize())

	for _, item := range items {
		item.Incref()
		*(**C.PyObject)(base) = c(item)

		// Move base to point to the next item, by incrementing by step bytes
		base = unsafe.Pointer(uintptr(base) + step)
	}

	return newTuple(ret), nil
}
开发者ID:xushiwei,项目名称:gopy,代码行数:32,代码来源:tuple.go

示例9: interfaceToPyObj

func interfaceToPyObj(o interface{}) *C.PyObject {
	switch o.(type) {
	case int:
		return C.PyInt_FromLong(C.long(o.(int)))
	case int64:
		return C.PyInt_FromLong(C.long(o.(int64)))
	case string:
		strvalue := C.CString(o.(string))
		defer C.free(unsafe.Pointer(strvalue))
		return C.PyString_FromStringAndSize(strvalue, C.Py_ssize_t(len(o.(string))))
	case map[interface{}]interface{}:
		dict := C.PyDict_New()
		for key, value := range o.(map[interface{}]interface{}) {
			dictAddItem(dict, key, value)
		}
		return dict
	case map[string]string:
		dict := C.PyDict_New()
		for key, value := range o.(map[string]string) {
			dictAddItem(dict, key, value)
		}
		return dict
	case map[string]interface{}:
		dict := C.PyDict_New()
		for key, value := range o.(map[string]interface{}) {
			dictAddItem(dict, key, value)
		}
		return dict
	default:
		return C.PyNone()
	}
}
开发者ID:pandemicsyn,项目名称:bertin,代码行数:32,代码来源:pickle.go

示例10: NewList

// NewList creates a new Python List instance.  The created list has initial
// length "size".
//
// Note: If size > 0, then the objects in the returned list are initialised to
// nil.  Thus you cannot use Abstract API functions, or expose the object to
// Python code without first filling in all the created slots with
// list.SetItem().
//
// Return value: New Reference.
func NewList(size int64) (*List, error) {
	ret := C.PyList_New(C.Py_ssize_t(size))
	if ret == nil {
		return nil, exception()
	}
	return newList(ret), nil
}
开发者ID:ericsnowcurrently,项目名称:qur-gopy,代码行数:16,代码来源:list.go

示例11: decodeMapping

// decodeMapping translates a Python object to a Go map.
func decodeMapping(pyMapping *C.PyObject) (mapping map[interface{}]interface{}, err error) {
	mapping = make(map[interface{}]interface{})

	pyItems := C.Mapping_Items(pyMapping)
	if pyItems == nil {
		err = getError()
		return
	}

	length := int(C.PyList_Size(pyItems))

	for i := 0; i < length; i++ {
		pyPair := C.PyList_GetItem(pyItems, C.Py_ssize_t(i))

		var (
			key   interface{}
			value interface{}
		)

		if key, err = decode(C.PyTuple_GetItem(pyPair, 0)); err != nil {
			return
		}

		if value, err = decode(C.PyTuple_GetItem(pyPair, 1)); err != nil {
			return
		}

		mapping[key] = value
	}

	return
}
开发者ID:tsavola,项目名称:go-python,代码行数:33,代码来源:python.go

示例12: PyMarshal_ReadObjectFromString

// PyObject* PyMarshal_ReadObjectFromString(char *string, Py_ssize_t len)
// Return value: New reference.
// Return a Python object from the data stream in a character buffer containing len bytes pointed to by string. On error, sets the appropriate exception (EOFError or TypeError) and returns NULL.
//
// Changed in version 2.5: This function used an int type for len. This might require changes in your code for properly supporting 64-bit systems.
func PyMarshal_ReadObjectFromString(str string) *PyObject {
	//FIXME: use []byte ?
	c_str := C.CString(str)
	defer C.free(unsafe.Pointer(c_str))

	return togo(C.PyMarshal_ReadObjectFromString(c_str, C.Py_ssize_t(len(str))))
}
开发者ID:hoangpq,项目名称:go-python,代码行数:12,代码来源:utilities.go

示例13: PyByteArray_FromStringAndSize

// PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len)
// Create a new bytearray object from string and its length, len. On failure, NULL is returned.
func PyByteArray_FromStringAndSize(str string) *PyObject {
	//FIXME use []byte instead ?
	c_str := C.CString(str)
	defer C.free(unsafe.Pointer(c_str))

	return togo(C.PyByteArray_FromStringAndSize(c_str, C.Py_ssize_t(len(str))))
}
开发者ID:gward,项目名称:go-python,代码行数:9,代码来源:sequence.go

示例14: goClassSeqLength

//export goClassSeqLength
func goClassSeqLength(obj unsafe.Pointer) C.Py_ssize_t {
	ctxt := getClassContext(obj)

	// Turn the function into something we can call
	f := (*func(unsafe.Pointer) int64)(unsafe.Pointer(&ctxt.sq_length))

	return C.Py_ssize_t((*f)(obj))
}
开发者ID:ericsnowcurrently,项目名称:qur-gopy,代码行数:9,代码来源:class_sequence.go

示例15: NewUnicode

func NewUnicode(s string) (*Unicode, error) {
	cs := C.CString(s)
	defer C.free(unsafe.Pointer(cs))
	ret := C.PyUnicode_FromStringAndSize(cs, C.Py_ssize_t(len(s)))
	if ret == nil {
		return nil, exception()
	}
	return newUnicode(ret), nil
}
开发者ID:gbbr,项目名称:textmate,代码行数:9,代码来源:unicode.go


注:本文中的C.Py_ssize_t函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。