|
6 | 6 | import sys |
7 | 7 | import warnings |
8 | 8 | import pytest |
9 | | -from collections import namedtuple |
10 | 9 |
|
11 | 10 |
|
12 | 11 | @pytest.yield_fixture |
13 | | -def recwarn(request): |
| 12 | +def recwarn(): |
14 | 13 | """Return a WarningsRecorder instance that provides these methods: |
15 | 14 |
|
16 | 15 | * ``pop(category=None)``: return last warning matching the category. |
@@ -115,19 +114,14 @@ def warns(expected_warning, *args, **kwargs): |
115 | 114 | return func(*args[1:], **kwargs) |
116 | 115 |
|
117 | 116 |
|
118 | | -RecordedWarning = namedtuple('RecordedWarning', ( |
119 | | - 'message', 'category', 'filename', 'lineno', 'file', 'line', |
120 | | -)) |
121 | | - |
122 | | - |
123 | | -class WarningsRecorder(object): |
| 117 | +class WarningsRecorder(warnings.catch_warnings): |
124 | 118 | """A context manager to record raised warnings. |
125 | 119 |
|
126 | 120 | Adapted from `warnings.catch_warnings`. |
127 | 121 | """ |
128 | 122 |
|
129 | | - def __init__(self, module=None): |
130 | | - self._module = sys.modules['warnings'] if module is None else module |
| 123 | + def __init__(self): |
| 124 | + super(WarningsRecorder, self).__init__(record=True) |
131 | 125 | self._entered = False |
132 | 126 | self._list = [] |
133 | 127 |
|
@@ -164,38 +158,20 @@ def __enter__(self): |
164 | 158 | if self._entered: |
165 | 159 | __tracebackhide__ = True |
166 | 160 | raise RuntimeError("Cannot enter %r twice" % self) |
167 | | - self._entered = True |
168 | | - self._filters = self._module.filters |
169 | | - self._module.filters = self._filters[:] |
170 | | - self._showwarning = self._module.showwarning |
171 | | - |
172 | | - def showwarning(message, category, filename, lineno, |
173 | | - file=None, line=None): |
174 | | - self._list.append(RecordedWarning( |
175 | | - message, category, filename, lineno, file, line)) |
176 | | - |
177 | | - # still perform old showwarning functionality |
178 | | - self._showwarning( |
179 | | - message, category, filename, lineno, file=file, line=line) |
180 | | - |
181 | | - self._module.showwarning = showwarning |
182 | | - |
183 | | - # allow the same warning to be raised more than once |
184 | | - |
185 | | - self._module.simplefilter('always') |
| 161 | + self._list = super(WarningsRecorder, self).__enter__() |
| 162 | + warnings.simplefilter('always') |
186 | 163 | return self |
187 | 164 |
|
188 | 165 | def __exit__(self, *exc_info): |
189 | 166 | if not self._entered: |
190 | 167 | __tracebackhide__ = True |
191 | 168 | raise RuntimeError("Cannot exit %r without entering first" % self) |
192 | | - self._module.filters = self._filters |
193 | | - self._module.showwarning = self._showwarning |
| 169 | + super(WarningsRecorder, self).__exit__(*exc_info) |
194 | 170 |
|
195 | 171 |
|
196 | 172 | class WarningsChecker(WarningsRecorder): |
197 | | - def __init__(self, expected_warning=None, module=None): |
198 | | - super(WarningsChecker, self).__init__(module=module) |
| 173 | + def __init__(self, expected_warning=None): |
| 174 | + super(WarningsChecker, self).__init__() |
199 | 175 |
|
200 | 176 | msg = ("exceptions must be old-style classes or " |
201 | 177 | "derived from Warning, not %s") |
|
0 commit comments