~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to numpy/testing/decorators.py

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2010-10-07 10:19:13 UTC
  • mfrom: (7.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20101007101913-8b1kmt8ho4upcl9s
Tags: 1:1.4.1-5
* debian/patches/10_use_local_python.org_object.inv_sphinx.diff
  - fixed small typo in description
* debian/patches/changeset_r8364.diff
  - fix memory corruption (double free); thanks to Joseph Barillari for the
    report and to Michael Gilbert for pushing resolution; Closes: #581058

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Decorators for labeling test objects
 
1
"""
 
2
Decorators for labeling and modifying behavior of test objects.
2
3
 
3
4
Decorators that merely return a modified version of the original
4
 
function object are straightforward.  Decorators that return a new
 
5
function object are straightforward. Decorators that return a new
5
6
function object need to use
6
 
nose.tools.make_decorator(original_function)(decorator) in returning
7
 
the decorator, in order to preserve metadata such as function name,
8
 
setup and teardown functions and so on - see nose.tools for more
9
 
information.
 
7
::
 
8
 
 
9
  nose.tools.make_decorator(original_function)(decorator)
 
10
 
 
11
in returning the decorator, in order to preserve meta-data such as
 
12
function name, setup and teardown functions and so on - see
 
13
``nose.tools`` for more information.
10
14
 
11
15
"""
 
16
import warnings
 
17
import sys
 
18
 
 
19
from numpy.testing.utils import \
 
20
        WarningManager, WarningMessage
12
21
 
13
22
def slow(t):
14
 
    """Labels a test as 'slow'.
 
23
    """
 
24
    Label a test as 'slow'.
15
25
 
16
26
    The exact definition of a slow test is obviously both subjective and
17
27
    hardware-dependent, but in general any individual test that requires more
18
28
    than a second or two should be labeled as slow (the whole suite consits of
19
 
    thousands of tests, so even a second is significant)."""
 
29
    thousands of tests, so even a second is significant).
 
30
 
 
31
    Parameters
 
32
    ----------
 
33
    t : callable
 
34
        The test to label as slow.
 
35
 
 
36
    Returns
 
37
    -------
 
38
    t : callable
 
39
        The decorated test `t`.
 
40
 
 
41
    Examples
 
42
    --------
 
43
    The `numpy.testing` module includes ``import decorators as dec``.
 
44
    A test can be decorated as slow like this::
 
45
 
 
46
      from numpy.testing import *
 
47
 
 
48
      @dec.slow
 
49
      def test_big(self):
 
50
          print 'Big, slow test'
 
51
 
 
52
    """
20
53
 
21
54
    t.slow = True
22
55
    return t
23
56
 
24
57
def setastest(tf=True):
25
 
    ''' Signals to nose that this function is or is not a test
 
58
    """
 
59
    Signals to nose that this function is or is not a test.
26
60
 
27
61
    Parameters
28
62
    ----------
29
63
    tf : bool
30
 
        If True specifies this is a test, not a test otherwise
31
 
 
32
 
    e.g
33
 
    >>> from numpy.testing.decorators import setastest
34
 
    >>> @setastest(False)
35
 
    ... def func_with_test_in_name(arg1, arg2): pass
36
 
    ...
37
 
    >>>
38
 
 
39
 
    This decorator cannot use the nose namespace, because it can be
40
 
    called from a non-test module. See also istest and nottest in
41
 
    nose.tools
42
 
 
43
 
    '''
 
64
        If True, specifies that the decorated callable is a test.
 
65
        If False, specifies that the decorated callable is not a test.
 
66
        Default is True.
 
67
 
 
68
    Notes
 
69
    -----
 
70
    This decorator can't use the nose namespace, because it can be
 
71
    called from a non-test module. See also ``istest`` and ``nottest`` in
 
72
    ``nose.tools``.
 
73
 
 
74
    Examples
 
75
    --------
 
76
    `setastest` can be used in the following way::
 
77
 
 
78
      from numpy.testing.decorators import setastest
 
79
 
 
80
      @setastest(False)
 
81
      def func_with_test_in_name(arg1, arg2):
 
82
          pass
 
83
 
 
84
    """
44
85
    def set_test(t):
45
86
        t.__test__ = tf
46
87
        return t
47
88
    return set_test
48
89
 
49
90
def skipif(skip_condition, msg=None):
50
 
    ''' Make function raise SkipTest exception if skip_condition is true
 
91
    """
 
92
    Make function raise SkipTest exception if a given condition is true.
 
93
 
 
94
    If the condition is a callable, it is used at runtime to dynamically
 
95
    make the decision. This is useful for tests that may require costly
 
96
    imports, to delay the cost until the test suite is actually executed.
51
97
 
52
98
    Parameters
53
99
    ----------
54
 
    skip_condition : bool or callable.
55
 
        Flag to determine whether to skip test.  If the condition is a
56
 
        callable, it is used at runtime to dynamically make the decision.  This
57
 
        is useful for tests that may require costly imports, to delay the cost
58
 
        until the test suite is actually executed.
59
 
    msg : string
60
 
        Message to give on raising a SkipTest exception
 
100
    skip_condition : bool or callable
 
101
        Flag to determine whether to skip the decorated test.
 
102
    msg : str, optional
 
103
        Message to give on raising a SkipTest exception. Default is None.
61
104
 
62
 
   Returns
63
 
   -------
64
 
   decorator : function
65
 
       Decorator, which, when applied to a function, causes SkipTest
66
 
       to be raised when the skip_condition was True, and the function
67
 
       to be called normally otherwise.
 
105
    Returns
 
106
    -------
 
107
    decorator : function
 
108
        Decorator which, when applied to a function, causes SkipTest
 
109
        to be raised when `skip_condition` is True, and the function
 
110
        to be called normally otherwise.
68
111
 
69
112
    Notes
70
113
    -----
71
 
    You will see from the code that we had to further decorate the
72
 
    decorator with the nose.tools.make_decorator function in order to
73
 
    transmit function name, and various other metadata.
74
 
    '''
 
114
    The decorator itself is decorated with the ``nose.tools.make_decorator``
 
115
    function in order to transmit function name, and various other metadata.
 
116
 
 
117
    """
75
118
 
76
119
    def skip_decorator(f):
77
120
        # Local import to avoid a hard nose dependency and only incur the
122
165
 
123
166
 
124
167
def knownfailureif(fail_condition, msg=None):
125
 
    ''' Make function raise KnownFailureTest exception if fail_condition is true
 
168
    """
 
169
    Make function raise KnownFailureTest exception if given condition is true.
 
170
 
 
171
    If the condition is a callable, it is used at runtime to dynamically
 
172
    make the decision. This is useful for tests that may require costly
 
173
    imports, to delay the cost until the test suite is actually executed.
126
174
 
127
175
    Parameters
128
176
    ----------
129
 
    fail_condition : bool or callable.
130
 
        Flag to determine whether to mark test as known failure (True)
131
 
        or not (False).  If the condition is a callable, it is used at
132
 
        runtime to dynamically make the decision.  This is useful for 
133
 
        tests that may require costly imports, to delay the cost
134
 
        until the test suite is actually executed.
135
 
    msg : string
136
 
        Message to give on raising a KnownFailureTest exception
 
177
    fail_condition : bool or callable
 
178
        Flag to determine whether to mark the decorated test as a known
 
179
        failure (if True) or not (if False).
 
180
    msg : str, optional
 
181
        Message to give on raising a KnownFailureTest exception.
 
182
        Default is None.
137
183
 
138
 
   Returns
139
 
   -------
140
 
   decorator : function
141
 
       Decorator, which, when applied to a function, causes SkipTest
142
 
       to be raised when the skip_condition was True, and the function
143
 
       to be called normally otherwise.
 
184
    Returns
 
185
    -------
 
186
    decorator : function
 
187
        Decorator, which, when applied to a function, causes SkipTest
 
188
        to be raised when `skip_condition` is True, and the function
 
189
        to be called normally otherwise.
144
190
 
145
191
    Notes
146
192
    -----
147
 
    You will see from the code that we had to further decorate the
148
 
    decorator with the nose.tools.make_decorator function in order to
149
 
    transmit function name, and various other metadata.
150
 
    '''
 
193
    The decorator itself is decorated with the ``nose.tools.make_decorator``
 
194
    function in order to transmit function name, and various other metadata.
 
195
 
 
196
    """
151
197
    if msg is None:
152
198
        msg = 'Test skipped due to known failure'
153
199
 
170
216
        return nose.tools.make_decorator(f)(knownfailer)
171
217
 
172
218
    return knownfail_decorator
 
219
 
 
220
def deprecated(conditional=True):
 
221
    """
 
222
    Filter deprecation warnings while running the test suite.
 
223
 
 
224
    This decorator can be used to filter DeprecationWarning's, to avoid
 
225
    printing them during the test suite run, while checking that the test
 
226
    actually raises a DeprecationWarning.
 
227
 
 
228
    Parameters
 
229
    ----------
 
230
    conditional : bool or callable, optional
 
231
        Flag to determine whether to mark test as deprecated or not. If the
 
232
        condition is a callable, it is used at runtime to dynamically make the
 
233
        decision. Default is True.
 
234
 
 
235
    Returns
 
236
    -------
 
237
    decorator : function
 
238
        The `deprecated` decorator itself.
 
239
 
 
240
    Notes
 
241
    -----
 
242
    .. versionadded:: 1.4.0
 
243
 
 
244
    """
 
245
    def deprecate_decorator(f):
 
246
        # Local import to avoid a hard nose dependency and only incur the
 
247
        # import time overhead at actual test-time.
 
248
        import nose
 
249
        from noseclasses import KnownFailureTest
 
250
 
 
251
        def _deprecated_imp(*args, **kwargs):
 
252
            # Poor man's replacement for the with statement
 
253
            ctx = WarningManager(record=True)
 
254
            l = ctx.__enter__()
 
255
            warnings.simplefilter('always')
 
256
            try:
 
257
                f(*args, **kwargs)
 
258
                if not len(l) > 0:
 
259
                    raise AssertionError("No warning raised when calling %s"
 
260
                            % f.__name__)
 
261
                if not l[0].category is DeprecationWarning:
 
262
                    raise AssertionError("First warning for %s is not a " \
 
263
                            "DeprecationWarning( is %s)" % (f.__name__, l[0]))
 
264
            finally:
 
265
                ctx.__exit__()
 
266
 
 
267
        if callable(conditional):
 
268
            cond = conditional()
 
269
        else:
 
270
            cond = conditional
 
271
        if cond:
 
272
            return nose.tools.make_decorator(f)(_deprecated_imp)
 
273
        else:
 
274
            return f
 
275
    return deprecate_decorator