Skip to content

builtins not available by default in py::eval #1091

Closed
@anntzer

Description

@anntzer

Issue description

builtins are not available by default when using py::eval, i.e. when using an empty dict as scope. Instead, it is necessary to pass e.g. a module's __dict__, which is actually semantically weird because a module's __dict__ does NOT, in fact, contain the builtins (as can be checked manually).

Instead, py::eval should just use the same semantics as the builtin eval, i.e. builtins.__dict__ should always be available in the execution scope, regardless of what gets passed in as globals and locals.

The same remark applies to py::exec, of course.

Reproducible example code

Starting from the python_example

#include <pybind11/pybind11.h>
#include <pybind11/eval.h>

namespace py = pybind11;

PYBIND11_MODULE(python_example, m) {
    // auto scope = py::module::import("__main__").attr("__dict__");  // this works
    auto scope = py::dict{};  // this crashes Py3.6 at import time with a SystemError
    py::eval("print(123)", scope);
}

build, and try to import python_example.


#887 appears to be a similar, but different issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions