@@ -43,45 +43,10 @@ def deprecated_call(func=None, *args, **kwargs):
43
43
in which case it will ensure calling ``func(*args, **kwargs)`` produces one of the warnings
44
44
types above.
45
45
"""
46
- if not func :
47
- return _DeprecatedCallContext ()
48
- else :
49
- __tracebackhide__ = True
50
- with _DeprecatedCallContext ():
51
- return func (* args , ** kwargs )
52
-
53
-
54
- class _DeprecatedCallContext (object ):
55
- """Implements the logic to capture deprecation warnings as a context manager."""
56
-
57
- def __enter__ (self ):
58
- self ._captured_categories = []
59
- self ._old_warn = warnings .warn
60
- self ._old_warn_explicit = warnings .warn_explicit
61
- warnings .warn_explicit = self ._warn_explicit
62
- warnings .warn = self ._warn
63
-
64
- def _warn_explicit (self , message , category , * args , ** kwargs ):
65
- self ._captured_categories .append (category )
66
-
67
- def _warn (self , message , category = None , * args , ** kwargs ):
68
- if isinstance (message , Warning ):
69
- self ._captured_categories .append (message .__class__ )
70
- else :
71
- self ._captured_categories .append (category )
72
-
73
- def __exit__ (self , exc_type , exc_val , exc_tb ):
74
- warnings .warn_explicit = self ._old_warn_explicit
75
- warnings .warn = self ._old_warn
76
-
77
- if exc_type is None :
78
- deprecation_categories = (DeprecationWarning , PendingDeprecationWarning )
79
- if not any (
80
- issubclass (c , deprecation_categories ) for c in self ._captured_categories
81
- ):
82
- __tracebackhide__ = True
83
- msg = "Did not produce DeprecationWarning or PendingDeprecationWarning"
84
- raise AssertionError (msg )
46
+ __tracebackhide__ = True
47
+ if func is not None :
48
+ args = (func ,) + args
49
+ return warns ((DeprecationWarning , PendingDeprecationWarning ), * args , ** kwargs )
85
50
86
51
87
52
def warns (expected_warning , * args , ** kwargs ):
@@ -116,6 +81,7 @@ def warns(expected_warning, *args, **kwargs):
116
81
Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted...
117
82
118
83
"""
84
+ __tracebackhide__ = True
119
85
match_expr = None
120
86
if not args :
121
87
if "match" in kwargs :
@@ -183,12 +149,25 @@ def __enter__(self):
183
149
raise RuntimeError ("Cannot enter %r twice" % self )
184
150
self ._list = super (WarningsRecorder , self ).__enter__ ()
185
151
warnings .simplefilter ("always" )
152
+ # python3 keeps track of a "filter version", when the filters are
153
+ # updated previously seen warnings can be re-warned. python2 has no
154
+ # concept of this so we must reset the warnings registry manually.
155
+ # trivial patching of `warnings.warn` seems to be enough somehow?
156
+ if six .PY2 :
157
+
158
+ def warn (* args , ** kwargs ):
159
+ return self ._saved_warn (* args , ** kwargs )
160
+
161
+ warnings .warn , self ._saved_warn = warn , warnings .warn
186
162
return self
187
163
188
164
def __exit__ (self , * exc_info ):
189
165
if not self ._entered :
190
166
__tracebackhide__ = True
191
167
raise RuntimeError ("Cannot exit %r without entering first" % self )
168
+ # see above where `self._saved_warn` is assigned
169
+ if six .PY2 :
170
+ warnings .warn = self ._saved_warn
192
171
super (WarningsRecorder , self ).__exit__ (* exc_info )
193
172
194
173
0 commit comments