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