Пример #1
0
// void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
// Retrieve the pointer stored in the capsule. On failure, set an exception and return NULL.
//
// The name parameter must compare exactly to the name stored in the capsule. If the name stored in the capsule is NULL, the name passed in must also be NULL. Python uses the C function strcmp() to compare capsule names.
func PyCapsule_GetPointer(capsule *PyObject, name string) *C.char {

	c_name := C.CString(name)
	defer C.free(unsafe.Pointer(c_name))

	//FIXME use an interface{} instead of *C.char ?
	ptr := C.PyCapsule_GetPointer(topy(capsule), c_name)
	return (*C.char)(ptr)
}
Пример #2
0
//export goClassGetProp
func goClassGetProp(obj, closure unsafe.Pointer) unsafe.Pointer {
	// Unpack set function from closure
	t := (*C.PyObject)(closure)
	m := C.PyCapsule_GetPointer(C.PyTuple_GetItem(t, 0), nil)

	// Turn the function into something we can call
	f := (*func(p unsafe.Pointer) (Object, error))(unsafe.Pointer(&m))

	ret, err := (*f)(obj)
	if err != nil {
		raise(err)
		return nil
	}

	return unsafe.Pointer(c(ret))
}
Пример #3
0
//export goClassCallMethod
func goClassCallMethod(obj, unused unsafe.Pointer) unsafe.Pointer {
	// Unpack context and self pointer from obj
	t := (*C.PyObject)(obj)
	pyobj := unsafe.Pointer(C.PyTuple_GetItem(t, 0))
	m := C.PyCapsule_GetPointer(C.PyTuple_GetItem(t, 1), nil)

	// Now call the actual struct method by pulling the method out of the
	// reflect.Type object stored in the context
	f := (*func(p unsafe.Pointer) (Object, error))(unsafe.Pointer(&m))

	ret, err := (*f)(pyobj)
	if err != nil {
		raise(err)
		return nil
	}

	return unsafe.Pointer(c(ret))
}
Пример #4
0
//export goClassSetProp
func goClassSetProp(obj, arg, closure unsafe.Pointer) int {
	// Unpack set function from closure
	t := (*C.PyObject)(closure)
	m := C.PyCapsule_GetPointer(C.PyTuple_GetItem(t, 1), nil)

	// Turn arg into something usable
	a := newObject((*C.PyObject)(arg))

	// Turn the function into something we can call
	f := (*func(p unsafe.Pointer, a Object) error)(unsafe.Pointer(&m))

	err := (*f)(obj, a)
	if err != nil {
		raise(err)
		return -1
	}

	return 0
}
Пример #5
0
//export goClassCallMethodArgs
func goClassCallMethodArgs(obj, args unsafe.Pointer) unsafe.Pointer {
	// Unpack context and self pointer from obj
	t := (*C.PyObject)(obj)
	pyobj := unsafe.Pointer(C.PyTuple_GetItem(t, 0))
	m := C.PyCapsule_GetPointer(C.PyTuple_GetItem(t, 1), nil)

	// Get args ready to use, by turning it into a pointer of the appropriate
	// type
	a := newTuple((*C.PyObject)(args))

	// Now call the actual struct method by pulling the method out of the
	// reflect.Type object stored in the context
	f := (*func(p unsafe.Pointer, a *Tuple) (Object, error))(unsafe.Pointer(&m))

	ret, err := (*f)(pyobj, a)
	if err != nil {
		raise(err)
		return nil
	}

	return unsafe.Pointer(c(ret))
}