Skip to content

Conversation

@kiranandcode
Copy link
Contributor

Building on top of #519, this provides another handler for parse, eval, compile that calls out to RestrictedPython instead to provide some safety. Closing #515, The core of this implementation is:

class RestrictedEvalProvider(ObjectInterpretation):
     ...
    @implements(compile)
    def compile(self, module: ast.Module, filename: str) -> CodeType:
        return compile_restricted(module, filename=filename, mode="exec")

    @implements(exec)
    def exec(self, bytecode: CodeType, env: dict[str, Any]) -> None:
        rglobals = safe_globals.copy()
        for k, v in env.items():
            if k != "__builtins__":
                rglobals[k] = v
        builtins.exec(bytecode, rglobals, env)

parse is the same as UnsafeEval. The tests have been updated to be parametric in the eval provider and test RestrictedEval as well.

@kiranandcode kiranandcode changed the title EvalProvider using RestrictedPython EvalProvider using RestrictedPython [Depends on #519] Jan 28, 2026
@kiranandcode kiranandcode linked an issue Jan 28, 2026 that may be closed by this pull request
Copy link
Contributor

@eb8680 eb8680 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good once you address the dependency comment

@eb8680 eb8680 merged commit 10a4ebd into master Jan 31, 2026
6 checks passed
@eb8680 eb8680 deleted the kg-restricted-eval branch January 31, 2026 03:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RestrictedPythonEval handler for program synthesis

2 participants