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 contextlib import AbstractContextManager
12from typing import TYPE_CHECKING, Callable
13
14from hypothesis._settings import Verbosity, settings
15from hypothesis.internal.compat import escape_unicode_characters
16from hypothesis.utils.dynamicvariables import DynamicVariable
17
18if TYPE_CHECKING:
19 from typing import TypeAlias
20
21
22def default(value: object) -> None:
23 try:
24 print(value)
25 except UnicodeEncodeError:
26 print(escape_unicode_characters(str(value)))
27
28
29ReporterT: "TypeAlias" = Callable[[object], None]
30reporter = DynamicVariable[ReporterT](default)
31
32
33def current_reporter() -> ReporterT:
34 return reporter.value
35
36
37def with_reporter(new_reporter: ReporterT) -> AbstractContextManager[None]:
38 return reporter.with_value(new_reporter)
39
40
41def current_verbosity() -> Verbosity:
42 assert settings.default is not None
43 return settings.default.verbosity
44
45
46def verbose_report(text: str) -> None:
47 if current_verbosity() >= Verbosity.verbose:
48 base_report(text)
49
50
51def debug_report(text: str) -> None:
52 if current_verbosity() >= Verbosity.debug:
53 base_report(text)
54
55
56def report(text: str) -> None:
57 if current_verbosity() >= Verbosity.normal:
58 base_report(text)
59
60
61def base_report(text: str) -> None:
62 assert isinstance(text, str), f"unexpected non-str {text=}"
63 current_reporter()(text)