Skip to content
Snippets Groups Projects
Commit d8af4afe authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

Contexts are experimental

Change-Id: I450ace29716a81eacb1b1c5b9326cada9ff516c4
parent c58184c0
No related branches found
No related tags found
No related merge requests found
# v2.24.2 # v2.24.2
* added DeferredValue * added DeferredValue
* satella.coding.Context are considered experimental
__version__ = '2.24.2a2' __version__ = '2.24.2a3'
...@@ -2,6 +2,7 @@ from __future__ import annotations ...@@ -2,6 +2,7 @@ from __future__ import annotations
import threading import threading
import typing as tp import typing as tp
import warnings
from satella.coding.typing import V from satella.coding.typing import V
...@@ -13,9 +14,12 @@ THEY_HATIN = object() ...@@ -13,9 +14,12 @@ THEY_HATIN = object()
class Context: class Context:
""" """
New layer of environment. Can have it's own variables, or can hoist them onto the parent. New layer of environment. Can have it's own variables, or can hoist them onto the parent.
.. warning:: This is considered experimental. I just haven't found out a good use case for it yet.
""" """
def __init__(self, parent: tp.Optional[Context] = None, **variables): def __init__(self, parent: tp.Optional[Context] = None, **variables):
warnings.warn('This is experimental', RuntimeWarning)
self.parent = parent self.parent = parent
self.variables = variables self.variables = variables
self.bool = None self.bool = None
......
import threading
import unittest import unittest
from satella.coding.environment import Context from satella.coding.environment import Context
...@@ -12,6 +13,19 @@ class TestEnvs(unittest.TestCase): ...@@ -12,6 +13,19 @@ class TestEnvs(unittest.TestCase):
with Context() as new_ctxt: with Context() as new_ctxt:
self.assertEqual(new_ctxt.value, 5) self.assertEqual(new_ctxt.value, 5)
def test_threads(self):
ctxt = Context.get()
ctxt.value = 2
with ctxt:
def thread():
with Context() as new_ctxt:
self.assertEqual(new_ctxt.value, 2)
new_ctxt.value2 = 2
thr = threading.Thread(target=thread)
thr.start()
thr.join()
self.assertRaises(AttributeError, lambda: ctxt.value2)
def test_push(self): def test_push(self):
ctxt = Context.get() ctxt = Context.get()
ctxt.value = 5 ctxt.value = 5
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment