Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9464a7d

Browse files
committed
- 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.
1 parent 8373218 commit 9464a7d

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

Lib/warnings.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,16 @@ def showwarning(message, category, filename, lineno, file=None):
8585
file.write(formatwarning(message, category, filename, lineno))
8686

8787
def formatwarning(message, category, filename, lineno):
88-
"""Hook to format a warning the standard way."""
88+
"""Function to format a warning the standard way."""
8989
import linecache
9090
s = "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
9191
line = linecache.getline(filename, lineno).strip()
9292
if line:
9393
s = s + " " + line + "\n"
9494
return s
9595

96-
def filterwarnings(action, message="", category=Warning, module="", lineno=0):
96+
def filterwarnings(action, message="", category=Warning, module="", lineno=0,
97+
append=0):
9798
"""Insert an entry into the list of warnings filters (at the front).
9899
99100
Use assertions to check that all arguments have the right type."""
@@ -105,8 +106,12 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0):
105106
assert type(module) is types.StringType, "module must be a string"
106107
assert type(lineno) is types.IntType and lineno >= 0, \
107108
"lineno must be an int >= 0"
108-
filters.insert(0, (action, re.compile(message, re.I), category,
109-
re.compile(module), lineno))
109+
item = (action, re.compile(message, re.I), category,
110+
re.compile(module), lineno)
111+
if append:
112+
filters.append(item)
113+
else:
114+
filters.insert(0, item)
110115

111116
def resetwarnings():
112117
"""Reset the list of warnings filters to its default state."""
@@ -128,7 +133,7 @@ def _processoptions(args):
128133
def _setoption(arg):
129134
parts = arg.split(':')
130135
if len(parts) > 5:
131-
raise _OptionError("unparsable -W option %s" % `arg`)
136+
raise _OptionError("too many fields (max 5): %s" % `arg`)
132137
while len(parts) < 5:
133138
parts.append('')
134139
action, message, category, module, lineno = [s.strip()

0 commit comments

Comments
 (0)