summaryrefslogtreecommitdiffstats
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-01-05 12:15:27 (GMT)
committerGitHub <noreply@github.com>2020-01-05 12:15:27 (GMT)
commit41ec17e45d54473d32f543396293256f1581e44d (patch)
tree3181bfa98e7f3c942fca53e339a2098af32e3cd1 /Lib/warnings.py
parent6a265f0d0c0a4b3b8fecf4275d49187a384167f4 (diff)
downloadcpython-41ec17e45d54473d32f543396293256f1581e44d.zip
cpython-41ec17e45d54473d32f543396293256f1581e44d.tar.gz
cpython-41ec17e45d54473d32f543396293256f1581e44d.tar.bz2
bpo-39056: Fix handling invalid warning category in the -W option. (GH-17618)
No longer import the re module if it is not needed.
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r--Lib/warnings.py30
1 files changed, 13 insertions, 17 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py
index 00f740c..691ccdd 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -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,))
@@ -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)
@@ -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