Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Apparently context managers have to be objects (not classes) in pytho…

  • Loading branch information...
commit 3fc417255af6d9b7c947784622cc63491d18f276 1 parent 09379d8
@gfxmonk authored
Showing with 20 additions and 17 deletions.
  1. +20 −17 mocktest/transaction.py
View
37 mocktest/transaction.py
@@ -2,39 +2,42 @@
__unittest = True
-class MockTransaction(object):
+__all__ = ['MockTransaction']
+
+class _MockTransaction(object):
"""
A context manager to encapsulate a single mocktest transaction.
**Note**: this is a global context manager - you
cannot have more than one.
"""
- teardown_actions = None
- started = False
- @classmethod
- def add_teardown(cls, func):
- cls.teardown_actions.append(func)
+ def __init__(self):
+ self.teardown_actions = None
+ self.started = False
+
+ def add_teardown(self, func):
+ self.teardown_actions.append(func)
- @classmethod
- def __enter__(cls):
+ def __enter__(self):
"""begin a new transaction"""
- if cls.started: raise MockError("MockTransaction started while already in progress!")
- cls.teardown_actions = []
- cls.started = True
+ if self.started: raise MockError("MockTransaction started while already in progress!")
+ self.teardown_actions = []
+ self.started = True
- @classmethod
- def __exit__(cls, *optional_err_info):
+ def __exit__(self, *optional_err_info):
"""end the current transaction, resetting all mocks and verifying all expectations"""
- if not cls.started: raise MockError("MockTransaction is not in progress!")
+ if not self.started: raise MockError("MockTransaction is not in progress!")
errors = []
- for action in reversed(cls.teardown_actions):
+ for action in reversed(self.teardown_actions):
try:
action()
except StandardError, e:
errors.append(e)
- cls.teardown_actions = None
- cls.started = False
+ self.teardown_actions = None
+ self.started = False
if errors:
raise errors[0]
return False
+# The global MockTransaction instance:
+MockTransaction = _MockTransaction()
Please sign in to comment.
Something went wrong with that request. Please try again.