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
commit95b4df5cbbc474b3cd90adbbdbdbf70ae89001f1 (patch)
tree7a3be448bd398fef95ec7767747e848a072cbba8 /coverage/misc.py
parentb65ca39306184ecc85b54d9fc0526f0311bde19b (diff)
downloadpython-coveragepy-95b4df5cbbc474b3cd90adbbdbdbf70ae89001f1.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 50396d6..44f8977 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):