/usr/lib/pypy/dist-packages/hypothesis/control.py is in pypy-hypothesis 3.44.1-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | # coding=utf-8
#
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis-python
#
# Most of this work is copyright (C) 2013-2017 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# 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 http://mozilla.org/MPL/2.0/.
#
# END HEADER
from __future__ import division, print_function, absolute_import
import traceback
from hypothesis.errors import CleanupFailed, InvalidArgument, \
UnsatisfiedAssumption
from hypothesis.reporting import report
from hypothesis.utils.dynamicvariables import DynamicVariable
def reject():
raise UnsatisfiedAssumption()
def assume(condition):
"""``assume()`` is like an :ref:`assert <python:assert>` that marks the
example as bad, rather than failing the test.
This allows you to specify properties that you *assume* will be
true, and let Hypothesis try to avoid similar examples in future.
"""
if not condition:
raise UnsatisfiedAssumption()
return True
_current_build_context = DynamicVariable(None)
def current_build_context():
context = _current_build_context.value
if context is None:
raise InvalidArgument(
u'No build context registered')
return context
class BuildContext(object):
def __init__(self, data, is_final=False, close_on_capture=True):
self.data = data
self.tasks = []
self.is_final = is_final
self.close_on_capture = close_on_capture
self.close_on_del = False
self.notes = []
def __enter__(self):
self.assign_variable = _current_build_context.with_value(self)
self.assign_variable.__enter__()
return self
def __exit__(self, exc_type, exc_value, tb):
self.assign_variable.__exit__(exc_type, exc_value, tb)
if self.close() and exc_type is None:
raise CleanupFailed()
def local(self):
return _current_build_context.with_value(self)
def close(self):
any_failed = False
for task in self.tasks:
try:
task()
except BaseException:
any_failed = True
report(traceback.format_exc())
return any_failed
def cleanup(teardown):
"""Register a function to be called when the current test has finished
executing. Any exceptions thrown in teardown will be printed but not
rethrown.
Inside a test this isn't very interesting, because you can just use
a finally block, but note that you can use this inside map, flatmap,
etc. in order to e.g. insist that a value is closed at the end.
"""
context = _current_build_context.value
if context is None:
raise InvalidArgument(
u'Cannot register cleanup outside of build context')
context.tasks.append(teardown)
def note(value):
"""Report this value in the final execution."""
context = _current_build_context.value
if context is None:
raise InvalidArgument(
'Cannot make notes outside of a test')
context.notes.append(value)
if context.is_final:
report(value)
def event(value):
"""Record an event that occurred this test. Statistics on number of test
runs with each event will be reported at the end if you run Hypothesis in
statistics reporting mode.
Events should be strings or convertible to them.
"""
context = _current_build_context.value
if context is None:
raise InvalidArgument(
'Cannot make record events outside of a test')
if context.data is not None:
context.data.note_event(value)
|