Пример #1
0
func InitModule(name string, methods []Method) (*Module, error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	cm := C.PyImport_AddModule(cname)
	if cm == nil {
		return nil, exception()
	}
	m := newModule(cm)
	if len(methods) == 0 {
		return m, nil
	}
	defer m.Decref()
	n := C.PyUnicode_FromString(cname)
	if n == nil {
		return nil, exception()
	}

	for _, method := range methods {
		pyF, err := makeCFunction(method.Name, method.Func, method.Doc, n)
		if err != nil {
			return nil, err
		}
		if m.AddObject(method.Name, pyF); err != nil {
			return nil, err
		}
	}
	m.Incref()
	return m, nil
}
Пример #2
0
//export analyze_images
func analyze_images(self, args *C.PyObject) *C.PyObject {
	var inputDir *C.char
	if C.PyArg_ParseTuple_S(args, &inputDir) == 0 {
		return nil
	}
	analysis := mosaic.AnalyzeAll(C.GoString(inputDir))
	return C.PyUnicode_FromString(C.CString(analysis))
}
Пример #3
0
func NewUnicode(s string) (*Unicode, error) {
	cs := C.CString(s)
	defer C.free(unsafe.Pointer(cs))
	ret := C.PyUnicode_FromString(cs)
	if ret == nil {
		return nil, exception()
	}
	return newUnicode(ret), nil
}
Пример #4
0
func Import(name string) (*Module, error) {
	s := C.CString(name)
	defer C.free(unsafe.Pointer(s))

	pyName := C.PyUnicode_FromString(s)
	defer C.decref(pyName)

	obj := C.PyImport_Import(pyName)
	if obj == nil {
		return nil, exception()
	}

	return newModule(obj), nil
}
Пример #5
0
func AddToPath(dir string) {
	p := C.CString("path")
	defer C.free(unsafe.Pointer(p))

	sys_path := C.PySys_GetObject(p)
	if sys_path == nil {
		return
	}

	s := C.CString(dir)
	defer C.free(unsafe.Pointer(s))

	pDir := C.PyUnicode_FromString(s)
	if pDir == nil {
		return
	}

	C.PyList_Append(sys_path, pDir)
}