Kamil Kisiel avatar Kamil Kisiel committed 1f9f788

Created exception hierarchy to match PEP-316 conventions

Comments (0)

Files changed (5)

covenant/__init__.py

 import sys
+
 from covenant.base import *
 from covenant.conditions import *
 from covenant.invariant import *

covenant/conditions.py

 from inspect import getcallargs
 from functools import wraps
+
 from covenant.util import toggled_decorator_func
+from covenant.exceptions import (PreconditionViolationError,
+                                 PostconditionViolationError)
 
 
 @toggled_decorator_func
             callargs = getcallargs(func, *args, **kwargs)
             result = condition(**callargs)
             if not result:
-                raise AssertionError("Precondition check failed.")
+                raise PreconditionViolationError("Precondition check failed.")
 
             return func(*args, **kwargs)
 
             result = condition(value, **callargs)
 
             if not result:
-                raise AssertionError("Precondition check failed.")
+                raise PostconditionViolationError("Precondition check failed.")
 
         return wrapped_func
     return _post

covenant/invariant.py

 from inspect import getcallargs, getargspec, isfunction, getmembers
 from functools import wraps
+
 from covenant.util import toggled_decorator_func
+from covenant.exceptions import InvariantViolationError
 
 
 # Keep track of which invariant checks are currently happening so that
 _INVARIANTS_IN_PROGRESS = set()
 
 
+def _check_invariant(obj, condition):
+    obj_id = id(obj)
+    if not obj_id in _INVARIANTS_IN_PROGRESS:
+        _INVARIANTS_IN_PROGRESS.add(obj_id)
+        result = condition(obj)
+        _INVARIANTS_IN_PROGRESS.remove(obj_id)
+        if not result:
+            raise InvariantViolationError("Invariant violated.")
+
+
+
 def _invariant_wrapper(attr, condition):
     @wraps(attr)
     def wrapper(*args, **kwargs):
         callargs = getcallargs(attr, *args, **kwargs)
         inst = callargs['self']
 
+        _check_invariant(inst, condition)
         value = attr(*args, **kwargs)
-
-        inst_id = id(inst)
-        if not inst_id in _INVARIANTS_IN_PROGRESS:
-            _INVARIANTS_IN_PROGRESS.add(inst_id)
-            result = condition(inst)
-            _INVARIANTS_IN_PROGRESS.remove(inst_id)
-            if not result:
-                raise AssertionError("Invariant violated.")
+        _check_invariant(inst, condition)
 
         return value
 
 from inspect import getcallargs
 from functools import wraps
+
 from covenant.util import toggled_decorator
+from covenant.exceptions import (PreconditionViolationError,
+                                 PostconditionViolationError)
 
 
 @toggled_decorator
             if arg in func.__annotations__:
                 result = func.__annotations__[arg](arg_value)
                 if not result:
-                    raise AssertionError("Precondition check failed: {0}"
-                                            .format(arg_value))
+                    raise PreconditionViolationError(
+                        "Precondition check failed: {0}".format(arg_value))
 
         value = func(*args, **kwargs)
 
         if "return" in func.__annotations__:
             result = func.__annotations__["return"](value)
             if not result:
-                raise AssertionError("Postcondtion check failed: {0}"
-                                     .format(value))
+                raise PostconditionViolationError(
+                    "Postcondtion check failed: {0}" .format(value))
 
         return value
 
 from functools import wraps
+
 from covenant.base import is_enabled
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.