|
@@ -48,16 +48,16 @@ class _LocalsContext:
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
def set_locals_context(self, context: t.Optional[str]) -> t.Iterator[None]:
|
|
|
- stack_count = len(self._lc_stack)
|
|
|
+ has_set_context = False
|
|
|
try:
|
|
|
if context in self._locals_map:
|
|
|
if hasattr(g, _LocalsContext.__ctx_g_name):
|
|
|
self._lc_stack.append(getattr(g, _LocalsContext.__ctx_g_name))
|
|
|
setattr(g, _LocalsContext.__ctx_g_name, context)
|
|
|
+ has_set_context = True
|
|
|
yield
|
|
|
finally:
|
|
|
- # Only reset the stack if `stack_count` has been changed, meaning the context has been set above in the try statement
|
|
|
- if hasattr(g, _LocalsContext.__ctx_g_name) and stack_count == (len(self._lc_stack) - 1):
|
|
|
+ if has_set_context and hasattr(g, _LocalsContext.__ctx_g_name):
|
|
|
if len(self._lc_stack) > 0:
|
|
|
setattr(g, _LocalsContext.__ctx_g_name, self._lc_stack.pop())
|
|
|
else:
|