diff options
author | Guido van Rossum <guido@python.org> | 2001-01-14 14:08:40 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-01-14 14:08:40 (GMT) |
commit | 9464a7de60e67f59d8773e868fb2271b32d837c6 (patch) | |
tree | 918a86fa54e278aebf9215d521d7f29f8250fa8c /Lib | |
parent | 8373218e28b27e22e50540eae1d335de46ff7185 (diff) | |
download | cpython-9464a7de60e67f59d8773e868fb2271b32d837c6.zip cpython-9464a7de60e67f59d8773e868fb2271b32d837c6.tar.gz cpython-9464a7de60e67f59d8773e868fb2271b32d837c6.tar.bz2 |
- Added keyword argument 'append' to filterwarnings(); if true, this
appends to list of filters instead of inserting at the front. This
is useful to add a filter with a lower priority than -W options.
- Cosmetic improvements to a docstring and an error message.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/warnings.py | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py index a894bf2..31d4ad1 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -85,7 +85,7 @@ def showwarning(message, category, filename, lineno, file=None): file.write(formatwarning(message, category, filename, lineno)) def formatwarning(message, category, filename, lineno): - """Hook to format a warning the standard way.""" + """Function to format a warning the standard way.""" import linecache s = "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message) line = linecache.getline(filename, lineno).strip() @@ -93,7 +93,8 @@ def formatwarning(message, category, filename, lineno): s = s + " " + line + "\n" return s -def filterwarnings(action, message="", category=Warning, module="", lineno=0): +def filterwarnings(action, message="", category=Warning, module="", lineno=0, + append=0): """Insert an entry into the list of warnings filters (at the front). Use assertions to check that all arguments have the right type.""" @@ -105,8 +106,12 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0): assert type(module) is types.StringType, "module must be a string" assert type(lineno) is types.IntType and lineno >= 0, \ "lineno must be an int >= 0" - filters.insert(0, (action, re.compile(message, re.I), category, - re.compile(module), lineno)) + item = (action, re.compile(message, re.I), category, + re.compile(module), lineno) + if append: + filters.append(item) + else: + filters.insert(0, item) def resetwarnings(): """Reset the list of warnings filters to its default state.""" @@ -128,7 +133,7 @@ def _processoptions(args): def _setoption(arg): parts = arg.split(':') if len(parts) > 5: - raise _OptionError("unparsable -W option %s" % `arg`) + raise _OptionError("too many fields (max 5): %s" % `arg`) while len(parts) < 5: parts.append('') action, message, category, module, lineno = [s.strip() |