Skip to content

Segfault when functions access objects outside closure #297

Description

@brandonchinn178

#232 added support for Python functions, but it doesn't properly capture objects within the closure.

from starlark_go import *

def global_foo(*args):
    print(args)

# works
Starlark(globals={"foo": global_foo}).exec("foo()")
Starlark(globals={"foo": global_foo}).exec("foo(1)")
Starlark(globals={"foo": global_foo}).exec("foo(1,2)")

def mk_foo():
    def foo(*args):
        print(args)
    return foo

# segfaults
Starlark(globals={"foo": mk_foo()}).exec("foo()")
Starlark(globals={"foo": mk_foo()}).exec("foo(1)")
Starlark(globals={"foo": mk_foo()}).exec("foo(1,2)")

Probably not easy to fix, but at least we can track the issue here.

Workaround is to add the objects to some global state, e.g.

_FOOS=[]
def mk_foo():
    def foo(*args):
        print(args)
    _FOOS.append(foo)
    return foo

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions