From 1e260e7a1e0c47f855a9a6d89f1dbc414a537c9b Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Fri, 11 Aug 2017 19:13:45 -0400 Subject: [PATCH] pep-550: Add a section about context isolation (#331) --- pep-0550.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pep-0550.rst b/pep-0550.rst index 5948ceeb4..dc642ef6e 100644 --- a/pep-0550.rst +++ b/pep-0550.rst @@ -968,6 +968,27 @@ trampoline, making it impossible to intercept their ``yield`` points outside of the Python interpreter. +Is it possible to write a context manager to isolate EC changes? +---------------------------------------------------------------- + +Yes! + +:: + + @contextlib.contextmanager + def isolated_context(): + old_ctx = sys.get_execution_context() + try: + yield + finally: + sys.set_execution_context(old_ctx) + + + with isolated_context(): + # Any Execution Context changes will not be visible + # outside of this block. + + Reference Implementation ========================