Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __builtins__ to globals argument of py::exec and py::eval if not present #2616

Merged
merged 2 commits into from
Oct 27, 2020

Conversation

YannickJadoul
Copy link
Collaborator

@YannickJadoul YannickJadoul commented Oct 23, 2020

Description

Python (even 2.7!) adds __builtins__ when calling exec or eval:

Python 3.6.9 (default, Oct  8 2020, 12:12:24) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {}
>>> d.keys()
dict_keys([])
>>> exec("print(42)", d)
42
>>> d.keys()
dict_keys(['__builtins__'])
Python 2.7.17 (default, Sep 30 2020, 13:38:04) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {}
>>> exec("print 42", d)
42
>>> d.keys()
['__builtins__']

For the C API's PyRun_String, this only happens since Python 3.8 (python/cpython#13362; see #1091 (comment)). It would make sense to me to backport/downport/sideport/port this to pybind11 on all versions of Python, for the sake of consistency. No?

Closes #1091
Closes #2557
Closes #1654 (thanks, @bstaletic!)

Suggested changelog entry:

`py::exec`, `py::eval`, and `py::eval_file` now add the builtins module as ``"__builtins__"`` to their ``globals`` argument, better matching `exec` and `eval` in pure Python.

@YannickJadoul YannickJadoul added this to the v2.6.1 milestone Oct 23, 2020
@bstaletic
Copy link
Collaborator

Fixes #1654 as well.

@martinRenou
Copy link
Contributor

Thanks a lot @YannickJadoul

Copy link
Member

@wjakob wjakob left a comment

Choose a reason for hiding this comment

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

Looks great!

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

This looks good to me as-is, but what do you think about moving the new code into inline void detail::ensure_global_contains_builtins()? That way you'd only need one #if (inside that function).

@YannickJadoul
Copy link
Collaborator Author

what do you think about moving the new code into inline void detail::ensure_global_contains_builtins()

I'm not entirely convinced on which side to pick here. On a high level, definitely, avoid code duplication. In practice ... I'm a bit in doubt whether it looks better. But I guess it also doesn't look worse.

@YannickJadoul
Copy link
Collaborator Author

One or two reviews of this last commit, please?

@YannickJadoul YannickJadoul merged commit 3a37d33 into master Oct 27, 2020
@YannickJadoul YannickJadoul deleted the exec-eval-builtins branch October 27, 2020 18:58
@YannickJadoul
Copy link
Collaborator Author

Thanks, all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants