summaryrefslogtreecommitdiffstats
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-11-22 22:51:42 (GMT)
committerGitHub <noreply@github.com>2017-11-22 22:51:42 (GMT)
commit82656276caf4cb889193572d2d14dbc5f3d2bdff (patch)
tree19060972ab524aa7962ca411f079cfa14ec614fa /Lib/warnings.py
parentbb11c3c967afaf263e00844d4ab461b7fafd6d36 (diff)
downloadcpython-82656276caf4cb889193572d2d14dbc5f3d2bdff.zip
cpython-82656276caf4cb889193572d2d14dbc5f3d2bdff.tar.gz
cpython-82656276caf4cb889193572d2d14dbc5f3d2bdff.tar.bz2
bpo-27535: Optimize warnings.warn() (#4508)
* Optimize warnings.filterwarnings(). Replace re.compile('') with None to avoid the cost of calling a regex.match() method, whereas it always matchs. * Optimize get_warnings_attr(): replace PyObject_GetAttrString() with _PyObject_GetAttrId(). Cleanup also create_filter(): * Use _Py_IDENTIFIER() to allow to cleanup strings at Python finalization * Replace Py_FatalError() with a regular exceptions
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r--Lib/warnings.py17
1 files changed, 14 insertions, 3 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py
index b2605f8..5b62569 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -128,7 +128,6 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
'lineno' -- an integer line number, 0 matches all warnings
'append' -- if true, append to the list of filters
"""
- import re
assert action in ("error", "ignore", "always", "default", "module",
"once"), "invalid action: %r" % (action,)
assert isinstance(message, str), "message must be a string"
@@ -137,8 +136,20 @@ 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 or module:
+ import re
+
+ if message:
+ message = re.compile(message, re.I)
+ else:
+ message = None
+ if module:
+ module = re.compile(module)
+ else:
+ module = None
+
+ _add_filter(action, message, 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).