Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-39056: Fix handling invalid warning category in the -W option. #17618

Merged
merged 1 commit into from
Jan 5, 2020
Merged
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
23 changes: 23 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def warnings_state(module):
module.filters = original_filters


class TestWarning(Warning):
pass


class BaseTest:

"""Basic bookkeeping required for testing."""
Expand Down Expand Up @@ -566,9 +570,28 @@ def test_improper_input(self):
self.module._setoption, 'bogus::Warning')
self.assertRaises(self.module._OptionError,
self.module._setoption, 'ignore:2::4:-5')
with self.assertRaises(self.module._OptionError):
self.module._setoption('ignore::123')
with self.assertRaises(self.module._OptionError):
self.module._setoption('ignore::123abc')
with self.assertRaises(self.module._OptionError):
self.module._setoption('ignore::===')
with self.assertRaisesRegex(self.module._OptionError, 'Wärning'):
self.module._setoption('ignore::Wärning')
self.module._setoption('error::Warning::0')
self.assertRaises(UserWarning, self.module.warn, 'convert to error')

def test_import_from_module(self):
with original_warnings.catch_warnings(module=self.module):
self.module._setoption('ignore::Warning')
with self.assertRaises(self.module._OptionError):
self.module._setoption('ignore::TestWarning')
with self.assertRaises(self.module._OptionError):
self.module._setoption('ignore::test.test_warnings.bogus')
self.module._setoption('error::test.test_warnings.TestWarning')
with self.assertRaises(TestWarning):
self.module.warn('test warning', TestWarning)


class CWCmdLineTests(WCmdLineTests, unittest.TestCase):
module = c_warnings
Expand Down
30 changes: 13 additions & 17 deletions Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ def _processoptions(args):

# Helper for _processoptions()
def _setoption(arg):
import re
parts = arg.split(':')
if len(parts) > 5:
raise _OptionError("too many fields (max 5): %r" % (arg,))
Expand All @@ -220,11 +219,13 @@ def _setoption(arg):
action, message, category, module, lineno = [s.strip()
for s in parts]
action = _getaction(action)
message = re.escape(message)
category = _getcategory(category)
module = re.escape(module)
if message or module:
import re
if message:
message = re.escape(message)
if module:
module = module + '$'
module = re.escape(module) + r'\Z'
if lineno:
try:
lineno = int(lineno)
Expand All @@ -248,26 +249,21 @@ def _getaction(action):

# Helper for _setoption()
def _getcategory(category):
import re
if not category:
return Warning
if re.match("^[a-zA-Z0-9_]+$", category):
try:
cat = eval(category)
except NameError:
raise _OptionError("unknown warning category: %r" % (category,)) from None
if '.' not in category:
import builtins as m
klass = category
else:
i = category.rfind(".")
module = category[:i]
klass = category[i+1:]
module, _, klass = category.rpartition('.')
try:
m = __import__(module, None, None, [klass])
except ImportError:
raise _OptionError("invalid module name: %r" % (module,)) from None
try:
cat = getattr(m, klass)
except AttributeError:
raise _OptionError("unknown warning category: %r" % (category,)) from None
try:
cat = getattr(m, klass)
except AttributeError:
raise _OptionError("unknown warning category: %r" % (category,)) from None
if not issubclass(cat, Warning):
raise _OptionError("invalid warning category: %r" % (category,))
return cat
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed handling invalid warning category in the -W option. No longer import
the re module if it is not needed.