[Python-checkins] python/dist/src/Lib warnings.py,1.20,1.21

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
Fri, 11 Jul 2003 08:38:00 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv2922

Modified Files:
	warnings.py 
Log Message:
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.


Index: warnings.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/warnings.py,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** warnings.py	14 May 2003 17:33:53 -0000	1.20
--- warnings.py	11 Jul 2003 15:37:56 -0000	1.21
***************
*** 10,15 ****
             "resetwarnings"]
  
! defaultaction = "default"
  filters = []
  onceregistry = {}
  
--- 10,23 ----
             "resetwarnings"]
  
! # 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 = {}
  
***************
*** 70,76 ****
      for item in filters:
          action, msg, cat, mod, ln = item
!         if (msg.match(text) and
              issubclass(category, cat) and
!             mod.match(module) and
              (ln == 0 or lineno == ln)):
              break
--- 78,84 ----
      for item in filters:
          action, msg, cat, mod, ln = item
!         if ((msg is None or msg.match(text)) and
              issubclass(category, cat) and
!             (msg is None or mod.match(module)) and
              (ln == 0 or lineno == ln)):
              break
***************
*** 146,149 ****
--- 154,172 ----
          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."""
***************
*** 226,267 ****
      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__":
--- 249,252 ----
***************
*** 271,274 ****
  else:
      _processoptions(sys.warnoptions)
!     filterwarnings("ignore", category=OverflowWarning, append=1)
!     filterwarnings("ignore", category=PendingDeprecationWarning, append=1)
--- 256,259 ----
  else:
      _processoptions(sys.warnoptions)
!     simplefilter("ignore", category=OverflowWarning, append=1)
!     simplefilter("ignore", category=PendingDeprecationWarning, append=1)