(*precs)
| 23 | |
| 24 | |
| 25 | def preconditions(*precs): |
| 26 | stripped_source = lambda obj: inspect.getsource(obj).strip() |
| 27 | |
| 28 | if not precs: |
| 29 | # This edge case makes ``@preconditions()`` efficiently delegate |
| 30 | # to the wrapped function, which I anticipate will be useful |
| 31 | # for stubbing and code consistency in applications: |
| 32 | def null_decorator(f): |
| 33 | f.nopre = f # Meet the .nopre interface requirement. |
| 34 | return f |
| 35 | |
| 36 | return null_decorator |
| 37 | |
| 38 | precinfo = [] |
| 39 | for p in precs: |
| 40 | spec = inspect.getfullargspec(p) |
| 41 | if spec.varargs or spec.varkw: |
| 42 | raise PreconditionError( |
| 43 | ( |
| 44 | "Invalid precondition must not accept * nor ** args:\n" + " {!s}\n" |
| 45 | ).format(stripped_source(p)) |
| 46 | ) |
| 47 | |
| 48 | i = -len(spec.defaults or ()) |
| 49 | if i == 0: |
| 50 | appargs, closureargs = spec.args, [] |
| 51 | else: |
| 52 | appargs, closureargs = spec.args[:i], spec.args[i:] |
| 53 | precinfo.append((appargs, closureargs, p)) |
| 54 | |
| 55 | def decorate(f): |
| 56 | fspec = inspect.getfullargspec(f) |
| 57 | |
| 58 | for (appargs, closureargs, p) in precinfo: |
| 59 | for apparg in appargs: |
| 60 | if apparg not in fspec.args: |
| 61 | raise PreconditionError( |
| 62 | ( |
| 63 | "Invalid precondition refers to unknown parameter {!r}:\n" |
| 64 | + " {!s}\n" |
| 65 | + "Known parameters: {!r}\n" |
| 66 | ).format(apparg, stripped_source(p), fspec.args) |
| 67 | ) |
| 68 | for carg in closureargs: |
| 69 | if carg in fspec.args: |
| 70 | raise PreconditionError( |
| 71 | ( |
| 72 | "Invalid precondition masks parameter {!r}:\n" |
| 73 | + " {!s}\n" |
| 74 | + "Known parameters: {!r}\n" |
| 75 | ).format(carg, stripped_source(p), fspec.args) |
| 76 | ) |
| 77 | |
| 78 | @wraps(f) |
| 79 | def g(*a, **kw): |
| 80 | args = inspect.getcallargs(f, *a, **kw) |
| 81 | for (appargs, _, p) in precinfo: |
| 82 | if not p(*[args[aa] for aa in appargs]): |
nothing calls this directly
no test coverage detected
searching dependent graphs…