Skip to content

Commit 3a58f45

Browse files
committed
Tidy up executors code
1 parent bb00b5e commit 3a58f45

2 files changed

Lines changed: 37 additions & 64 deletions

File tree

hypothesis-python/src/hypothesis/core.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
Unsatisfiable,
6767
UnsatisfiedAssumption,
6868
)
69-
from hypothesis.executors import default_new_style_executor, new_style_executor
7069
from hypothesis.internal.compat import (
7170
PYPY,
7271
BaseExceptionGroup,
@@ -635,8 +634,6 @@ def process_arguments_to_given(wrapped_test, arguments, kwargs, given_kwargs, pa
635634
if is_mock(selfy):
636635
selfy = None
637636

638-
test_runner = new_style_executor(selfy)
639-
640637
arguments = tuple(arguments)
641638

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

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

649-
return arguments, kwargs, test_runner, stuff
646+
return arguments, kwargs, stuff
650647

651648

652649
def skip_exceptions_to_reraise():
@@ -704,9 +701,38 @@ def new_given_signature(original_sig, given_kwargs):
704701
)
705702

706703

704+
def default_executor(data, function):
705+
return function(data)
706+
707+
708+
def get_executor(runner):
709+
try:
710+
execute_example = runner.execute_example
711+
except AttributeError:
712+
pass
713+
else:
714+
return lambda data, function: execute_example(partial(function, data))
715+
716+
if hasattr(runner, "setup_example") or hasattr(runner, "teardown_example"):
717+
setup = getattr(runner, "setup_example", None) or (lambda: None)
718+
teardown = getattr(runner, "teardown_example", None) or (lambda ex: None)
719+
720+
def execute(data, function):
721+
token = None
722+
try:
723+
token = setup()
724+
return function(data)
725+
finally:
726+
teardown(token)
727+
728+
return execute
729+
730+
return default_executor
731+
732+
707733
class StateForActualGivenExecution:
708-
def __init__(self, test_runner, stuff, test, settings, random, wrapped_test):
709-
self.test_runner = test_runner
734+
def __init__(self, stuff, test, settings, random, wrapped_test):
735+
self.test_runner = get_executor(stuff.selfy)
710736
self.stuff = stuff
711737
self.settings = settings
712738
self.last_exception = None
@@ -1264,14 +1290,13 @@ def wrapped_test(*arguments, **kwargs):
12641290

12651291
random = get_random_for_wrapped_test(test, wrapped_test)
12661292

1267-
processed_args = process_arguments_to_given(
1293+
arguments, kwargs, stuff = process_arguments_to_given(
12681294
wrapped_test, arguments, kwargs, given_kwargs, new_signature.parameters
12691295
)
1270-
arguments, kwargs, test_runner, stuff = processed_args
12711296

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

13241349
state = StateForActualGivenExecution(
1325-
test_runner, stuff, test, settings, random, wrapped_test
1350+
stuff, test, settings, random, wrapped_test
13261351
)
13271352

13281353
reproduce_failure = wrapped_test._hypothesis_internal_use_reproduce_failure
@@ -1465,13 +1490,13 @@ def _get_fuzz_target() -> (
14651490
parent=wrapped_test._hypothesis_internal_use_settings, deadline=None
14661491
)
14671492
random = get_random_for_wrapped_test(test, wrapped_test)
1468-
_args, _kwargs, test_runner, stuff = process_arguments_to_given(
1493+
_args, _kwargs, stuff = process_arguments_to_given(
14691494
wrapped_test, (), {}, given_kwargs, new_signature.parameters
14701495
)
14711496
assert not _args
14721497
assert not _kwargs
14731498
state = StateForActualGivenExecution(
1474-
test_runner, stuff, test, settings, random, wrapped_test
1499+
stuff, test, settings, random, wrapped_test
14751500
)
14761501
digest = function_digest(test)
14771502
# We track the minimal-so-far example for each distinct origin, so

hypothesis-python/src/hypothesis/executors.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)