Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of Hypothesis, which may be found at 

2# https://github.com/HypothesisWorks/hypothesis/ 

3# 

4# Most of this work is copyright (C) 2013-2020 David R. MacIver 

5# (david@drmaciver.com), but it contains contributions by others. See 

6# CONTRIBUTING.rst for a full list of people who may hold copyright, and 

7# consult the git log if you need to determine who owns an individual 

8# contribution. 

9# 

10# This Source Code Form is subject to the terms of the Mozilla Public License, 

11# v. 2.0. If a copy of the MPL was not distributed with this file, You can 

12# obtain one at https://mozilla.org/MPL/2.0/. 

13# 

14# END HEADER 

15 

16import threading 

17from contextlib import contextmanager 

18 

19 

20class DynamicVariable: 

21 def __init__(self, default): 

22 self.default = default 

23 self.data = threading.local() 

24 

25 @property 

26 def value(self): 

27 return getattr(self.data, "value", self.default) 

28 

29 @value.setter 

30 def value(self, value): 

31 self.data.value = value 

32 

33 @contextmanager 

34 def with_value(self, value): 

35 old_value = self.value 

36 try: 

37 self.data.value = value 

38 yield 

39 finally: 

40 self.data.value = old_value