Skip to content

Commit

Permalink
Tidy up executors code
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Sep 24, 2023
1 parent bb00b5e commit fec0179
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 67 deletions.
55 changes: 40 additions & 15 deletions hypothesis-python/src/hypothesis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import warnings
import zlib
from collections import defaultdict
from functools import partial
from functools import lru_cache, partial
from random import Random
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -66,7 +66,6 @@
Unsatisfiable,
UnsatisfiedAssumption,
)
from hypothesis.executors import default_new_style_executor, new_style_executor
from hypothesis.internal.compat import (
PYPY,
BaseExceptionGroup,
Expand Down Expand Up @@ -635,8 +634,6 @@ def process_arguments_to_given(wrapped_test, arguments, kwargs, given_kwargs, pa
if is_mock(selfy):
selfy = None

test_runner = new_style_executor(selfy)

arguments = tuple(arguments)

with ensure_free_stackframes():
Expand All @@ -646,7 +643,7 @@ def process_arguments_to_given(wrapped_test, arguments, kwargs, given_kwargs, pa

stuff = Stuff(selfy=selfy, args=arguments, kwargs=kwargs, given_kwargs=given_kwargs)

return arguments, kwargs, test_runner, stuff
return arguments, kwargs, stuff


def skip_exceptions_to_reraise():
Expand Down Expand Up @@ -704,9 +701,38 @@ def new_given_signature(original_sig, given_kwargs):
)


def default_executor(data, function):
return function(data)


@lru_cache
def get_executor(runner):
try:
execute_example = runner.execute_example
except AttributeError:
pass
else:
return lambda data, function: execute_example(partial(function, data))

if hasattr(runner, "setup_example") or hasattr(runner, "teardown_example"):
setup = getattr(runner, "setup_example", None) or (lambda: None)
teardown = getattr(runner, "teardown_example", None) or (lambda ex: None)

def execute(data, function):
token = None
try:
token = setup()
return function(data)
finally:
teardown(token)

return execute

return default_executor


class StateForActualGivenExecution:
def __init__(self, test_runner, stuff, test, settings, random, wrapped_test):
self.test_runner = test_runner
def __init__(self, stuff, test, settings, random, wrapped_test):
self.stuff = stuff
self.settings = settings
self.last_exception = None
Expand Down Expand Up @@ -831,14 +857,14 @@ def run(data):
report(printer.getvalue())
return test(*args, **kwargs)

# self.test_runner can include the execute_example method, or setup/teardown
# the executor can include the execute_example method, or setup/teardown
# _example, so it's important to get the PRNG and build context in place first.
with local_settings(self.settings):
with deterministic_PRNG():
with BuildContext(data, is_final=is_final) as context:
# Run the test function once, via the executor hook.
# In most cases this will delegate straight to `run(data)`.
result = self.test_runner(data, run)
result = get_executor(self.stuff.selfy)(data, run)

# If a failure was expected, it should have been raised already, so
# instead raise an appropriate diagnostic error.
Expand Down Expand Up @@ -1264,14 +1290,13 @@ def wrapped_test(*arguments, **kwargs):

random = get_random_for_wrapped_test(test, wrapped_test)

processed_args = process_arguments_to_given(
arguments, kwargs, stuff = process_arguments_to_given(
wrapped_test, arguments, kwargs, given_kwargs, new_signature.parameters
)
arguments, kwargs, test_runner, stuff = processed_args

if (
inspect.iscoroutinefunction(test)
and test_runner is default_new_style_executor
and get_executor(stuff.selfy) is default_executor
):
# See https://github.com/HypothesisWorks/hypothesis/issues/3054
# If our custom executor doesn't handle coroutines, or we return an
Expand Down Expand Up @@ -1322,7 +1347,7 @@ def wrapped_test(*arguments, **kwargs):
fail_health_check(settings, msg, HealthCheck.differing_executors)

state = StateForActualGivenExecution(
test_runner, stuff, test, settings, random, wrapped_test
stuff, test, settings, random, wrapped_test
)

reproduce_failure = wrapped_test._hypothesis_internal_use_reproduce_failure
Expand Down Expand Up @@ -1465,13 +1490,13 @@ def _get_fuzz_target() -> (
parent=wrapped_test._hypothesis_internal_use_settings, deadline=None
)
random = get_random_for_wrapped_test(test, wrapped_test)
_args, _kwargs, test_runner, stuff = process_arguments_to_given(
_args, _kwargs, stuff = process_arguments_to_given(
wrapped_test, (), {}, given_kwargs, new_signature.parameters
)
assert not _args
assert not _kwargs
state = StateForActualGivenExecution(
test_runner, stuff, test, settings, random, wrapped_test
stuff, test, settings, random, wrapped_test
)
digest = function_digest(test)
# We track the minimal-so-far example for each distinct origin, so
Expand Down
52 changes: 0 additions & 52 deletions hypothesis-python/src/hypothesis/executors.py

This file was deleted.

0 comments on commit fec0179

Please sign in to comment.