#158370·Python

`Interpreter` objects should support the context manager protocol

Author: ZeroIntensityCreated Sep 28, 2026Updated Sep 28, 2026
Labelstype-featurestdlibtopic-subinterpreters

Feature or enhancement

Proposal:

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

python
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:

python
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

  • gh-158372