diff --git a/satella/__init__.py b/satella/__init__.py index 9015163e78de0fadeee4903d6861cdef1a2e1f84..fe1f0f4e4a80453c535196f40843cb533ddb9c13 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.22.0rc2' +__version__ = '2.22.0' diff --git a/satella/coding/environment.py b/satella/coding/environment.py index c0482a1d344dcc9156e5ec8f4309b61ad45cc906..a871ac4a2bd128dd75f71820bb5ea98a04108b8d 100644 --- a/satella/coding/environment.py +++ b/satella/coding/environment.py @@ -1,14 +1,12 @@ from __future__ import annotations -import typing as tp -import threading -from satella.coding.typing import V -from .misc import Closeable +import threading +import typing as tp +from satella.coding.typing import V local = threading.local() - THEY_HATIN = object() @@ -19,19 +17,23 @@ class Context: def __init__(self, parent: tp.Optional[Context] = None, **variables): self.parent = parent - self.variables = {} + self.variables = variables self.bool = None def __str__(self): return str(id(self)) - def push_up(self, item: str) -> None: + def push_up(self, item: str, value=THEY_HATIN) -> None: """ Advance current variable to the top of the card stack. :param item: variable name + :param value: if not given, current value of given variable will be taken """ - var = self.variables.pop(item) + if value is THEY_HATIN: + var = self.variables.pop(item) + else: + var = value self.parent.variables[item] = var def __getattr__(self, item: str): @@ -51,8 +53,7 @@ class Context: except AttributeError: parent = None ctxt = Context(parent=parent) - if ctxt is not parent: - ctxt.parent = parent + ctxt.parent = parent local.thread_context = ctxt return ctxt diff --git a/satella/debug/test_environment.py b/satella/debug/test_environment.py index c6c743466e18bd7ce4d306feae5037c495b997c3..361a6156116de941299286e1865ad4f5de3c9521 100644 --- a/satella/debug/test_environment.py +++ b/satella/debug/test_environment.py @@ -12,6 +12,18 @@ class TestEnvs(unittest.TestCase): with Context() as new_ctxt: self.assertEqual(new_ctxt.value, 5) + def test_push(self): + ctxt = Context.get() + ctxt.value = 5 + self.assertEqual(ctxt.value, 5) + with Context() as new_ctxt: + self.assertEqual(new_ctxt.value, 5) + with Context() as new_ctxt2: + new_ctxt2.push_up('value', 6) + self.assertEqual(new_ctxt.value, 6) + + self.assertEqual(ctxt.value, 5) + def test_delete_envs(self): ctxt = Context.get() ctxt.value = 5