Skip to content

Interpreter objects should support the context manager protocol #158370

Description

@ZeroIntensity

Feature or enhancement

Proposal:

When using subinterpreters, it's common to write code like this:

interp = interpreters.create()
try:
    ...  # Do super cool stuff with the interpreter
finally:
    interp.close()

This pattern is particularly common in our test suite.

Lucky for us, Python has had a feature for ~20 years that simplifies this case (with!). Let's support that with subinterpreters by implementing the context manager protocol for Interpreter objects, like so:

with interpreters.create() as interp:
    ...  # Do something with 'interp'

cc @ericsnowcurrently as the (concurrent.)interpreters mastermind

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytopic-subinterpreterstype-featureA feature request or enhancement

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions