From 3a58f4510342ab1907beb4af50a813b36c583ea3 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Sun, 24 Sep 2023 11:58:45 -0700 Subject: [PATCH] Tidy up executors code --- hypothesis-python/src/hypothesis/core.py | 49 ++++++++++++----- hypothesis-python/src/hypothesis/executors.py | 52 ------------------- 2 files changed, 37 insertions(+), 64 deletions(-) delete mode 100644 hypothesis-python/src/hypothesis/executors.py diff --git a/hypothesis-python/src/hypothesis/core.py b/hypothesis-python/src/hypothesis/core.py index aa53c6b9a7..72d73aadb7 100644 --- a/hypothesis-python/src/hypothesis/core.py +++ b/hypothesis-python/src/hypothesis/core.py @@ -66,7 +66,6 @@ Unsatisfiable, UnsatisfiedAssumption, ) -from hypothesis.executors import default_new_style_executor, new_style_executor from hypothesis.internal.compat import ( PYPY, BaseExceptionGroup, @@ -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(): @@ -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(): @@ -704,9 +701,38 @@ def new_given_signature(original_sig, given_kwargs): ) +def default_executor(data, function): + return function(data) + + +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.test_runner = get_executor(stuff.selfy) self.stuff = stuff self.settings = settings self.last_exception = None @@ -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 @@ -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 @@ -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 diff --git a/hypothesis-python/src/hypothesis/executors.py b/hypothesis-python/src/hypothesis/executors.py deleted file mode 100644 index e19921bbed..0000000000 --- a/hypothesis-python/src/hypothesis/executors.py +++ /dev/null @@ -1,52 +0,0 @@ -# This file is part of Hypothesis, which may be found at -# https://github.com/HypothesisWorks/hypothesis/ -# -# Copyright the Hypothesis Authors. -# Individual contributors are listed in AUTHORS.rst and the git log. -# -# This Source Code Form is subject to the terms of the Mozilla Public License, -# v. 2.0. If a copy of the MPL was not distributed with this file, You can -# obtain one at https://mozilla.org/MPL/2.0/. - - -def setup_teardown_executor(setup, teardown): - setup = setup or (lambda: None) - teardown = teardown or (lambda ex: None) - - def execute(function): - token = None - try: - token = setup() - return function() - finally: - teardown(token) - - return execute - - -def executor(runner): - try: - return runner.execute_example - except AttributeError: - pass - - if hasattr(runner, "setup_example") or hasattr(runner, "teardown_example"): - return setup_teardown_executor( - getattr(runner, "setup_example", None), - getattr(runner, "teardown_example", None), - ) - - -def default_new_style_executor(data, function): - return function(data) - - -def new_style_executor(runner): - if runner is None: - return default_new_style_executor - - old_school = executor(runner) - if old_school is None: - return default_new_style_executor - else: - return lambda data, function: old_school(lambda: function(data))