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
11from weakref import WeakKeyDictionary
12
13from hypothesis.control import note
14from hypothesis.errors import InvalidState
15from hypothesis.internal.reflection import (
16 convert_positional_arguments,
17 nicerepr,
18 proxies,
19 repr_call,
20)
21from hypothesis.strategies._internal.strategies import SearchStrategy
22
23
24class FunctionStrategy(SearchStrategy):
25 supports_find = False
26
27 def __init__(self, like, returns, pure):
28 super().__init__()
29 self.like = like
30 self.returns = returns
31 self.pure = pure
32 # Using wekrefs-to-generated-functions means that the cache can be
33 # garbage-collected at the end of each example, reducing memory use.
34 self._cache = WeakKeyDictionary()
35
36 def calc_is_empty(self, recur):
37 return recur(self.returns)
38
39 def do_draw(self, data):
40 @proxies(self.like)
41 def inner(*args, **kwargs):
42 if data.frozen:
43 raise InvalidState(
44 f"This generated {nicerepr(self.like)} function can only "
45 "be called within the scope of the @given that created it."
46 )
47 if self.pure:
48 args, kwargs = convert_positional_arguments(self.like, args, kwargs)
49 key = (args, frozenset(kwargs.items()))
50 cache = self._cache.setdefault(inner, {})
51 if key not in cache:
52 cache[key] = data.draw(self.returns)
53 rep = repr_call(self.like, args, kwargs, reorder=False)
54 note(f"Called function: {rep} -> {cache[key]!r}")
55 return cache[key]
56 else:
57 val = data.draw(self.returns)
58 rep = repr_call(self.like, args, kwargs, reorder=False)
59 note(f"Called function: {rep} -> {val!r}")
60 return val
61
62 return inner