Skip to content

Globals not set by default for eval() when embedding #887

Description

@gelldur

Issue description

Example from documentation isn't working
http://pybind11.readthedocs.io/en/master/advanced/embedding.html#getting-started

 py::exec(R"(
kwargs = dict(name="World", number=42)
message = "Hello, {name}! The answer is {number}".format(**kwargs)
print(message)
)");

Error:

terminate called after throwing an instance of 'pybind11::error_already_set'
what(): NameError: name 'dict' is not defined
At:
<string>(3): <module>

This also isn't working:

	py::exec(R"(
#kwargs = {'name':"World", 'number':42}
#message = "Hello, {name}! The answer is {number}".format(**kwargs)
print("Hello")
)");

Error:

terminate called after throwing an instance of 'pybind11::error_already_set'
what(): NameError: name 'print' is not defined
At:
<string>(5): <module>

Reproducible example code

#include<iostream>
#include<pybind11/embed.h>// everything needed for embeddingnamespacepy= pybind11;
intmain()
{
py::scoped_interpreter guard{}; // start the interpreter and keep it alivepy::exec(R"( kwargs = dict(name="World", number=42) message = "Hello, {name}! The answer is {number}".format(**kwargs) print(message))");
py::exec(R"( #kwargs = {'name':"World", 'number':42} #message = "Hello, {name}! The answer is {number}".format(**kwargs) print("Hello"))");
return0;
}

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions