summaryrefslogtreecommitdiffstats
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2003-07-11 15:37:59 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2003-07-11 15:37:59 (GMT)
commit8501466c7f0843e68ee6fb91e3d8221d5eb1a019 (patch)
tree97403c9fde25171a8e37a3a9a4a44bd3eabe3836 /Lib/warnings.py
parent1e5fc55c4dcddcd97951d54c04b321c0a284affa (diff)
downloadcpython-8501466c7f0843e68ee6fb91e3d8221d5eb1a019.zip
cpython-8501466c7f0843e68ee6fb91e3d8221d5eb1a019.tar.gz
cpython-8501466c7f0843e68ee6fb91e3d8221d5eb1a019.tar.bz2
Change warnings to avoid importing re module during startup.
Add API function simplefilter() that does not create or install regular expressions to match message or module. Extend the filters data structure to store None as an alternative to re.compile(""). Move the _test() function to test_warnings and add some code to try and avoid disturbing the global state of the warnings module.
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r--Lib/warnings.py71
1 files changed, 28 insertions, 43 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py
index 1c55fb2..c2bc06e 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -9,8 +9,16 @@ import linecache
__all__ = ["warn", "showwarning", "formatwarning", "filterwarnings",
"resetwarnings"]
-defaultaction = "default"
+# filters contains a sequence of filter 5-tuples
+# The components of the 5-tuple are:
+# - an action: error, ignore, always, default, module, or once
+# - a compiled regex that must match the warning message
+# - a class representing the warning category
+# - a compiled regex that must match the module that is being warned
+# - a line number for the line being warning, or 0 to mean any line
+# If either if the compiled regexs are None, match anything.
filters = []
+defaultaction = "default"
onceregistry = {}
def warn(message, category=None, stacklevel=1):
@@ -69,9 +77,9 @@ def warn_explicit(message, category, filename, lineno,
# Search the filters
for item in filters:
action, msg, cat, mod, ln = item
- if (msg.match(text) and
+ if ((msg is None or msg.match(text)) and
issubclass(category, cat) and
- mod.match(module) and
+ (msg is None or mod.match(module)) and
(ln == 0 or lineno == ln)):
break
else:
@@ -145,6 +153,21 @@ def filterwarnings(action, message="", category=Warning, module="", lineno=0,
else:
filters.insert(0, item)
+def simplefilter(action, category=Warning, lineno=0, append=0):
+ """Insert a simple entry into the list of warnings filters (at the front).
+
+ A simple filter matches all modules and messages.
+ """
+ assert action in ("error", "ignore", "always", "default", "module",
+ "once"), "invalid action: %s" % `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:
+ filters.insert(0, item)
+
def resetwarnings():
"""Clear the list of warning filters, so that no filters are active."""
filters[:] = []
@@ -225,44 +248,6 @@ def _getcategory(category):
raise _OptionError("invalid warning category: %s" % `category`)
return cat
-# Self-test
-def _test():
- import getopt
- testoptions = []
- try:
- opts, args = getopt.getopt(sys.argv[1:], "W:")
- except getopt.error, msg:
- print >>sys.stderr, msg
- return
- for o, a in opts:
- testoptions.append(a)
- try:
- _processoptions(testoptions)
- except _OptionError, msg:
- print >>sys.stderr, msg
- return
- for item in filters: print item
- hello = "hello world"
- warn(hello); warn(hello); warn(hello); warn(hello)
- warn(hello, UserWarning)
- warn(hello, DeprecationWarning)
- for i in range(3):
- warn(hello)
- filterwarnings("error", "", Warning, "", 0)
- try:
- warn(hello)
- except Exception, msg:
- print "Caught", msg.__class__.__name__ + ":", msg
- else:
- print "No exception"
- resetwarnings()
- try:
- filterwarnings("booh", "", Warning, "", 0)
- except Exception, msg:
- print "Caught", msg.__class__.__name__ + ":", msg
- else:
- print "No exception"
-
# Module initialization
if __name__ == "__main__":
import __main__
@@ -270,5 +255,5 @@ if __name__ == "__main__":
_test()
else:
_processoptions(sys.warnoptions)
- filterwarnings("ignore", category=OverflowWarning, append=1)
- filterwarnings("ignore", category=PendingDeprecationWarning, append=1)
+ simplefilter("ignore", category=OverflowWarning, append=1)
+ simplefilter("ignore", category=PendingDeprecationWarning, append=1)