Content-Length: 221559 | pFad | https://github.com/python/cpython/issues/92203

0B Add closure support to exec() · Issue #92203 · python/cpython · GitHub
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 closure support to exec() #92203

Closed
larryhastings opened this issue May 2, 2022 · 1 comment
Closed

Add closure support to exec() #92203

larryhastings opened this issue May 2, 2022 · 1 comment
Labels
type-feature A feature request or enhancement

Comments

@larryhastings
Copy link
Contributor

Feature or enhancement

exec() can run either a string or a code object. Usually the code object run in exec() is produced directly using compile(). But it's completely fine to pull the code object out of a function object and execute it with exec(). This can be useful if you want to run the code object in some exotic environment, e.g. with a modified globals.

Some code objects refer to free variables, and can only run when a closure is set. Currently, exec() simply rejects these code objects--you aren't permitted to use them with exec(), it throws an exception if you try.

It's possible to work around this limitation. Instead of using exec(), one could simply bind a new function using the code object and whatever closure or globals you wanted. But this seems like an unnecessary limitation on the functionality of exec().

I propose to add a closure keyword-only argument to eval(). It can only be specified if the code object uses free variables. When specified, it must be a tuple, with exactly the number of cell variables referenced by the code object. closure has a default value of None, and it must be None if the code object doesn't refer to any free variables.

We do have a use case for this, in conjunction with PEP 649. Carl Meyer proposes to pull the code object out of a co_annotations function, and run it with a globals object that enables us to handle undefined (forward-defined) names.

@larryhastings larryhastings added the type-feature A feature request or enhancement label May 2, 2022
larryhastings added a commit to larryhastings/cpython that referenced this issue May 2, 2022
larryhastings added a commit that referenced this issue May 6, 2022
Add a closure keyword-only parameter to exec(). It can only be specified when exec-ing a code object that uses free variables. When specified, it must be a tuple, with exactly the number of cell variables referenced by the code object. closure has a default value of None, and it must be None if the code object doesn't refer to any free variables.
@larryhastings
Copy link
Contributor Author

Next-to-last commit for v3.11.0b1!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/python/cpython/issues/92203

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy