summaryrefslogtreecommitdiff
path: root/coverage/misc.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-08-09 17:15:25 -0400
committerNed Batchelder <ned@nedbatchelder.com>2015-08-09 17:15:25 -0400
commitd023208415eb330daf2a09ade85a0240c2a9453f (patch)
tree6f4d74a2fb0effccd4e8b50d34182be072180646 /coverage/misc.py
parent5c8eea66f2bb46ee5c36ca7d32aebe0b62e4d2e9 (diff)
downloadpython-coveragepy-git-d023208415eb330daf2a09ade85a0240c2a9453f.tar.gz
Make sure FileReporters are called once for their data.
Diffstat (limited to 'coverage/misc.py')
-rw-r--r--coverage/misc.py25
1 files changed, 15 insertions, 10 deletions
diff --git a/coverage/misc.py b/coverage/misc.py
index 50396d61..44f89772 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -83,19 +83,24 @@ def format_lines(statements, lines):
def expensive(fn):
- """A decorator to cache the result of an expensive operation.
+ """A decorator to indicate that a method shouldn't be called more than once.
- Only applies to methods with no arguments.
+ Normally, this does nothing. During testing, this raises an exception if
+ called more than once.
"""
- attr = "_cache_" + fn.__name__
-
- def _wrapped(self):
- """Inner function that checks the cache."""
- if not hasattr(self, attr):
- setattr(self, attr, fn(self))
- return getattr(self, attr)
- return _wrapped
+ if env.TESTING:
+ attr = "_once_" + fn.__name__
+
+ def _wrapped(self):
+ """Inner function that checks the cache."""
+ if hasattr(self, attr):
+ raise Exception("Shouldn't have called %s more than once" % fn.__name__)
+ setattr(self, attr, True)
+ return fn(self)
+ return _wrapped
+ else:
+ return fn
def bool_or_none(b):