From e8a06c9a790f39451d9e99e203b13b3ad73a1d01 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Sat, 12 Aug 2017 01:36:43 -0400 Subject: [PATCH] pep-550: Fix PEP 521 example. (#337) --- pep-0550.rst | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pep-0550.rst b/pep-0550.rst index 98970880f..23b27c69a 100644 --- a/pep-0550.rst +++ b/pep-0550.rst @@ -942,30 +942,32 @@ The following code:: would become this:: + local = threading.local() + class Context: def __enter__(self): - self.old_x = get_execution_context_item('x') - set_execution_context_item('x', 'something') + self.old_x = getattr(local, 'x', None) + local.x = 'something' def __suspend__(self): - set_execution_context_item('x', self.old_x) + local.x = self.old_x def __resume__(self): - set_execution_context_item('x', 'something') + local.x = 'something' def __exit__(self, *err): - set_execution_context_item('x', self.old_x) + local.x = self.old_x Besides complicating the protocol, the implementation will likely negatively impact performance of coroutines, generators, and any code that uses context managers, and will notably complicate the -interpreter implementation. It also does not solve the leaking state -problem for greenlet/gevent. +interpreter implementation. :pep:`521` also does not provide any mechanism to propagate state in a local context, like storing a request object in an HTTP request -handler to have better logging. +handler to have better logging. Nor does it solve the leaking state +problem for greenlet/gevent. Can Execution Context be implemented outside of CPython?