Show More
@@ -0,0 +1,120 b'' | |||
|
1 | # IPython: modified copy of numpy.testing.utils, so numpy.testing.decorators | |
|
2 | # works without numpy being installed. | |
|
3 | """ | |
|
4 | Utility function to facilitate testing. | |
|
5 | """ | |
|
6 | ||
|
7 | import os | |
|
8 | import sys | |
|
9 | import re | |
|
10 | import operator | |
|
11 | import types | |
|
12 | import warnings | |
|
13 | ||
|
14 | # The following two classes are copied from python 2.6 warnings module (context | |
|
15 | # manager) | |
|
16 | class WarningMessage(object): | |
|
17 | ||
|
18 | """ | |
|
19 | Holds the result of a single showwarning() call. | |
|
20 | ||
|
21 | Notes | |
|
22 | ----- | |
|
23 | `WarningMessage` is copied from the Python 2.6 warnings module, | |
|
24 | so it can be used in NumPy with older Python versions. | |
|
25 | ||
|
26 | """ | |
|
27 | ||
|
28 | _WARNING_DETAILS = ("message", "category", "filename", "lineno", "file", | |
|
29 | "line") | |
|
30 | ||
|
31 | def __init__(self, message, category, filename, lineno, file=None, | |
|
32 | line=None): | |
|
33 | local_values = locals() | |
|
34 | for attr in self._WARNING_DETAILS: | |
|
35 | setattr(self, attr, local_values[attr]) | |
|
36 | if category: | |
|
37 | self._category_name = category.__name__ | |
|
38 | else: | |
|
39 | self._category_name = None | |
|
40 | ||
|
41 | def __str__(self): | |
|
42 | return ("{message : %r, category : %r, filename : %r, lineno : %s, " | |
|
43 | "line : %r}" % (self.message, self._category_name, | |
|
44 | self.filename, self.lineno, self.line)) | |
|
45 | ||
|
46 | class WarningManager: | |
|
47 | """ | |
|
48 | A context manager that copies and restores the warnings filter upon | |
|
49 | exiting the context. | |
|
50 | ||
|
51 | The 'record' argument specifies whether warnings should be captured by a | |
|
52 | custom implementation of ``warnings.showwarning()`` and be appended to a | |
|
53 | list returned by the context manager. Otherwise None is returned by the | |
|
54 | context manager. The objects appended to the list are arguments whose | |
|
55 | attributes mirror the arguments to ``showwarning()``. | |
|
56 | ||
|
57 | The 'module' argument is to specify an alternative module to the module | |
|
58 | named 'warnings' and imported under that name. This argument is only useful | |
|
59 | when testing the warnings module itself. | |
|
60 | ||
|
61 | Notes | |
|
62 | ----- | |
|
63 | `WarningManager` is a copy of the ``catch_warnings`` context manager | |
|
64 | from the Python 2.6 warnings module, with slight modifications. | |
|
65 | It is copied so it can be used in NumPy with older Python versions. | |
|
66 | ||
|
67 | """ | |
|
68 | def __init__(self, record=False, module=None): | |
|
69 | self._record = record | |
|
70 | if module is None: | |
|
71 | self._module = sys.modules['warnings'] | |
|
72 | else: | |
|
73 | self._module = module | |
|
74 | self._entered = False | |
|
75 | ||
|
76 | def __enter__(self): | |
|
77 | if self._entered: | |
|
78 | raise RuntimeError("Cannot enter %r twice" % self) | |
|
79 | self._entered = True | |
|
80 | self._filters = self._module.filters | |
|
81 | self._module.filters = self._filters[:] | |
|
82 | self._showwarning = self._module.showwarning | |
|
83 | if self._record: | |
|
84 | log = [] | |
|
85 | def showwarning(*args, **kwargs): | |
|
86 | log.append(WarningMessage(*args, **kwargs)) | |
|
87 | self._module.showwarning = showwarning | |
|
88 | return log | |
|
89 | else: | |
|
90 | return None | |
|
91 | ||
|
92 | def __exit__(self): | |
|
93 | if not self._entered: | |
|
94 | raise RuntimeError("Cannot exit %r without entering first" % self) | |
|
95 | self._module.filters = self._filters | |
|
96 | self._module.showwarning = self._showwarning | |
|
97 | ||
|
98 | def assert_warns(warning_class, func, *args, **kw): | |
|
99 | """Fail unless a warning of class warning_class is thrown by callable when | |
|
100 | invoked with arguments args and keyword arguments kwargs. | |
|
101 | ||
|
102 | If a different type of warning is thrown, it will not be caught, and the | |
|
103 | test case will be deemed to have suffered an error. | |
|
104 | """ | |
|
105 | ||
|
106 | # XXX: once we may depend on python >= 2.6, this can be replaced by the | |
|
107 | # warnings module context manager. | |
|
108 | ctx = WarningManager(record=True) | |
|
109 | l = ctx.__enter__() | |
|
110 | warnings.simplefilter('always') | |
|
111 | try: | |
|
112 | func(*args, **kw) | |
|
113 | if not len(l) > 0: | |
|
114 | raise AssertionError("No warning raised when calling %s" | |
|
115 | % func.__name__) | |
|
116 | if not l[0].category is warning_class: | |
|
117 | raise AssertionError("First warning for %s is not a " \ | |
|
118 | "%s( is %s)" % (func.__name__, warning_class, l[0])) | |
|
119 | finally: | |
|
120 | ctx.__exit__() |
@@ -16,8 +16,17 b' function name, setup and teardown functions and so on - see' | |||
|
16 | 16 | import warnings |
|
17 | 17 | import sys |
|
18 | 18 | |
|
19 | from numpy.testing.utils import \ | |
|
20 | WarningManager, WarningMessage | |
|
19 | # IPython changes: make this work if numpy not available | |
|
20 | # Original code: | |
|
21 | #from numpy.testing.utils import \ | |
|
22 | # WarningManager, WarningMessage | |
|
23 | # Our version: | |
|
24 | try: | |
|
25 | from numpy.testing.utils import WarningManager, WarningMessage | |
|
26 | except ImportError: | |
|
27 | from _numpy_testing_utils import WarningManager, WarningMessage | |
|
28 | ||
|
29 | # End IPython changes | |
|
21 | 30 | |
|
22 | 31 | def slow(t): |
|
23 | 32 | """ |
General Comments 0
You need to be logged in to leave comments.
Login now