Skip to content

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

@gelldur

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 embedding

namespace py = pybind11;

int main()
{
	py::scoped_interpreter guard{}; // start the interpreter and keep it alive

	py::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")
    )");
	return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions