1# This file is part of Hypothesis, which may be found at
2# https://github.com/HypothesisWorks/hypothesis/
3#
4# Copyright the Hypothesis Authors.
5# Individual contributors are listed in AUTHORS.rst and the git log.
6#
7# This Source Code Form is subject to the terms of the Mozilla Public License,
8# v. 2.0. If a copy of the MPL was not distributed with this file, You can
9# obtain one at https://mozilla.org/MPL/2.0/.
10
11import threading
12from contextlib import contextmanager
13
14
15class DynamicVariable:
16 def __init__(self, default):
17 self.default = default
18 self.data = threading.local()
19
20 @property
21 def value(self):
22 return getattr(self.data, "value", self.default)
23
24 @value.setter
25 def value(self, value):
26 self.data.value = value
27
28 @contextmanager
29 def with_value(self, value):
30 old_value = self.value
31 try:
32 self.data.value = value
33 yield
34 finally:
35 self.data.value = old_value