|
9 | 9 | __all__ = ["warn", "showwarning", "formatwarning", "filterwarnings",
|
10 | 10 | "resetwarnings"]
|
11 | 11 |
|
12 |
| -defaultaction = "default" |
| 12 | +# filters contains a sequence of filter 5-tuples |
| 13 | +# The components of the 5-tuple are: |
| 14 | +# - an action: error, ignore, always, default, module, or once |
| 15 | +# - a compiled regex that must match the warning message |
| 16 | +# - a class representing the warning category |
| 17 | +# - a compiled regex that must match the module that is being warned |
| 18 | +# - a line number for the line being warning, or 0 to mean any line |
| 19 | +# If either if the compiled regexs are None, match anything. |
13 | 20 | filters = []
|
| 21 | +defaultaction = "default" |
14 | 22 | onceregistry = {}
|
15 | 23 |
|
16 | 24 | def warn(message, category=None, stacklevel=1):
|
@@ -69,9 +77,9 @@ def warn_explicit(message, category, filename, lineno,
|
69 | 77 | # Search the filters
|
70 | 78 | for item in filters:
|
71 | 79 | action, msg, cat, mod, ln = item
|
72 |
| - if (msg.match(text) and |
| 80 | + if ((msg is None or msg.match(text)) and |
73 | 81 | issubclass(category, cat) and
|
74 |
| - mod.match(module) and |
| 82 | + (msg is None or mod.match(module)) and |
75 | 83 | (ln == 0 or lineno == ln)):
|
76 | 84 | break
|
77 | 85 | else:
|
@@ -145,6 +153,21 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
|
145 | 153 | else:
|
146 | 154 | filters.insert(0, item)
|
147 | 155 |
|
| 156 | +def simplefilter(action, category=Warning, lineno=0, append=0): |
| 157 | + """Insert a simple entry into the list of warnings filters (at the front). |
| 158 | +
|
| 159 | + A simple filter matches all modules and messages. |
| 160 | + """ |
| 161 | + assert action in ("error", "ignore", "always", "default", "module", |
| 162 | + "once"), "invalid action: %s" % `action` |
| 163 | + assert isinstance(lineno, int) and lineno >= 0, \ |
| 164 | + "lineno must be an int >= 0" |
| 165 | + item = (action, None, category, None, lineno) |
| 166 | + if append: |
| 167 | + filters.append(item) |
| 168 | + else: |
| 169 | + filters.insert(0, item) |
| 170 | + |
148 | 171 | def resetwarnings():
|
149 | 172 | """Clear the list of warning filters, so that no filters are active."""
|
150 | 173 | filters[:] = []
|
@@ -225,50 +248,12 @@ def _getcategory(category):
|
225 | 248 | raise _OptionError("invalid warning category: %s" % `category`)
|
226 | 249 | return cat
|
227 | 250 |
|
228 |
| -# Self-test |
229 |
| -def _test(): |
230 |
| - import getopt |
231 |
| - testoptions = [] |
232 |
| - try: |
233 |
| - opts, args = getopt.getopt(sys.argv[1:], "W:") |
234 |
| - except getopt.error, msg: |
235 |
| - print >>sys.stderr, msg |
236 |
| - return |
237 |
| - for o, a in opts: |
238 |
| - testoptions.append(a) |
239 |
| - try: |
240 |
| - _processoptions(testoptions) |
241 |
| - except _OptionError, msg: |
242 |
| - print >>sys.stderr, msg |
243 |
| - return |
244 |
| - for item in filters: print item |
245 |
| - hello = "hello world" |
246 |
| - warn(hello); warn(hello); warn(hello); warn(hello) |
247 |
| - warn(hello, UserWarning) |
248 |
| - warn(hello, DeprecationWarning) |
249 |
| - for i in range(3): |
250 |
| - warn(hello) |
251 |
| - filterwarnings("error", "", Warning, "", 0) |
252 |
| - try: |
253 |
| - warn(hello) |
254 |
| - except Exception, msg: |
255 |
| - print "Caught", msg.__class__.__name__ + ":", msg |
256 |
| - else: |
257 |
| - print "No exception" |
258 |
| - resetwarnings() |
259 |
| - try: |
260 |
| - filterwarnings("booh", "", Warning, "", 0) |
261 |
| - except Exception, msg: |
262 |
| - print "Caught", msg.__class__.__name__ + ":", msg |
263 |
| - else: |
264 |
| - print "No exception" |
265 |
| - |
266 | 251 | # Module initialization
|
267 | 252 | if __name__ == "__main__":
|
268 | 253 | import __main__
|
269 | 254 | sys.modules['warnings'] = __main__
|
270 | 255 | _test()
|
271 | 256 | else:
|
272 | 257 | _processoptions(sys.warnoptions)
|
273 |
| - filterwarnings("ignore", category=OverflowWarning, append=1) |
274 |
| - filterwarnings("ignore", category=PendingDeprecationWarning, append=1) |
| 258 | + simplefilter("ignore", category=OverflowWarning, append=1) |
| 259 | + simplefilter("ignore", category=PendingDeprecationWarning, append=1) |
0 commit comments