-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Description
Issue description
A lambda function constructed with a certain scope (local and global) from within py::eval cannot be called, as the scope is unavailable when the function is called.
Reproducible example code
#include <pybind11/embed.h>
namespace py = pybind11;
int main(int, char**) {
py::scoped_interpreter guard{};
auto global = py::dict(py::module::import("__main__").attr("__dict__"));
auto local = py::dict();
local["x"] = 42;
py::function f = py::eval("lambda: x", global, local);
py::print("defined f");
f();
py::print("called f");
}Output:
defined f
terminate called after throwing an instance of 'pybind11::error_already_set'
what(): NameError: name 'x' is not defined
Initial discussion
The problem is that the local variable a is not available when f is called; in fact, f does not seem to have a scope.
Note that the following works in Python:
def make_f():
a=42
return eval('lambda: a')
a=123
print(make_f()()) # outputs 123I have no idea how, but it would be great if there was a way to provide a scope to py::functions to make above example work.
bilke
Metadata
Metadata
Assignees
Labels
No labels