Skip to content

Commit

Permalink
share belongs_to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed Feb 6, 2025
1 parent 3a76506 commit a76ef13
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

Improves sharing of some internal cache behavior.
12 changes: 5 additions & 7 deletions hypothesis-python/src/hypothesis/internal/escalation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,34 @@
from hypothesis.internal.compat import BaseExceptionGroup
from hypothesis.utils.dynamicvariables import DynamicVariable

FILE_CACHE: dict[ModuleType, dict[str, bool]] = {}


def belongs_to(package: ModuleType) -> Callable[[str], bool]:
if getattr(package, "__file__", None) is None: # pragma: no cover
return lambda filepath: False

assert package.__file__ is not None
FILE_CACHE[package] = {}
root = Path(package.__file__).resolve().parent
cache: dict[type, dict[str, bool]] = {str: {}, bytes: {}}

def accept(filepath: str) -> bool:
ftype = type(filepath)
try:
return cache[ftype][filepath]
return FILE_CACHE[package][filepath]
except KeyError:
pass
try:
Path(filepath).resolve().relative_to(root)
result = True
except Exception:
result = False
cache[ftype][filepath] = result
FILE_CACHE[package][filepath] = result
return result

accept.__name__ = f"is_{package.__name__}_file"
return accept


FILE_CACHE: dict[bytes, bool] = {}


is_hypothesis_file = belongs_to(hypothesis)


Expand Down

0 comments on commit a76ef13

Please sign in to comment.