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 }
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 }