Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def test_ignore(self):
self.module.filterwarnings("ignore", category=UserWarning)
self.module.warn("FilterTests.test_ignore", UserWarning)
self.assertEqual(len(w), 0)
self.assertEqual(list(__warningregistry__.keys()),
['version'])

def test_ignore_after_default(self):
with original_warnings.catch_warnings(record=True,
Expand Down Expand Up @@ -705,6 +707,7 @@ def test_default_action(self):
self.module.defaultaction = "ignore"
__warningregistry__ = {}
registry = {}
self.module._filters_mutated()
self.module.warn_explicit(message, UserWarning, "<test>", 44,
registry=registry)
self.assertEqual(len(w), 0)
Expand Down Expand Up @@ -847,13 +850,14 @@ def test_issue31416(self):
# bad warnings.filters or warnings.defaultaction.
wmod = self.module
with original_warnings.catch_warnings(module=wmod):
wmod.filters = [(None, None, Warning, None, 0)]
with self.assertRaises(TypeError):
wmod._add_filter(None, None, Warning, None, 0)
wmod.warn_explicit('foo', Warning, 'bar', 1)

wmod.filters = []
with original_warnings.catch_warnings(module=wmod):
with support.swap_attr(wmod, 'defaultaction', None), \
self.assertRaises(TypeError):
wmod.resetwarnings()
wmod.warn_explicit('foo', Warning, 'bar', 1)

@support.cpython_only
Expand Down Expand Up @@ -1011,14 +1015,14 @@ def test_catch_warnings_defaults(self):
with wmod.catch_warnings(module=wmod) as w:
self.assertIsNone(w)
self.assertIs(wmod.showwarning, orig_showwarning)
self.assertIsNot(wmod.filters, orig_filters)
self.assertIs(wmod.filters, orig_filters)
self.assertIs(wmod.filters, orig_filters)
if wmod is sys.modules['warnings']:
# Ensure the default module is this one
with wmod.catch_warnings() as w:
self.assertIsNone(w)
self.assertIs(wmod.showwarning, orig_showwarning)
self.assertIsNot(wmod.filters, orig_filters)
self.assertIs(wmod.filters, orig_filters)
self.assertIs(wmod.filters, orig_filters)

def test_record_override_showwarning_before(self):
Expand Down
32 changes: 22 additions & 10 deletions Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,16 @@ 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"
_add_filter(action, re.compile(message, re.I), category,
re.compile(module), lineno, append=append)

if message:
regex = re.compile(message, re.I)
else:
regex = None
if module:
module = re.compile(module)
else:
module = None
_add_filter(action, regex, category, 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).
Expand All @@ -157,22 +165,26 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
_add_filter(action, None, category, None, lineno, append=append)

def _add_filter(*item, append):
global filters
# Remove possible duplicate filters, so new one will be placed
# in correct place. If append=True and duplicate exists, do nothing.
filters_list = list(filters)
if not append:
try:
filters.remove(item)
filters_list.remove(item)
except ValueError:
pass
filters.insert(0, item)
filters_list.insert(0, item)
else:
if item not in filters:
filters.append(item)
if item not in filters_list:
filters_list.append(item)
filters = tuple(filters_list)
_filters_mutated()

def resetwarnings():
"""Clear the list of warning filters, so that no filters are active."""
filters[:] = []
global filters
filters = ()
_filters_mutated()

class _OptionError(Exception):
Expand Down Expand Up @@ -353,7 +365,6 @@ def warn_explicit(message, category, filename, lineno,
action = defaultaction
# Early exit actions
if action == "ignore":
registry[key] = 1
return

# Prime the linecache for formatting, in case the
Expand Down Expand Up @@ -455,7 +466,7 @@ def __enter__(self):
raise RuntimeError("Cannot enter %r twice" % self)
self._entered = True
self._filters = self._module.filters
self._module.filters = self._filters[:]
self._module.filters = tuple(self._filters)
self._module._filters_mutated()
self._showwarning = self._module.showwarning
self._showwarnmsg_impl = self._module._showwarnmsg_impl
Expand Down Expand Up @@ -492,8 +503,9 @@ def __exit__(self, *exc_info):
defaultaction = _defaultaction
onceregistry = _onceregistry
_warnings_defaults = True
filters = tuple(filters)
except ImportError:
filters = []
filters = ()
defaultaction = "default"
onceregistry = {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The warnings module doesn't leak memory anymore in the hidden warnings
registry for the "ignore" action of warnings filters. warn_explicit()
function doesn't add the warning key to the registry anymore for the
"ignore" action.
Loading