Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
The default is to always capture the snapshot unless the interpreter runs in optimized mode (``-O`` or
``-OO``).
"""
self._snapshot = None # type: Optional[Snapshot]
self.enabled = enabled
# Resolve the snapshot only if enabled so that no overhead is incurred
if enabled:
location = None # type: Optional[str]
tb_stack = traceback.extract_stack(limit=2)[:1]
if len(tb_stack) > 0:
frame = tb_stack[0]
location = 'File {}, line {} in {}'.format(frame.filename, frame.lineno, frame.name)
self._snapshot = Snapshot(capture=capture, name=name, location=location)
require = icontract._decorators.require
snapshot = icontract._decorators.snapshot
ensure = icontract._decorators.ensure
invariant = icontract._decorators.invariant
import icontract._globals
aRepr = icontract._globals.aRepr
SLOW = icontract._globals.SLOW
import icontract._metaclass
DBCMeta = icontract._metaclass.DBCMeta
DBC = icontract._metaclass.DBC
import icontract._types
_Contract = icontract._types.Contract
_Snapshot = icontract._types.Snapshot
import icontract.errors
ViolationError = icontract.errors.ViolationError
contract_checker = icontract._checkers.find_checker(func=func)
if contract_checker is None:
raise ValueError("You are decorating a function with a snapshot, but no postcondition was defined "
"on the function before.")
assert self._snapshot is not None, "Expected the enabled snapshot to have the property ``snapshot`` set."
# Add the snapshot to the list of snapshots stored at the checker
assert hasattr(contract_checker, "__postcondition_snapshots__")
snapshots = getattr(contract_checker, "__postcondition_snapshots__")
assert isinstance(snapshots, list)
for snap in snapshots:
assert isinstance(snap, Snapshot)
if snap.name == self._snapshot.name:
raise ValueError("There are conflicting snapshots with the name: {!r}".format(snap.name))
snapshots.append(self._snapshot)
return func