Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
func pyRun(args *py.Tuple, kw *py.Dict) (py.Object, error) {
	command := ""
	var bg py.Object
	var argO py.Object

	kwlist := []string{"cmd", "args", "bg"}

	err := py.ParseTupleAndKeywords(args, kw, "s|OO", kwlist, &command, &argO, &bg)
	if err != nil {
		return nil, err
	}

	var argS []py.Object

	switch a := argO.(type) {
	case *py.Tuple:
		argS = a.Slice()
	case *py.List:
		argS = a.Slice()
	default:
		return nil, py.TypeError.Err("args must be tuple or list")
	}

	cArgs := make([]string, len(argS))
	for i, obj := range argS {
		str, err := obj.Base().Str()
		if err != nil {
			return nil, err
		}
		pyS := str.(*py.String)
		cArgs[i] = pyS.String()
	}

	if bg == nil {
		bg = py.False
	}

	err = runCmd(command, cArgs, bg.IsTrue())
	if err != nil {
		return nil, err
	}

	py.None.Incref()
	return py.None, nil
}