示例#1
0
文件: python.go 项目: xushiwei/gopy
func callPyCmd(fn py.Object, args []string) error {
	lock := py.NewLock()
	defer lock.Unlock()

	pyArgs, err := py.NewTuple(int64(len(args)))
	if err != nil {
		return nil
	}
	defer pyArgs.Decref()

	for i, arg := range args {
		s, err := py.NewString(arg)
		if err != nil {
			return err
		}
		err = pyArgs.SetItem(int64(i), s)
		if err != nil {
			s.Decref()
			return err
		}
	}

	ret, err := fn.Base().CallObject(pyArgs)
	if err != nil {
		return err
	}
	ret.Decref()

	return nil
}
示例#2
0
文件: python.go 项目: xushiwei/gopy
func tryPyFile(cmd, filename string) (CmdFunc, error) {
	lock := py.NewLock()
	defer lock.Unlock()

	code, err := py.CompileFile(filename)
	if err != nil {
		return nil, err
	}

	mod, err := py.ExecCodeModule("sh.__cmds__."+cmd, code)
	if err != nil {
		return nil, err
	}

	fn, err := mod.Base().GetAttrString(cmd)
	if err != nil {
		return nil, err
	}

	return func(args []string) error { return callPyCmd(fn, args) }, nil
}