~python-fixtures/debian/sid/python-fixtures/sid

« back to all changes in this revision

Viewing changes to lib/fixtures/fixture.py

  • Committer: Robert Collins
  • Date: 2010-09-18 03:08:13 UTC
  • mfrom: (5.1.2 upstream)
  • Revision ID: robertc@robertcollins.net-20100918030813-32ae9lk8rirs2ebp
Tags: 0.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
__all__ = [
17
17
    'Fixture',
18
18
    'FunctionFixture',
 
19
    'MultipleExceptions',
19
20
    ]
20
21
 
21
22
import sys
22
23
 
 
24
try:
 
25
    from testtools import MultipleExceptions
 
26
except ImportError:
 
27
    class MultipleExceptions(Exception):
 
28
        """Report multiple exc_info tuples in self.args."""
 
29
 
23
30
 
24
31
class Fixture(object):
25
32
    """A Fixture representing some state or resource.
48
55
        """
49
56
        self._cleanups.append((cleanup, args, kwargs))
50
57
 
51
 
    def cleanUp(self, raise_first=False):
 
58
    def cleanUp(self, raise_first=True):
52
59
        """Cleanup the fixture.
53
60
 
54
61
        This function will free all resources managed by the Fixture, restoring
57
64
 
58
65
        This should not typically be overridden, see addCleanup instead.
59
66
 
60
 
        :return: A list of the exc_info() for each exception that occured.
 
67
        :param raise_first: Deprecated parameter from before testtools gained
 
68
            MultipleExceptions.
 
69
        :return: A list of the exc_info() for each exception that occured if
 
70
            raise_first was False
61
71
        """
62
72
        cleanups = reversed(self._cleanups)
63
73
        self._clear_cleanups()
68
78
            except Exception:
69
79
                result.append(sys.exc_info())
70
80
        if result and raise_first:
71
 
            # Additional errors get swallowed - a context manager limitation.
72
 
            error = result[0]
73
 
            raise error[0], error[1], error[2]
74
 
        return result
 
81
            if 1 == len(result):
 
82
                error = result[0]
 
83
                raise error[0], error[1], error[2]
 
84
            else:
 
85
                raise MultipleExceptions(*result)
 
86
        if not raise_first:
 
87
            return result
75
88
 
76
89
    def _clear_cleanups(self):
77
90
        """Clean the cleanup queue without running them.
87
100
        return self
88
101
 
89
102
    def __exit__(self, exc_type, exc_val, exc_tb):
90
 
        errors = self.cleanUp(raise_first=True)
 
103
        errors = self.cleanUp()
91
104
        return False # propogate exceptions from the with body.
92
105
 
93
106
    def setUp(self):
108
121
        """Reset a setUp Fixture to the 'just setUp' state again.
109
122
 
110
123
        The default implementation calls
111
 
        self.cleanUp(raise_first=True)
 
124
        self.cleanUp()
112
125
        self.setUp()
113
126
 
114
127
        but this function may be overridden to provide an optimised routine to
116
129
 
117
130
        :return: None.
118
131
        """
119
 
        self.cleanUp(raise_first=True)
 
132
        self.cleanUp()
120
133
        self.setUp()
121
134
 
122
135