caketop / python-starlark-go

🐍 Python bindings for starlark-go 🐍
https://python-starlark-go.readthedocs.io/
Apache License 2.0
24 stars 8 forks source link

Support builtin fail() function #231

Closed brandonchinn178 closed 5 months ago

brandonchinn178 commented 5 months ago

It'd be nice to be able to throw an error and get a stack trace in Starlark. Even if #230 isn't feasible, could a hardcoded fail function be implemented? Something like:

state.Globals["fail"] = starlark.NewBuiltin(
    "fail",
    func(thread *Thread, fn *Builtin, args Tuple, kwargs []Tuple) (starlark.Value, error) {
        var msg string
        err = UnpackPositionalArgs("fail", args, kwargs, 1, &msg)
        if err != nil {
            return nil, err
        }
        return nil, fmt.Errorf(msg)
    },
)
brandonchinn178 commented 5 months ago

I'm dumb, fail() does work today