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
11import inspect
12
13from hypothesis._settings import Verbosity, settings
14from hypothesis.internal.compat import escape_unicode_characters
15from hypothesis.utils.dynamicvariables import DynamicVariable
16
17
18def default(value):
19 try:
20 print(value)
21 except UnicodeEncodeError:
22 print(escape_unicode_characters(value))
23
24
25reporter = DynamicVariable(default)
26
27
28def current_reporter():
29 return reporter.value
30
31
32def with_reporter(new_reporter):
33 return reporter.with_value(new_reporter)
34
35
36def current_verbosity():
37 return settings.default.verbosity
38
39
40def to_text(textish):
41 if inspect.isfunction(textish):
42 textish = textish()
43 if isinstance(textish, bytes):
44 textish = textish.decode()
45 return textish
46
47
48def verbose_report(text):
49 if current_verbosity() >= Verbosity.verbose:
50 base_report(text)
51
52
53def debug_report(text):
54 if current_verbosity() >= Verbosity.debug:
55 base_report(text)
56
57
58def report(text):
59 if current_verbosity() >= Verbosity.normal:
60 base_report(text)
61
62
63def base_report(text):
64 current_reporter()(to_text(text))