Skip to content

Allow eval and exec to take keyword arguments #105879

Description

@rtpg

Feature or enhancement

Allow for globals and locals to be passed in as keyword arguments to exec and eval.

Pitch

exec and eval both can take globals and locals as positional arguments.
The built-in functions documentation shows the argument defaults, and every time I have the documentation open and am writing these, I default to keyword arguments, and it fails because the arguments are positional-only.

For functions like this that might only be present in a handful of places, kwargs give a good way to document what parameters each argument is referring to. I don't think that in a universe in which we were introducing eval/exec now that we would explicitly disallow kwargs.

Previous discussion

I sent this email to python-ideas

Extra Links

Here is an implementation, which just consists in changing the argument clinic config and rerunning. I set up some very basic kwarg tests mostly to test the case of "locals provided but globals not provided", but fortunately the existing implementations already handled this cleanly!

Linked PRs

Activity

  1. changed the title [-]Alow eval and exec to take keyword arguments[/-] [+]Allow eval and exec to take keyword arguments[/+] on Jun 17, 2023
  2. sunmy2019 commented on Jun 17, 2023

    @sunmy2019
    Member

    My concern is, will this hurt performance a lot?

  3. JelleZijlstra commented on Jun 17, 2023

    @JelleZijlstra
    Member

    We can benchmark it, but that seems unlikely; parsing code is probably a lot slower than accepting some keyword arguments.

  4. rtpg commented on Jun 17, 2023

    @rtpg
    ContributorAuthor

    @sunmy2019 I added a link to a commit implementing this. The short version is eval now has a bit more parsing code, but exec already has support for one kwarg, so we're still looking at the same order of magnitude of instructions or existing code IMO.

  5. JelleZijlstra commented on Jun 17, 2023

    @JelleZijlstra
    Member

    Make a PR :)

  6. rtpg commented on Jun 19, 2023

    @rtpg
    ContributorAuthor

    Created the PR, and tests seem to be passing now

  7. self-assigned this
    on May 1, 2024
  8. added a commit that references this issue on May 2, 2024
  9. JelleZijlstra commented on May 2, 2024

    @JelleZijlstra
    Member

    This will be in Python 3.13, thank you!

  10. added a commit that references this issue on May 8, 2024
  11. added a commit that references this issue on Jul 16, 2024
  12. added a commit that references this issue on Jul 16, 2024
  13. added a commit that references this issue on Jul 16, 2024
  14. added a commit that references this issue on Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions