diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-05-26 09:39:41 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-05-26 09:39:41 (GMT) |
commit | b6ae2ae47ca754aed313e84990f220777cf45905 (patch) | |
tree | 39a3d3e4e0bc7765bb1e50ea83b84316d31d4d7e /Lib/warnings.py | |
parent | f157982b2c66be30d94a3f3ce0dfc2d9ebd3bd19 (diff) | |
parent | 43593a1892b73754e509713799a043ac756ae1e1 (diff) | |
download | cpython-b6ae2ae47ca754aed313e84990f220777cf45905.zip cpython-b6ae2ae47ca754aed313e84990f220777cf45905.tar.gz cpython-b6ae2ae47ca754aed313e84990f220777cf45905.tar.bz2 |
Issue #18383: Merge warnings fix from 3.5
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r-- | Lib/warnings.py | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py index f4c8cdc..2b407ff 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -129,13 +129,8 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0, assert isinstance(module, str), "module must be a string" assert isinstance(lineno, int) and lineno >= 0, \ "lineno must be an int >= 0" - item = (action, re.compile(message, re.I), category, - re.compile(module), lineno) - if append: - filters.append(item) - else: - filters.insert(0, item) - _filters_mutated() + _add_filter(action, re.compile(message, re.I), category, + re.compile(module), lineno, append=append) def simplefilter(action, category=Warning, lineno=0, append=False): """Insert a simple entry into the list of warnings filters (at the front). @@ -151,11 +146,20 @@ def simplefilter(action, category=Warning, lineno=0, append=False): "once"), "invalid action: %r" % (action,) assert isinstance(lineno, int) and lineno >= 0, \ "lineno must be an int >= 0" - item = (action, None, category, None, lineno) - if append: - filters.append(item) - else: + _add_filter(action, None, category, None, lineno, append=append) + +def _add_filter(*item, append): + # Remove possible duplicate filters, so new one will be placed + # in correct place. If append=True and duplicate exists, do nothing. + if not append: + try: + filters.remove(item) + except ValueError: + pass filters.insert(0, item) + else: + if item not in filters: + filters.append(item) _filters_mutated() def resetwarnings(): |